Ejemplo n.º 1
0
 private void sendRpcReply(ErlPid from, ErlRef eref, IErlObject reply)
 {
     if (from.Empty)
     {
         return;
     }
     Node.Send(from, ErlTuple.Create(eref, reply));
 }
Ejemplo n.º 2
0
        private void btnErlang_Click(object sender, EventArgs e)
        {
            //connect to erlang
            var n = new ErlLocalNode("b", new ErlAtom("hahaha"));

            n.AcceptConnections = false;
            n.Start();

            var m = n.CreateMbox("test");

            var res = n.Send(m.Self, "[email protected]", "me", new ErlString("Hello! " + DateTime.Now));

            if (!res)
            {
                Console.WriteLine("Can not send message");
            }
            else
            {
                Console.WriteLine("Message sent");
            }
        }
Ejemplo n.º 3
0
        private void startIO(object obj)
        {
            // For Erlang I/O protocol see:
            // http://erlang.org/doc/apps/stdlib/io_protocol.html

            var patterns = new ErlPatternMatcher
            {
                { "{put_chars, ~v::atom(), ~v::string()}".ErlArgs(E, C),
                  (_p, _t, b, a) => ioProcessPutChars(
                      b.Cast <ErlAtom>(E), b.Cast <ErlString>(C), (IErlObject)a[1]) },
                { "{put_chars, ~v::atom(), ~v::atom(), ~v::atom(), ~v::list()}".ErlArgs(E, M, F, A),
                  (_p, _t, b, a) => ioProcessPutChars(
                      b.Cast <ErlAtom>(E), b.Cast <ErlAtom>(M),
                      b.Cast <ErlAtom>(F), b.Cast <ErlList>(A),
                      (IErlObject)a[1]) },
                { "{put_chars, ~v::string()}".ErlArgs(C),
                  (_p, _t, b, a) => ioProcessPutChars(
                      ConstAtoms.Latin1, b.Cast <ErlString>(C), (IErlObject)a[1]) },
                { "{put_chars, ~v::atom(), ~v::atom(), ~v::list()}".ErlArgs(M, F, A),
                  (_p, _t, b, a) => ioProcessPutChars(
                      ConstAtoms.Latin1, b.Cast <ErlAtom>(M),
                      b.Cast <ErlAtom>(F), b.Cast <ErlList>(A),
                      (IErlObject)a[1]) },
                { "{get_until, ~v::atom(), ~v, ~v::atom(), ~v::atom(), ~v::list()}".ErlArgs(E, P, M, F, A),
                  (_p, _t, b, a) => ioProcessGetUntil(
                      b.Cast <ErlAtom>(E), b[P], b.Cast <ErlAtom>(M), b.Cast <ErlAtom>(F),
                      b.Cast <ErlList>(A), (IErlObject)a[1]) },
                { "{get_chars, ~v::atom(), ~v, ~v::integer()}".ErlArgs(E, P, N),
                  (_p, _t, b, a) => ioProcessGetChars(
                      b.Cast <ErlAtom>(E), b[P], b.Cast <ErlLong>(N), (IErlObject)a[1]) },
                { "{get_line, ~v::atom(), ~v}".ErlArgs(E, P),
                  (_p, _t, b, a) => ioProcessGetLine(
                      b.Cast <ErlAtom>(E), b[P], (IErlObject)a[1]) },
                { "{get_until, ~v, ~v::atom(), ~v::atom(), ~v::list()}".ErlArgs(P, M, F, A),
                  (_p, _t, b, a) => ioProcessGetUntil(
                      ConstAtoms.Latin1, b[P], b.Cast <ErlAtom>(M), b.Cast <ErlAtom>(F),
                      b.Cast <ErlList>(A), (IErlObject)a[1]) },
                { "{get_chars, ~v, ~v::integer()}".ErlArgs(P, N),
                  (_p, _t, b, a) => ioProcessGetChars(
                      ConstAtoms.Latin1, b[P], b.Cast <ErlLong>(N), (IErlObject)a[1]) },
                { "{get_line, ~v}".ErlArgs(P),
                  (_p, _t, b, a) => ioProcessGetLine(
                      ConstAtoms.Latin1, b[P], (IErlObject)a[1]) },
                { "{requests, ~v::list()}".ErlArgs(R),
                  (_p, _t, b, a) => ioProcessRequests(
                      (ErlPatternMatcher)a[0], b.Cast <ErlList>(R), (IErlObject)a[1]) },
            };

            while (m_Active)
            {
                Tuple <IErlObject, ErlVarBind> res = Node.GroupLeader.ReceiveMatch(s_RequestPattern, 1500);

                if (res.Item1 == null)     // Timeout
                {
                    continue;
                }
                else if (res.Item2 == null)     // No match
                {
                    App.Log.Write(Log.MessageType.Error,
                                  StringConsts.ERL_INVALID_IO_REQUEST + res.Item1.ToString(), from: GetType().Name);
                }

                var from    = res.Item2.Cast <ErlPid>(F);
                var replyAs = res.Item2[RA];
                var request = res.Item2.Cast <ErlTuple>(R);
                var req     = (IErlObject)request;

                if (patterns.Match(ref req, patterns, replyAs) >= 0)
                {
                    Node.Send(from, req);
                    continue;
                }

                var reply = s_ReplyPattern.Subst(new ErlVarBind {
                    { RA, replyAs }, { R, ConstAtoms.Request }
                });

                Debug.Assert(reply != null);
                Node.Send(from, reply);
            }

            App.Log.Write(Log.MessageType.Info, StringConsts.ERL_STOPPING_SERVER.Args(Node.NodeLongName, "I/O"));

            m_Thread = null;
        }