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;
22:
23: import java.io.IOException;
24:
25: import org.junit.Assert;
26: import org.junit.Test;
27: import org.xsocket.connection.BlockingConnection;
28: import org.xsocket.connection.IBlockingConnection;
29: import org.xsocket.connection.IConnectHandler;
30: import org.xsocket.connection.IDataHandler;
31: import org.xsocket.connection.INonBlockingConnection;
32: import org.xsocket.connection.IServer;
33: import org.xsocket.connection.Server;
34: import org.xsocket.connection.ConnectionUtils;
35: import org.xsocket.connection.IConnection.FlushMode;
36:
37: /**
38: *
39: * @author grro@xsocket.org
40: */
41: public final class OnConnectTest {
42:
43: private static final String DELIMITER = "x";
44: private static final String GREETING = "helo";
45:
46: @Test
47: public void testSimple() throws Exception {
48: IServer server = new Server(new ServerHandler());
49: ConnectionUtils.start(server);
50:
51: IBlockingConnection connection = new BlockingConnection(
52: "localhost", server.getLocalPort());
53: connection.setAutoflush(true);
54:
55: String greeting = connection.readStringByDelimiter(DELIMITER,
56: Integer.MAX_VALUE);
57: Assert.assertEquals(greeting, GREETING);
58:
59: String request = "reert";
60: connection.write(request + DELIMITER);
61:
62: String response = connection.readStringByDelimiter(DELIMITER,
63: Integer.MAX_VALUE);
64:
65: Assert.assertEquals(request, response);
66:
67: connection.close();
68: server.close();
69: }
70:
71: private static class ServerHandler implements IDataHandler,
72: IConnectHandler {
73:
74: public boolean onConnect(INonBlockingConnection connection)
75: throws IOException {
76: connection.setAutoflush(false);
77: connection.setFlushmode(FlushMode.ASYNC);
78:
79: connection.write(GREETING + DELIMITER);
80: connection.flush();
81: return true;
82: }
83:
84: public boolean onData(INonBlockingConnection connection)
85: throws IOException {
86: connection.write(connection.readByteBufferByDelimiter(
87: DELIMITER, Integer.MAX_VALUE));
88: connection.write(DELIMITER);
89: connection.flush();
90: return true;
91: }
92: }
93: }
|