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
85 Future<Void> fut = sendObjectViaFuture(data);
86 try
87 {
88 fut.get();
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 LOG.info("sendText({})", TextUtil.hint(text));
104 assertMessageNotNull(text);
105 if (LOG.isDebugEnabled())
106 {
107 LOG.debug("sendText({})",TextUtil.hint(text));
108 }
109 jettyRemote.sendString(text);
110 }
111
112 @Override
113 public void sendText(String partialMessage, boolean isLast) throws IOException
114 {
115 assertMessageNotNull(partialMessage);
116 if (LOG.isDebugEnabled())
117 {
118 LOG.debug("sendText({},{})",TextUtil.hint(partialMessage),isLast);
119 }
120 jettyRemote.sendPartialString(partialMessage,isLast);
121 }
122 }