private void receiveMessage(MessagePayload messagePayload) { switch (messagePayload.getCommand()) { case INV: receiveMessage((Inv) messagePayload); break; case GETDATA: receiveMessage((GetData) messagePayload); break; case OBJECT: receiveMessage((ObjectMessage) messagePayload); break; case ADDR: receiveMessage((Addr) messagePayload); break; case CUSTOM: case VERACK: case VERSION: default: throw new IllegalStateException("Unexpectedly received '" + messagePayload.getCommand() + "' command"); } }
String command = payload.getCommand().name().toLowerCase(); out.write(command.getBytes("ASCII")); for (int i = command.length(); i < 12; i++) { payload.write(payloadStream); byte[] payloadBytes = payloadStream.toByteArray();
@Override public void write(OutputStream out) throws IOException { // magic Encode.int32(MAGIC, out); // ASCII string identifying the packet content, NULL padded (non-NULL padding results in packet rejected) String command = payload.getCommand().name().toLowerCase(); out.write(command.getBytes("ASCII")); for (int i = command.length(); i < 12; i++) { out.write('\0'); } byte[] payloadBytes = Encode.bytes(payload); // Length of payload in number of bytes. Because of other restrictions, there is no reason why this length would // ever be larger than 1600003 bytes. Some clients include a sanity-check to avoid processing messages which are // larger than this. Encode.int32(payloadBytes.length, out); // checksum try { out.write(getChecksum(payloadBytes)); } catch (GeneralSecurityException e) { throw new ApplicationException(e); } // message payload out.write(payloadBytes); }
String command = payload.getCommand().name().toLowerCase(); try { out.put(command.getBytes("ASCII"));
@Override public CustomMessage send(InetAddress server, int port, CustomMessage request) { try (Socket socket = new Socket(server, port)) { socket.setSoTimeout(Connection.READ_TIMEOUT); new NetworkMessage(request).write(socket.getOutputStream()); NetworkMessage networkMessage = Factory.getNetworkMessage(3, socket.getInputStream()); if (networkMessage != null && networkMessage.getPayload() instanceof CustomMessage) { return (CustomMessage) networkMessage.getPayload(); } else { if (networkMessage == null) { throw new NodeException("No response from node " + server); } else { throw new NodeException("Unexpected response from node " + server + ": " + networkMessage.getPayload().getCommand()); } } } catch (IOException e) { throw new NodeException(e.getMessage(), e); } }
private void handleCommand(MessagePayload payload) { switch (payload.getCommand()) { case VERSION: handleVersion((Version) payload); break; case VERACK: if (verackSent) { activateConnection(); } verackReceived = true; break; case CUSTOM: MessagePayload response = ctx.getCustomCommandHandler().handle((CustomMessage) payload); if (response == null) { disconnect(); } else { send(response); } break; default: throw new NodeException("Command 'version' or 'verack' expected, but was '" + payload.getCommand() + "'"); } }