/** * <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 bool ping(String node, long timeout) { if (node.Equals(this.Node)) { return(true); } else if (node.IndexOf('@', 0) < 0 && node.Equals(this.Node.Substring(0, this.Node.IndexOf('@', 0)))) { return(true); } // other node OtpMbox mbox = null; try { mbox = createMbox(true); mbox.send("net_kernel", node, getPingTuple(mbox)); OtpErlangObject reply = mbox.receive(timeout); OtpErlangTuple t = (OtpErlangTuple)reply; OtpErlangAtom a = (OtpErlangAtom)t.elementAt(1); return("yes".Equals(a.atomValue())); } catch (Exception) { } finally { closeMbox(mbox); } return(false); }
// this function used internally when "process" dies // since Erlang discerns between exit and exit/2. private void exit(int arity, OtpErlangPid to, OtpErlangObject reason) { try { String node = to.Node; if (node.Equals(home.Node)) { home.deliver(new OtpMsg(OtpMsg.exitTag, self, to, reason)); } else { OtpCookedConnection conn = home.getConnection(node); if (conn == null) { return; } switch (arity) { case 1: conn.exit(self, to, reason); break; case 2: conn.exit2(self, to, reason); break; } } } catch (Exception) { } }
/* * this method simulates net_kernel only for the purpose of replying to * pings. */ private bool netKernel(OtpMsg m) { OtpMbox mbox = null; try { OtpErlangTuple t = (OtpErlangTuple)m.getMsg(); OtpErlangTuple req = (OtpErlangTuple)t.elementAt(1); // actual // request OtpErlangPid pid = (OtpErlangPid)req.elementAt(0); // originating // pid OtpErlangObject[] pong = new OtpErlangObject[2]; pong[0] = req.elementAt(1); // his #Ref pong[1] = new OtpErlangAtom("yes"); mbox = createMbox(true); mbox.send(pid, new OtpErlangTuple(pong)); return(true); } catch (Exception) { } finally { closeMbox(mbox); } return(false); }
/** * Wait for a message to arrive for this mailbox. * * @param timeout * the time, in milliseconds, to wait for a message. * * @return an {@link OtpMsg OtpMsg} containing the header information as * well as the body of the next message waiting in this mailbox. * * @exception OtpErlangExit * if a linked {@link OtpErlangPid pid} has exited or has * sent an exit signal to this mailbox. * * @exception InterruptedException * if no message if the method times out before a message * becomes available. */ public virtual OtpMsg receiveMsg(long timeout) { OtpMsg m = (OtpMsg)queue.get(timeout); if (m == null) { return(null); } switch (m.type()) { case OtpMsg.exitTag: case OtpMsg.exit2Tag: try { OtpErlangObject o = m.getMsg(); throw new OtpErlangExit(o, m.getSenderPid()); } catch (OtpErlangDecodeException) { throw new OtpErlangExit("unknown", m.getSenderPid()); } default: return(m); } }
/** * 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(String name, String node, OtpErlangObject msg) { try { String currentNode = home.Node; if (node.Equals(currentNode)) { send(name, msg); } else if (node.IndexOf('@', 0) < 0 && node.Equals(currentNode.Substring(0, currentNode.IndexOf('@', 0)))) { send(name, msg); } else { // other node OtpCookedConnection conn = home.getConnection(node); if (conn == null) { return; } conn.send(self, name, msg); } } catch (Exception) { } }
public int CompareTo(OtpErlangMap other) { if (other is null) { return(1); } return(OtpErlangObject.CompareTo(this.OrderBy(kvp => kvp.Key), other.OrderBy(kvp => kvp.Key), new KeyValueComparer())); }
// exit (etc) has from, to, reason internal OtpMsg(int tag, OtpErlangPid from, OtpErlangPid to, OtpErlangObject reason) { this.tag = tag; this.from = from; this.to = to; paybuf = null; payload = reason; }
// special case when reason is an atom (i.e. most of the time) internal OtpMsg(int tag, OtpErlangPid from, OtpErlangPid to, String reason) { this.tag = tag; this.from = from; this.to = to; paybuf = null; payload = new OtpErlangAtom(reason); }
/** * <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 OtpErlangDecodeException * if the byte stream could not be deserialized. * */ public OtpErlangObject getMsg() { if (payload == null) { payload = paybuf.read_any(); } return(payload); }
// send_reg has sender pid and receiver name internal OtpMsg(OtpErlangPid from, String toName, OtpErlangObject payload) { tag = regSendTag; this.from = from; this.toName = toName; to = null; paybuf = null; this.payload = payload; }
// send has receiver pid but no sender information internal OtpMsg(OtpErlangPid to, OtpInputStream paybuf) { tag = sendTag; from = null; this.to = to; toName = null; this.paybuf = paybuf; payload = null; }
// send has receiver pid but no sender information internal OtpMsg(OtpErlangPid to, OtpErlangObject payload) { tag = sendTag; from = null; this.to = to; toName = null; paybuf = null; this.payload = payload; }
// send_reg has sender pid and receiver name internal OtpMsg(OtpErlangPid from, String toName, OtpInputStream paybuf) { tag = regSendTag; this.from = from; this.toName = toName; to = null; this.paybuf = paybuf; payload = null; }
/** * Create a stream containing the encoded version of the given Erlang term. */ public OtpOutputStream(OtpErlangObject o, bool distributed) : base() { // write the version header if we are not distributed if (!distributed) { write1 (131); } write_any(o); }
/** * Close the specified mailbox with the given reason. * * @param mbox * the mailbox to close. * @param reason * an Erlang term describing the reason for the termination. * * <p> * After this operation, the mailbox will no longer be able to * receive messages. Any delivered but as yet unretrieved * messages can still be retrieved however. * </p> * * <p> * If there are links from the mailbox to other * {@link OtpErlangPid pids}, they will be broken when this * method is called and exit signals with the given reason will * be sent. * </p> * */ public void closeMbox(OtpMbox mbox, OtpErlangObject reason) { if (mbox != null) { mboxes.remove(mbox); mbox.Name = null; mbox.breakLinks(reason); } }
/* * this one called explicitely by user code => use exit2 */ public void exit2(OtpErlangPid from, OtpErlangPid to, OtpErlangObject reason) { try { base.sendExit2(from, to, reason); } catch (Exception) { } }
public override OtpErlangObject[] elements() { int n = parent.arity() - start; OtpErlangObject[] res = new OtpErlangObject[n]; for (int i = 0; i < res.Length; i++) { res[i] = parent.elementAt(i + start); } return(res); }
/** * Create a unary tuple containing the given element. * * @param elem * the element to create the tuple from. * * @exception java.lang.IllegalArgumentException * if the element is null. */ public OtpErlangTuple(OtpErlangObject elem) { if (elem == null) { throw new ArgumentException("Tuple element cannot be null"); } else { elems = new OtpErlangObject[] { elem }; } }
/// <summary>The return value from executing the Erlang RPC.</summary> /// <param name="module">The module to call</param> /// <param name="function">The function to invoke</param> /// <param name="erlangObject">The erlang object that is passed in as a parameter</param> /// <returns>The converted .NET object return value from the RPC call.</returns> /// <exception cref="ErlangConversionException">in case of conversion failures</exception> public override object FromErlangRpc(string module, string function, OtpErlangObject erlangObject) { var converter = this.GetConverter(module, function); if (converter != null) { return converter.FromErlang(erlangObject); } else { return base.FromErlangRpc(module, function, erlangObject); } }
public OtpErlangFun(OtpErlangPid pid, String module, long index, long uniq, OtpErlangObject[] freeVars) { this.pid = pid; this.module = module; arity = -1; md5 = null; this.index = index; old_index = 0; this.uniq = uniq; this.freeVars = freeVars; }
public OtpErlangFun(OtpErlangPid pid, String module, int arity, byte[] md5, int index, long old_index, long uniq, OtpErlangObject[] freeVars) { this.pid = pid; this.module = module; this.arity = arity; this.md5 = md5; this.index = index; this.old_index = old_index; this.uniq = uniq; this.freeVars = freeVars; }
// used to break all known links to this mbox public void breakLinks(OtpErlangObject reason) { Link[] l = links.clearLinks(); if (l != null) { int len = l.Length; for (int i = 0; i < len; i++) { exit(1, l[i].Remote, reason); } } }
/** * Receive an RPC reply from the remote Erlang node. This convenience * function receives a message from the remote node, and expects it to have * the following format: * * <pre> * { rex, Term } * </pre> * * @return the second element of the tuple if the received message is a * two-tuple, otherwise null. No further error checking is * performed. * * @exception java.io.IOException * if the connection is not active or a communication * error occurs. * * @exception OtpErlangExit * if an exit signal is received from a process on the * peer node. * * @exception OtpAuthException * if the remote node sends a message containing an * invalid cookie. */ public OtpErlangObject receiveRPC() { OtpErlangObject msg = receive(); if (msg is OtpErlangTuple) { OtpErlangTuple t = (OtpErlangTuple)msg; if (t.arity() == 2) { return(t.elementAt(1)); // obs: second element } } return(null); }
/** * 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 java.io.IOException * if the connection is not active or a communication * error occurs. */ public void sendRPC(String mod, String fun, OtpErlangList args) { OtpErlangObject[] rpc = new OtpErlangObject[2]; OtpErlangObject[] call = new OtpErlangObject[5]; /* {self, { call, Mod, Fun, Args, user}} */ call[0] = new OtpErlangAtom("call"); call[1] = new OtpErlangAtom(mod); call[2] = new OtpErlangAtom(fun); call[3] = args; call[4] = new OtpErlangAtom("user"); rpc[0] = self.Pid; rpc[1] = new OtpErlangTuple(call); send("rex", new OtpErlangTuple(rpc)); }
/** * Write an arbitrary Erlang term to the stream in compressed format. * * @param o * the Erlang tem to write. */ public void write_compressed(OtpErlangObject o) { OtpOutputStream oos = new OtpOutputStream(o); write1(OtpExternal.compressedTag); write4BE(oos.Length); DeflateStream dos = new DeflateStream(this, CompressionMode.Compress, true); try { oos.WriteTo(dos); dos.Close(); } catch (ObjectDisposedException) { throw new ArgumentException("Intremediate stream failed for Erlang object " + o); } }
/* create the outgoing ping message */ private OtpErlangTuple getPingTuple(OtpMbox mbox) { OtpErlangObject[] ping = new OtpErlangObject[3]; OtpErlangObject[] pid = new OtpErlangObject[2]; OtpErlangObject[] node = new OtpErlangObject[2]; pid[0] = mbox.Self; pid[1] = createRef(); node[0] = new OtpErlangAtom("is_auth"); node[1] = new OtpErlangAtom(Node); ping[0] = new OtpErlangAtom("$gen_call"); ping[1] = new OtpErlangTuple(pid); ping[2] = new OtpErlangTuple(node); return(new OtpErlangTuple(ping)); }
public override object FromErlang(OtpErlangObject erlangObject) { IList<string> users = new List<string>(); if (erlangObject is OtpErlangList) { OtpErlangList erlangList = (OtpErlangList) erlangObject; foreach (OtpErlangObject obj in erlangList) { if (obj is OtpErlangBinary) { OtpErlangBinary binary = (OtpErlangBinary) obj; users.Add(new ASCIIEncoding().GetString(binary.binaryValue())); } } } return users; }
public bool Equals(OtpErlangFun o) { if (o is null) { return(false); } if (ReferenceEquals(this, o)) { return(true); } return(Pid == o.Pid && Module == o.Module && Index == o.Index && OldIndex == o.OldIndex && Uniq == o.Uniq && Arity == o.Arity && OtpErlangObject.SequenceEqual(FreeVars, o.FreeVars) && OtpErlangObject.SequenceEqual(Md5, o.Md5)); }
public OtpErlangFun read_fun() { int tag = read1skip_version(); if (tag == OtpExternal.funTag) { int nFreeVars = read4BE(); OtpErlangPid pid = read_pid(); String module = read_atom(); long index = read_long(); long uniq = read_long(); OtpErlangObject[] freeVars = new OtpErlangObject[nFreeVars]; for (int i = 0; i < nFreeVars; ++i) { freeVars[i] = read_any(); } return(new OtpErlangFun(pid, module, index, uniq, freeVars)); } else if (tag == OtpExternal.newFunTag) { read4BE(); int arity = read1(); byte[] md5 = new byte[16]; readN(md5); int index = read4BE(); int nFreeVars = read4BE(); String module = read_atom(); long oldIndex = read_long(); long uniq = read_long(); OtpErlangPid pid = read_pid(); OtpErlangObject[] freeVars = new OtpErlangObject[nFreeVars]; for (int i = 0; i < nFreeVars; ++i) { freeVars[i] = read_any(); } return(new OtpErlangFun(pid, module, arity, md5, index, oldIndex, uniq, freeVars)); } else { throw new OtpErlangDecodeException("Wrong tag encountered, expected fun, got " + tag); } }
/** * Send a message to a remote {@link OtpErlangPid pid}, representing either * another {@link OtpMbox mailbox} or an Erlang process. * * @param to * the {@link OtpErlangPid pid} identifying the intended * recipient of the message. * * @param msg * the body of the message to send. * */ public void send(OtpErlangPid to, OtpErlangObject msg) { try { String node = to.Node; if (node.Equals(home.Node)) { home.deliver(new OtpMsg(to, (OtpErlangObject)msg.Clone())); } else { OtpCookedConnection conn = home.getConnection(node); if (conn == null) { return; } conn.send(self, to, msg); } } catch (Exception) { } }
public override IEnumerator<OtpActor.Continuation> GetEnumerator() { while (true) { OtpMsg msg = null; OtpErlangPid sender = null; OtpErlangTuple reply = null; yield return (delegate(OtpMsg received) { msg = received; }); try { OtpErlangTuple t = (OtpErlangTuple)msg.getMsg(); sender = (OtpErlangPid)t.elementAt(0); string instr = ((OtpErlangAtom)t.elementAt(1)).ToString(); if (instr == "echo") { OtpErlangObject[] v = new OtpErlangObject[3]; v[0] = sender; v[1] = new OtpErlangAtom("ok"); v[2] = t.elementAt(2); reply = new OtpErlangTuple(v); } else if (instr == "new") { OtpActorMbox newmbox = (OtpActorMbox)m_node.createMbox(false); PCVMActor newactor = new PCVMActor(m_scene, m_source, m_node, newmbox); m_node.react(newactor); OtpErlangObject[] v = new OtpErlangObject[3]; v[0] = sender; v[1] = new OtpErlangAtom("ok"); v[2] = newmbox.Self; reply = new OtpErlangTuple(v); } } catch (Exception e) { m_log.Debug("[Distributed PC] Invalid message format: " + msg.getMsg()); OtpErlangObject[] v = new OtpErlangObject[3]; v[0] = sender; v[1] = new OtpErlangAtom("error"); v[2] = new OtpErlangString(e.Message); reply = new OtpErlangTuple(v); } finally { if (sender != null) { m_mbox.send(sender, reply); } } } }
/** * Create an OtpErlangExit exception with the given reason and sender pid. * * @param reason * the reason this exit signal has been sent. * * @param pid * the pid that sent this exit. */ public OtpErlangExit(OtpErlangObject reason, OtpErlangPid pid) : base(reason.ToString()) { this.reason = reason; this.pid = pid; }
/* create the outgoing ping message */ private OtpErlangTuple getPingTuple(OtpMbox mbox) { OtpErlangObject[] ping = new OtpErlangObject[3]; OtpErlangObject[] pid = new OtpErlangObject[2]; OtpErlangObject[] node = new OtpErlangObject[2]; pid[0] = mbox.Self; pid[1] = createRef(); node[0] = new OtpErlangAtom("is_auth"); node[1] = new OtpErlangAtom(Node); ping[0] = new OtpErlangAtom("$gen_call"); ping[1] = new OtpErlangTuple(pid); ping[2] = new OtpErlangTuple(node); return new OtpErlangTuple(ping); }
public static String ExtractPid(OtpErlangObject value) { return value.ToString(); }
/// <summary>The from erlang.</summary> /// <param name="erlangObject">The erlang object.</param> /// <returns>The System.Object.</returns> public override object FromErlang(OtpErlangObject erlangObject) { var queueInfoList = new List<QueueInfo>(); if (erlangObject is OtpErlangList) { var erlangList = (OtpErlangList)erlangObject; foreach (var element in erlangList) { var queueInfo = new QueueInfo(); var itemList = (OtpErlangList)element; foreach (var item in itemList) { var tuple = (OtpErlangTuple)item; if (tuple.arity() == 2) { var key = tuple.elementAt(0).ToString(); var value = tuple.elementAt(1); switch (ToQueueInfoField(key)) { case QueueInfoField.name: queueInfo.Name = this.ExtractNameValueFromTuple((OtpErlangTuple)value); break; case QueueInfoField.transactions: queueInfo.Transactions = ExtractLong(value); break; case QueueInfoField.acks_uncommitted: queueInfo.AcksUncommitted = ExtractLong(value); break; case QueueInfoField.consumers: queueInfo.Consumers = ExtractLong(value); break; case QueueInfoField.pid: queueInfo.Pid = ExtractPid(value); break; case QueueInfoField.durable: queueInfo.Durable = this.ExtractAtomBoolean(value); break; case QueueInfoField.messages: queueInfo.Messages = ExtractLong(value); break; case QueueInfoField.memory: queueInfo.Memory = ExtractLong(value); break; case QueueInfoField.auto_delete: queueInfo.AutoDelete = this.ExtractAtomBoolean(value); break; case QueueInfoField.messages_ready: queueInfo.MessagesReady = ExtractLong(value); break; case QueueInfoField.arguments: var list = (OtpErlangList)value; if (list != null) { var args = new string[list.arity()]; for (var i = 0; i < list.arity(); i++) { var obj = list.elementAt(i); args[i] = obj.ToString(); } queueInfo.Arguments = args; } break; case QueueInfoField.messages_unacknowledged: queueInfo.MessagesUnacknowledged = ExtractLong(value); break; case QueueInfoField.messages_uncommitted: queueInfo.MessageUncommitted = ExtractLong(value); break; default: break; } } } queueInfoList.Add(queueInfo); } } return queueInfoList; }
/// <summary>Extracts the string.</summary> /// <param name="obj">The obj.</param> /// <returns>The string.</returns> private string ExtractString(OtpErlangObject obj) { if (obj is OtpErlangBinary) { var binary = (OtpErlangBinary)obj; return new UTF8Encoding().GetString(binary.binaryValue()); } else if (obj is OtpErlangTuple) { var tuple = (OtpErlangTuple)obj; return this.ExtractString(tuple.elementAt(0)); } return null; }
private OtpErlangObject ErlangObjectFromPCVMObject(PCObj[] input) { OtpErlangObject[] vals = new OtpErlangObject[input.Length]; for (int i = 0; i <input.Length; i++) { vals[i] = ErlangObjectFromPCVMObject(input[i]); } return new OtpErlangList(vals); }
/// <summary> /// The return value from executing the Erlang RPC. /// </summary> /// <param name="module">The module to call</param> /// <param name="function">The function to invoke</param> /// <param name="erlangObject">The erlang object that is passed in as a parameter</param> /// <returns>The converted .NET object return value from the RPC call.</returns> /// <exception cref="ErlangConversionException">in case of conversion failures</exception> public virtual object FromErlangRpc(string module, string function, OtpErlangObject erlangObject) { return this.FromErlang(erlangObject); }
/* * send to remote name dest is recipient's registered name, the nodename is * implied by the choice of connection. */ public void send(OtpErlangPid from, String dest, OtpErlangObject msg) { // encode and send the message sendBuf(from, dest, new OtpOutputStream(msg)); }
/** * Send an exit signal to a remote process. * * @param dest * the Erlang PID of the remote process. * @param reason * an Erlang term describing the exit reason. * * @exception java.io.IOException * if the connection is not active or a communication * error occurs. */ public void exit(OtpErlangPid dest, OtpErlangObject reason) { base.sendExit2(self.Pid, dest, reason); }
/** * Create an OtpErlangExit exception with the given reason. * * @param reason * the reason this exit signal has been sent. */ public OtpErlangExit(OtpErlangObject reason) : base(reason.ToString()) { this.reason = reason; }
/** * Create a tuple from an array of terms. * * @param elems * the array of terms to create the tuple from. * * @exception java.lang.IllegalArgumentException * if the array is empty (null) or contains null * elements. */ public OtpErlangTuple(OtpErlangObject[] elems) : this(elems, 0, elems.Length) { }
public override IEnumerator<OtpActor.Continuation> GetEnumerator() { m_log.Info("[Distributed PC] New PCVM is ready: " + m_mbox.Self); while (true) { OtpMsg msg = null; yield return (delegate(OtpMsg received) { msg = received; }); OtpErlangObject obj = msg.getMsg(); if (obj is OtpErlangAtom) { string atom = ((OtpErlangAtom)obj).atomValue(); if (!String.IsNullOrEmpty(atom) && atom == "noconnection") { break; } } OtpErlangPid sender = null; OtpErlangObject[] reply = new OtpErlangObject[3]; try { OtpErlangTuple t = (OtpErlangTuple)obj; sender = (OtpErlangPid)t.elementAt(0); reply[0] = sender; reply[1] = new OtpErlangAtom("ok"); string instr = ((OtpErlangAtom)t.elementAt(1)).ToString(); if (instr == "load") { Parser parser = PCVM.MakeParser(); string script = String.Empty; if (t.elementAt(2) is OtpErlangString) { script = ((OtpErlangString)t.elementAt(2)).stringValue(); } else { script = OtpErlangString.newString(((OtpErlangBinary)t.elementAt(2)).binaryValue()); } bool debug = ((OtpErlangAtom)t.elementAt(3)).boolValue(); SYMBOL ast = parser.Parse(script); m_vm.Call((Compiler.ExpPair)ast); if (debug) { reply[2] = new OtpErlangAtom("continue"); } else { Queue<PCObj> popped = new Queue<PCObj>(); try { m_vm.Finish(popped); } finally { reply = new OtpErlangObject[] { reply[0], reply[1], new OtpErlangAtom("finished"), ErlangObjectFromPCVMObject(popped.ToArray()) }; } } } else if (instr == "step") { bool cont = true; Queue<PCObj> popped = new Queue<PCObj>(); try { cont = m_vm.Step(popped); } finally { reply = new OtpErlangObject[] { reply[0], reply[1], new OtpErlangAtom(cont ? "continue" : "finished"), ErlangObjectFromPCVMObject(popped.ToArray()) }; } } else if (instr == "finish") { Queue<PCObj> popped = new Queue<PCObj>(); try { m_vm.Finish(popped); } finally { reply = new OtpErlangObject[] { reply[0], reply[1], new OtpErlangAtom("finished"), ErlangObjectFromPCVMObject(popped.ToArray()) }; } } else if (instr == "echo") { reply[2] = t.elementAt(2); } else if (instr == "exit") { reply[2] = new OtpErlangAtom("bye"); break; } } catch (Exception e) { m_log.Debug("[Distributed PC] Invalid message format: " + msg.getMsg()); reply[1] = new OtpErlangAtom("error"); reply[2] = new OtpErlangString(e.Message); } finally { if (sender != null) { m_mbox.send(sender, new OtpErlangTuple(reply)); } } } m_log.Info("[Distributed PC] Delete PCVM instance: " + m_mbox.Self); m_vm.Dispose(); }
private OtpErlangObject ErlangObjectFromPCVMObject(PCObj input) { if (input is PCFloat) return new OtpErlangFloat(((PCFloat)input).val); if (input is PCInt) return new OtpErlangInt(((PCInt)input).val); if (input is PCBool) return new OtpErlangBoolean(((PCBool)input).val); if (input is PCSym) return new OtpErlangAtom(((PCSym)input).val); if (input is PCStr) return new OtpErlangString(((PCStr)input).val); if (input is PCMark) return new OtpErlangString(((PCMark)input).ToString()); if (input is PCUUID) return new OtpErlangString(((PCUUID)input).ToString()); if (input is PCVector2) { OtpErlangObject[] items = new OtpErlangObject[2]; items[0] = new OtpErlangFloat(((PCVector2)input).val.X); items[1] = new OtpErlangFloat(((PCVector2)input).val.Y); return new OtpErlangTuple(items); } if (input is PCVector3) { OtpErlangObject[] items = new OtpErlangObject[3]; items[0] = new OtpErlangFloat(((PCVector3)input).val.X); items[1] = new OtpErlangFloat(((PCVector3)input).val.Y); items[2] = new OtpErlangFloat(((PCVector3)input).val.Z); return new OtpErlangTuple(items); } if (input is PCVector4) { OtpErlangObject[] items = new OtpErlangObject[4]; items[0] = new OtpErlangFloat(((PCVector4)input).val.X); items[1] = new OtpErlangFloat(((PCVector4)input).val.Y); items[2] = new OtpErlangFloat(((PCVector4)input).val.Z); items[3] = new OtpErlangFloat(((PCVector4)input).val.W); return new OtpErlangTuple(items); } if (input is PCNull) return new OtpErlangAtom(((PCNull)input).ToString()); if (input is PCOp) return new OtpErlangAtom(((PCOp)input).ToString()); if (input is PCFun) return new OtpErlangAtom(((PCFun)input).ToString()); if (input is PCDict) { PCDict dict = (PCDict)input; OtpErlangObject[] items = new OtpErlangObject[dict.Dict.Count*2]; int i = 0; foreach (KeyValuePair<string, PCObj> pair in dict.Dict) { items[i * 2] = new OtpErlangAtom(pair.Key); items[i * 2 + 1] = ErlangObjectFromPCVMObject(pair.Value); i++; } return new OtpErlangList(items); } if (input is PCArray) { PCArray array = (PCArray)input; OtpErlangObject[] items = new OtpErlangObject[array.Length]; for (int i = 0; i < array.Length; i++) { items[i] = ErlangObjectFromPCVMObject(array[i]); } return new OtpErlangList(items); } if (input is PCSceneObjectPart) return ErlangObjectFromPCVMObject(new PCUUID(((PCSceneObjectPart)input).val.UUID)); if (input is PCSceneSnapshot) { PCSceneSnapshot.SnapshotItem[] array = ((PCSceneSnapshot)input).val; OtpErlangObject[] items = new OtpErlangObject[array.Length]; for (int i = 0; i < array.Length; i++) { items[i] = ErlangObjectFromPCVMObject(array[i].PCSceneObjectPart); } return new OtpErlangList(items); } return new OtpErlangAtom("nonobject"); }
/** * 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 java.io.IOException * if the connection is not active or a communication * error occurs. */ public void send(String dest, OtpErlangObject msg) { // encode and send the message base.sendBuf(self.Pid, dest, new OtpOutputStream(msg)); }
/// <summary>Convert from an Erlang data type to a .NET data type.</summary> /// <param name="erlangObject">The erlang object.</param> /// <returns>The converted .NET object</returns> /// <exception cref="ErlangConversionException">in case of conversion failures</exception> public override object FromErlang(OtpErlangObject erlangObject) { var users = new List<string>(); if (erlangObject is OtpErlangList) { var erlangList = (OtpErlangList)erlangObject; foreach (var obj in erlangList) { if (obj is OtpErlangList) { var value = this.ExtractString(((OtpErlangTuple)((OtpErlangList)obj).elementAt(0)).elementAt(1)); if (value != null) { users.Add(value); } } } } return users; }
/// <summary> /// Extracts the boolean. /// </summary> /// <param name="erlangObject">The erlang object.</param> /// <returns>The boolean.</returns> /// <remarks></remarks> public static bool ExtractBoolean(OtpErlangObject erlangObject) { // TODO Erlang.NET has wrong capitilization return ((OtpErlangBoolean)erlangObject).boolValue(); }
/// <summary>Convert from an Erlang data type to a .NET data type.</summary> /// <param name="erlangObject">The erlang object.</param> /// <returns>The converted .NET object</returns> /// <exception cref="ErlangConversionException">in case of conversion failures</exception> public override object FromErlang(OtpErlangObject erlangObject) { var applications = new List<Application>(); var nodes = new List<Node>(); var runningNodes = new List<Node>(); if (erlangObject is OtpErlangList) { var erlangList = (OtpErlangList)erlangObject; var nodesTuple = (OtpErlangTuple)erlangList.elementAt(0); var nodesList = (OtpErlangList)nodesTuple.elementAt(1); this.ExtractNodes(nodes, nodesList); var runningNodesTuple = (OtpErlangTuple)erlangList.elementAt(1); nodesList = (OtpErlangList)runningNodesTuple.elementAt(1); this.ExtractNodes(runningNodes, nodesList); } return new RabbitStatus(applications, nodes, runningNodes); }
public override object FromErlang(OtpErlangObject erlangObject) { IList<Application> applications = new List<Application>(); IList<Node> nodes = new List<Node>(); IList<Node> runningNodes = new List<Node>(); if (erlangObject is OtpErlangList) { OtpErlangList erlangList = (OtpErlangList)erlangObject; OtpErlangTuple runningAppTuple = (OtpErlangTuple)erlangList.elementAt(0); OtpErlangList appList = (OtpErlangList)runningAppTuple.elementAt(1); ExtractApplications(applications, appList); OtpErlangTuple nodesTuple = (OtpErlangTuple)erlangList.elementAt(1); OtpErlangList nodesList = (OtpErlangList)nodesTuple.elementAt(1); ExtractNodes(nodes, nodesList); OtpErlangTuple runningNodesTuple = (OtpErlangTuple)erlangList.elementAt(2); nodesList = (OtpErlangList)runningNodesTuple.elementAt(1); ExtractNodes(runningNodes, nodesList); } return new RabbitStatus(applications, nodes, runningNodes); }
/// <summary>Extracts the atom boolean.</summary> /// <param name="value">The value.</param> /// <returns>The atom boolean.</returns> private bool ExtractAtomBoolean(OtpErlangObject value) { return ((OtpErlangAtom)value).boolValue(); }
/// <summary> /// Extracts the long. /// </summary> /// <param name="value">The value.</param> /// <returns>The long.</returns> /// <remarks></remarks> public static long ExtractLong(OtpErlangObject value) { return ((OtpErlangLong)value).longValue(); }
public override object FromErlang(OtpErlangObject erlangObject) { IList<QueueInfo> queueInfoList = new List<QueueInfo>(); if (erlangObject is OtpErlangList) { OtpErlangList erlangList = (OtpErlangList) erlangObject; foreach (OtpErlangObject element in erlangList) { QueueInfo queueInfo = new QueueInfo(); OtpErlangList itemList = (OtpErlangList) element; foreach (OtpErlangObject item in itemList) { OtpErlangTuple tuple = (OtpErlangTuple) item; if (tuple.arity() == 2) { string key = tuple.elementAt(0).ToString(); OtpErlangObject value = tuple.elementAt(1); switch (key) { case "name": queueInfo.Name = ExtractNameValueFromTuple((OtpErlangTuple) value); break; case "transactions": queueInfo.Transactions = ExtractLong(value); break; case "acks_uncommitted": queueInfo.AcksUncommitted = ExtractLong(value); break; case "consumers": queueInfo.Consumers = ExtractLong(value); break; case "pid": queueInfo.Pid = ExtractPid(value); break; case "durable": queueInfo.Durable = ExtractAtomBoolean(value); break; case "messages": queueInfo.Messages = ExtractLong(value); break; case "memory": queueInfo.Memory = ExtractLong(value); break; case "auto_delete": queueInfo.AutoDelete = ExtractAtomBoolean(value); break; case "messages_ready": queueInfo.MessagesReady = ExtractLong(value); break; case "arguments": OtpErlangList list = (OtpErlangList)value; if (list != null) { String[] args = new String[list.arity()]; for (int i = 0; i < list.arity(); i++) { OtpErlangObject obj = list.elementAt(i); args[i] = obj.ToString(); } queueInfo.Arguments = args; } break; case "messages_unacknowledged": queueInfo.MessagesUnacknowledged = ExtractLong(value); break; case "messages_uncommitted": queueInfo.MessageUncommitted = ExtractLong(value); break; default: break; } queueInfoList.Add(queueInfo); } } } } return queueInfoList; }
/* * this method simulates net_kernel only for the purpose of replying to * pings. */ private bool netKernel(OtpMsg m) { OtpMbox mbox = null; try { OtpErlangTuple t = (OtpErlangTuple)m.getMsg(); OtpErlangTuple req = (OtpErlangTuple)t.elementAt(1); // actual // request OtpErlangPid pid = (OtpErlangPid)req.elementAt(0); // originating // pid OtpErlangObject[] pong = new OtpErlangObject[2]; pong[0] = req.elementAt(1); // his #Ref pong[1] = new OtpErlangAtom("yes"); mbox = createMbox(true); mbox.send(pid, new OtpErlangTuple(pong)); return true; } catch (Exception) { } finally { closeMbox(mbox); } return false; }
/** * Create a tuple from an array of terms. * * @param elems * the array of terms to create the tuple from. * @param start * the offset of the first term to insert. * @param count * the number of terms to insert. * * @exception java.lang.IllegalArgumentException * if the array is empty (null) or contains null * elements. */ public OtpErlangTuple(OtpErlangObject[] elems, int start, int count) { if (elems == null) { throw new ArgumentException("Tuple content can't be null"); } else if (count < 1) { elems = NO_ELEMENTS; } else { this.elems = new OtpErlangObject[count]; for (int i = 0; i < count; i++) { if (elems[start + i] != null) { this.elems[i] = elems[start + i]; } else { throw new ArgumentException("Tuple element cannot be null (element" + (start + i) + ")"); } } } }
/** * Get all the elements from the tuple as an array. * * @return an array containing all of the tuple's elements. */ public OtpErlangObject[] elements() { OtpErlangObject[] res = new OtpErlangObject[arity()]; Array.Copy(elems, 0, res, 0, res.Length); return res; }
/// <summary> /// Convert from an Erlang data type to a .NET data type. /// </summary> /// <param name="erlangObject">The erlang object.</param> /// <returns>The converted .NET object</returns> /// <exception cref="ErlangConversionException">in case of conversion failures</exception> public virtual object FromErlang(OtpErlangObject erlangObject) { // TODO: support arrays return this.ConvertErlangToBasicType(erlangObject); }