@Override public void send(int partyId, byte[] data) { if (partyId == conf.getMyId()) { this.selfQueue.add(data); } else { inRange(partyId); if (!senders.get(partyId).isRunning()) { throw new RuntimeException( "P" + conf.getMyId() + ": Unable to send to P" + partyId + ". Sender not running"); } this.senders.get(partyId).queueMessage(data); } }
/** * Closes the network down and releases held resources. */ @Override public void close() { if (alive) { alive = false; if (conf.noOfParties() < 2) { logger.info("P{}: Network closed", conf.getMyId()); return; } ExceptionConverter.safe(() -> { closeCommunication(); logger.info("P{}: Network closed", conf.getMyId()); return null; }, "Unable to properly close the network."); } else { logger.info("P{}: Network already closed", conf.getMyId()); } }
/** * Listens for connections from the opposing parties with lower id's. * * @throws IOException thrown if an {@link IOException} occurs while listening. */ private Map<Integer, Socket> connectServer(final NetworkConfiguration conf) throws IOException { Map<Integer, Socket> socketMap = new HashMap<>(conf.getMyId() - 1); if (conf.getMyId() > 1) { try (ServerSocket server = serverFactory.createServerSocket(conf.getMe().getPort())) { logger.info("P{}: bound at port {}", conf.getMyId(), conf.getMe().getPort()); for (int i = 1; i < conf.getMyId(); i++) { Socket sock = server.accept(); int id = 0; for (int j = 0; j < PARTY_ID_BYTES; j++) { id ^= sock.getInputStream().read() << j * Byte.SIZE; } socketMap.put(id, sock); logger.info("P{}: accepted connection from P{}", conf.getMyId(), id); socketMap.put(id, sock); } } } return socketMap; }
Map<Integer, Socket> socketMap = new HashMap<>(conf.noOfParties() - conf.getMyId()); for (int i = conf.getMyId() + 1; i <= conf.noOfParties(); i++) { Party p = conf.getParty(i); boolean connectionMade = false; int attempts = 0; Socket sock = socketFactory.createSocket(p.getHostname(), p.getPort()); for (int j = 0; j < PARTY_ID_BYTES; j++) { byte b = (byte) (conf.getMyId() >>> j * Byte.SIZE); sock.getOutputStream().write(b); logger.info("P{}: connected to {}", conf.getMyId(), p); } catch (ConnectException e) {
@Test public void testToString() { String s = netConf.toString(); assertTrue(s.startsWith("NetworkConfigurationImpl")); assertTrue(s.contains("myId=" + me.getPartyId())); for (int i = 1; i < netConf.noOfParties() + 1; i++) { assertTrue(s.contains(netConf.getParty(i).toString())); } }
@Override public int getNoOfParties() { return this.conf.noOfParties(); } }
@Test(expected = RuntimeException.class, timeout = TWO_MINUTE_TIMEOUT_MILLIS) public void testFailToBind() throws Throwable { ServerSocket socket = null; try { List<NetworkConfiguration> confs = getNetConfs(2); socket = ServerSocketFactory.getDefault().createServerSocket(confs.get(1).getMe().getPort()); newCloseableNetwork(confs.get(1)); } finally { socket.close(); } }
@Test public void testPortIsFree() throws IOException { int port = confs.get(1).getParty(1).getPort(); ServerSocket s = new ServerSocket(port); s.close(); }
Map<Integer, Socket> socketMap = new HashMap<>(conf.noOfParties());
@Override public byte[] receive(final int partyId) { if (partyId == conf.getMyId()) { return ExceptionConverter.safe(selfQueue::take, "Receiving from self failed"); } inRange(partyId); byte[] data; data = receivers.get(partyId).pollMessage(RECEIVE_TIMEOUT); while (data == null) { if (!receivers.get(partyId).isRunning()) { throw new RuntimeException("P" + conf.getMyId() + ": Unable to recieve from P" + partyId + ". Receiver not running"); } data = receivers.get(partyId).pollMessage(RECEIVE_TIMEOUT); } return data; }
Objects.requireNonNull(conf); Objects.requireNonNull(socketMap); for (int i = 1; i < conf.noOfParties() + 1; i++) { if (i == conf.getMyId()) { continue; int externalParties = conf.noOfParties() - 1; this.receivers = new HashMap<>(externalParties); this.senders = new HashMap<>(externalParties); this.alive = true; this.selfQueue = new LinkedBlockingQueue<>(); if (conf.noOfParties() > 1) { this.sockets = Collections.unmodifiableCollection(new ArrayList<>(socketMap.values())); startCommunication(socketMap);
@Test public void testNoOfParties() { assertEquals(numParties, netConf.noOfParties()); }
NetworkConfiguration networkConfiguration = cmdUtil.getNetworkConfiguration(); if (networkConfiguration.getMyId() == 1 || networkConfiguration.getMyId() == 2) { if (!cmd.hasOption("x") || !cmd.hasOption("y")) { cmdUtil.displayHelp(); DistanceDemo distDemo = new DistanceDemo(networkConfiguration.getMyId(), x, y); SecureComputationEngine<ResourcePoolT, ProtocolBuilderNumeric> sce = cmdUtil.getSce(); ResourcePoolT resourcePool = cmdUtil.getResourcePool();
protected Map<Integer, CloseableNetwork> createNetworks(List<NetworkConfiguration> confs) { int numParties = confs.get(0).noOfParties(); ExecutorService es = Executors.newFixedThreadPool(numParties); Map<Integer, CloseableNetwork> netMap = new HashMap<>(numParties); Map<Integer, Future<CloseableNetwork>> futureMap = new HashMap<>(numParties); try { for (NetworkConfiguration conf : confs) { Future<CloseableNetwork> f = es.submit(() -> newCloseableNetwork(conf)); futureMap.put(conf.getMyId(), f); } for (Entry<Integer, Future<CloseableNetwork>> entry : futureMap.entrySet()) { netMap.put(entry.getKey(), entry.getValue().get()); } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); fail("Failed to setup networks."); } finally { es.shutdownNow(); } return netMap; }
if (networkConfiguration.getMyId() == 1 || networkConfiguration.getMyId() == 2) { if (!cmd.hasOption("in")) { throw new IllegalArgumentException("Player 1 and 2 must submit inputs"); PrivateSetDemo privateSetDemo = new PrivateSetDemo(networkConfiguration.getMyId(), key, inputs); ProtocolSuite<T, ProtocolBuilderBinary> psConf = util.getProtocolSuite(); SecureComputationEngine<T, ProtocolBuilderBinary> sce =
cmd.getOptionProperties("D"), this.networkConfiguration.getMyId(), networkConfiguration.noOfParties(), network ); protocolSuite = (ProtocolSuite<ResourcePoolT, BuilderT>)
@Test public void testMyIdIsCorrect() { assertEquals(1, confs.get(1).getMyId()); }