Exemplo n.º 1
0
        private void sendBuf(ErlTuple ctrl, ErlOutputStream payload = null)
        {
            if (!m_Connected)
            {
                throw new ErlException(StringConsts.ERL_CONN_NOT_CONNECTED_ERROR);
            }

            var header = new ErlOutputStream(writeVersion: false, capacity: HEADER_LEN);

            // preamble: 4 byte length + "PASS_THROUGH" tag + version
            header.Write4BE(0); // reserve space for length
            header.Write1(PASS_THROUGH);
            header.Write1((byte)ErlExternalTag.Version);

            // header info
            header.WriteTuple(ctrl);

            // version for payload
            //header.Write1((byte)ErlExternalTag.Version); // Note that it's already written in the payload

            if (payload == null)
            {
                header.Poke4BE(0, (int)(header.Length - 4));
                DoSend(header);
            }
            else
            {
                header.Poke4BE(0, (int)(header.Length + payload.Length - 4));
                DoSend(header, payload);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Write Erlang tuple to stream
 /// </summary>
 public void WriteTuple(ErlTuple tup)
 {
     WriteTupleHead(tup.Count);
     foreach (var t in tup)
     {
         Write(t);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Read Erlang tuple from the stream
        /// </summary>
        public ErlTuple ReadTuple()
        {
            int arity = readTupleHead();
            var list  = new List <IErlObject>(arity);

            for (int i = 0; i < arity; i++)
            {
                list.Add(Read());
            }
            var res = new ErlTuple();

            res.Value = list;
            return(res);
        }
Exemplo n.º 4
0
        public void ErlFormatTest()
        {
            var tests = new Dictionary<string, ErlList>
              {
            {"abc 10.5",  new ErlList("abc ~w.~w", 10, 5)},
            {"xx 8",      new ErlList("xx ~i~w", 12, 8)},
            {"~z",        new ErlList("~~z", 16)},
            {"a 16",      new ErlList("~c ~w", (byte)'a', 16)},
            {"xyz 12\n",  new ErlList("xyz ~10.6.B~n", 12)},
            {"x~y21",     new ErlList("x~~y~w1", 2)},
            {"{ok, A}",   new ErlList("{ok, ~v}", "A")},
            {"{ok, A}.",  new ErlList("{ok, ~v}.", new ErlAtom("A"))},
            {"{ok, A} ",  new ErlList("{ok, ~v} ", new ErlString("A"))},
            {"{ok, A}  ", new ErlList("{ok, ~v}  ", new ErlVar("A"))},
            {
              "{ok, A::a()}",
              new ErlList("{ok, ~v::a()}", new ErlVar("A", ErlTypeOrder.ErlLong))
            },
            {"{ok, A::int()}", new ErlList("{ok, ~v}", new ErlVar("A", ErlTypeOrder.ErlLong))},
              };

              foreach (var t in tests)
            Assert.AreEqual(t.Key, ErlObject.Format(t.Value),
                        "Error in test: {0} <- format({1})".Args(t.Key, t.Value.ToString()));

              var failTests = new List<ErlList>
              {
            new ErlList("abc ~w.~w"),
            new ErlList("xx ~i~w", 12),
            new ErlList("~y",      12),
              };

              foreach (var t in failTests)
            Assert.Throws<ErlException>(
              () => ErlObject.Format(t), "Errorneously formatted term: {0}".Args(t));

              var V = new ErlVar("V", ErlTypeOrder.ErlLong);
              var expected = new ErlTuple(new ErlAtom("ok"), V).ToString();

              Assert.AreEqual(expected, "{ok, V::int()}".ToErlObject().ToString());
              Assert.AreEqual(expected, "{ok, ~w}".ToErlObject(V).ToString());
        }
Exemplo n.º 5
0
    public void ErlTermSerializeTest()
    {
      {
        var b = new byte[] { 131, 100, 0, 3, 97, 98, 99 };
        var t = new ErlAtom("abc");
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 109, 0, 0, 0, 3, 1, 2, 3 };
        var t = new ErlBinary(new byte[] { 1, 2, 3 });
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b1 = new byte[] { 131, 100, 0, 4, 116, 114, 117, 101 };
        var t1 = new ErlBoolean(true);
        var os1 = new ErlOutputStream(t1);
        Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
        var es1 = new ErlInputStream(b1);
        Assert.AreEqual(t1, es1.Read());

        var b2 = new byte[] { 131, 100, 0, 5, 102, 97, 108, 115, 101 };
        var t2 = new ErlBoolean(false);
        var os2 = new ErlOutputStream(t2);
        Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
        var es2 = new ErlInputStream(b2);
        Assert.AreEqual(t2, es2.Read());
      }
      {
        var b = new byte[] { 131, 97, 127 };
        var t = new ErlByte(127);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 70, 64, 36, 62, 249, 219, 34, 208, 229 };
        var t = new ErlDouble(10.123);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131,108,0,0,0,3,97,1,70,64,36,61,112,163,215,10,61,108,0,0,0,2,
                             100,0,4,116,114,117,101,107,0,1,97,106,106 };
        var t = new ErlList(1, 10.12, new ErlList(true, "a"));
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b1 = new byte[] { 131, 98, 255, 255, 255, 251 };
        var t1 = new ErlLong(-5);
        var os1 = new ErlOutputStream(t1);
        Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
        var es1 = new ErlInputStream(b1);
        Assert.AreEqual(t1, es1.Read());

        var b2 = new byte[] { 131, 97, 5 };
        var t2 = new ErlLong(5);
        var os2 = new ErlOutputStream(t2);
        Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
        var es2 = new ErlInputStream(b2);
        Assert.AreEqual(t2, es2.Read());

        var b3 = new byte[] { 131, 98, 0, 16, 0, 0 };
        var t3 = new ErlLong(1024 * 1024);
        var os3 = new ErlOutputStream(t3);
        Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
        var es3 = new ErlInputStream(b3);
        Assert.AreEqual(t3, es3.Read());

        var b4 = new byte[] { 131, 110, 6, 0, 0, 0, 0, 0, 0, 4 };
        var t4 = new ErlLong(1024L * 1024 * 1024 * 1024 * 4);
        var os4 = new ErlOutputStream(t4);
        Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
        var es4 = new ErlInputStream(b4);
        Assert.AreEqual(t4, es4.Read());

        var b5 = new byte[] { 131, 110, 8, 0, 0, 0, 0, 0, 0, 0, 0, 128 };
        var t5 = new ErlLong(1L << 63);
        var os5 = new ErlOutputStream(t5);
        Assert.AreEqual(b5, os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray());
        var es5 = new ErlInputStream(b5);
        Assert.AreEqual(t5, es5.Read());

        var b6 = new byte[] { 131, 110, 8, 0, 0, 0, 0, 0, 0, 0, 0, 128 };
        var t6 = new ErlLong(-1L << 63);
        var os6 = new ErlOutputStream(t6);
        Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
        var es6 = new ErlInputStream(b6);
        Assert.AreEqual(t6, es6.Read());

        var b7 = new byte[] { 131, 110, 8, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
        var es7 = new ErlInputStream(b7);
        var t7 = new ErlLong(-1);
        Assert.AreEqual(t7, es7.Read());
        var bi7 = new byte[] {131, 98, 255, 255, 255, 255};
        var os7 = new ErlOutputStream(t7);
        Assert.AreEqual(bi7, os7.GetBuffer().TakeWhile((_, i) => i < bi7.Length).ToArray());
      }
      {
        var b = new byte[] { 131, 103, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 0, 0, 0, 0, 1 };
        var t = new ErlPid("b@pipit", 38, 0, 1);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 102, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 1 };
        var t = new ErlPort("b@pipit", 38, 1);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 114, 0, 3, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 1, 0, 0, 0, 181, 0, 0, 0, 0, 0, 0, 0, 0 };
        var t = new ErlRef("b@pipit", 181, 0, 0, 1);
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 107, 0, 3, 115, 116, 114 };
        var t = new ErlString("str");
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
      {
        var b = new byte[] { 131, 104, 3, 97, 1, 100, 0, 1, 97, 104, 2, 97, 10, 70, 63, 241, 247, 206, 217, 22, 135, 43 };
        var t = new ErlTuple(1, new ErlAtom("a"), new ErlTuple(10, 1.123));
        var os = new ErlOutputStream(t);
        Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
        var es = new ErlInputStream(b);
        Assert.AreEqual(t, es.Read());
      }
    }
