/// <summary> /// Send an RPC cast request to the remote Erlang node /// </summary> public void SendRPCcast(ErlPid from, string mod, string fun, ErlList args) { IErlObject rpc = Internal.ErlRpcServer.EncodeRPCcast(from, mod, fun, args, ConstAtoms.User); var msg = ErlMsg.RegSend(from, ConstAtoms.Rex, rpc); Send(msg); }
/// <summary> /// Write Erlang list to stream /// </summary> public void WriteList(ErlList list) { int n = list.Count; if (n == 0) { WriteNil(); } else { var isStr = n < 256 && list.All(i => { if (i.TypeOrder != ErlTypeOrder.ErlLong && i.TypeOrder != ErlTypeOrder.ErlByte) { return(false); } var k = i.ValueAsInt; return(k >= 0 && k <= 255); }); WriteListHead(list.Count, isStr); if (isStr) { list.ForEach(t => Write1(t.ValueAsLong)); } else { list.ForEach(t => Write(t)); WriteNil(); } } }
private static IErlObject coreToErlObject(object o, ErlTypeOrder etp, bool strict) { var eh = strict ? ConvertErrorHandling.Throw : ConvertErrorHandling.ReturnDefault; var e = o as IErlObject; if (e != null) { return(e); } try { switch (etp) { case ErlTypeOrder.ErlObject: case ErlTypeOrder.ErlAtom: return(new ErlAtom(o.ToString())); case ErlTypeOrder.ErlBinary: return(new ErlBinary((byte[])o)); case ErlTypeOrder.ErlBoolean: return(new ErlBoolean(o.AsBool(handling: eh))); case ErlTypeOrder.ErlByte: return(new ErlByte(o.AsByte(handling: eh))); case ErlTypeOrder.ErlDouble: return(new ErlDouble(o.AsDouble(handling: eh))); case ErlTypeOrder.ErlLong: return(new ErlLong(o.AsLong(handling: eh))); case ErlTypeOrder.ErlList: { var list = new ErlList(); foreach (var item in (IEnumerable)o) { list.Add(item.ToErlObject()); } return(list); } case ErlTypeOrder.ErlString: return(new ErlString(o.AsString(handling: eh))); case ErlTypeOrder.ErlTuple: return(new ErlTuple((object[])o)); case ErlTypeOrder.ErlPid: case ErlTypeOrder.ErlPort: case ErlTypeOrder.ErlRef: case ErlTypeOrder.ErlVar: default: throw new Exception(); } } catch (Exception) { throw new ErlException (StringConsts.ERL_CANNOT_CONVERT_TYPES_ERROR, o.GetType().ToString(), etp.ToString()); } }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, int timeout, ErlAtom?remoteCookie = null) { AsyncRPC(node, mod, fun, args, remoteCookie); var r = m_Monitors.Add(node); using (Scope.OnExit(() => m_Monitors.Remove(r))) { return(ReceiveRPC(timeout)); } }
/// <summary> /// Write Erlang list to stream /// </summary> public void WriteList(ErlList list) { int n = list.Count; if (n > 0) { WriteListHead(list.Count); foreach (var t in list) { Write(t); } } WriteNil(); }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, int timeout) { var mbox = m_Mailboxes.Create(true); try { var res = mbox.RPC(node, mod, fun, args, timeout); return(res); } finally { m_Mailboxes.Unregister(mbox); } }
public void RPCcast(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, IErlObject ioServer) { if (node.Equals(m_Node.NodeName)) { throw new ErlException(StringConsts.ERL_CONN_CANT_RPC_TO_LOCAL_NODE_ERROR); } else { var msg = Internal.ErlRpcServer.EncodeRPCcast(m_Self, mod, fun, args, ioServer); var conn = m_Node.Connection(node); if (conn == null) { throw new ErlConnectionException( node, StringConsts.ERL_CONN_CANT_CONNECT_TO_NODE_ERROR.Args(node)); } conn.Send(m_Self, ConstAtoms.Rex, msg); } }
public void AsyncRPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, ErlAtom? remoteCookie = null) { AsyncRPC(node, mod, fun, args, (IErlObject)m_Node.GroupLeader.Self, remoteCookie); }
public void RPCcast(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args) { RPCcast(node, mod, fun, args, ConstAtoms.User); }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, ErlAtom? remoteCookie = null) { return RPC(node, mod, fun, args, -1, remoteCookie); }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, int timeout, ErlAtom? remoteCookie = null) { AsyncRPC(node, mod, fun, args, remoteCookie); var r = m_Monitors.Add(node); using (Scope.OnExit(() => m_Monitors.Remove(r))) { return ReceiveRPC(timeout); } }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args) { return(RPC(node, mod, fun, args, -1)); }
private static IErlObject coreToErlObject(object o) { // Erlang terms if (o is IErlObject) { return((IErlObject)o); } // Native types if (o is int) { return(new ErlLong((int)o)); } else if (o is uint) { return(new ErlLong((uint)o)); } else if (o is long) { return(new ErlLong((long)o)); } else if (o is ulong) { return(new ErlLong((long)(ulong)o)); } else if (o is double) { return(new ErlDouble((double)o)); } else if (o is float) { return(new ErlDouble((float)o)); } else if (o is string) { return(new ErlString((string)o)); } else if (o is bool) { return(new ErlBoolean((bool)o)); } else if (o is char) { return(new ErlByte((char)o)); } else if (o is byte) { return(new ErlByte((byte)o)); } else if (o is short) { return(new ErlLong((short)o)); } else if (o is ushort) { return(new ErlLong((ushort)o)); } else if (o is decimal) { return(new ErlDouble((double)(decimal)o)); } else if (o is DateTime) { var ts = ((DateTime)o).ToMicrosecondsSinceUnixEpochStart(); var s = ts / 1000000; var us = ts - (s * 1000000); return((IErlObject) new ErlTuple(s / 1000000, s % 1000000, us)); } else if (o is byte[]) { return(new ErlBinary((byte[])o, true)); } // TODO: Add support for IDictionary else if (o is IEnumerable) { var list = new ErlList(); foreach (var item in (IEnumerable)o) { list.Add(item.ToErlObject()); } return(list); } else { throw new ErlException(StringConsts.ERL_CANNOT_CONVERT_TYPES_ERROR, o.GetType().FullName, typeof(IErlObject).Name); } }
/// <summary> /// Converts ErlCRUD response to CLR CRUD rowset /// </summary> /// <remarks> /// An Example data packet is field defs as speced in schema: /// "tca_jaba": has two field in PK /// [ /// {tca_jaba, {1234, tav}, "User is cool", true}, /// {tca_jaba, {2344, zap}, "A bird wants to drink", false}, /// {tca_jaba, {8944, tav}, "Have you seen this?", false} /// ] /// /// "aaa": has one field in PK - notice no tuple in key /// [ /// {aaa, 1234, tav, "User is cool", true}, /// {aaa, 2344, zap, "A bird wants to drink", false}, /// {aaa, 8944, tav, "Have you seen this?", false} /// ] /// </remarks> public RowsetBase ErlCRUDResponseToRowset(string schemaName, ErlList erlData) { var crudSchema = GetCRUDSchemaForName(schemaName); var result = new Rowset(crudSchema); foreach(var elm in erlData) { var tuple = elm as ErlTuple; if (tuple==null) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"ErlCRUDResponseToRowset(list element is not tuple)"); var row = ErlTupleToRow(schemaName, tuple, crudSchema); result.Add( row ); } return result; }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, ErlAtom?remoteCookie = null) { return(RPC(node, mod, fun, args, -1, remoteCookie)); }
public void AsyncRPC(ErlAtom node, string mod, string fun, ErlList args, ErlPid ioServer) { AsyncRPC(node, new ErlAtom(mod), new ErlAtom(fun), args, (IErlObject)ioServer); }
public void AsyncRPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args) { AsyncRPC(node, mod, fun, args, (IErlObject)m_Node.GroupLeader.Self); }
public void AsyncRPC(ErlAtom node, string mod, string fun, ErlList args) { AsyncRPC(node, new ErlAtom(mod), new ErlAtom(fun), args); }
public IErlObject RPC(ErlAtom node, string mod, string fun, ErlList args, int timeout) { return(this.RPC(node, new ErlAtom(mod), new ErlAtom(fun), args, timeout)); }
public IErlObject RPC(ErlAtom node, string mod, string fun, ErlList args, int timeout) { return this.RPC(node, new ErlAtom(mod), new ErlAtom(fun), args, timeout); }
public void ErlListTest() { var l = new ErlList("test", 1, 1.1, true, (byte)255, 'x', new ErlAtom("a")); var r = new ErlList("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.ErlList, l.TypeOrder); IErlObject temp = null; Assert.IsFalse(l.Subst(ref temp, new ErlVarBind())); Assert.IsTrue(new ErlList(new ErlVar(X), true, 1).Subst(ref temp, new ErlVarBind { { X, new ErlLong(10) } })); Assert.AreEqual("[10,true,1]", temp.ToString()); Assert.AreEqual(1, l.Visit(0, (acc, o) => acc + (o is ErlAtom ? 1 : 0))); 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 ErlTuple(1, 1.0, "a").Equals(new ErlList(1, 1.0, "a"))); Assert.IsFalse(new ErlTuple(1, 1.0, "a") == new ErlList(1, 1.0, "a")); }
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()); } }
public IErlObject RPC(ErlAtom node, string mod, string fun, ErlList args) { return RPC(node, mod, fun, args, -1); }
/// <summary> /// Determine if two Erlang lists are equal /// </summary> public bool Equals(ErlList o) { return m_Items.SequenceEqual(o.m_Items); }
/// <summary> /// Substitute variables in the args[0] string /// </summary> public static string Format(ErlList args) { return(Internal.Parser.Format(args)); }
public ErlList(ErlList list) : base((ErlTupleBase)list) { }
/// <summary> /// Write Erlang list to stream /// </summary> public void WriteList(ErlList list) { int n = list.Count; if (n == 0) WriteNil(); else { var isStr = n < 256 && list.All(i => { if (i.TypeOrder != ErlTypeOrder.ErlLong && i.TypeOrder != ErlTypeOrder.ErlByte) return false; var k = i.ValueAsInt; return k >= 0 && k <= 255; }); WriteListHead(list.Count, isStr); if (isStr) list.ForEach(t => Write1(t.ValueAsLong)); else { list.ForEach(t => Write(t)); WriteNil(); } } }
/// <summary> /// Determine if two Erlang lists are equal /// </summary> public bool Equals(ErlList o) { return(m_Items.SequenceEqual(o.m_Items)); }
public IErlObject RPC(ErlAtom node, string mod, string fun, ErlList args, int timeout, ErlAtom? remoteCookie = null) { return this.RPC(node, new ErlAtom(mod), new ErlAtom(fun), args, timeout, remoteCookie); }
public void AsyncRPC(ErlAtom node, string mod, string fun, ErlList args, ErlAtom? remoteCookie = null) { AsyncRPC(node, new ErlAtom(mod), new ErlAtom(fun), args, remoteCookie); }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, int timeout) { var mbox = m_Mailboxes.Create(true); try { var res = mbox.RPC(node, mod, fun, args, timeout); return res; } finally { m_Mailboxes.Unregister(mbox); } }
public void AsyncRPC(ErlAtom node, string mod, string fun, ErlList args, ErlPid ioServer, ErlAtom? remoteCookie = null) { AsyncRPC(node, new ErlAtom(mod), new ErlAtom(fun), args, (IErlObject)ioServer, remoteCookie); }
//used for subscription public int ExecuteWithoutFetch(ICRUDQueryExecutionContext context, Query query) { var store = ((ErlCRUDQueryExecutionContext)context).DataStore; var mbox = ((ErlCRUDQueryExecutionContext)context).ErlMailBox; var ts = ((ErlCRUDQueryExecutionContext)context).SubscriptionTimestamp; if (!ts.HasValue) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_TMSTAMP_CTX_ABSENT_ERROR); var parsed = prepareQuery(m_Source); var reqID = m_Store.NextRequestID; var bind = new ErlVarBind(); var wass = false; var wast = false; foreach(var erlVar in parsed.ArgVars) { var name = erlVar.Name.Value; if (erlVar.Name==ATOM_Subscriber && erlVar.ValueType==ErlTypeOrder.ErlPid) { bind.Add(ATOM_Subscriber, mbox.Self); wass = true; continue; } if (erlVar.Name==ATOM_Timestamp && erlVar.ValueType==ErlTypeOrder.ErlLong) { bind.Add(ATOM_Timestamp, new ErlLong(ts.Value.Microseconds)); wast = true; continue; } var clrPar = query[name]; if (clrPar==null) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_PARAM_NOT_FOUND_ERROR.Args(parsed.Source, name)); bind.Add(erlVar, clrPar.Value); } if (!wass) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_SUBSCR_NOT_FOUND_ERROR); if (!wast) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_TMSTAMP_NOT_FOUND_ERROR); var request = parsed.ArgTerm.Subst(bind); var args = new ErlList { reqID.ToErlObject(), parsed.Module, parsed.Function, request }; var rawResp = store.ExecuteRPC(ErlDataStore.NFX_CRUD_MOD, ErlDataStore.NFX_SUBSCRIBE_FUN, args, null); var response = rawResp as ErlTuple; // {ReqID, {ok, SchemaID, [{row},{row}...]}} // {ReqID, {ReqID::int(), {error, Code::int(), Msg}}} if (response==null) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESP_PROTOCOL_ERROR+"QryHndlr.Response==null"); bind = response.Match(EXECUTE_SUBSCRIBE_OK_PATTERN); checkForError(EXECUTE_SUBSCRIBE_ERROR_PATTERN, response, bind, reqID); //{ReqID::int(), ok} return 0; }
public void RPCcast(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, IErlObject ioServer) { if (node.Equals(m_Node.NodeName)) throw new ErlException(StringConsts.ERL_CONN_CANT_RPC_TO_LOCAL_NODE_ERROR); else { var msg = Internal.ErlRpcServer.EncodeRPCcast(m_Self, mod, fun, args, ioServer); var conn = m_Node.Connection(node); if (conn == null) throw new ErlConnectionException( node, StringConsts.ERL_CONN_CANT_CONNECT_TO_NODE_ERROR.Args(node)); conn.Send(m_Self, ConstAtoms.Rex, msg); } }
public void AsyncRPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args, ErlAtom?remoteCookie = null) { AsyncRPC(node, mod, fun, args, (IErlObject)m_Node.GroupLeader.Self, remoteCookie); }
public IErlObject RPC(ErlAtom node, ErlAtom mod, ErlAtom fun, ErlList args) { return RPC(node, mod, fun, args, -1); }
/// <summary> /// Write Erlang list to stream /// </summary> public void WriteList(ErlList list) { int n = list.Count; if (n > 0) { WriteListHead(list.Count); foreach (var t in list) Write(t); } WriteNil(); }
public IErlObject RPC(ErlAtom node, string mod, string fun, ErlList args, int timeout, ErlAtom?remoteCookie = null) { return(this.RPC(node, new ErlAtom(mod), new ErlAtom(fun), args, timeout, remoteCookie)); }
public RowsetBase Execute(ICRUDQueryExecutionContext context, Query query, bool oneRow = false) { var store = ((ErlCRUDQueryExecutionContext)context).DataStore; var mbox = ((ErlCRUDQueryExecutionContext)context).ErlMailBox; var parsed = prepareQuery(m_Source); var reqID = m_Store.NextRequestID; var bind = new ErlVarBind(); foreach(var erlVar in parsed.ArgVars) { var name = erlVar.Name.Value; var clrPar = query[name]; if (clrPar==null) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_PARAM_NOT_FOUND_ERROR.Args(parsed.Source, name)); bind.Add(erlVar, clrPar.Value); } var request = parsed.ArgTerm.Subst(bind); var args = new ErlList { reqID.ToErlObject(), parsed.Module, parsed.Function, request }; var rawResp = store.ExecuteRPC(ErlDataStore.NFX_CRUD_MOD, ErlDataStore.NFX_RPC_FUN, args, mbox); var response = rawResp as ErlTuple; // {ReqID, {ok, SchemaID, [{row},{row}...]}} // {ReqID, {error, Reason}} if (response==null) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"QryHndlr.Response timeout"); bind = response.Match(EXECUTE_OK_PATTERN); if (bind==null) { bind = response.Match(EXECUTE_ERROR_PATTERN); if (bind==null || bind[ATOM_ReqID].ValueAsLong != reqID) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"QryHndlr.Response wrong error"); throw new ErlDataAccessException("Remote error code {0}. Message: '{1}'".Args(bind[ATOM_Code], bind[ATOM_Msg])); } if (bind[ATOM_ReqID].ValueAsLong != reqID) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"QryHndlr.Response.ReqID mismatch"); //{ReqID::int(), {ok, SchemaID::atom(), Rows::list()}} var schema = bind[ATOM_SchemaID].ValueAsString; var rows = bind[ATOM_Rows] as ErlList; //{ok, "tca_jaba", //[ // {tca_jaba, 1234, tav, "User is cool", true}, // {tca_jaba, 2344, zap, "Zaplya xochet pit", false}, // {tca_jaba, 8944, tav, "User is not good", false} //]}; return m_Store.Map.ErlCRUDResponseToRowset(schema, rows, query.ResultRowType); }
public void AsyncRPC(ErlAtom node, string mod, string fun, ErlList args, ErlAtom?remoteCookie = null) { AsyncRPC(node, new ErlAtom(mod), new ErlAtom(fun), args, remoteCookie); }
public void AsyncRPC(ErlAtom node, string mod, string fun, ErlList args, ErlPid ioServer, ErlAtom?remoteCookie = null) { AsyncRPC(node, new ErlAtom(mod), new ErlAtom(fun), args, (IErlObject)ioServer, remoteCookie); }
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()); } }
//used for subscription public int ExecuteWithoutFetch(ICRUDQueryExecutionContext context, Query query) { var store = ((ErlCRUDQueryExecutionContext)context).DataStore; var mbox = ((ErlCRUDQueryExecutionContext)context).ErlMailBox; var ts = ((ErlCRUDQueryExecutionContext)context).SubscriptionTimestamp; if (!ts.HasValue) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_TIMESTAMP_CTX_ABSENT_ERROR); var parsed = prepareQuery(m_Source); var reqID = m_Store.NextRequestID; var bind = new ErlVarBind(); var wass = false; var wast = false; foreach(var erlVar in parsed.ArgVars) { var name = erlVar.Name.Value; if (erlVar.Name==ATOM_Subscriber && erlVar.ValueType==ErlTypeOrder.ErlPid) { bind.Add(ATOM_Subscriber, mbox.Self); wass = true; continue; } if (erlVar.Name==ATOM_Timestamp && erlVar.ValueType==ErlTypeOrder.ErlLong) { bind.Add(ATOM_Timestamp, new ErlLong(ts.Value.Microseconds)); wast = true; continue; } var clrPar = query[name]; if (clrPar==null) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_PARAM_NOT_FOUND_ERROR.Args(parsed.Source, name)); bind.Add(erlVar, clrPar.Value); } if (!wass) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_SUBSCRIBER_NOT_FOUND_ERROR); if (!wast) throw new ErlDataAccessException(StringConsts.ERL_DS_QUERY_TIMESTAMP_NOT_FOUND_ERROR); var request = parsed.ArgTerm.Subst(bind); var args = new ErlList { reqID.ToErlObject(), parsed.Module, parsed.Function, request }; var rawResp = store.ExecuteRPC(ErlDataStore.NFX_CRUD_MOD, ErlDataStore.NFX_SUBSCRIBE_FUN, args, null); var response = rawResp as ErlTuple; // {ReqID, {ok, SchemaID, [{row},{row}...]}} // {ReqID, {ReqID::int(), {error, Code::int(), Msg}}} if (response==null) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"QryHndlr.Response==null"); bind = response.Match(EXECUTE_SUBSCRIBE_OK_PATTERN); if (bind==null) { bind = response.Match(EXECUTE_SUBSCRIBE_ERROR_PATTERN); if (bind==null || bind[ATOM_ReqID].ValueAsLong != reqID) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"QryHndlr.Response wrong error"); var ecode = bind[ATOM_Code].ValueAsInt; var emsg = bind[ATOM_Msg].ToString(); Exception error = new ErlDataAccessException("Remote error code {0}. Message: '{1}'".Args(ecode, emsg)); if (ecode==INVALID_SUBSCRIPTION_REQUEST_EXCEPTION) error = new NFX.DataAccess.CRUD.Subscriptions.InvalidSubscriptionRequestException(emsg, error); throw error; } if (bind[ATOM_ReqID].ValueAsLong != reqID) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESPONSE_PROTOCOL_ERROR+"QryHndlr.Response.ReqID mismatch"); //{ReqID::int(), ok} return 0; }
/// <summary> /// Converts ErlCRUD response to CLR CRUD rowset /// </summary> /// <remarks> /// An Example data packet is field defs as speced in schema: /// "tca_jaba": has two field in PK /// [ /// {tca_jaba, {1234, tav}, "User is cool", true}, /// {tca_jaba, {2344, zap}, "A bird wants to drink", false}, /// {tca_jaba, {8944, tav}, "Have you seen this?", false} /// ] /// /// "aaa": has one field in PK - notice no tuple in key /// [ /// {aaa, 1234, tav, "User is cool", true}, /// {aaa, 2344, zap, "A bird wants to drink", false}, /// {aaa, 8944, tav, "Have you seen this?", false} /// ] /// </remarks> public RowsetBase ErlCRUDResponseToRowset(string schemaName, ErlList erlData, Type tRow = null) { var crudSchema = GetCRUDSchemaForName(schemaName); var tSchema = tRow == null ? crudSchema : Schema.GetForTypedRow(tRow); var result = new Rowset(tSchema); foreach(var elm in erlData) { var tuple = elm as ErlTuple; if (tuple==null) throw new ErlDataAccessException(StringConsts.ERL_DS_INVALID_RESP_PROTOCOL_ERROR+"ErlCRUDResponseToRowset(list element is not tuple)"); var row = ErlTupleToRow(schemaName, tuple, crudSchema); if (tRow != null) { var trow = Row.MakeRow(tSchema, tRow); row.CopyFields(trow); row = trow; } result.Add( row ); } return result; }