private List <Tuple> Take(TupleSchema tupleSchema) { List <Tuple> tuples = TupleSpace.Take(tupleSchema); if (tuples.Count > 0) { List <Tuple> tuplesWriteBack = new List <Tuple>(tuples); tuplesWriteBack.Remove(tuplesWriteBack.First()); Write(tuplesWriteBack); Log("Took (first tuple): " + tuples.First()); } return(tuples); }
public Message ProcessRequest(Request request) { var tupleSchema = new TupleSchema(request.Tuple); if (request.GetType() == typeof(WriteRequest)) { TupleSpace.Write(request.Tuple); // notify a blocked thread that tried to read/take this tuple before Request readOrTakeRequest = GetAndRemoveSinglePendingRequest(request.Tuple); if (readOrTakeRequest != null) { PulseMessage(readOrTakeRequest); } // Send back an Ack of the write return(new Ack(EndpointURL, request)); } if (request.GetType() == typeof(ReadRequest)) { List <Tuple> tuples = TupleSpace.Read(tupleSchema); while (!tuples.Any()) { lock (PendingRequestList){ PendingRequestList.Add(request); } WaitMessage(request, View.Nodes); tuples = TupleSpace.Read(tupleSchema); } Response response = new Response(request, tuples, EndpointURL); return(response); } if (request.GetType() == typeof(TakeRequest)) { tupleSchema = new TupleSchema(request.Tuple); List <Tuple> resultTuples = TupleSpace.Take(tupleSchema); Response response = null; while (!resultTuples.Any()) { lock (PendingRequestList){ PendingRequestList.Add(request); } WaitMessage(request, View.Nodes); resultTuples = TupleSpace.Take(tupleSchema); } // "lock" the tuples taken from the tuple space LockedTuples.TryAdd(request.SeqNum, resultTuples); response = new Response(request, resultTuples, EndpointURL); SendMessageToRemoteURL(request.SrcRemoteURL, response); return(response); } throw new NotImplementedException(); }
private void Receiver(object state) { var pipeline = new Pipeline(); pipeline.Push(new NboFrameLengthSink(2) { IncludeHeaderLength = false, MaxFrameLength = 1024 }); pipeline.Push( new MessageFormatterSink(new Iso8583MessageFormatter((@"..\Formatters\Iso8583Bin1987.xml")))); var ts = new TupleSpace <ReceiveDescriptor>(); var server = new TcpServerChannel(new Pipeline(), new ClonePipelineFactory(pipeline), ts, new FieldsMessagesIdentifier(new[] { 11, 41 })) { Port = 8583, LocalInterface = (string)state, Name = "ISO8583Server" }; server.StartListening(); while (!_stop) { ReceiveDescriptor rcvDesc = ts.Take(null, 100); if (rcvDesc == null) { continue; } _requestsCnt++; var message = rcvDesc.ReceivedMessage as Iso8583Message; if (message == null) { continue; } Iso8583Message response; if (message.IsAuthorization()) { Console.WriteLine("go to handle sale msg"); Sale auth = new Sale(message); response = auth.GetMessage(); } else if (message.IsNetworkManagement()) { Console.WriteLine("go to handle logon msg"); Logon logon = new Logon(message); logon.BuildResponse(); response = logon.GetMessage(); Console.WriteLine(response.ToString()); } else { response = null; } //message.SetResponseMessageTypeIdentifier(); //message.Fields.Add(Field39ResponseCode, "00"); var addr = rcvDesc.ChannelAddress as ReferenceChannelAddress; if (addr == null) { continue; } var child = addr.Channel as ISenderChannel; if (child != null) { child.Send(response); } } // Stop listening and shutdown the connection with the sender. server.StopListening(); }
public void Take1() { var result = _tupleSpace.Take(_tuple1Schema); Assert.IsEmpty(result); }