Exemplo n.º 6
0
 public ErlTrace(ErlTuple list) : base((ErlTupleBase)list)
 {
 }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            
            //here we handle password requests
            ErlTransportPasswordSource.PasswordRequired += (ps) =>
            {
              Console.Write("Username: {0}\nPassword: "******"{0} ({1}): {2}", t, d, text);

                n.Start();

                var cfg = App.ConfigRoot.NavigateSection("/erlang");
                var remoteName = cfg.Children.Select(nd => nd.Name == "node" ? nd.Value : string.Empty)
                                             .FirstOrDefault(s => s.Contains("@"));

                Console.WriteLine("\n\nExecute the following code on remote node {0}:", remoteName);
                Console.WriteLine("1> F = fun F() -> receive {From, Msg} -> From ! Msg, F() end end.");
                Console.WriteLine("2> spawn_link(fun() -> register(me, self()), F() end).\n");
                Console.WriteLine("Press any key when ready...");

                Console.ReadKey();

                var m = n.CreateMbox("test");
                var a = new ErlAtom("hello");
                var msg = new ErlTuple(m.Self, a);
                var remoteNode = new ErlAtom(remoteName);

                DateTime empty = new DateTime(2000, 1, 1, 0, 0, 0);
                DateTime start = empty;
                long count = 0;
                long msgs = 30000;

                do
                {
                    var res = n.Send(m.Self, remoteNode, "me", msg);
                    if (!res)
                    {
                        Console.WriteLine("Can not send message");
                        break;
                    }

                    if (start == empty)
                        start = DateTime.UtcNow;

                    var got = m.Receive(5000);

                    if (!got.Equals((IErlObject)a))
                    {
                        Console.WriteLine("Got wrong result! Expected: {0}, Got: {1}", a, got);
                        count = -1;
                        break;
                    }

                    count++;

                    if ((count % 10000) == 0)
                        Console.WriteLine("Processed {0} messages", count);
                    //Console.ReadLine();
                } while (count < msgs);

                var end = DateTime.UtcNow;
                var diff = (end - start);

                if (count > 0)
                    Console.WriteLine("Processed {0} messages. Speed: {1:F2}msgs/s, Latency: {2}us",
                        msgs, msgs / diff.TotalSeconds, 1000.0 * diff.TotalMilliseconds / msgs);
            }

            if (Debugger.IsAttached)
                Console.ReadKey();
        }
