Author: markt Date: Wed Feb 20 22:48:46 2013 New Revision: 1448455 URL: http://svn.apache.org/r1448455 Log: First draft (untested) of sendObject implementation
Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointBase.java Modified: tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties?rev=1448455&r1=1448454&r2=1448455&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/LocalStrings.properties Wed Feb 20 22:48:46 2013 @@ -36,6 +36,7 @@ wsRemoteEndpoint.changeType=When sending wsRemoteEndpoint.concurrentMessageSend=Messages may not be sent concurrently even when using the asynchronous send messages. The client must wait for the previous message to complete before sending the next. wsRemoteEndpoint.flushOnCloseFailed=Flushing batched messages before closing the session failed wsRemoteEndpoint.inProgress=Message will not be sent because the WebSocket session is currently sending another message +wsRemoteEndpoint.noEncoder=No encoder specified for object of class [{0}] # Note the following message is used as a close reason in a WebSocket control # frame and therefore must be 123 bytes (not characters) or less in length. Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointBase.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointBase.java?rev=1448455&r1=1448454&r2=1448455&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointBase.java (original) +++ tomcat/trunk/java/org/apache/tomcat/websocket/WsRemoteEndpointBase.java Wed Feb 20 22:48:46 2013 @@ -428,21 +428,50 @@ public abstract class WsRemoteEndpointBa @Override - public void sendObject(Object o) throws IOException, EncodeException { - // TODO Auto-generated method stub - + public void sendObject(Object obj) throws IOException, EncodeException { + Future<SendResult> f = sendObjectByFuture(obj); + try { + f.get(); + } catch (InterruptedException | ExecutionException e) { + throw new IOException(e); + } } @Override public Future<SendResult> sendObjectByFuture(Object obj) { - // TODO Auto-generated method stub - return null; + FutureToSendHandler f2sh = new FutureToSendHandler(); + sendObjectByCompletion(obj, f2sh); + return f2sh; } + + @SuppressWarnings({"unchecked", "rawtypes"}) @Override public void sendObjectByCompletion(Object obj, SendHandler completion) { - // TODO Auto-generated method stub + Encoder encoder = findEncoder(obj); + + try { + if (encoder instanceof Encoder.Text) { + String msg = ((Encoder.Text) encoder).encode(obj); + sendStringByCompletion(msg, completion); + } else if (encoder instanceof Encoder.TextStream) { + Writer w = getSendWriter(); + ((Encoder.TextStream) encoder).encode(obj, w); + } else if (encoder instanceof Encoder.Binary) { + ByteBuffer msg = ((Encoder.Binary) encoder).encode(obj); + sendBytesByCompletion(msg, completion); + } else if (encoder instanceof Encoder.BinaryStream) { + OutputStream os = getSendStream(); + ((Encoder.BinaryStream) encoder).encode(obj, os); + } else { + throw new EncodeException(obj, sm.getString( + "wsRemoteEndpoint.noEncoder", obj.getClass())); + } + } catch (EncodeException | IOException e) { + SendResult sr = new SendResult(e); + completion.setResult(sr); + } } @@ -460,11 +489,38 @@ public abstract class WsRemoteEndpointBa } } + + private Encoder findEncoder(Object obj) { + for (EncoderEntry entry : encoderEntries) { + if (entry.getClazz().isAssignableFrom(obj.getClass())) { + return entry.getEncoder(); + } + } + + if (obj instanceof Byte || obj instanceof Short || + obj instanceof Integer || obj instanceof Long || + obj instanceof Float || obj instanceof Double || + obj instanceof Character || obj instanceof Boolean) { + return new ToStringEncoder(); + } + return null; + } + + protected abstract void doWrite(SendHandler handler, ByteBuffer... data); protected abstract boolean isMasked(); protected abstract void close(); + private static class ToStringEncoder implements Encoder.Text<Object> { + + @Override + public String encode(Object object) throws EncodeException { + return object.toString(); + } + } + + private static void writeHeader(ByteBuffer headerBuffer, byte opCode, ByteBuffer payload, boolean first, boolean last, boolean masked, byte[] mask) { --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org