public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException { String cmd = connection.readStringByDelimiter("\r\n"); if (cmd.startsWith("A")) { connection.write("OK\r\n"); } else { connection.write(cmd + " failed\r\n"); } return true; }
@Before public void init() throws Exception { networkingService.start(); verify(discoveryService).addLanDiscoveryListener(networkingService); when(acceptor.isOpen()).thenReturn(true); networkingService.addMessageListener(listener); when(nodeFactory.newNode(anyString())).thenReturn(lanNode); when(localInetAddress.getHostAddress()).thenReturn(localAddress); when(lanNode.getConnection()).thenReturn(connection); when(connection.getLocalAddress()).thenReturn(localInetAddress); when(connection.getRemoteAddress()).thenReturn(remoteInetAddress); }
public static void main(String[] args) throws IOException { INonBlockingConnection nbc = new NonBlockingConnection("localhost", 1234, new ClientHandler()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String msg = null; while ((msg = br.readLine()) != null) { nbc.write(msg + "\r\n"); nbc.flush(); if ("bye".equals(msg)) { nbc.close(); System.exit(1); } } }
public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, MaxReadSizeExceededException { connection.readStringByDelimiter("\r\n"); File file = QAUtil.createTestfile_400k(); RandomAccessFile raf = new RandomAccessFile(file, "r"); ReadableByteChannel in = raf.getChannel(); connection.transferFrom(in); in.close(); raf.close(); file.delete(); return true; }
@Test public void shouldCallListenersAsynchronouslyOnMessageReceived() throws Exception { when(connection.readStringByDelimiter(anyString())).thenReturn(encodedMessage); networkingService.onData(connection); verify(listener, timeout(2000)).onMessage(any(LanDownloaderMessage.class)); }
@Override public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException { Msg msg = null; // 先读取字节的数目 int length = connection.readInt(); // 根据字节数目读取对应的长度的字节 byte[] data = connection.readBytesByLength(length); // 反序列化对象 msg = (Msg) SerializableUtil.deserialize(data); System.out.println("response:" + msg); return false; }