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(); }
// send has receiver pid but no sender information internal OtpMsg(Erlang.Pid to, OtpInputStream paybuf) { this.tag = sendTag; this.from = null; this.to = to; this.toName = null; this.paybuf = paybuf; this.payload = null; }
public void TestFormatVariable() { var cases = new Dictionary <string, Erlang.TermType> { { "B", Erlang.TermType.Object }, { "B::int()", Erlang.TermType.Int }, { "B::integer()", Erlang.TermType.Int }, { "B::string()", Erlang.TermType.String }, { "B::atom()", Erlang.TermType.Atom }, { "B::float()", Erlang.TermType.Double }, { "B::double()", Erlang.TermType.Double }, { "B::binary()", Erlang.TermType.Binary }, { "B::bool()", Erlang.TermType.Boolean }, { "B::boolean()", Erlang.TermType.Boolean }, { "B::byte()", Erlang.TermType.Byte }, { "B::char()", Erlang.TermType.Char }, { "B::list()", Erlang.TermType.List }, { "B::tuple()", Erlang.TermType.Tuple }, { "B::pid()", Erlang.TermType.Pid }, { "B::ref()", Erlang.TermType.Ref }, { "B::reference()", Erlang.TermType.Ref }, { "B::port()", Erlang.TermType.Port } }; foreach (var p in cases) { Erlang.Object o = Erlang.Object.Format(p.Key); Assert.IsInstanceOf(typeof(Erlang.Var), o); Assert.AreEqual(p.Value, o.Cast <Erlang.Var>().VarTermType); } var pat1 = Erlang.Object.Format("{A::char(), B::tuple(), C::float(), D::list(), [E::string(), F::int()], G::bool()}"); var obj1 = Erlang.Object.Format("{$a, {1,2,3}, 10.0, [5,6], [\"abc\", 190], true}"); var binding = new Erlang.VarBind(); Assert.IsTrue(pat1.match(obj1, binding)); // Match unbound variables Assert.IsTrue(pat1.match(obj1, binding)); // Match bound variables var obj2 = Erlang.Object.Format("{$a, {1,2,3}, 20.0, [5,6], [\"abc\", 190], true}"); Assert.IsFalse(pat1.match(obj2, binding)); // Match bound variables binding.clear(); var obj3 = Erlang.Object.Format("{$a, {1,2,3}, 10.0, [5,6], [\"abc\", bad], false}"); Assert.IsFalse(pat1.match(obj3, binding)); }
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); } }
/*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 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) { } }
/* * Create a stream containing the encoded version of the given * Erlang term. **/ public OtpOutputStream(Erlang.Object o) : this() { this.write_any(o); }
// send_reg has sender pid and receiver name internal OtpMsg(Erlang.Pid from, System.String toName, Erlang.Object payload) { this.tag = regSendTag; this.from = from; this.toName = toName; this.to = null; this.paybuf = null; this.payload = payload; }
/* * 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)); }
/*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()); } }
// send_reg has sender pid and receiver name internal OtpMsg(Erlang.Pid from, System.String toName, OtpInputStream paybuf) { this.tag = regSendTag; this.from = from; this.toName = toName; this.to = null; this.paybuf = paybuf; this.payload = null; }
public void TestMatchVariable() { var cases = new KeyValueList <string, Erlang.Object> { { "B", new Erlang.Int(1) }, { "B", new Erlang.Atom("abc") }, { "B", new Erlang.String("efg") }, { "B", new Erlang.Double(10.0) }, { "B::int()", new Erlang.Int(10) }, { "B::integer()", new Erlang.Int(20) }, { "B::string()", new Erlang.String("xxx") }, { "B::atom()", new Erlang.Atom("xyz") }, { "B::float()", new Erlang.Double(5.0) }, { "B::double()", new Erlang.Double(3.0) }, { "B::binary()", new Erlang.Binary(new byte[] { 1, 2, 3 }) }, { "B::bool()", new Erlang.Boolean(true) }, { "B::boolean()", new Erlang.Boolean(false) }, { "B::byte()", new Erlang.Byte(1) }, { "B::char()", new Erlang.Char('a') }, { "B::list()", new Erlang.List(1, 2, 3) }, { "B::tuple()", new Erlang.Tuple(new Erlang.Char('a'), 1, "aaa") }, { "B::pid()", new Erlang.Pid("xxx", 1, 2, 3) }, { "B::ref()", new Erlang.Ref("xxx", 1, 3) }, { "B::reference()", new Erlang.Ref("xxx", 1, 3) }, { "B::port()", new Erlang.Port("xxx", 1, 3) } }; foreach (var p in cases) { { Erlang.Object pat = Erlang.Object.Format(p.Key); Erlang.Object obj = p.Value; var binding = new Erlang.VarBind(); binding["B"] = obj; Assert.IsTrue(pat.match(obj, binding)); } { Erlang.Object pat = Erlang.Object.Format(p.Key); Erlang.Object obj = p.Value; var binding = new Erlang.VarBind(); Assert.IsTrue(pat.match(obj, binding)); var b = binding["B"]; Assert.AreEqual(obj.Type, b.Type); Assert.IsTrue(obj.Equals(b)); } } var revCases = cases.Reverse <KeyValuePair <string, Erlang.Object> >().ToList(); cases.Zip(revCases, (p1, p2) => { Erlang.Var pat = Erlang.Object.Format(p1.Key).AsVar(); Erlang.Object obj = p2.Value; var binding = new Erlang.VarBind(); if (pat.VarTermType == Erlang.TermType.Object || pat.VarTermType == obj.TermType) { Assert.IsTrue(pat.match(obj, binding)); } else { Assert.IsFalse(pat.match(obj, binding)); } return(false); }).ToList(); }
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(); }
static public void Main(String[] args) { { Erlang.Object obj1 = Erlang.Object.Format("{a, b, 10, 2.0}"); Erlang.Object obj2 = Erlang.Object.Format("{a, [b, 1, 2.0, \"abc\"], {1, 2}}"); } System.Console.Out.WriteLine("Otp test..."); string cookie = OtpNode.defaultCookie; AbstractConnection.traceLevel = OtpTrace.Type.sendThreshold; if (args.Length < 1) { System.Console.Out.WriteLine( "Usage: {0} nodename [cookie] [-notrace]\n" + " nodename - is the name of the remote Erlang node\n" + " cookie - is the optional cookie string to use\n" + " -notrace - disable debug trace\n", Environment.GetCommandLineArgs()[0]); return; } else if (args.Length > 1) { cookie = args[1].ToString(); } for (int i = 0; i < args.Length; i++) { if (args[i].Equals("-notrace")) { AbstractConnection.traceLevel = OtpTrace.Type.defaultLevel; break; } } String host = System.Net.Dns.GetHostName(); String remote = (args[0].IndexOf('@') < 0) ? args[0] + "@" + host : args[0]; OtpNode node = new OtpNode(false, Environment.UserName + "123@" + host, cookie, true); System.Console.Out.WriteLine("This node is called {0} and is using cookie='{1}'.", node.node(), node.cookie()); bool ok = node.ping(remote, 1000); if (!ok) { Console.WriteLine("Can't connect to node " + remote); return; } // If using short names, get the short name of the peer. remote = node.connection(remote).peer.node(); if (remote != null) { 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(); { 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(); }
/*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); }
/* * 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; }
// send has receiver pid but no sender information internal OtpMsg(Erlang.Pid to, Erlang.Object payload) : this(Tag.sendTag, null, to, null, payload, null) { }
// exit (etc) has from, to, reason internal OtpMsg(Tag tag, Erlang.Pid from, Erlang.Pid to, Erlang.Ref eref, Erlang.Object reason) : this(tag, from, to, eref, reason, null) { }
// send_reg has sender pid and receiver name internal OtpMsg(Erlang.Pid from, string toName, Erlang.Object payload) : this(Tag.regSendTag, from, toName, (Erlang.Ref)null, payload, null) { }
// exit (etc) has from, to, reason internal OtpMsg(int tag, Erlang.Pid from, Erlang.Pid to, Erlang.Object reason) { this.tag = tag; this.from = from; this.to = to; this.paybuf = null; this.payload = reason; }
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()); } }
// special case when reason is an atom (i.e. most of the time) internal OtpMsg(int tag, Erlang.Pid from, Erlang.Pid to, System.String reason) { this.tag = tag; this.from = from; this.to = to; this.paybuf = null; this.payload = new Erlang.Atom(reason); }
/* * Create a stream containing the serialized Erlang term. * Optionally include in the beginning Erlang protocol version byte. **/ public OtpOutputStream(Erlang.Object o, bool writeVersion) : this(o, writeVersion, false) { }
// send has receiver pid but no sender information internal OtpMsg(Erlang.Pid to, Erlang.Object payload) { this.tag = sendTag; this.from = null; this.to = to; this.toName = null; this.paybuf = null; this.payload = payload; }
// special case when reason is an atom (i.e. most of the time) private OtpMsg(Tag tag, Erlang.Pid from, Object /* Pid or string */ to, Erlang.Ref eref, System.String reason, OtpInputStream paybuf) { this.tag = tag; this.from = from; this.to = (Erlang.Pid)(to is Erlang.Pid ? to : null); this.toName = (string)(to is string ? to : null); this.paybuf = paybuf; this.payload = new Erlang.Atom(reason); this.eref = eref; }
/* * Send a message to a named process on a remote node. * * @param dest the name of the remote process. * @param msg the message to send. * * @exception C#.io.IOException if the connection is not active or * a communication error occurs. **/ public virtual void send(System.String dest, Erlang.Object msg) { // encode and send the message base.sendBuf(this._self.pid(), dest, new OtpOutputStream(msg)); }
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()); } }
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(); }
/* * send to remote name * dest is recipient's registered name, the nodename is implied by * the choice of connection. */ internal virtual void send(Erlang.Pid from, System.String dest, Erlang.Object msg) { // encode and send the message sendBuf(from, dest, new OtpOutputStream(msg)); }
/* * Create a stream containing the serialized Erlang term. **/ public OtpOutputStream(Erlang.Object o) : this(o, false, false) { }
/* * Send a message to a named mailbox created from the same node as * this mailbox. * * @param name the registered name of recipient mailbox. * * @param msg the body of the message to send. * **/ public void send(System.String name, Erlang.Object msg) { home.deliver(new OtpMsg(_self, name, (Erlang.Object)(msg.clone()))); }
/* * This does not work when char > 1 byte Unicode is used * * public void write_string(String s) { * this.write1(OtpExternal.stringTag); * this.write2BE(s.length()); * this.writeN(s.getBytes()); * }*/ /* * Write an arbitrary Erlang term to the stream. * * @param o the Erlang term to write. */ public virtual void write_any(Erlang.Object o) { // calls one of the above functions, depending on o o.encode(this); }
public void TestFormat() { { Erlang.Object obj1 = Erlang.Object.Format("a"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Atom)); Assert.AreEqual("a", (obj1 as Erlang.Atom).atomValue()); } { Erlang.Object obj1 = Erlang.Object.Format("$a"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Char)); Assert.AreEqual('a', (obj1 as Erlang.Char).charValue()); } { Erlang.Object obj1 = Erlang.Object.Format("'Abc'"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Atom)); Assert.AreEqual("Abc", (obj1 as Erlang.Atom).atomValue()); } { Erlang.Object obj1 = Erlang.Object.Format("\"Abc\""); Assert.IsInstanceOfType(obj1, typeof(Erlang.String)); Assert.AreEqual("Abc", (obj1 as Erlang.String).stringValue()); } { Erlang.Object obj1 = Erlang.Object.Format("Abc"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Var)); Assert.AreEqual("Abc", (obj1 as Erlang.Var).name()); } { Erlang.Object obj1 = Erlang.Object.Format("1"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Long)); Assert.AreEqual(1, (obj1 as Erlang.Long).longValue()); } { Erlang.Object obj1 = Erlang.Object.Format("1.23"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Double)); Assert.AreEqual(1.23, (obj1 as Erlang.Double).doubleValue()); } { Erlang.Object obj1 = Erlang.Object.Format("V"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Var)); Assert.AreEqual("V", (obj1 as Erlang.Var).name()); } { Erlang.Object obj1 = Erlang.Object.Format("{1}"); Assert.IsInstanceOfType(obj1, typeof(Erlang.Tuple)); Assert.AreEqual(1, (obj1 as Erlang.Tuple).arity()); Assert.IsInstanceOfType((obj1 as Erlang.Tuple)[0], typeof(Erlang.Long)); Assert.AreEqual(1, ((obj1 as Erlang.Tuple)[0] as Erlang.Long).longValue()); } { Erlang.Object obj0 = Erlang.Object.Format("[]"); Assert.IsInstanceOfType(obj0, typeof(Erlang.List)); Assert.AreEqual(0, (obj0 as Erlang.List).arity()); Erlang.Object obj1 = Erlang.Object.Format("[1]"); Assert.IsInstanceOfType(obj1, typeof(Erlang.List)); Assert.AreEqual(1, (obj1 as Erlang.List).arity()); Assert.IsInstanceOfType((obj1 as Erlang.List)[0], typeof(Erlang.Long)); Assert.AreEqual(1, ((obj1 as Erlang.List)[0] as Erlang.Long).longValue()); } { Erlang.Object obj1 = Erlang.Object.Format("[{1,2}, []]"); Assert.IsInstanceOfType(obj1, typeof(Erlang.List)); Assert.AreEqual(2, (obj1 as Erlang.List).arity()); Assert.IsInstanceOfType((obj1 as Erlang.List)[0], typeof(Erlang.Tuple)); 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.IsInstanceOfType(obj1, typeof(Erlang.Tuple)); Assert.AreEqual(3, (obj1 as Erlang.Tuple).arity()); } { Erlang.Object obj1 = Erlang.Object.Format("~w", 1); Assert.IsInstanceOfType(obj1, typeof(Erlang.Long)); Assert.AreEqual(1, (obj1 as Erlang.Long).longValue()); Erlang.Object obj2 = Erlang.Object.Format("{~w, ~w,~w}", 1, 2, 3); Assert.IsInstanceOfType(obj2, typeof(Erlang.Tuple)); Assert.AreEqual(3, (obj2 as Erlang.Tuple).arity()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[0], typeof(Erlang.Long)); Assert.AreEqual(1, ((obj2 as Erlang.Tuple)[0] as Erlang.Long).longValue()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[1], typeof(Erlang.Long)); Assert.AreEqual(2, ((obj2 as Erlang.Tuple)[1] as Erlang.Long).longValue()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[2], typeof(Erlang.Long)); 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.IsInstanceOfType(obj2, typeof(Erlang.Tuple)); Assert.AreEqual(5, (obj2 as Erlang.Tuple).arity()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[0], typeof(Erlang.Double)); Assert.AreEqual(1.0, ((obj2 as Erlang.Tuple)[0] as Erlang.Double).doubleValue()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[1], typeof(Erlang.Char)); Assert.AreEqual('a', ((obj2 as Erlang.Tuple)[1] as Erlang.Char).charValue()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[2], typeof(Erlang.String)); Assert.AreEqual("abc", ((obj2 as Erlang.Tuple)[2] as Erlang.String).stringValue()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[3], typeof(Erlang.Long)); Assert.AreEqual(2, ((obj2 as Erlang.Tuple)[3] as Erlang.Long).longValue()); Assert.IsInstanceOfType((obj2 as Erlang.Tuple)[4], typeof(Erlang.Boolean)); Assert.AreEqual(true, ((obj2 as Erlang.Tuple)[4] as Erlang.Boolean).booleanValue()); } }
/* * <p> Deserialize and return a new copy of the message contained in * this OtpMsg. </p> * * <p> The first time this method is called the actual payload is * deserialized and the Erlang term is created. Calling this method * subsequent times will not cuase the message to be deserialized * additional times, instead the same Erlang term object will be * returned. </p> * * @return an Erlang term. * * @exception DecodeException if the byte stream could not be deserialized. * **/ public Erlang.Object getMsg() { if (payload == null) { payload = paybuf.read_any(); } return payload; }
public void TestPatternMatch() { { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format("{snapshot, x12, []}"); Erlang.Object pat = Erlang.Object.Format("{snapshot, N, L}"); Assert.IsTrue(pat.match(obj, binding)); Erlang.Atom n = binding.find("N") as Erlang.Atom; Erlang.List l = binding.find("L") as Erlang.List; Assert.IsNotNull(n); Assert.IsNotNull(l); Assert.IsTrue(l.Length == 0); } { Erlang.Object pat = Erlang.Object.Format("{test, A, B, C}"); Erlang.Object obj = Erlang.Object.Format("{test, 10, a, [1,2,3]}"); Erlang.VarBind binding = new Otp.Erlang.VarBind(); Assert.IsTrue(pat.match(obj, binding)); Assert.AreEqual(3, binding.Count); Assert.AreEqual(10, binding["A"].longValue()); Assert.AreEqual("a", binding["B"].atomValue()); Assert.AreEqual("[1,2,3]", binding["C"].ToString()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format("[1,a,$b,\"xyz\",{1,10.0},[]]"); Erlang.Object pat = Erlang.Object.Format("[A,B,C,D,E,F]"); Assert.IsTrue(pat.match(obj, binding)); Assert.IsNotNull(binding.find("A") as Erlang.Long); Assert.IsNotNull(binding.find("B") as Erlang.Atom); Assert.IsNotNull(binding.find("C") as Erlang.Char); Assert.IsNotNull(binding.find("D") as Erlang.String); Assert.IsNotNull(binding.find("E") as Erlang.Tuple); Assert.IsNotNull(binding.find("F") as Erlang.List); Assert.IsTrue(binding.find("E").Cast <Erlang.Tuple>().arity() == 2); Assert.IsTrue(binding.find("F").Cast <Erlang.List>().Length == 0); } Erlang.Object pattern = Erlang.Object.Format("{test, T}"); string exp = "{test, ~w}"; { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format(exp, (int)3); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(3, binding.find("T").intValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format(exp, (long)100); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(100, binding.find("T").longValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format(exp, 100.0); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(100.0, binding.find("T").doubleValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format(exp, "test"); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual("test", binding.find("T").stringValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format(exp, true); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(true, binding.find("T").boolValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Object obj = Erlang.Object.Format(exp, 'c'); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual('c', binding.find("T").charValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Pid pid = new Erlang.Pid("tmp", 1, 2, 3); Erlang.Object obj = Erlang.Object.Format(exp, pid as Erlang.Object); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(pid, binding.find("T").pidValue()); obj = Erlang.Object.Format(exp, pid); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(pid, binding.find("T").pidValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.Ref reference = new Erlang.Ref("tmp", 1, 2); Erlang.Object obj = Erlang.Object.Format(exp, reference); Assert.IsTrue(pattern.match(obj, binding)); Assert.AreEqual(reference, binding.find("T").refValue()); } { Erlang.VarBind binding = new Otp.Erlang.VarBind(); Erlang.List obj = new Erlang.List(new Erlang.Int(10), new Erlang.Double(30.0), new Erlang.String("abc"), new Erlang.Atom("a"), new Erlang.Binary(new byte[] { 1, 2, 3 }), false, new Erlang.Boolean(true)); Erlang.Object pat = Erlang.Object.Format("T"); Assert.IsTrue(pat.match(obj, binding)); Erlang.Object expected = Erlang.Object.Format("[10, 30.0, \"abc\", 'a', ~w, \'false\', true]", new Erlang.Binary(new byte[] { 1, 2, 3 })); Erlang.Object result = binding.find("T"); Assert.IsTrue(expected.Equals(result)); } }