public static OtpErlangTuple quote(@NotNull String code) throws IOException, OtpErlangExit, OtpErlangDecodeException { final OtpNode otpNode = IntellijElixir.getLocalNode(); final OtpMbox otpMbox = otpNode.createMbox(); OtpErlangObject request = elixirString(code); return (OtpErlangTuple) GenericServer.INSTANCE.call( otpMbox, otpNode, REMOTE_NAME, IntellijElixir.REMOTE_NODE, request, TIMEOUT_IN_MILLISECONDS ); }
public static OtpNode getLocalNode() throws IOException { if (localNode == null) { localNode = new OtpNode("intellij-elixir@127.0.0.1", "intellij_elixir"); } return localNode; } }
private void exit(final int arity, final OtpErlangPid to, final OtpErlangObject reason) { try { final String node = to.node(); if (node.equals(home.node())) { home.deliver(new OtpMsg(OtpMsg.exitTag, self, to, reason)); } else { final OtpCookedConnection conn = home.getConnection(node); if (conn == null) { return; } switch (arity) { case 1: conn.exit(self, to, reason); break; case 2: conn.exit2(self, to, reason); break; } } } catch (final Exception e) { } }
mbox = createMbox(); mbox.send("net_kernel", anode, getPingTuple(mbox)); final OtpErlangObject reply = mbox.receive(timeout); } catch (final Exception e) { } finally { closeMbox(mbox);
OtpCookedConnection conn = null; localStatus(node, true, null); localStatus(node, false, e); conn = new OtpCookedConnection(OtpNode.this, newsock); conn.setFlags(connFlags); addConnection(conn); connAttempt(conn.name, true, e); } else { connAttempt("unknown", true, e); connAttempt(conn.name, true, e); } else { connAttempt("unknown", true, e); closeSock(newsock); closeSock(sock); localStatus(node, false, e); break accept_loop;
private boolean netKernel(final OtpMsg m) { OtpMbox mbox = null; try { final OtpErlangTuple t = (OtpErlangTuple) m.getMsg(); final OtpErlangTuple req = (OtpErlangTuple) t.elementAt(1); // actual // request final OtpErlangPid pid = (OtpErlangPid) req.elementAt(0); // originating // pid final OtpErlangObject[] pong = new OtpErlangObject[2]; pong[0] = req.elementAt(1); // his #Ref pong[1] = new OtpErlangAtom("yes"); mbox = createMbox(); mbox.send(pid, new OtpErlangTuple(pong)); return true; } catch (final Exception e) { } finally { closeMbox(mbox); } return false; }
OtpCookedConnection getConnection(final String anode) { OtpPeer peer = null; OtpCookedConnection conn = null; synchronized (connections) { // first just try looking up the name as-is conn = connections.get(anode); if (conn == null) { // in case node had no '@' add localhost info and try again peer = new OtpPeer(anode); conn = connections.get(peer.node()); if (conn == null) { try { conn = new OtpCookedConnection(this, peer); conn.setFlags(connFlags); addConnection(conn); } catch (final Exception e) { /* false = outgoing */ connAttempt(peer.node(), false, e); } } } return conn; } }
private OtpErlangTuple getPingTuple(final OtpMbox mbox) { final OtpErlangObject[] ping = new OtpErlangObject[3]; final OtpErlangObject[] pid = new OtpErlangObject[2]; final OtpErlangObject[] anode = new OtpErlangObject[2]; pid[0] = mbox.self(); pid[1] = createRef(); anode[0] = new OtpErlangAtom("is_auth"); anode[1] = new OtpErlangAtom(node()); ping[0] = new OtpErlangAtom("$gen_call"); ping[1] = new OtpErlangTuple(pid); ping[2] = new OtpErlangTuple(anode); return new OtpErlangTuple(ping); }
/** * Send a message to a named mailbox created from the same node as this * mailbox. * * @param aname * the registered name of recipient mailbox. * * @param msg * the body of the message to send. * */ public void send(final String aname, final OtpErlangObject msg) { home.deliver(new OtpMsg(self, aname, (OtpErlangObject) msg.clone())); }
/** * Close this mailbox. * * <p> * After this operation, the mailbox will no longer be able to receive * messages. Any delivered but as yet unretrieved messages can still be * retrieved however. * </p> * * <p> * If there are links from this mailbox to other {@link OtpErlangPid pids}, * they will be broken when this method is called and exit signals with * reason 'normal' will be sent. * </p> * * <p> * This is equivalent to {@link #exit(String) exit("normal")}. * </p> */ public void close() { home.closeMbox(this); }
public OtpMbox create() { final OtpErlangPid pid = createPid(); final OtpMbox m = new OtpMbox(OtpNode.this, pid); byPid.put(pid, new WeakReference<OtpMbox>(m)); return m; }
@Override protected void finalize() { close(); }
Acceptor(final int port) throws IOException { sock = createServerTransport(port); acceptorPort = sock.getLocalPort(); OtpNode.this.port = acceptorPort; setDaemon(true); setName("acceptor"); publishPort(); start(); }
/** * Send a message to a remote {@link OtpErlangPid pid}, representing either * another {@link OtpMbox mailbox} or an Erlang process. * * @param to * the {@link OtpErlangPid pid} identifying the intended * recipient of the message. * * @param msg * the body of the message to send. * */ public void send(final OtpErlangPid to, final OtpErlangObject msg) { try { final String node = to.node(); if (node.equals(home.node())) { home.deliver(new OtpMsg(to, (OtpErlangObject) msg.clone())); } else { final OtpCookedConnection conn = home.getConnection(node); if (conn == null) { return; } conn.send(self, to, msg); } } catch (final Exception e) { } }
synchronized void breakLinks() { if (links != null) { final Link[] l = links.clearLinks(); if (l != null) { final int len = l.length; for (int i = 0; i < len; i++) { // send exit "from" remote pids to local ones self.deliver(new OtpMsg(OtpMsg.exitTag, l[i].remote(), l[i] .local(), new OtpErlangAtom("noconnection"))); } } } } }
/** * Close this mailbox with the given reason. * * <p> * After this operation, the mailbox will no longer be able to receive * messages. Any delivered but as yet unretrieved messages can still be * retrieved however. * </p> * * <p> * If there are links from this mailbox to other {@link OtpErlangPid pids}, * they will be broken when this method is called and exit signals will be * sent. * </p> * * @param reason * an Erlang term describing the reason for the exit. */ public void exit(final OtpErlangObject reason) { home.closeMbox(this, reason); }
public OtpMbox create(final String name) { OtpMbox m = null; synchronized (byName) { if (get(name) != null) { return null; } final OtpErlangPid pid = createPid(); m = new OtpMbox(OtpNode.this, pid, name); byPid.put(pid, new WeakReference<OtpMbox>(m)); byName.put(name, new WeakReference<OtpMbox>(m)); } return m; }
/** * <p> * Remove a link to a remote mailbox or Erlang process. This method removes * a link created with {@link #link link()}. Links are idempotent; calling * this method once will remove all links between this mailbox and the * remote {@link OtpErlangPid pid}. * </p> * * @param to * the {@link OtpErlangPid pid} representing the object to unlink * from. * */ public void unlink(final OtpErlangPid to) { links.removeLink(self, to); try { final String node = to.node(); if (node.equals(home.node())) { home.deliver(new OtpMsg(OtpMsg.unlinkTag, self, to)); } else { final OtpCookedConnection conn = home.getConnection(node); if (conn != null) { conn.unlink(self, to); } } } catch (final Exception e) { } }
@Override public void deliver(final OtpMsg msg) { final boolean delivered = self.deliver(msg); switch (msg.type()) { case OtpMsg.linkTag: if (delivered) { links.addLink(msg.getRecipientPid(), msg.getSenderPid()); } else { try { // no such pid - send exit to sender super.sendExit(msg.getRecipientPid(), msg.getSenderPid(), new OtpErlangAtom("noproc")); } catch (final IOException e) { } } break; case OtpMsg.unlinkTag: case OtpMsg.exitTag: links.removeLink(msg.getRecipientPid(), msg.getSenderPid()); break; case OtpMsg.exit2Tag: break; } return; }
/** * Close the specified mailbox with reason 'normal'. * * @param mbox * the mailbox to close. * * <p> * After this operation, the mailbox will no longer be able to * receive messages. Any delivered but as yet unretrieved * messages can still be retrieved however. * </p> * * <p> * If there are links from the mailbox to other * {@link OtpErlangPid pids}, they will be broken when this * method is called and exit signals with reason 'normal' will be * sent. * </p> * */ public void closeMbox(final OtpMbox mbox) { closeMbox(mbox, new OtpErlangAtom("normal")); }