Exemplo n.º 8
0
 /// <summary>
 /// Determine if two Erlang tuples are equal
 /// </summary>
 public bool Equals(ErlTuple o)
 {
     return(m_Items.SequenceEqual(o.m_Items));
 }
Exemplo n.º 9
0
        public void ErlTermSerializeTest()
        {
            {
            var b = new byte[] { 131, 100, 0, 3, 97, 98, 99 };
            var t = new ErlAtom("abc");
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 109, 0, 0, 0, 3, 1, 2, 3 };
            var t = new ErlBinary(new byte[] { 1, 2, 3 });
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b1 = new byte[] { 131, 100, 0, 4, 116, 114, 117, 101 };
            var t1 = new ErlBoolean(true);
            var os1 = new ErlOutputStream(t1);
            Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
            var es1 = new ErlInputStream(b1);
            Assert.AreEqual(t1, es1.Read());

            var b2 = new byte[] { 131, 100, 0, 5, 102, 97, 108, 115, 101 };
            var t2 = new ErlBoolean(false);
            var os2 = new ErlOutputStream(t2);
            Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
            var es2 = new ErlInputStream(b2);
            Assert.AreEqual(t2, es2.Read());
              }
              {
            var b = new byte[] { 131, 97, 127 };
            var t = new ErlByte(127);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 70, 64, 36, 62, 249, 219, 34, 208, 229 };
            var t = new ErlDouble(10.123);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 108, 0, 0, 0, 2, 107, 0, 1, 1, 107, 0, 1, 2, 106 };
            var t = new ErlList(new ErlList(1), new ErlList(2));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131,108,0,0,0,2,108,0,0,0,2,97,1,107,0,1,2,106,107,0,1,3,106 };
            var t = new ErlList(new ErlList(1, new ErlList(2)), new ErlList(3));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131,108,0,0,0,3,97,1,70,64,36,61,112,163,215,10,61,108,0,0,0,2,
                             100,0,4,116,114,117,101,107,0,1,97,106,106 };
            var t = new ErlList(1, 10.12, new ErlList(true, "a"));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] {
              131,108,0,0,0,3,97,23,97,4,104,1,108,0,0,0,1,104,2,109,0,0,0,5,101,118,101,
              110,116,104,1,108,0,0,0,2,104,2,109,0,0,0,10,102,108,101,101,116,95,104,97,
              115,104,109,0,0,0,36,97,54,97,50,50,100,49,52,45,56,52,56,51,45,52,49,102,99,
              45,97,52,54,98,45,50,56,51,98,57,55,55,55,99,50,97,50,104,2,109,0,0,0,4,116,
              121,112,101,109,0,0,0,13,102,108,101,101,116,95,99,104,97,110,103,101,100,
              106,106,106 };
            var t = ErlObject.Parse("[23,4,{[{<<\"event\">>,"+
                                "{[{<<\"fleet_hash\">>,<<\"a6a22d14-8483-41fc-a46b-283b9777c2a2\">>},"+
                                "{<<\"type\">>,<<\"fleet_changed\">>}]}}]}]");
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b1 = new byte[] { 131, 98, 255, 255, 255, 251 };
            var t1 = new ErlLong(-5);
            var os1 = new ErlOutputStream(t1);
            Assert.AreEqual(b1, os1.GetBuffer().TakeWhile((_, i) => i < b1.Length).ToArray());
            var es1 = new ErlInputStream(b1);
            Assert.AreEqual(t1, es1.Read());

            var b2 = new byte[] { 131, 97, 5 };
            var t2 = new ErlLong(5);
            var os2 = new ErlOutputStream(t2);
            Assert.AreEqual(b2, os2.GetBuffer().TakeWhile((_, i) => i < b2.Length).ToArray());
            var es2 = new ErlInputStream(b2);
            Assert.AreEqual(t2, es2.Read());

            var b3 = new byte[] { 131, 98, 0, 16, 0, 0 };
            var t3 = new ErlLong(1024 * 1024);
            var os3 = new ErlOutputStream(t3);
            Assert.AreEqual(b3, os3.GetBuffer().TakeWhile((_, i) => i < b3.Length).ToArray());
            var es3 = new ErlInputStream(b3);
            Assert.AreEqual(t3, es3.Read());

            var b4 = new byte[] { 131, 110, 6, 0, 0, 0, 0, 0, 0, 4 };
            var t4 = new ErlLong(1024L * 1024 * 1024 * 1024 * 4);
            var os4 = new ErlOutputStream(t4);
            Assert.AreEqual(b4, os4.GetBuffer().TakeWhile((_, i) => i < b4.Length).ToArray());
            var es4 = new ErlInputStream(b4);
            Assert.AreEqual(t4, es4.Read());

            var b5 = new byte[] { 131, 110, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128 };
            var t5 = new ErlLong(1L << 63);
            var os5 = new ErlOutputStream(t5);
            Assert.AreEqual(b5, os5.GetBuffer().TakeWhile((_, i) => i < b5.Length).ToArray());
            var es5 = new ErlInputStream(b5);
            Assert.AreEqual(t5, es5.Read());

            var b6 = new byte[] { 131, 110, 8, 1, 0, 0, 0, 0, 0, 0, 0, 128 };
            var t6 = new ErlLong(-1L << 63);
            var os6 = new ErlOutputStream(t6);
            Assert.AreEqual(b6, os6.GetBuffer().TakeWhile((_, i) => i < b6.Length).ToArray());
            var es6 = new ErlInputStream(b6);
            Assert.AreEqual(t6, es6.Read());

            var b7 = new byte[] { 131, 110, 8, 0, 255, 255, 255, 255, 255, 255, 255, 255 };
            var es7 = new ErlInputStream(b7);
            var t7 = new ErlLong(-1);
            Assert.AreEqual(t7, es7.Read());
            var bi7 = new byte[] {131, 98, 255, 255, 255, 255};
            var os7 = new ErlOutputStream(t7);
            Assert.AreEqual(bi7, os7.GetBuffer().TakeWhile((_, i) => i < bi7.Length).ToArray());
              }
              {
            var b = new byte[] { 131, 103, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 0, 0, 0, 0, 1 };
            var t = new ErlPid("b@pipit", 38, 0, 1);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 102, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 0, 0, 0, 38, 1 };
            var t = new ErlPort("b@pipit", 38, 1);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 114, 0, 3, 100, 0, 7, 98, 64, 112, 105, 112, 105, 116, 1, 0, 0, 0, 181, 0, 0, 0, 0, 0, 0, 0, 0 };
            var t = new ErlRef("b@pipit", 181, 0, 0, 1);
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 107, 0, 3, 115, 116, 114 };
            var t = new ErlString("str");
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
              {
            var b = new byte[] { 131, 104, 3, 97, 1, 100, 0, 1, 97, 104, 2, 97, 10, 70, 63, 241, 247, 206, 217, 22, 135, 43 };
            var t = new ErlTuple(1, new ErlAtom("a"), new ErlTuple(10, 1.123));
            var os = new ErlOutputStream(t);
            Assert.AreEqual(b, os.GetBuffer().TakeWhile((_, i) => i < b.Length).ToArray());
            var es = new ErlInputStream(b);
            Assert.AreEqual(t, es.Read());
              }
        }
