001: /*
002: * Copyright (c) xsocket.org, 2006-2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.spi;
022:
023: import java.io.IOException;
024: import java.nio.BufferUnderflowException;
025: import java.util.concurrent.Executors;
026:
027: import org.junit.Assert;
028: import org.xsocket.MaxReadSizeExceededException;
029: import org.xsocket.QAUtil;
030: import org.xsocket.connection.BlockingConnection;
031: import org.xsocket.connection.IBlockingConnection;
032: import org.xsocket.connection.IConnectHandler;
033: import org.xsocket.connection.IDataHandler;
034: import org.xsocket.connection.IDisconnectHandler;
035: import org.xsocket.connection.INonBlockingConnection;
036: import org.xsocket.connection.IServer;
037: import org.xsocket.connection.Server;
038: import org.xsocket.connection.ConnectionUtils;
039: import org.xsocket.connection.spi.IServerIoProvider;
040:
041: /**
042: *
043: * @author grro@xsocket.org
044: */
045: public final class Example {
046:
047: private static final String DELIMITER = "\r\n";
048: private static final String KILL = "KILL";
049:
050: public static void main(String... args) throws Exception {
051: new Example().testSimple();
052: }
053:
054: public void testSimple() throws Exception {
055: //QAUtil.setLogLevel(Level.FINE);
056:
057: // set example nio provider
058: System.setProperty(IServerIoProvider.PROVIDER_CLASSNAME_KEY,
059: ExampleServerIoProvider.class.getName());
060:
061: // the remaining stay the same
062: Handler handler = new Handler();
063: IServer server = new Server(0, handler);
064: server.setWorkerpool(Executors.newCachedThreadPool());
065: ConnectionUtils.start(server);
066:
067: IBlockingConnection connection = new BlockingConnection(
068: "127.0.0.1", server.getLocalPort());
069: String greeting = connection.readStringByDelimiter(DELIMITER);
070: Assert.assertEquals("Helo", greeting);
071:
072: connection.write("it`s me, the client" + DELIMITER);
073: String echo = connection.readStringByDelimiter(DELIMITER);
074:
075: Assert.assertEquals("it`s me, the client", echo);
076:
077: connection.write(KILL + DELIMITER);
078: QAUtil.sleep(200);
079:
080: Assert.assertTrue(handler.isDisconnected);
081:
082: connection.close();
083: server.close();
084:
085: System.out.println("example io simple simple test passed ");
086: }
087:
088: private static final class Handler implements IConnectHandler,
089: IDataHandler, IDisconnectHandler {
090:
091: private boolean isDisconnected = false;
092:
093: public boolean onConnect(INonBlockingConnection connection)
094: throws IOException {
095: connection.write("Helo" + DELIMITER);
096: return true;
097: }
098:
099: public boolean onData(INonBlockingConnection connection)
100: throws IOException, BufferUnderflowException,
101: MaxReadSizeExceededException {
102: String message = connection
103: .readStringByDelimiter(DELIMITER);
104:
105: if (message.equals(KILL)) {
106: connection.close();
107: } else {
108: connection.write(message + DELIMITER);
109: }
110:
111: return true;
112: }
113:
114: public boolean onDisconnect(INonBlockingConnection connection)
115: throws IOException {
116: isDisconnected = true;
117: return true;
118: }
119:
120: }
121: }
|