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 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, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args) { sendRPC(node, mod, fun, args, _self /* new Erlang.Atom("user") */); }
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)); }
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(); }
/* * 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; }
/*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 void sendRPCcast(string node, Erlang.Atom mod, Erlang.Atom fun, Erlang.List args) { sendRPCcast(node, mod, fun, args, new Erlang.Atom("user")); }
/* * 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 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)); } }