Exemplo n.º 10
0
 public ErlTuple(ErlTuple list) : base((ErlTupleBase)list) {}
Exemplo n.º 11
0
    /// <summary>
    /// Converts CLR row data into ErlTuple
    /// </summary>
    public ErlTuple RowToErlTuple(Row row, bool keysOnly = false)
    {
      var result = new ErlTuple();
      
      result.Add(new ErlAtom(row.Schema.Name));

      var keys = new List<IErlObject>();

      foreach(var def in row.Schema)
      {
        var atr = def[m_Store.TargetName];

        if (keysOnly && !atr.Key) break;

        if (keys!=null && !atr.Key)
        {
          if (keys.Count>1)
            result.Add( new ErlTuple(keys, false) );
          else
            foreach(var key in keys) result.Add( key ); 
          keys = null;
        }


        IErlObject erlValue = ErlAtom.Undefined;

        var clrValue = row.GetFieldValue(def);
        if (clrValue!=null)
          erlValue = ClrToErlValue(atr.BackendType, clrValue);
        
        if (keys!=null)
          keys.Add( erlValue );
        else
          result.Add( erlValue );
      }

      if (keys!=null)
      {
        if (keys.Count>1)
         result.Add( new ErlTuple(keys, false) );
        else
         foreach(var key in keys) result.Add( key ); 
      }

      return result;
    }
