View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.jsr356;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.Writer;
24  import java.nio.ByteBuffer;
25  import java.util.concurrent.ExecutionException;
26  import java.util.concurrent.Future;
27  
28  import javax.websocket.EncodeException;
29  import javax.websocket.RemoteEndpoint;
30  
31  import org.eclipse.jetty.util.BufferUtil;
32  import org.eclipse.jetty.util.log.Log;
33  import org.eclipse.jetty.util.log.Logger;
34  import org.eclipse.jetty.websocket.common.message.MessageOutputStream;
35  import org.eclipse.jetty.websocket.common.message.MessageWriter;
36  import org.eclipse.jetty.websocket.common.util.TextUtil;
37  
38  public class JsrBasicRemote extends AbstractJsrRemote implements RemoteEndpoint.Basic
39  {
40      private static final Logger LOG = Log.getLogger(JsrBasicRemote.class);
41  
42      protected JsrBasicRemote(JsrSession session)
43      {
44          super(session);
45      }
46  
47      @Override
48      public OutputStream getSendStream() throws IOException
49      {
50          return new MessageOutputStream(session);
51      }
52  
53      @Override
54      public Writer getSendWriter() throws IOException
55      {
56          return new MessageWriter(session);
57      }
58  
59      @Override
60      public void sendBinary(ByteBuffer data) throws IOException
61      {
62          assertMessageNotNull(data);
63          if (LOG.isDebugEnabled())
64          {
65              LOG.debug("sendBinary({})",BufferUtil.toDetailString(data));
66          }
67          jettyRemote.sendBytes(data);
68      }
69  
70      @Override
71      public void sendBinary(ByteBuffer partialByte, boolean isLast) throws IOException
72      {
73          assertMessageNotNull(partialByte);
74          if (LOG.isDebugEnabled())
75          {
76              LOG.debug("sendBinary({},{})",BufferUtil.toDetailString(partialByte),isLast);
77          }
78          jettyRemote.sendPartialBytes(partialByte,isLast);
79      }
80  
81      @Override
82      public void sendObject(Object data) throws IOException, EncodeException
83      {
84          // TODO avoid the use of a Future
85          Future<Void> fut = sendObjectViaFuture(data);
86          try
87          {
88              fut.get(); // block till done
89          }
90          catch (ExecutionException e)
91          {
92              throw new IOException("Failed to write object",e.getCause());
93          }
94          catch (InterruptedException e)
95          {
96              throw new IOException("Failed to write object",e);
97          }
98      }
99  
100     @Override
101     public void sendText(String text) throws IOException
102     {
103         assertMessageNotNull(text);
104         if (LOG.isDebugEnabled())
105         {
106             LOG.debug("sendText({})",TextUtil.hint(text));
107         }
108         jettyRemote.sendString(text);
109     }
110 
111     @Override
112     public void sendText(String partialMessage, boolean isLast) throws IOException
113     {
114         assertMessageNotNull(partialMessage);
115         if (LOG.isDebugEnabled())
116         {
117             LOG.debug("sendText({},{})",TextUtil.hint(partialMessage),isLast);
118         }
119         jettyRemote.sendPartialString(partialMessage,isLast);
120     }
121 }