1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| /*
* Copyright (c) xsocket.org, 2006-2008. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xsocket.org/
*/
import java.io.*;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import org.xsocket.Execution;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IConnection.FlushMode;
/**
*
* @author grro@xsocket.org
*/
@Execution(Execution.NONTHREADED)
public final class EchoHandler implements IConnectHandler, IDataHandler {
public static final String DELIMITER = "\r";
public boolean onConnect(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException {
connection.setFlushmode(FlushMode.ASYNC);
return true;
}
public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException {
ByteBuffer[] buffer = connection.readByteBufferByDelimiter(DELIMITER, null, Integer.MAX_VALUE);
connection.write(buffer);
connection.write(DELIMITER);
//System.out.print(".");
return true;
}
} |
Partager