Exemplo n.º 12
0
        private void sendBuf(ErlTuple ctrl, ErlOutputStream payload = null)
        {
            if (!m_Connected)
            throw new ErlException(StringConsts.ERL_CONN_NOT_CONNECTED_ERROR);

              var header = new ErlOutputStream(writeVersion: false, capacity: HEADER_LEN);

              // preamble: 4 byte length + "PASS_THROUGH" tag + version
              header.Write4BE(0); // reserve space for length
              header.Write1(PASS_THROUGH);
              header.Write1((byte)ErlExternalTag.Version);

              // header info
              header.WriteTuple(ctrl);

              // version for payload
              //header.Write1((byte)ErlExternalTag.Version); // Note that it's already written in the payload

              if (payload == null)
              {
            header.Poke4BE(0, (int)(header.Length - 4));
            DoSend(header);
              }
              else
              {
            header.Poke4BE(0, (int)(header.Length + payload.Length - 4));
            DoSend(header, payload);
              }
        }
Exemplo n.º 13
0
    private static void Main(string[] args)
    {
      //here we handle password requests
      ErlTransportPasswordSource.PasswordRequired += ps =>
      {
        Console.Write("Username: {0}\nPassword: "******"batch"].AttrByIndex(0).ValueAsInt(1);
        var totalMsgs = cfg["count"].AttrByIndex(0).ValueAsInt(100000);
        var traceAttr = cfg["trace"].AttrByIndex(0);
        var trace     = traceAttr.ValueAsEnum(ErlTraceLevel.Handshake);
        var noSSH     = cfg["nossh"].Exists;
        var noTrace   = cfg["notrace"].Exists;
        var user      = cfg["user"].AttrByIndex(0).ValueAsString("");
        var privkey   = cfg["sshkey"].AttrByIndex(0).ValueAsString("");

        if (cfg["h", "help"].Exists)
        {
          Console.WriteLine(
            "Usage: {0} [-batch BatchSize] [-count Iterations] [-trace wire|handshake|send] [-notrace]\n" +
            "       [-nossh] [-user User] [-sshkey SSHKeyFile]",
            MiscUtils.EntryExeName(false));
          Environment.ExitCode = 1;
          return;
        }

        var n = ErlApp.Node;
        n.AcceptConnections = false;
        n.TraceLevel        = traceAttr.Exists && n.TraceLevel == ErlTraceLevel.Off
                            ? trace : n.TraceLevel;
        if (noTrace)
          n.TraceLevel = ErlTraceLevel.Off;

        n.Trace += (_n, t, d, text) =>
          Console.WriteLine("{0,-9} {1} {2}", t, d == Direction.Inbound ? "<-" : "->", text);

        //n.Start();

        cfg            = App.ConfigRoot["erlang"];
        var remote     = cfg.Children
                            .FirstOrDefault(nd => nd.IsSameName("node") && nd.Value.Contains("@"));
        var remoteName = remote.ValueAsString(string.Empty);

        if (noSSH)
        {
          var mmm = remote.Attributes.FirstOrDefault(c => c.IsSameName("transport-type"));
          if (mmm.Exists)
            mmm.Delete();
        }

        if (user.IsNotNullOrWhiteSpace())
          remote.AttrByName("ssh-user-name").Value = user;

        if (privkey.IsNotNullOrWhiteSpace())
        {
          remote.AttrByName("ssh-private-key-file").Value    = privkey;
          remote.AttrByName("ssh-authentication-type").Value = "PublicKey";
        }

        Console.WriteLine(
          "\n\nCopy echo_server.erl to remote node {0}, and execute in the same directory:",
          remoteName);
        Console.WriteLine("1> c(echo_server).");
        Console.WriteLine("2> echo_server:run({0}).\n", batchSize == 1 ? "1" : "");
        Console.WriteLine("Press <Enter> when ready...");

        Console.ReadLine();

        var m = n.CreateMbox("test");
        var a = new ErlAtom("hello");
        var msg = new ErlTuple(m.Self, a);
        var remoteNode = new ErlAtom(remoteName);

        DateTime empty = new DateTime(2000, 1, 1, 0, 0, 0);
        DateTime start = empty;
        long count = 0;

        do
        {
          int i = 0;
          for (; i < batchSize && count < totalMsgs; ++i, ++count)
          {
            var res = n.Send(m.Self, remoteNode, "me", msg);
            if (!res)
            {
              Console.WriteLine("Can not send message");
              goto DONE;
            }

            if (start == empty)
            {
              Console.WriteLine("\nTurning off tracing");
              n.TraceLevel = ErlTraceLevel.Off;
              start = DateTime.UtcNow;
            }
          }

          for (int j = 0; j < i; ++j)
          {
            var got = m.Receive(5000);

            if (got == null)
            {
              Console.WriteLine("Timeout waiting for reply (count = {0})".Args(count));
              goto DONE;
            }

            if (!got.Equals(a))
            {
              Console.WriteLine("Got wrong result! Expected: {0}, Got: {1}", a, got);
              count = -1;
              goto DONE;
            }
          }

          if ((count%10000) == 0)
            Console.WriteLine("Processed {0} messages", count);
        } while (count < totalMsgs);

      DONE:
        var end = DateTime.UtcNow;
        var diff = (end - start);

        if (count > 0)
          Console.WriteLine("Processed {0} messages. Speed: {1:F2}msgs/s, Latency: {2}us",
                            totalMsgs, totalMsgs/diff.TotalSeconds,
                            1000.0*diff.TotalMilliseconds/totalMsgs);
      }

      if (Debugger.IsAttached)
      {
        Console.WriteLine("Press <Enter> when ready...");
        Console.Read();
      }
    }
