Refine search
/** * Bind to network interface to a random port. Start listenting for new connections. * The main difference from bindToRandomPort is that it allows system to pick up the right port. * For this reason return type is a string and its responsibility of a caller to parse into * the right integer type (e.g. for vmci transport it should be long). * * @param addr the endpoint to bind to. */ public String bindToSystemRandomPort(String addr) { if (ZMQ.version_full() < ZMQ.make_version(3, 2, 0)) throw new UnsupportedOperationException(); bind(String.format("%s:*", addr)); byte[] endpoint_bytes = getBytesSockopt(LAST_ENDPOINT); // NULL-terminated String endpoint_str = new String(Arrays.copyOf(endpoint_bytes, endpoint_bytes.length - 1)); String port = endpoint_str.substring(endpoint_str.lastIndexOf(":") + 1); return port; }
/** * Send a String. * * @param msg the message to send, as a String. * @param flags the flags to apply to the send operation. * @return true if send was successful, false otherwise. */ public boolean send(String msg, int flags) { byte[] b = msg.getBytes(); return send(b, 0, b.length, flags); }
/** * Bind to network interface to a random port. Start listening for new connections. * * @param addr the endpoint to bind to. * @param min_port The minimum port in the range of ports to try. * @param max_port The maximum port in the range of ports to try. * @param max_tries The number of attempt to bind. */ public int bindToRandomPort(String addr, int min_port, int max_port, int max_tries) { int port; Random rand = new Random(); for (int i = 0; i < max_tries; i++) { port = rand.nextInt(max_port - min_port + 1) + min_port; try { bind(String.format("%s:%s", addr, port)); return port; } catch (ZMQException e) { if (e.getErrorCode() != ZMQ.EADDRINUSE()) { throw e; } continue; } } throw new ZMQException("Could not bind socket to random port.", (int) ZMQ.EADDRINUSE()); }
@Override public List<String> call() throws Exception { try (ZMQ.Socket subscriber = context.socket(ZMQ.SUB)) { subscriber.connect(endpoint); subscriber.subscribe(new byte[0]); for (int messageNum = 0; messageNum < receiveCount && !Thread.currentThread().isInterrupted(); messageNum++) { // Use trim to remove the tailing '0' character messages.add(subscriber.recvStr(0).trim()); } } return messages; } }
private void handleStdIn() { byte[] buffer = stdinSocket.recv(); logger.info("Stdin: {}", new String(buffer)); }
public static void close_virtual_sockets(Map<Integer, Socket> mapping_atom) { synchronized (mapping_atom) { for (Entry<Integer, Socket> entry : mapping_atom.entrySet()) { entry.getValue().close(); } mapping_atom.clear(); } }
@Override public void send_local_task_empty(String storm_id, int virtual_port) { Socket pusher = ZeroMq.socket(context, ZeroMq.push); pusher = VirtualPort.virtual_connect(pusher, virtual_port); ZeroMq.send(pusher, new byte[0]); pusher.close(); };
public static Socket subscribe(Socket socket, byte[] topic) { socket.subscribe(topic); return socket; }
public static Socket send(Socket socket, byte[] message, int flags) { socket.send(message, flags); return socket; }
public static Socket set_linger(Socket socket, long linger_ms) { socket.setLinger(linger_ms); return socket; }
@Override public void close() { socket.close(); }
public static Socket connect(Socket socket, String url) { socket.connect(url); return socket; }
public static Socket bind(Socket socket, String url) { socket.bind(url); return socket; }
public static byte[] recv(Socket socket, int flags) { return socket.recv(flags); }
@Override public void shutdown() { Socket kill_socket = ZeroMq.socket(context, ZeroMq.push); ZeroMq.connect(kill_socket, url); VirtualPort.virtual_send(kill_socket, -1, new byte[0]); kill_socket.close(); LOG.info("Waiting for virtual port at url " + url + " to die"); try { vthread.join(); } catch (InterruptedException e) { } LOG.info("Shutdown virtual port at url: " + url); } }
private void closeSockets() { try { if (shellSocket != null) { shellSocket.close(); } if (controlSocket != null) { controlSocket.close(); } if (iopubSocket != null) { iopubSocket.close(); } if (stdinSocket != null) { stdinSocket.close(); } if (hearbeatSocket != null) { hearbeatSocket.close(); } context.close(); } catch (Exception e) { } }
@Override public void run() { if (socket != null) { byte[] data = ZeroMq.recv(socket); PacketPair packet = VirtualPort.parse_packet(data); if (packet.getPort() == -1) { LOG.info("Virtual port " + url + " received shutdown notice"); VirtualPort.close_virtual_sockets(virtual_mapping); socket.close(); socket = null; } else { if (valid_ports == null || valid_ports.contains(packet.getPort())) { Socket virtual_socket = VirtualPort.get_virtual_socket( context, virtual_mapping, packet.getPort(), url); ZeroMq.send(virtual_socket, packet.getMessage()); } else { //LOG.info("Received invalid message directed at port " // + packet.getPort() + ". Dropping..."); } } } }