public NodeInfo getInfo(int number) { return netConfig.getInfo(number); }
/** * @see UDPAdapter#getOrCreateSession * @param remoteId id of remote node, {@link NodeInfo} will be got from {@link UDPAdapter#netConfig} * @return session */ private Session getOrCreateSession(int remoteId) { NodeInfo destination = netConfig.getInfo(remoteId); if (destination == null) { callErrorCallbacks("(getOrCreateSession) unknown nodeId has received: "+remoteId); return null; } return getOrCreateSession(destination); }
/** * We have received {@link PacketTypes#HELLO} packet. Should create localNonce and send it in reply. * @param packet received {@link Packet} */ private void onReceiveHello(Packet packet) throws EncryptionError { report(logLabel, ()->"received hello from " + packet.senderNodeId, VerboseLevel.BASE); NodeInfo nodeInfo = netConfig.getInfo(packet.senderNodeId); if (nodeInfo != null) { SessionReader sessionReader = getOrCreateSessionReaderCandidate(packet.senderNodeId); if (sessionReader != null) { sessionReader.protectFromDuples(packet.packetId, ()->{ if (sessionReader.nextLocalNonceGenerationTime.isBefore(Instant.now())) { sessionReader.localNonce = Do.randomBytes(64); sessionReader.nextLocalNonceGenerationTime = Instant.now().plusMillis(HANDSHAKE_TIMEOUT_MILLIS); } sessionReader.handshake_keyReqPart1 = null; sessionReader.handshake_keyReqPart2 = null; sendWelcome(sessionReader); }); } } else { throw new EncryptionError(Errors.BAD_VALUE + ": block got from unknown node " + packet.senderNodeId); } }
/** * If sessionRader for remote node is already created - returns it, otherwise creates new {@link SessionReader} * @param remoteId id of remote node, {@link NodeInfo} will be got from {@link UDPAdapter#netConfig} * @return sessionReader */ private SessionReader getOrCreateSessionReaderCandidate(int remoteId) { NodeInfo destination = netConfig.getInfo(remoteId); if (destination == null) { callErrorCallbacks("(getOrCreateSessionReaderCandidate) unknown nodeId has received: "+remoteId); return null; } SessionReader sr = sessionReaderCandidates.computeIfAbsent(destination.getNumber(), (k)-> { SessionReader sessionReader = new SessionReader(); sessionReader.remoteNodeInfo = destination; report(logLabel, ()->"sessionReader created for nodeId "+destination.getNumber(), VerboseLevel.BASE); return sessionReader; }); return sr; }
/** * Each adapter will try to send blocks until have got special {@link Packet} with type {@link PacketTypes#ACK}, * that means receiver have got block. So when we got block, but something went wrong - call this method. Note that * for success blocks needs to call {@link UDPAdapter#sendAck(SessionReader, Integer)} * @param nodeNumber node id in which sending is * @param packetId is id of block we have got. */ private void sendNack(Integer nodeNumber, Integer packetId) { try { NodeInfo destination = netConfig.getInfo(nodeNumber); if (destination != null) { report(logLabel, ()->"send nack to "+nodeNumber, VerboseLevel.DETAILED); byte[] randomSeed = Do.randomBytes(64); byte[] data = Boss.dumpToArray(Arrays.asList(packetId, randomSeed)); byte[] sign = new PrivateKey(ownPrivateKey.pack()).sign(data, HashType.SHA512); byte[] payload = Boss.dumpToArray(Arrays.asList(data, sign)); Packet packet = new Packet(0, myNodeInfo.getNumber(), nodeNumber, PacketTypes.NACK, payload); sendPacket(destination, packet); } } catch (EncryptionError e) { callErrorCallbacks("(sendNack) can't send NACK, EncryptionError: " + e); } }
nodesMap_s.put(nc_s.getInfo(i), nodes_s.get(i));