예제 #1
0
    public static Tuple2<PrivateNodeId, RemoteNodeAddress> generate() {
      final Tuple2<RSAPublicKey, RSAPrivateKey> kp = TrCrypto.createRsaKeyPair();

      final PrivateNodeId privateNodeId = new PrivateNodeId();
      privateNodeId.privateKey = kp.b;

      final RemoteNodeAddress remoteNodeAddress = new RemoteNodeAddress(null, kp.a);

      return Tuple2.of(privateNodeId, remoteNodeAddress);
    }
예제 #2
0
  public TrNode(final File rootDirectory, final TrNodeConfig config) throws SocketException {
    this.rootDirectory = rootDirectory;
    this.config = config;
    this.identityStoreFile =
        new File(rootDirectory + System.getProperty("file.separator") + "id-store.json");

    privNodeIdFile = new File(rootDirectory, config.privateNodeId);
    pubNodeIdFile = new File(rootDirectory, config.publicNodeId);
    if (!privNodeIdFile.exists()) {
      logger.info("Generating new Node ID");
      final Tuple2<PrivateNodeId, RemoteNodeAddress> kp = PrivateNodeId.generate();
      Persistence.save(privNodeIdFile, kp.a);
      Persistence.save(pubNodeIdFile, kp.b);
    }
    if (config.localHostName != null) {
      modifyPublicNodeId(
          new ModifyBlock<RemoteNodeAddress>() {

            public void run(final RemoteNodeAddress remoteNodeAddress, final Modified modified) {
              try {
                remoteNodeAddress.physicalLocation =
                    new UdpNetworkLocation(
                        InetAddress.getByName(config.localHostName), config.udp.listenPort);
              } catch (final UnknownHostException e) {
                logger.error("Failed to set local node address", e);
              }
            }
          });
    }
    publicNodeIdsDir = new File(rootDirectory, config.publicNodeIdsDir);
    if (!publicNodeIdsDir.exists()) {
      publicNodeIdsDir.mkdir();
    }

    logger.info("Set up UDP network interface");
    final Tuple2<RSAPublicKey, RSAPrivateKey> keyPair =
        Tuple2.of(getRemoteNodeAddress().publicKey, getPrivateNodeId().privateKey);
    final TrNetworkInterface uni = new UdpNetworkInterface(config.udp, keyPair);
    sessionMgr = new TrSessionManager(this, uni, config.capabilities.allowsUnsolicitiedInbound);

    logger.info("Set up peer manager");
    peerManager = new TrPeerManager(config.peers, this);

    registerSessions();

    if (config.peers.runBroadcast) {
      mbClasses = new MicrobloggingClasses(this);
    }
  }