1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 Future<Void> fut = sendObjectViaFuture(data);
85 try
86 {
87 fut.get();
88 }
89 catch (ExecutionException e)
90 {
91 throw new IOException("Failed to write object",e.getCause());
92 }
93 catch (InterruptedException e)
94 {
95 throw new IOException("Failed to write object",e);
96 }
97 }
98
99 @Override
100 public void sendText(String text) throws IOException
101 {
102 assertMessageNotNull(text);
103 if (LOG.isDebugEnabled())
104 {
105 LOG.debug("sendText({})",TextUtil.hint(text));
106 }
107 jettyRemote.sendString(text);
108 }
109
110 @Override
111 public void sendText(String partialMessage, boolean isLast) throws IOException
112 {
113 assertMessageNotNull(partialMessage);
114 if (LOG.isDebugEnabled())
115 {
116 LOG.debug("sendText({},{})",TextUtil.hint(partialMessage),isLast);
117 }
118 jettyRemote.sendPartialString(partialMessage,isLast);
119 }
120 }