Exemplo n.º 14
0
        public void ErlTupleTest()
        {
            var l = new ErlTuple("test", 1, 1.1, true, (byte)255, 'x', new ErlAtom("a"));
              var r = new ErlTuple("test", 1, 1.1, true, (byte)255, 'x', new ErlAtom("a"));

              Assert.AreEqual(7, l.Count);
              Assert.AreEqual(ErlTypeOrder.ErlString, l[0].TypeOrder);
              Assert.AreEqual("test", l[0].ValueAsString);
              Assert.AreEqual(ErlTypeOrder.ErlLong, l[1].TypeOrder);
              Assert.AreEqual(1, l[1].ValueAsInt);
              Assert.AreEqual(ErlTypeOrder.ErlDouble, l[2].TypeOrder);
              Assert.AreEqual(1.1, l[2].ValueAsDouble);
              Assert.AreEqual(ErlTypeOrder.ErlBoolean, l[3].TypeOrder);
              Assert.AreEqual(true, l[3].ValueAsBool);
              Assert.AreEqual(ErlTypeOrder.ErlByte, l[4].TypeOrder);
              Assert.AreEqual(255, l[4].ValueAsInt);
              Assert.AreEqual(ErlTypeOrder.ErlByte, l[5].TypeOrder);
              Assert.AreEqual('x', l[5].ValueAsChar);
              Assert.AreEqual(ErlTypeOrder.ErlAtom, l[6].TypeOrder);
              Assert.AreEqual("a", l[6].ValueAsString);

              Assert.IsTrue(l.Matches(r));
              Assert.AreEqual(new ErlVarBind(), l.Match(r));

              Assert.AreEqual(l, r);
              Assert.IsTrue(l.Equals(r));
              Assert.AreEqual("{\"test\",1,1.1,true,255,120,a}", l.ToString());
              Assert.IsFalse(l.IsScalar);
              Assert.AreEqual(ErlTypeOrder.ErlTuple, l.TypeOrder);

              IErlObject temp = null;
              Assert.IsFalse(l.Subst(ref temp, new ErlVarBind()));
              Assert.AreEqual(1, l.Visit(0, (acc, o) => acc + (o is ErlAtom ? 1 : 0)));
              Assert.IsTrue(new ErlTuple(new ErlVar(X), true, 1).Subst(ref temp, new ErlVarBind { { X, new ErlLong(10) } }));
              Assert.AreEqual("{10,true,1}", temp.ToString());

              var d = new DateTime(2013, 1, 2);
              var ts = new TimeSpan(1, 2, 3);

              Assert.DoesNotThrow(() => { var x = l.ValueAsObject; });
              Assert.AreEqual(1, new ErlList("1")[0].ValueAsInt);
              Assert.AreEqual(1, new ErlList("1")[0].ValueAsLong);
              Assert.AreEqual(1, new ErlList("1")[0].ValueAsDecimal);
              Assert.AreEqual(d, new ErlList(d.ToString())[0].ValueAsDateTime);
              Assert.AreEqual(ts, new ErlList(ts.ToString())[0].ValueAsTimeSpan);
              Assert.AreEqual(1.0, new ErlList("1.0")[0].ValueAsDouble);
              Assert.AreEqual("a", new ErlList("a")[0].ValueAsString);
              Assert.IsTrue(new ErlList("true")[0].ValueAsBool);
              Assert.IsFalse(new ErlList("xxxx")[0].ValueAsBool);

              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsInt; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsLong; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsDecimal; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsDateTime; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsTimeSpan; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsDouble; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsString; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsBool; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsChar; });
              Assert.Throws<ErlIncompatibleTypesException>(() => { var x = l.ValueAsByteArray; });

              List<IErlObject> s = l;
              Assert.AreEqual(l.Value, s);

              Assert.IsFalse(new ErlList(1, 1.0, "a").Equals(new ErlTuple(1, 1.0, "a")));
              Assert.IsFalse(new ErlList(1, 1.0, "a") == new ErlTuple(1, 1.0, "a"));
              Assert.IsTrue(new ErlList(1, 1.0, "a") == new ErlList(1, 1.0, "a"));
        }
