01: /*
02: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
19: * The latest copy of this software may be found on http://www.xsocket.org/
20: */
21: package org.xsocket.connection.multiplexed;
22:
23: import java.io.IOException;
24: import java.net.InetAddress;
25: import java.nio.BufferUnderflowException;
26: import java.util.logging.Level;
27:
28: import org.junit.Assert;
29: import org.junit.Test;
30:
31: import org.xsocket.MaxReadSizeExceededException;
32: import org.xsocket.connection.IDataHandler;
33: import org.xsocket.connection.INonBlockingConnection;
34: import org.xsocket.connection.IServer;
35: import org.xsocket.connection.NonBlockingConnection;
36: import org.xsocket.connection.Server;
37: import org.xsocket.connection.ConnectionUtils;
38: import org.xsocket.connection.multiplexed.IMultiplexedConnection;
39: import org.xsocket.connection.multiplexed.INonBlockingPipeline;
40: import org.xsocket.connection.multiplexed.MultiplexedConnection;
41: import org.xsocket.connection.multiplexed.MultiplexedProtocolAdapter;
42:
43: /**
44: *
45: * @author grro@xsocket.org
46: */
47: public final class MarkTest {
48:
49: @Test
50: public void testSimple() throws Exception {
51: IServer server = new Server(new MultiplexedProtocolAdapter(
52: new MyEchoHandler()));
53: ConnectionUtils.start(server);
54:
55: IMultiplexedConnection connection = new MultiplexedConnection(
56: new NonBlockingConnection(InetAddress
57: .getByName("localhost"), server.getLocalPort()));
58:
59: String pipelineId = connection.createPipeline();
60: INonBlockingPipeline pipeline = connection
61: .getNonBlockingPipeline(pipelineId);
62: pipeline.setAutoflush(false);
63:
64: pipeline.markWritePosition();
65: pipeline.write((int) 0); // write length field
66: int written = pipeline.write("test");
67: pipeline.resetToWriteMark();
68: pipeline.write(written);
69: pipeline.flush();
70:
71: QAUtil.sleep(400);
72:
73: int length = pipeline.readInt();
74: String data = pipeline.readStringByLength(length);
75:
76: Assert.assertEquals("test", data);
77:
78: pipeline.close();
79:
80: connection.close();
81: server.close();
82: }
83:
84: private static final class MyEchoHandler implements IDataHandler {
85:
86: public boolean onData(INonBlockingConnection connection)
87: throws IOException, BufferUnderflowException,
88: MaxReadSizeExceededException {
89: connection.write(connection
90: .readByteBufferByLength(connection.available()));
91: return true;
92: }
93:
94: }
95: }
|