View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.BatchMode;
22  import org.eclipse.jetty.websocket.api.WriteCallback;
23  import org.eclipse.jetty.websocket.api.extensions.Frame;
24  import org.eclipse.jetty.websocket.api.extensions.IncomingFrames;
25  import org.eclipse.jetty.websocket.api.extensions.OutgoingFrames;
26  
27  public class FramePipes
28  {
29      private static class In2Out implements IncomingFrames
30      {
31          private OutgoingFrames outgoing;
32  
33          public In2Out(OutgoingFrames outgoing)
34          {
35              this.outgoing = outgoing;
36          }
37  
38          @Override
39          public void incomingError(Throwable t)
40          {
41              /* cannot send exception on */
42          }
43  
44          @Override
45          public void incomingFrame(Frame frame)
46          {
47              this.outgoing.outgoingFrame(frame,null, BatchMode.OFF);
48          }
49      }
50  
51      private static class Out2In implements OutgoingFrames
52      {
53          private IncomingFrames incoming;
54  
55          public Out2In(IncomingFrames incoming)
56          {
57              this.incoming = incoming;
58          }
59  
60          @Override
61          public void outgoingFrame(Frame frame, WriteCallback callback, BatchMode batchMode)
62          {
63              try
64              {
65                  this.incoming.incomingFrame(frame);
66                  callback.writeSuccess();
67              }
68              catch (Throwable t)
69              {
70                  callback.writeFailed(t);
71              }
72          }
73      }
74  
75      public static OutgoingFrames to(final IncomingFrames incoming)
76      {
77          return new Out2In(incoming);
78      }
79  
80      public static IncomingFrames to(final OutgoingFrames outgoing)
81      {
82          return new In2Out(outgoing);
83      }
84  }