Exemplo n.º 15
0
      private IEnumerable<IErlObject> enumErlResponseTuple(ErlTuple tuple, bool singleKey)
      {
         if (singleKey)
         {
           foreach(var elm in tuple)
            yield return elm;

           yield break;
         }
         
         for(var i=0; i<tuple.Count; i++)
         {
            var elm = tuple[i];
            if (i==1)
            {
              var ktuple = elm as ErlTuple;
              if (ktuple==null)
               throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"key tuple is missing");

              foreach(var keye in ktuple)
               yield return keye;
              continue;
            }
            yield return elm;
         }
      }
Exemplo n.º 16
0
    /// <summary>
    /// Converts CLR row data into ErlTuple
    /// </summary>
    public ErlTuple RowToErlTuple(Row row, bool keysOnly = false)
    {
      var result = new ErlTuple();
      
      result.Add(new ErlAtom(row.Schema.Name));

      var keys = new List<IErlObject>();

      foreach(var def in row.Schema)
      {
        var atr = def[m_Store.TargetName];

        if (keysOnly && !atr.Key) break;

        if (keys!=null && !atr.Key)
        {
          if (keys.Count>1)
            result.Add( new ErlTuple(keys, false) );
          else
            foreach(var key in keys) result.Add( key ); 
          keys = null;
        }


        IErlObject erlValue = ErlAtom.Undefined;

        var clrValue = row.GetFieldValue(def);
        if (clrValue!=null)
        {
           Func<object, IErlObject> fconv;
           if (!CLR_TO_ERL_TYPEMAP.TryGetValue(atr.BackendType, out fconv))
            throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"erltype'{0}' not matched in the dict".Args(atr.BackendType));

           erlValue = fconv(clrValue);
        }
        
        if (keys!=null)
          keys.Add( erlValue );
        else
          result.Add( erlValue );
      }

      if (keys!=null)
      {
        if (keys.Count>1)
         result.Add( new ErlTuple(keys, false) );
        else
         foreach(var key in keys) result.Add( key ); 
      }

      return result;
    }
Exemplo n.º 17
0
    /// <summary>
    /// Maps ErlRow to CLR row supplied by schema, either Dynamic or TypedRow 
    /// </summary>
    public Row ErlTupleToRow(string schemaName, ErlTuple tuple, Schema schema)
    {
      var singleKey = schema.ExtraData[SCHEMA_KEY_COUNT].AsInt(0) < 2;

      var row = Row.MakeRow(schema, schema.TypedRowType);

      var i = -1;
      foreach(var elm in enumErlResponseTuple(tuple, singleKey))
      {
        i++;
        if (i==0)
        {
          var thisname = tuple[0].ValueAsString;
          if (!schemaName.EqualsOrdSenseCase(thisname))
            throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESPONSE_SCHEMA_MISMATCH_ERROR.Args(thisname, schemaName));
          continue;
        }

        //map fields now
        if (i-1 >= schema.FieldCount)
          throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESPONSE_SCHEMA_FLD_COUNT_MISMATCH_ERROR.Args(schemaName, schema.Name));
        var fdef = schema[i-1];
        var atr = fdef[m_Store.TargetName];

        var erlType = atr.BackendType;
        if (erlType.IsNullOrWhiteSpace())
          throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"fielddef('{0}') has no backend type".Args(fdef.Name));


        if (elm.IsNull())
        {
          row.SetFieldValue(fdef, null);
          continue;
        }

        Tuple<Type, Func<IErlObject, object>> mapping;
        if (ERL_TO_CLR_TYPEMAP.TryGetValue(erlType, out mapping))
        {
          var clrValue = mapping.Item2(elm);
          row.SetFieldValue(fdef, clrValue);
        }
        else throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"erltype'{0}' not matched in the dict".Args(erlType));
      }
  
      return row;
    }
