public static 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; } String host = System.Net.Dns.GetHostName(); String remote = args[0]+"@"+host; OtpNode node = new OtpNode("q@"+host); System.Console.Out.WriteLine("This node is called {0} and is using cookie='{1}'.", node.node(), node.cookie()); bool ok=false; ok = node.ping(remote, 1000); if (ok) System.Console.Out.WriteLine(" successfully pinged node "+remote+"\n"); else System.Console.Out.WriteLine(" could not ping node "+remote+"\n"); OtpMbox mbox = null; try { mbox = node.createMbox(); Erlang.Object[] rpc = new Erlang.Object[2]; Erlang.Object[] call = new Erlang.Object[5]; call[0] = new Erlang.Atom("call"); call[1] = new Erlang.Atom("lists"); call[2] = new Erlang.Atom("reverse"); call[3] = new Erlang.List(new Erlang.List("Hello Erlang world!")); call[4] = mbox.self(); rpc[0] = mbox.self(); rpc[1] = new Erlang.Tuple(call); Erlang.Tuple rpcTuple = new Erlang.Tuple(rpc); System.Console.Out.WriteLine("=> "+rpcTuple.ToString()); mbox.send("rex", remote, rpcTuple); Erlang.Object reply = mbox.receive(1000); System.Console.Out.WriteLine("<= "+reply.ToString()); } catch (System.Exception) { } finally { node.closeMbox(mbox); } node.close(); }
/* * this method simulates net_kernel only for * the purpose of replying to pings. */ private bool netKernel(OtpMsg m) { OtpMbox mbox = null; try { Erlang.Tuple t = (Erlang.Tuple)(m.getMsg()); Erlang.Tuple req = (Erlang.Tuple)t.elementAt(1); // actual request Erlang.Pid pid = (Erlang.Pid)req.elementAt(0); // originating pid Erlang.Object[] pong = new Erlang.Object[2]; pong[0] = req.elementAt(1); // his #Ref pong[1] = new Erlang.Atom("yes"); mbox = createMbox(); mbox.send(pid, new Erlang.Tuple(pong)); return(true); } catch (System.Exception) { } finally { closeMbox(mbox); } return(false); }
/* * <p> Determine if another node is alive. This method has the side * effect of setting up a connection to the remote node (if * possible). Only a single outgoing message is sent; the timeout is * how long to wait for a response. </p> * * <p> Only a single attempt is made to connect to the remote node, * so for example it is not possible to specify an extremely long * timeout and expect to be notified when the node eventually comes * up. If you wish to wait for a remote node to be started, the * following construction may be useful: </p> * * <pre> * // ping every 2 seconds until positive response * while (!me.ping(him,2000)); * </pre> * * @param node the name of the node to ping. * * @param timeout the time, in milliseconds, to wait for response * before returning false. * * @return true if the node was alive and the correct ping response * was returned. false if the correct response was not returned on * time. **/ /*internal info about the message formats... * * the request: * -> REG_SEND {6,#Pid<[email protected]>,'',net_kernel} * {'$gen_call',{#Pid<[email protected]>,#Ref<[email protected]>},{is_auth,bingo@aule}} * * the reply: * <- SEND {2,'',#Pid<[email protected]>} * {#Ref<[email protected]>,yes} */ public virtual bool ping(System.String node, long timeout) { if (node.Equals(this._node)) { return(true); } OtpMbox mbox = null; try { mbox = createMbox(); mbox.send("net_kernel", node, getPingTuple(mbox)); Erlang.Object reply = mbox.receive(timeout); Erlang.Tuple t = (Erlang.Tuple)reply; Erlang.Atom a = (Erlang.Atom)(t.elementAt(1)); return("yes".Equals(a.atomValue())); } catch (System.Exception) { } finally { closeMbox(mbox); } return(false); }
/*create the outgoing ping message */ private Erlang.Tuple getPingTuple(OtpMbox mbox) { Erlang.Object[] ping = new Erlang.Object[3]; Erlang.Object[] pid = new Erlang.Object[2]; Erlang.Object[] _node = new Erlang.Object[2]; pid[0] = mbox.self(); pid[1] = createRef(); _node[0] = new Erlang.Atom("is_auth"); _node[1] = new Erlang.Atom(node()); ping[0] = new Erlang.Atom("$gen_call"); ping[1] = new Erlang.Tuple(pid); ping[2] = new Erlang.Tuple(_node); return(new Erlang.Tuple(ping)); }
public int encode_size(Erlang.Object o) { if (o is Erlang.Atom) { return(1 + 2 + o.atomValue().Length); } else if (o is Erlang.Boolean) { return(1 + 2 + (o.boolValue() ? Erlang.Boolean.s_true.atomValue().Length : Erlang.Boolean.s_false.atomValue().Length)); } else if (o is Erlang.Binary) { return(5 + o.binaryValue().Length); } else if (o is Erlang.Long) { long l = o.longValue(); if ((l & 0xff) == l) { return(2); } else if ((l <= OtpExternal.erlMax) && (l >= OtpExternal.erlMin)) { return(5); } return(long_arity(l)); } else if (o is Erlang.Byte) { return(1 + 1); } else if (o is Erlang.Double) { return(9); } else if (o is Erlang.String) { string l = o.stringValue(); if (l.Length == 0) { return(1); } if (l.Length < 0xffff) { return(2 + l.Length); } return(1 + 4 + 2 * l.Length); } else if (o is Erlang.List) { Erlang.List l = o.listValue(); if (l.arity() == 0) { return(1); } int sz = 5; for (int i = 0; i < l.arity(); i++) { sz += encode_size(l[i]); } return(sz); } else if (o is Erlang.Tuple) { Erlang.Tuple l = o.tupleValue(); int sz = 1 + (l.arity() < 0xff ? 1 : 4); for (int i = 0; i < l.arity(); i++) { sz += encode_size(l[i]); } return(sz); } else if (o is Erlang.Pid) { Erlang.Pid p = o.pidValue(); return(1 + (1 + 2 + p.node().Length) + 4 + 4 + 1); } else if (o is Erlang.Ref) { Erlang.Ref p = o.refValue(); int[] ids = p.ids(); return(1 + (1 + 2 + p.node().Length) + 1 + 4 * ids.Length); } else if (o is Erlang.Port) { Erlang.Port p = o.portValue(); return(1 + (1 + 2 + p.node().Length) + 4 + 1); } else { throw new Erlang.Exception("Unknown encode size for object: " + o.ToString()); } }
/*Send an auth error to peer because he sent a bad cookie. * The auth error uses his cookie (not revealing ours). * This is just like send_reg otherwise */ private void cookieError(OtpLocalNode local, Erlang.Atom cookie) { try { OtpOutputStream header = new OtpOutputStream(headerLen); // preamble: 4 byte length + "passthrough" tag + version header.write4BE(0); // reserve space for length header.write1(passThrough); header.write1(version); header.write_tuple_head(4); header.write_long((long)OtpMsg.Tag.regSendTag); header.write_any(local.createPid()); // disposable pid header.write_atom(cookie.atomValue()); // important: his cookie, not mine... header.write_atom("auth"); // version for payload header.write1(version); // the payload // the no_auth message (copied from Erlang) Don't change this (Erlang will crash) // {$gen_cast, {print, "~n** Unauthorized cookie ~w **~n", [foo@aule]}} Erlang.Object[] msg = new Erlang.Object[2]; Erlang.Object[] msgbody = new Erlang.Object[3]; msgbody[0] = new Erlang.Atom("print"); msgbody[1] = new Erlang.String("~n** Bad cookie sent to " + local + " **~n"); // Erlang will crash and burn if there is no third argument here... msgbody[2] = new Erlang.List(); // empty list msg[0] = new Erlang.Atom("$gen_cast"); msg[1] = new Erlang.Tuple(msgbody); OtpOutputStream payload = new OtpOutputStream(new Erlang.Tuple(msg)); // fix up length in preamble header.poke4BE(0, header.count() + payload.count() - 4); try { do_send(header, payload); } catch (System.IO.IOException) { } // ignore } finally { close(); throw new OtpAuthException("Remote cookie not authorized: " + cookie.atomValue()); } }
static public void Main(String[] args) { OtpTrace.TraceEvent("Otp test..."); if (args.Length < 1) { OtpTrace.TraceEvent("Usage: Otp sname\n where sname is" + "the short name of the Erlang node"); return; } String host = System.Net.Dns.GetHostName(); String remote = args[0] + "@" + host; OtpNode node = new OtpNode("q@" + host); OtpTrace.TraceEvent("This node is called {0} and is using cookie='{1}'.", node.node(), node.cookie()); bool ok = false; ok = node.ping(remote, 1000); if (ok) { OtpTrace.TraceEvent(" successfully pinged node " + remote + "\n"); } else { OtpTrace.TraceEvent(" could not ping node " + remote + "\n"); } OtpMbox mbox = null; try { mbox = node.createMbox(); Erlang.Object[] rpc = new Erlang.Object[2]; Erlang.Object[] call = new Erlang.Object[5]; call[0] = new Erlang.Atom("call"); call[1] = new Erlang.Atom("lists"); call[2] = new Erlang.Atom("reverse"); call[3] = new Erlang.List(new Erlang.List("Hello Erlang world!")); call[4] = mbox.self(); rpc[0] = mbox.self(); rpc[1] = new Erlang.Tuple(call); Erlang.Tuple rpcTuple = new Erlang.Tuple(rpc); OtpTrace.TraceEvent("=> " + rpcTuple.ToString()); mbox.send("rex", remote, rpcTuple); Erlang.Object reply = mbox.receive(1000); OtpTrace.TraceEvent("<= " + reply.ToString()); } catch (System.Exception) { } finally { node.closeMbox(mbox); } node.close(); }
/*create the outgoing ping message */ private Erlang.Tuple getPingTuple(OtpMbox mbox) { Erlang.Object[] ping = new Erlang.Object[3]; Erlang.Object[] pid = new Erlang.Object[2]; Erlang.Object[] _node = new Erlang.Object[2]; pid[0] = mbox.self(); pid[1] = createRef(); _node[0] = new Erlang.Atom("is_auth"); _node[1] = new Erlang.Atom(node()); ping[0] = new Erlang.Atom("$gen_call"); ping[1] = new Erlang.Tuple(pid); ping[2] = new Erlang.Tuple(_node); return new Erlang.Tuple(ping); }
/* * Send an RPC request to the remote Erlang node. This convenience * function creates the following message and sends it to 'rex' on * the remote node: * * <pre> * { self, { call, Mod, Fun, Args, user }} * </pre> * * <p> Note that this method has unpredicatble results if the remote * node is not an Erlang node. </p> * * @param mod the name of the Erlang module containing the function to be called. * @param fun the name of the function to call. * @param args a list of Erlang terms, to be used as arguments to the function. * * @exception C#.io.IOException if the connection is not active * or a communication error occurs. **/ public virtual void sendRPC(System.String mod, System.String fun, Erlang.List args) { Erlang.Object[] rpc = new Erlang.Object[2]; Erlang.Object[] call = new Erlang.Object[5]; /*{self, { call, Mod, Fun, Args, user}} */ call[0] = new Erlang.Atom("call"); call[1] = new Erlang.Atom(mod); call[2] = new Erlang.Atom(fun); call[3] = args; call[4] = new Erlang.Atom("user"); rpc[0] = this._self.pid(); rpc[1] = new Erlang.Tuple(call); send("rex", new Erlang.Tuple(rpc)); }
public void TestFormat() { { Erlang.Object obj1 = Erlang.Object.Format("a"); Assert.IsInstanceOf(typeof(Erlang.Atom), obj1); Assert.AreEqual("a", (obj1 as Erlang.Atom).atomValue()); } { Erlang.Object obj1 = Erlang.Object.Format("$a"); Assert.IsInstanceOf(typeof(Erlang.Char), obj1); Assert.AreEqual('a', (obj1 as Erlang.Char).charValue()); } { Erlang.Object obj1 = Erlang.Object.Format("'Abc'"); Assert.IsInstanceOf(typeof(Erlang.Atom), obj1); Assert.AreEqual("Abc", (obj1 as Erlang.Atom).atomValue()); } { Erlang.Object obj1 = Erlang.Object.Format("{'true', 'false', true, false}"); Assert.IsInstanceOf(typeof(Erlang.Tuple), obj1); Erlang.Tuple t = obj1.Cast <Erlang.Tuple>(); Assert.AreEqual(4, t.arity()); foreach (Erlang.Object term in t.elements()) { Assert.IsInstanceOf(typeof(Erlang.Boolean), term); } Assert.AreEqual(true, t[0].boolValue()); Assert.AreEqual(false, t[1].boolValue()); Assert.AreEqual(true, t[2].boolValue()); Assert.AreEqual(false, t[3].boolValue()); } { Erlang.Object obj1 = Erlang.Object.Format("\"Abc\""); Assert.IsInstanceOf(typeof(Erlang.String), obj1); Assert.AreEqual("Abc", (obj1 as Erlang.String).stringValue()); } { Erlang.Object obj1 = Erlang.Object.Format("Abc"); Assert.IsInstanceOf(typeof(Erlang.Var), obj1); Assert.AreEqual("Abc", (obj1 as Erlang.Var).name()); } { Erlang.Object obj1 = Erlang.Object.Format("1"); Assert.IsInstanceOf(typeof(Erlang.Long), obj1); Assert.AreEqual(1, (obj1 as Erlang.Long).longValue()); } { Erlang.Object obj1 = Erlang.Object.Format("1.23"); Assert.IsInstanceOf(typeof(Erlang.Double), obj1); Assert.AreEqual(1.23, (obj1 as Erlang.Double).doubleValue()); } { Erlang.Object obj1 = Erlang.Object.Format("V"); Assert.IsInstanceOf(typeof(Erlang.Var), obj1); Assert.AreEqual("V", (obj1 as Erlang.Var).name()); } { Erlang.Object obj1 = Erlang.Object.Format("{1}"); Assert.IsInstanceOf(typeof(Erlang.Tuple), obj1); Assert.AreEqual(1, (obj1 as Erlang.Tuple).arity()); Assert.IsInstanceOf(typeof(Erlang.Long), (obj1 as Erlang.Tuple)[0]); Assert.AreEqual(1, ((obj1 as Erlang.Tuple)[0] as Erlang.Long).longValue()); } { Erlang.Object obj0 = Erlang.Object.Format("[]"); Assert.IsInstanceOf(typeof(Erlang.List), obj0); Assert.AreEqual(0, (obj0 as Erlang.List).arity()); Erlang.Object obj1 = Erlang.Object.Format("[1]"); Assert.IsInstanceOf(typeof(Erlang.List), obj1); Assert.AreEqual(1, (obj1 as Erlang.List).arity()); Assert.IsInstanceOf(typeof(Erlang.Long), (obj1 as Erlang.List)[0]); Assert.AreEqual(1, ((obj1 as Erlang.List)[0] as Erlang.Long).longValue()); } { Erlang.Object obj1 = Erlang.Object.Format("[{1,2}, []]"); Assert.IsInstanceOf(typeof(Erlang.List), obj1); Assert.AreEqual(2, (obj1 as Erlang.List).arity()); Assert.IsInstanceOf(typeof(Erlang.Tuple), (obj1 as Erlang.List)[0]); Assert.AreEqual(2, ((obj1 as Erlang.List)[0] as Erlang.Tuple).arity()); Assert.AreEqual(0, ((obj1 as Erlang.List)[1] as Erlang.List).arity()); } { Erlang.Object obj1 = Erlang.Object.Format("{a, [b, 1, 2.0, \"abc\"], {1, 2}}"); Assert.IsInstanceOf(typeof(Erlang.Tuple), obj1); Assert.AreEqual(3, (obj1 as Erlang.Tuple).arity()); } { Erlang.Object obj1 = Erlang.Object.Format("~w", 1); Assert.IsInstanceOf(typeof(Erlang.Long), obj1); Assert.AreEqual(1, (obj1 as Erlang.Long).longValue()); Erlang.Object obj2 = Erlang.Object.Format("{~w, ~w,~w}", 1, 2, 3); Assert.IsInstanceOf(typeof(Erlang.Tuple), obj2); Assert.AreEqual(3, (obj2 as Erlang.Tuple).arity()); Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[0]); Assert.AreEqual(1, ((obj2 as Erlang.Tuple)[0] as Erlang.Long).longValue()); Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[1]); Assert.AreEqual(2, ((obj2 as Erlang.Tuple)[1] as Erlang.Long).longValue()); Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[2]); Assert.AreEqual(3, ((obj2 as Erlang.Tuple)[2] as Erlang.Long).longValue()); } { Erlang.Object obj2 = Erlang.Object.Format("{~w, ~w,~w,~w, ~w}", 1.0, 'a', "abc", 2, true); Assert.IsInstanceOf(typeof(Erlang.Tuple), obj2); Assert.AreEqual(5, (obj2 as Erlang.Tuple).arity()); Assert.IsInstanceOf(typeof(Erlang.Double), (obj2 as Erlang.Tuple)[0]); Assert.AreEqual(1.0, ((obj2 as Erlang.Tuple)[0] as Erlang.Double).doubleValue()); Assert.IsInstanceOf(typeof(Erlang.Char), (obj2 as Erlang.Tuple)[1]); Assert.AreEqual('a', ((obj2 as Erlang.Tuple)[1] as Erlang.Char).charValue()); Assert.IsInstanceOf(typeof(Erlang.String), (obj2 as Erlang.Tuple)[2]); Assert.AreEqual("abc", ((obj2 as Erlang.Tuple)[2] as Erlang.String).stringValue()); Assert.IsInstanceOf(typeof(Erlang.Long), (obj2 as Erlang.Tuple)[3]); Assert.AreEqual(2, ((obj2 as Erlang.Tuple)[3] as Erlang.Long).longValue()); Assert.IsInstanceOf(typeof(Erlang.Boolean), (obj2 as Erlang.Tuple)[4]); Assert.AreEqual(true, ((obj2 as Erlang.Tuple)[4] as Erlang.Boolean).booleanValue()); } }