View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.websocket.common.io;
20  
21  import org.eclipse.jetty.websocket.api.WriteCallback;
22  import org.eclipse.jetty.websocket.api.extensions.Frame;
23  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
24  import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
25  
26  public class FramePipes
27  {
28      private static class In2Out implements IncomingFrames
29      {
30          private OutgoingFrames outgoing;
31  
32          public In2Out(OutgoingFrames outgoing)
33          {
34              this.outgoing = outgoing;
35          }
36  
37          @Override
38          public void incomingError(Throwable t)
39          {
40              /* cannot send exception on */
41          }
42  
43          @Override
44          public void incomingFrame(Frame frame)
45          {
46              this.outgoing.outgoingFrame(frame,null);
47          }
48      }
49  
50      private static class Out2In implements OutgoingFrames
51      {
52          private IncomingFrames incoming;
53  
54          public Out2In(IncomingFrames incoming)
55          {
56              this.incoming = incoming;
57          }
58  
59          @Override
60          public void outgoingFrame(Frame frame, WriteCallback callback)
61          {
62              try
63              {
64                  this.incoming.incomingFrame(frame);
65                  callback.writeSuccess();
66              }
67              catch (Throwable t)
68              {
69                  callback.writeFailed(t);
70              }
71          }
72      }
73  
74      public static OutgoingFrames to(final IncomingFrames incoming)
75      {
76          return new Out2In(incoming);
77      }
78  
79      public static IncomingFrames to(final OutgoingFrames outgoing)
80      {
81          return new In2Out(outgoing);
82      }
83  }