/* * find or create a connection to the given node */ public virtual OtpCookedConnection connection(System.String node) { OtpPeer peer = null; OtpCookedConnection conn = null; lock (connections) { // first just try looking up the name as-is conn = (OtpCookedConnection)connections[node]; if (conn == null) { // in case node had no '@' add localhost info and try again peer = new OtpPeer(node); conn = (OtpCookedConnection)connections[peer.node()]; if (conn == null) { try { conn = new OtpCookedConnection(this, peer); addConnection(conn); } catch (System.Exception e) { /*false = outgoing */ connAttempt(peer.node(), false, e); } } } return(conn); } }
// this function used internally when "process" dies // since Erlang discerns between exit and exit/2. private void exit(int arity, Erlang.Pid to, System.String reason) { try { System.String node = to.node(); if (node.Equals(home.node())) { home.deliver(new OtpMsg(OtpMsg.Tag.exitTag, _self, to, reason)); } else { OtpCookedConnection conn = home.connection(node); if (conn == null) { return; } switch (arity) { case 1: conn.exit(_self, to, reason); break; case 2: conn.exit2(_self, to, reason); break; } } } catch (System.Exception) { } }
private void removeConnection(OtpCookedConnection conn) { if ((conn != null) && (conn.name != null)) { connections.Remove(conn.name); } }
/* * <p> Link to a remote mailbox or Erlang process. Links are * idempotent, calling this method multiple times will not result in * more than one link being created. </p> * * <p> If the remote process subsequently exits or the mailbox is * closed, a subsequent attempt to retrieve a message through this * mailbox will cause an {@link Exit Exit} * exception to be raised. Similarly, if the sending mailbox is * closed, the linked mailbox or process will receive an exit * signal. </p> * * <p> If the remote process cannot be reached in order to set the * link, the exception is raised immediately. </p> * * @param to the {@link Pid pid} representing the object to * link to. * * @exception Exit if the {@link Pid pid} referred * to does not exist or could not be reached. * **/ public virtual void link(Erlang.Pid to) { try { System.String node = to.node(); if (node.Equals(home.node())) { if (!home.deliver(new OtpMsg(OtpMsg.Tag.linkTag, _self, to))) { throw new Erlang.Exit("noproc", to); } } else { OtpCookedConnection conn = home.connection(node); if (conn != null) { conn.link(_self, to); } else { throw new Erlang.Exit("noproc", to); } } } catch (Erlang.Exit e) { throw e; } catch (System.Exception) { } links.addLink(_self, to); }
private void addConnection(OtpCookedConnection conn) { if ((conn != null) && (conn.name != null)) { connections[conn.name] = conn; remoteStatus(conn.name, true, null); } }
public void sendRPCcast(string node, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args, Erlang.Object ioServer) { if (node.Equals(home.node())) { throw new System.ArgumentException("Cannot make rpc cast on local node!"); } else { Erlang.Object msg = AbstractConnection.encodeRPCcast(_self, mod, fun, args, ioServer); OtpCookedConnection conn = home.connection(node); if (conn == null) { throw new System.Exception("Cannot establish connection to node " + node); } conn.send(_self, "rex", msg); } }
/* * Send a message to a named mailbox created from another node. * * @param name the registered name of recipient mailbox. * * @param node the name of the remote node where the recipient * mailbox is registered. * * @param msg the body of the message to send. * **/ public void send(System.String name, System.String node, Erlang.Object msg) { try { if (node.Equals(home.node())) { send(name, msg); } else { OtpCookedConnection conn = home.connection(node); if (conn == null) { return; } conn.send(_self, name, msg); } } catch (System.Exception) { } }
/* * Send a message to a remote {@link Pid pid}, representing * either another {@link OtpMbox mailbox} or an Erlang process. * * @param to the {@link Pid pid} identifying the intended * recipient of the message. * * @param msg the body of the message to send. * **/ public void send(Erlang.Pid to, Erlang.Object msg) { try { System.String node = to.node(); if (node.Equals(home.node())) { home.deliver(new OtpMsg(to, (Erlang.Object)(msg.clone()))); } else { OtpCookedConnection conn = home.connection(node); if (conn == null) { return; } conn.send(_self, to, msg); } } catch (System.Exception) { } }
/* * <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 Pid * pid}. </p> * * @param to the {@link Pid pid} representing the object to * unlink from. * **/ public virtual void unlink(Erlang.Pid to) { links.removeLink(_self, to); try { System.String node = to.node(); if (node.Equals(home.node())) { home.deliver(new OtpMsg(OtpMsg.Tag.unlinkTag, _self, to)); } else { OtpCookedConnection conn = home.connection(node); if (conn != null) { conn.unlink(_self, to); } } } catch (System.Exception) { } }
static public void Main(String[] args) { System.Console.Out.WriteLine("Otp test..."); if (args.Length < 1) { System.Console.Out.WriteLine("Usage: Otp sname\n where sname is" + "the short name of the Erlang node"); return; } OtpNode.useShortNames = true; String host = System.Net.Dns.GetHostName(); String user = Environment.UserName; OtpNode node = new OtpNode(user + "@" + host); String remote = (args[0].Contains("@")) ? args[0] : remote = args[0] + "@" + host; OtpMbox mbox = null; System.Console.Out.WriteLine("This node is: {0} (cookie='{1}'). Remote: {2}", node.node(), node.cookie(), remote); //bool ok = node.ping(remote, 1000*300); OtpCookedConnection conn = node.getConnection(remote); try { if (conn != null) { System.Console.Out.WriteLine(" successfully pinged node " + remote + "\n"); } else { throw new System.Exception("Could not ping node: " + remote); } conn.traceLevel = 1; mbox = node.createMbox(); mbox.registerName("server"); mbox.sendRPC(conn.peer.node(), "lists", "reverse", new Erlang.List(new Erlang.String("Hello world!"))); Erlang.Object reply = mbox.receiveRPC(5000); System.Console.Out.WriteLine("<= " + reply.ToString()); { Erlang.List rpcArgs = new Erlang.List( new Erlang.Object[] { mbox.self(), new Erlang.Tuple( new Erlang.Object[] { new Erlang.Atom("table"), new Erlang.Atom("test"), new Erlang.Atom("simple") } ) } ); mbox.sendRPC(conn.peer.node(), "mnesia_subscr", "subscribe", rpcArgs); reply = mbox.receiveRPC(5000); System.Console.Out.WriteLine("<= " + reply.ToString()); } while (true) { Erlang.Object msg = mbox.receive(); System.Console.Out.WriteLine("IN msg: " + msg.ToString() + "\n"); } } catch (System.Exception e) { System.Console.Out.WriteLine("Error: " + e.ToString()); } finally { node.closeMbox(mbox); } node.close(); }
public void Start() { System.Net.Sockets.TcpClient newsock = null; OtpCookedConnection conn = null; a_node.localStatus(a_node._node, true, null); while (!a_done) { conn = null; try { newsock = a_sock.AcceptTcpClient(); } catch (System.Exception e) { a_node.localStatus(a_node._node, false, e); goto accept_loop_brk; } try { lock (a_node.connections) { conn = new OtpCookedConnection(a_node, newsock); a_node.addConnection(conn); } } catch (OtpAuthException e) { if (conn != null && conn.name != null) { a_node.connAttempt(conn.name, true, e); } else { a_node.connAttempt("unknown", true, e); } closeSock(newsock); } catch (System.IO.IOException e) { if (conn != null && conn.name != null) { a_node.connAttempt(conn.name, true, e); } else { a_node.connAttempt("unknown", true, e); } closeSock(newsock); } catch (System.Exception e) { closeSock(newsock); closeSock(a_sock); a_node.localStatus(a_node._node, false, e); goto accept_loop_brk; } } accept_loop_brk :; // while // if we have exited loop we must do this too unPublishPort(); }
/* * find or create a connection to the given node */ public virtual OtpCookedConnection connection(System.String node, string cookie) { OtpPeer peer = null; OtpCookedConnection conn = null; lock(connections) { // first just try looking up the name as-is conn = (OtpCookedConnection) connections[node]; if (conn == null) { // in case node had no '@' add localhost info and try again peer = new OtpPeer(node, _useShortName); conn = (OtpCookedConnection) connections[peer.node()]; if (conn == null) { try { conn = new OtpCookedConnection(this, peer, cookie); addConnection(conn); } catch (System.Exception e) { /*false = outgoing */ connAttempt(peer.node(), false, e); } } } return conn; } }
/* * OtpCookedConnection delivers errors here, we send them on to the * handler specified by the application */ internal virtual void deliverError(OtpCookedConnection conn, System.Exception e) { removeConnection(conn); remoteStatus(conn.name, false, e); }
static public void Main(String[] args) { System.Console.Out.WriteLine("Otp test..."); string cookie = OtpNode.defaultCookie; string host = System.Net.Dns.GetHostName(); string remote = (args[0].IndexOf('@') < 0) ? args[0] + "@" + host : args[0]; string nodename = Environment.UserName + "123@" + host; AbstractConnection.traceLevel = OtpTrace.Type.sendThreshold; if (args.Length < 1) { System.Console.Out.WriteLine( "Usage: {0} remotenode [cookie] [-notrace]\n" + " nodename - is the name of the remote Erlang node\n" + " cookie - is the optional cookie string to use\n" + " -name - this node name\n" + " -wire - wire-level tracing\n" + " -notrace - disable debug trace\n", Environment.GetCommandLineArgs()[0]); return; } else if (args.Length > 1 && args[1][0] != '-') { cookie = args[1].ToString(); } for (int i = 0; i < args.Length; i++) { if (args[i].Equals("-wire")) { AbstractConnection.traceLevel = OtpTrace.Type.wireThreshold; } else if (args[i].Equals("-notrace")) { AbstractConnection.traceLevel = OtpTrace.Type.defaultLevel; } else if (args[i].Equals("-name") && i + 1 < args.Length) { nodename = args[i++ + 1]; if (nodename.IndexOf('@') < 0) { nodename += '@' + host; } } } OtpNode node = new OtpNode(false, nodename, cookie, true); System.Console.Out.WriteLine("This node is called {0} and is using cookie='{1}'.", node.node(), node.cookie()); OtpCookedConnection.ConnectTimeout = 2000; OtpCookedConnection conn = node.connection(remote); conn.OnReadWrite += OnReadWrite; if (conn == null) { Console.WriteLine("Can't connect to node " + remote); return; } // If using short names or IP address as the host part of the node name, // get the short name of the peer. remote = conn.peer.node(); System.Console.Out.WriteLine(" successfully connected to node " + remote + "\n"); OtpMbox mbox = null; try { mbox = node.createMbox(); { Otp.Erlang.Object reply = mbox.rpcCall( remote, "lists", "reverse", new Otp.Erlang.List("Abcdef!")); System.Console.Out.WriteLine("<= [REPLY1]:" + (reply == null ? "null" : reply.ToString())); } { Otp.Erlang.Object reply = mbox.rpcCall( remote, "global", "register_name", new Otp.Erlang.List(new Otp.Erlang.Atom("me"), mbox.self())); System.Console.Out.WriteLine("<= [REPLY2]:" + (reply == null ? "null" : reply.ToString())); } { Otp.Erlang.Object reply = mbox.rpcCall(remote, "global", "register_name", new Otp.Erlang.List(new Otp.Erlang.Atom("me"), mbox.self()), 5000); System.Console.Out.WriteLine("<= [REPLY3]:" + (reply == null ? "null" : reply.ToString())); } { Otp.Erlang.Object reply = mbox.rpcCall( remote, "io", "format", new Otp.Erlang.List( "Test: ~w -> ~w\n", new Otp.Erlang.List(mbox.self(), new Otp.Erlang.Atom("ok")) )); System.Console.Out.WriteLine("<= [REPLY4]:" + (reply == null ? "null" : reply.ToString())); } while (true) { Otp.Erlang.Object msg = mbox.receive(); if (msg is Otp.Erlang.Tuple) { Otp.Erlang.Tuple m = msg as Otp.Erlang.Tuple; if (m.arity() == 2 && m.elementAt(0) is Otp.Erlang.Pid) { mbox.send(m.elementAt(0) as Otp.Erlang.Pid, m.elementAt(1)); } } System.Console.Out.WriteLine("IN msg: " + msg.ToString() + "\n"); } } catch (System.Exception e) { System.Console.Out.WriteLine("Error: " + e.ToString()); } finally { node.closeMbox(mbox); } node.close(); }
public void Start() { System.Net.Sockets.TcpClient newsock = null; OtpCookedConnection conn = null; a_node.localStatus(a_node._node, true, null); while (!a_done) { conn = null; try { newsock = a_sock.AcceptTcpClient(); } catch (System.Exception e) { a_node.localStatus(a_node._node, false, e); goto accept_loop_brk; } try { lock(a_node.connections) { conn = new OtpCookedConnection(a_node, newsock); a_node.addConnection(conn); } } catch (OtpAuthException e) { if (conn != null && conn.name != null) a_node.connAttempt(conn.name, true, e); else a_node.connAttempt("unknown", true, e); closeSock(newsock); } catch (System.IO.IOException e) { if (conn != null && conn.name != null) a_node.connAttempt(conn.name, true, e); else a_node.connAttempt("unknown", true, e); closeSock(newsock); } catch (System.Exception e) { closeSock(newsock); closeSock(a_sock); a_node.localStatus(a_node._node, false, e); goto accept_loop_brk; } } accept_loop_brk: ; // while // if we have exited loop we must do this too unPublishPort(); }