Exemplo n.º 18
0
    /// <summary>
    /// Maps ErlRow to CLR row supplied by schema, either Dynamic or TypedRow 
    /// </summary>
    public Row ErlTupleToRow(string schemaName, ErlTuple tuple, Schema schema)
    {
      var singleKey = schema.ExtraData[SCHEMA_KEY_COUNT].AsInt(0) < 2;

      var row = Row.MakeRow(schema, schema.TypedRowType);

      var i = -1;
      foreach(var elm in enumErlResponseTuple(tuple, singleKey))
      {
        i++;
        if (i==0)
        {
          var thisname = tuple[0].ValueAsString;
          if (!schemaName.EqualsOrdSenseCase(thisname))
            throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESPONSE_SCHEMA_MISMATCH_ERROR.Args(thisname, schemaName));
          continue;
        }

        //map fields now
        if (i-1 >= schema.FieldCount)
          throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESPONSE_SCHEMA_FLD_COUNT_MISMATCH_ERROR.Args(schemaName, schema.Name));
        var fdef = schema[i-1];
        var atr = fdef[m_Store.TargetName];

        var erlType = atr.BackendType;
        if (erlType.IsNullOrWhiteSpace())
          throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"fielddef('{0}') has no backend type".Args(fdef.Name));


        if (elm.IsNull())
        {
          row.SetFieldValue(fdef, null);
          continue;
        }

        Tuple<Type, Func<IErlObject, object>> mapping;
        if (ERL_TO_CLR_TYPEMAP.TryGetValue(erlType, out mapping))
        {
          object clrValue = null;

          try
          {
            clrValue = mapping.Item2(elm);
          }
          catch(Exception error)
          {
            App.Log.Write( new Log.Message
            {
              Type = Log.MessageType.TraceErl,
              Topic = CoreConsts.ERLANG_TOPIC,
              From = "SchemaMap.ErlTupleToRow({0})".Args(schemaName),
              Text = "Error converting element '{0}'->'{1}': {2}".Args(erlType, elm.GetType(), error.ToMessageWithType()),
              Exception = error
            });
            throw;
          }
          row.SetFieldValue(fdef, clrValue);
        }
        else throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"erltype'{0}' not matched in the dict".Args(erlType));
      }
  
      return row;
    }
Exemplo n.º 19
0
 /// <summary>
 /// Read Erlang tuple from the stream
 /// </summary>
 public ErlTuple ReadTuple()
 {
     int arity = readTupleHead();
     var list = new List<IErlObject>(arity);
     for (int i = 0; i < arity; i++)
       list.Add(Read());
     var res = new ErlTuple();
     res.Value = list;
     return res;
 }
Exemplo n.º 20
0
 /// <summary>
 /// Determine if two Erlang tuples are equal
 /// </summary>
 public bool Equals(ErlTuple o) { return m_Items.SequenceEqual(o.m_Items); }
Exemplo n.º 21
0
 /// <summary>
 /// Write Erlang tuple to stream
 /// </summary>
 public void WriteTuple(ErlTuple tup)
 {
   WriteTupleHead(tup.Count);
   foreach (var t in tup)
     Write(t);
 }
Exemplo n.º 22
0
    /// <summary>
    /// Maps ErlRow to CLR row supplied by schema, either Dynamic or TypedRow 
    /// </summary>
    public Row ErlTupleToRow(string schemaName, ErlTuple tuple, Schema schema)
    {
      var singleKey = schema.ExtraData[SCHEMA_KEY_COUNT].AsInt(0) < 2;

      var row = Row.MakeRow(schema, schema.TypedRowType);

      var i = -1;
      foreach(var elm in enumErlResponseTuple(tuple, singleKey))
      {
        i++;
        if (i==0)
        {
          var thisname = tuple[0].ValueAsString;
          if (!schemaName.EqualsOrdSenseCase(thisname))
            throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESP_SCH_MISMATCH_ERROR.Args(thisname, schemaName));
          continue;
        }

        //map fields now
        if (i-1 >= schema.FieldCount)
          throw new ErlDataAccessException(StringConsts.ERL_DS_CRUD_RESP_SCH_FLD_COUNT_ERROR.Args(schemaName, schema.Name));
        var fdef = schema[i-1];
        var atr = fdef[m_Store.TargetName];

        var erlType = atr.BackendType;
        if (erlType.IsNullOrWhiteSpace())
          throw new ErlDataAccessException(StringConsts.ERL_DS_INTERNAL_MAPPING_ERROR+"fielddef('{0}') has no backend type".Args(fdef.Name));

        var clrValue = ErlToClrValue(elm, schema, fdef, m_Store.TargetName, tuple);
        row.SetFieldValue(fdef, clrValue);
      }
  
      return row;
    }