/// <summary> /// test if a request or reply with the GUID exists /// </summary> /// <param name="op">commands whether it is for a REQUEST or a REPLY</param> /// <param name="id">the GUID für the Request/Reply</param> /// <returns> /// true if it exists and false otherwise and if 'op' is not one /// of the aforementioned /// </returns> public bool ExistsMessage(TitanicOperation op, Guid id) { if (op == TitanicOperation.Close) { return(false); } var filename = op == TitanicOperation.Request ? GetRequestFileName(id) : GetReplyFileName(id); return(File.Exists(filename)); }
/// <summary> /// save a NetMQ message under a GUID /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to save</param> /// <param name="message">the message to save</param> /// <exception cref="OverflowException">max. number of elements exceeded, <see cref="F:System.Int32.MaxValue" />.</exception> public bool SaveMessage(TitanicOperation op, Guid id, NetMQMessage message) { var entry = new RequestEntry { RequestId = id, Request = message, State = GetStateFromOperation(op) }; m_titanicQueue.AddOrUpdate(id, entry, (i, oldValue) => entry); return(true); }
/// <summary> /// save a NetMQ message with a GUID asynchronously /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to save</param> /// <param name="message">the message to save</param> public Task <bool> SaveMessageAsync(TitanicOperation op, Guid id, [NotNull] NetMQMessage message) { var tcs = new TaskCompletionSource <bool> (); try { tcs.SetResult(SaveMessage(op, id, message)); } catch (Exception ex) { tcs.SetException(ex); } return(tcs.Task); }
/// <summary> /// read a NetMQ message identified by a GUID asynchronously /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to read</param> public Task <NetMQMessage> GetMessageAsync(TitanicOperation op, Guid id) { var tcs = new TaskCompletionSource <NetMQMessage> (); try { tcs.SetResult(GetMessage(op, id)); } catch (Exception ex) { tcs.SetException(ex); } return(tcs.Task); }
/// <summary> /// save a NetMQ message under a GUID /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to save</param> /// <param name="message">the message to save</param> public bool SaveMessage(TitanicOperation op, Guid id, [NotNull] NetMQMessage message) { var filename = op == TitanicOperation.Request ? GetRequestFileName(id) : GetReplyFileName(id); using (var file = File.Open(filename, FileMode.OpenOrCreate)) using (var w = new StreamWriter(file)) { // save each frame as string -> upon reading it back use Read (string) for (var i = 0; i < message.FrameCount; i++) { w.WriteLine(message[i].ConvertToString()); } } return(true); // just needed for the async method(!) }
/// <summary> /// get a NetMQ message identified by a GUID from the infrastructure /// </summary> /// <param name="id">the guid of the message to read</param> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> public NetMQMessage GetMessage(TitanicOperation op, Guid id) { var filename = op == TitanicOperation.Request ? GetRequestFileName(id) : GetReplyFileName(id); var message = new NetMQMessage(); using (var file = File.Open(filename, FileMode.Open)) using (var r = new StreamReader(file)) { string s; while ((s = r.ReadLine()) != null) { var b = Encoding.UTF8.GetBytes(s); // first read -> first frame => append each read frame(!) message.Append(b); } } return(message); }
public void Exists_ExistingReply_ShouldReturnTrue() { var sut = new TitanicFileIO(Path.GetTempPath()); const TitanicOperation op = TitanicOperation.Reply; var message = new NetMQMessage(); message.Push("Hello World"); message.Push("echo"); var id = Guid.NewGuid(); sut.SaveMessage(op, id, message); sut.ExistsMessage(op, id).Should().BeTrue("because it has been created."); var expectedDir = sut.TitanicDirectory; var expectedFile = Path.Combine(expectedDir, id + _reply_ending); File.Delete(expectedFile); }
/// <summary> /// save a NetMQ message with a GUID asynchronously /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to save</param> /// <param name="message">the message to save</param> public Task<bool> SaveMessageAsync(TitanicOperation op, Guid id, NetMQMessage message) { var tcs = new TaskCompletionSource<bool> (); try { tcs.SetResult (SaveMessage (op, id, message)); } catch (Exception ex) { tcs.SetException (ex); } return tcs.Task; }
private byte GetStateFromOperation(TitanicOperation op) { return op == TitanicOperation.Request ? RequestEntry.Is_Pending : RequestEntry.Is_Processed; }
/// <summary> /// read a NetMQ message identified by a GUID asynchronously /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to read</param> public Task<NetMQMessage> GetMessageAsync(TitanicOperation op, Guid id) { var tcs = new TaskCompletionSource<NetMQMessage> (); try { tcs.SetResult (GetMessage (op, id)); } catch (Exception ex) { tcs.SetException (ex); } return tcs.Task; }
/// <summary> /// save a NetMQ message under a GUID /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to save</param> /// <param name="message">the message to save</param> /// <exception cref="OverflowException">max. number of elements exceeded, <see cref="F:System.Int32.MaxValue" />.</exception> public bool SaveMessage(TitanicOperation op, Guid id, NetMQMessage message) { var entry = new RequestEntry { RequestId = id, Request = message, State = GetStateFromOperation (op) }; m_titanicQueue.AddOrUpdate (id, entry, (i, oldValue) => entry); return true; }
/// <summary> /// test if a request or reply with the GUID exists /// </summary> /// <param name="op">commands whether it is for a REQUEST or a REPLY</param> /// <param name="id">the GUID für the Request/Reply</param> /// <returns> /// true if it exists and false otherwise and if 'op' is not one /// of the aforementioned /// </returns> public bool ExistsMessage(TitanicOperation op, Guid id) { return op != TitanicOperation.Close && m_titanicQueue.Any (e => e.Value.RequestId == id && e.Value.State == GetStateFromOperation (op)); }
/// <summary> /// get a NetMQ message identified by a GUID from the infrastructure /// </summary> /// <param name="id">the guid of the message to read, must not be empty</param> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> public NetMQMessage GetMessage(TitanicOperation op, Guid id) { RequestEntry entry; return m_titanicQueue.TryGetValue (id, out entry) ? entry.Request : new NetMQMessage (); }
/// <summary> /// test if a request or reply with the GUID exists /// </summary> /// <param name="op">commands whether it is for a REQUEST or a REPLY</param> /// <param name="id">the GUID für the Request/Reply</param> /// <returns> /// true if it exists and false otherwise and if 'op' is not one /// of the aforementioned /// </returns> public bool ExistsMessage(TitanicOperation op, Guid id) { return(op != TitanicOperation.Close && m_titanicQueue.Any(e => e.Value.RequestId == id && e.Value.State == GetStateFromOperation(op))); }
/// <summary> /// get a NetMQ message identified by a GUID from the infrastructure /// </summary> /// <param name="id">the guid of the message to read, must not be empty</param> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> public NetMQMessage GetMessage(TitanicOperation op, Guid id) { RequestEntry entry; return(m_titanicQueue.TryGetValue(id, out entry) ? entry.Request : new NetMQMessage()); }
private static byte GetStateFromOperation(TitanicOperation op) { return(op == TitanicOperation.Request ? RequestEntry.Is_Pending : RequestEntry.Is_Processed); }
private Tuple <NetMQMessage, TitanicReturnCode> ServiceCall(IMDPClient session, TitanicOperation op, NetMQMessage message) { // message can be: // REQUEST: // in goes -> [titanic operation][service requested][request] // returns -> [return code][Guid] // REPLY // in goes -> [titanic operation][request id] // returns -> [return code][reply] // CLOSE // in goes -> [titanic operation][request id] var reply = session.Send(op.ToString(), message); Log(string.Format("received message: {0}", reply)); if (ReferenceEquals(reply, null) || reply.IsEmpty) { return(null); // went wrong - why? I don't care! } // we got a reply -> [return code][data] var rc = reply.Pop().ConvertToString(); // [return code] <- [data] or [service name] if 'Unknown' var status = (TitanicReturnCode)Enum.Parse(typeof(TitanicReturnCode), rc); switch (status) { case TitanicReturnCode.Ok: return(new Tuple <NetMQMessage, TitanicReturnCode> (reply, TitanicReturnCode.Ok)); case TitanicReturnCode.Pending: return(new Tuple <NetMQMessage, TitanicReturnCode> (reply, TitanicReturnCode.Pending)); case TitanicReturnCode.Unknown: Log("ERROR: Service unknown!"); return(new Tuple <NetMQMessage, TitanicReturnCode> (reply, TitanicReturnCode.Unknown)); default: Log("ERROR: FATAL ERROR ABANDONING!"); return(new Tuple <NetMQMessage, TitanicReturnCode> (null, TitanicReturnCode.Failure)); } }
/// <summary> /// get a NetMQ message identified by a GUID from the infrastructure /// </summary> /// <param name="id">the guid of the message to read</param> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> public NetMQMessage GetMessage (TitanicOperation op, Guid id) { var filename = op == TitanicOperation.Request ? GetRequestFileName (id) : GetReplyFileName (id); var message = new NetMQMessage (); using (var file = File.Open (filename, FileMode.Open)) using (var r = new StreamReader (file)) { string s; while ((s = r.ReadLine ()) != null) { var b = Encoding.UTF8.GetBytes (s); // first read -> first frame => append each read frame(!) message.Append (b); } } return message; }
/// <summary> /// test if a request or reply with the GUID exists /// </summary> /// <param name="op">commands whether it is for a REQUEST or a REPLY</param> /// <param name="id">the GUID für the Request/Reply</param> /// <returns> /// true if it exists and false otherwise and if 'op' is not one /// of the aforementioned /// </returns> public bool ExistsMessage (TitanicOperation op, Guid id) { if (op == TitanicOperation.Close) return false; var filename = op == TitanicOperation.Request ? GetRequestFileName (id) : GetReplyFileName (id); return File.Exists (filename); }
/// <summary> /// save a NetMQ message under a GUID /// </summary> /// <param name="op">defines whether it is a REQUEST or REPLY message</param> /// <param name="id">the guid of the message to save</param> /// <param name="message">the message to save</param> public bool SaveMessage (TitanicOperation op, Guid id, NetMQMessage message) { var filename = op == TitanicOperation.Request ? GetRequestFileName (id) : GetReplyFileName (id); using (var file = File.Open (filename, FileMode.OpenOrCreate)) using (var w = new StreamWriter (file)) { // save each frame as string -> upon reading it back use Read (string) for (var i = 0; i < message.FrameCount; i++) w.WriteLine (message[i].ConvertToString ()); } return true; // just needed for the async method(!) }