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(); }
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); } }
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()); } }
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 virtual void sendRPC(System.String mod, System.String fun, Erlang.List args) { sendRPC(_self.pid(), mod, fun, args); }
/*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()); } }
public void sendRPC(string node, string mod, string fun, Erlang.List args, Erlang.Pid ioServer) { sendRPC(node, new Erlang.Atom(mod), new Erlang.Atom(fun), args, ioServer); }
public void sendRPC(string node, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args) { sendRPC(node, mod, fun, args, _self /* new Erlang.Atom("user") */); }
public void sendRPC(string node, string mod, string fun, Erlang.List args) { sendRPC(node, new Erlang.Atom(mod), new Erlang.Atom(fun), args); }
public Erlang.Object rpcCall(string node, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args, int timeout) { sendRPC(node, mod, fun, args); return(receiveRPC(timeout)); }
public Erlang.Object rpcCall(string node, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args) { return(this.rpcCall(node, mod, fun, args, -1)); }
public Erlang.Object rpcCall(string node, string mod, string fun, Erlang.List args, int timeout) { return(this.rpcCall(node, new Erlang.Atom(mod), new Erlang.Atom(fun), args, -1)); }
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(); }
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; } 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 sendRPCcast(string node, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args) { sendRPCcast(node, mod, fun, args, new Erlang.Atom("user")); }
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)); } }