public void sendNetworkEvent(BaseEvent event) throws IOException { if (this.getStatus() == Node.CLOSE) { return; } if (writeTarget == null) { throw new NotYetConnectedException(); } if (this.status != Node.HANDSHAKE && !isHandShakeMessage(event)) { throw new NotYetConnectedException(); } lock.lock(); try { byte[] data = event.serialize(); NulsMessage message = new NulsMessage(magicNumber, data); this.writeTarget.write(message.serialize()); } finally { lock.unlock(); } }
private BroadcastResult broadcastToGroup(NulsMessage message, String groupName, String excludeNodeId) { List<Node> broadNodes = nodesManager.getGroupAvailableNodes(groupName, excludeNodeId); if (broadNodes.size() <= 1) { return new BroadcastResult(false, "no node can be broadcast"); } int successCount = 0; for (Node node : broadNodes) { try { node.sendMessage(message); successCount++; } catch (NotYetConnectedException | IOException e) { Log.warn("broadcast message error , maybe the node closed ! node ip :{}, {}", node.getIp(), e.getMessage()); } } if (successCount == 0) { new BroadcastResult(false, "broadcast fail", broadNodes); } Log.debug("成功广播给{}个节点,消息{}", successCount, message); return new BroadcastResult(true, "OK"); }
/** * Sends the given message to the peer. Due to the asynchronousness of network programming, there is no guarantee * the peer will have received it. Throws NotYetConnectedException if we are not yet connected to the remote peer. * TODO: Maybe use something other than the unchecked NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) throw new NotYetConnectedException(); } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }
public void invokeMethod(String method, Object paramsObject, @Nullable PendingRequestCallback callback) throws NotYetConnectedException { Util.throwIfNull(method); Long requestId = (callback != null) ? preparePendingRequest(callback) : null; // magic, can basically convert anything for some amount of runtime overhead... JSONObject params = mObjectMapper.convertValue(paramsObject, JSONObject.class); JsonRpcRequest message = new JsonRpcRequest(requestId, method, params); String requestString; JSONObject jsonObject = mObjectMapper.convertValue(message, JSONObject.class); requestString = jsonObject.toString(); mPeer.sendText(requestString); }
public long write (ByteBuffer[] srcs, int offset, int length) throws IOException { if (!isConnected()) throw new NotYetConnectedException(); if ((offset < 0) || (offset > srcs.length) || (length < 0) || (length > (srcs.length - offset))) throw new IndexOutOfBoundsException(); /* We are connected, meaning we will write these bytes to * the host we connected to, so we don't need to explicitly * give the host. */ return channel.writeGathering(srcs, offset, length); }
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { Throwable cause = e.getCause(); // do not print exception if it is BindException. // we are trying to search available port below 1024. It is not good to // print a flood // of error logs during the searching. if (cause instanceof java.net.BindException) { return; } LOG.error("Exception on connection to " + getRemoteAddress(), e.getCause()); // close the channel unless we are connecting and it is // NotYetConnectedException if (!((cause instanceof NotYetConnectedException) && _connection.getConnectionState().equals(Connection.State.CONNECTING))) { ctx.getChannel().close(); } }
@Override protected void doWriteFileRegion(FileRegion region) throws Exception { OutputStream os = this.os; if (os == null) { throw new NotYetConnectedException(); } if (outChannel == null) { outChannel = Channels.newChannel(os); } long written = 0; for (;;) { long localWritten = region.transferTo(outChannel, written); if (localWritten == -1) { checkEOF(region); return; } written += localWritten; if (written >= region.count()) { return; } } }
@TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Doesn't verify all exceptions according to specification.", method = "write", args = {java.nio.ByteBuffer[].class} ) public void testWriteByteBufferArray_Block() throws IOException { ByteBuffer[] writeBuf = new ByteBuffer[2]; writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); InetSocketAddress ipAddr = localAddr1; try { this.channel1.write(writeBuf); fail("Should throw NotYetConnectedException."); } catch (NotYetConnectedException e) { // correct } this.channel1.connect(ipAddr); assertTrue(this.channel1.isConnected()); assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf)); // cannot be buffered again! assertEquals(0, this.channel1.write(writeBuf)); }
/** * @tests serialization/deserialization compatibility. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationSelf", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "NotYetConnectedException", args = {} ) }) public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new NotYetConnectedException()); }
/** * @tests serialization/deserialization compatibility with RI. */ @TestTargets({ @TestTargetNew( level = TestLevel.COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "!SerializationGolden", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies serialization/deserialization compatibility.", method = "NotYetConnectedException", args = {} ) }) public void testSerializationCompatibility() throws Exception { SerializationTest.verifyGolden(this, new NotYetConnectedException()); }
public void testOpen() throws IOException { java.nio.ByteBuffer[] buf = new java.nio.ByteBuffer[1]; buf[0] = java.nio.ByteBuffer.allocateDirect(CAPACITY_NORMAL); MockSocketChannel testMSChannel = new MockSocketChannel(null); MockSocketChannel testMSChannelnotnull = new MockSocketChannel( SelectorProvider.provider()); SocketChannel testSChannel = MockSocketChannel.open(); assertTrue(testSChannel.isOpen()); assertNull(testMSChannel.provider()); assertNotNull(testSChannel.provider()); assertEquals(SelectorProvider.provider(), testSChannel.provider()); assertNotNull(testMSChannelnotnull.provider()); assertEquals(this.channel1.provider(), testMSChannelnotnull.provider()); try { this.channel1.write(buf); fail("Should throw NotYetConnectedException"); } catch (NotYetConnectedException e) { // correct } }
/** * Sends <var>text</var> to server * * @param text * String to send to server */ @JavascriptInterface public void send(final String text) { new Thread(new Runnable() { @Override public void run() { if (instance.readyState == WEBSOCKET_STATE_OPEN) { try { instance._send(text); } catch (Exception e) { instance.onError(e); } } else { instance.onError(new NotYetConnectedException()); } } }).start(); }
/** * Sends the given message to the peer. Due to the asynchronousness of network programming, * there is no guarantee * the peer will have received it. Throws NotYetConnectedException if we are not yet * connected to the remote peer. * TODO: Maybe use something other than the unchecked NotYetConnectedException here */ public void sendMessage(Message message) throws NotYetConnectedException { lock.lock(); try { if (writeTarget == null) { throw new NotYetConnectedException(); } } finally { lock.unlock(); } // TODO: Some round-tripping could be avoided here ByteArrayOutputStream out = new ByteArrayOutputStream(); try { serializer.serialize(message, out); writeTarget.writeBytes(out.toByteArray()); } catch (IOException e) { exceptionCaught(e); } }
public void testWriteByteBufferArrayIntInt_Block() throws IOException { ByteBuffer[] writeBuf = new ByteBuffer[2]; writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); InetSocketAddress ipAddr = localAddr1; try { this.channel1.write(writeBuf, 0, 2); fail("Should throw NotYetConnectedException."); } catch (NotYetConnectedException e) { // correct } this.channel1.connect(ipAddr); assertTrue(this.channel1.isConnected()); assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); // cannot be buffered again! assertEquals(0, this.channel1.write(writeBuf, 0, 1)); }
public void testWriteByteBufferArrayIntInt_NonBlock() throws IOException { ByteBuffer[] writeBuf = new ByteBuffer[2]; writeBuf[0] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); writeBuf[1] = ByteBuffer.allocateDirect(CAPACITY_NORMAL); InetSocketAddress ipAddr = localAddr1; // non-block mode this.channel1.configureBlocking(false); try { this.channel1.write(writeBuf, 0, 2); fail("Should throw NotYetConnectedException."); } catch (NotYetConnectedException e) { // correct } this.channel1.connect(ipAddr); assertTrue(this.channel1.isConnected()); assertEquals(CAPACITY_NORMAL * 2, this.channel1.write(writeBuf, 0, 2)); // cannot be buffered again! assertEquals(0, this.channel1.write(writeBuf, 0, 1)); }
public void sendMessage(NulsMessage message) throws IOException { if (this.getStatus() == Node.CLOSE) { return; } if (writeTarget == null || this.status != Node.HANDSHAKE) { throw new NotYetConnectedException(); } lock.lock(); try { System.out.println("---send message:" + Hex.encode(message.serialize())); this.writeTarget.write(message.serialize()); } finally { lock.unlock(); } }