private void RemoveFromCache(bool localOnly, string id) { if (String.IsNullOrEmpty(id)) { throw new ArgumentException("The key cannot be null or empty", "id"); } ResultCache.Remove(id, localOnly); if (!localOnly) { Task.Run(() => { InterNodeCommunicationMessage msg = new InterNodeCommunicationMessage { Tag = id, IsLocalOnly = true, MessageType = InterNodeMessageType.RemoveFromCache }; // not sure this is valid as its non deterministic lock (KnownNodes) { foreach (var el in KnownNodes) { SendToNode(el, InterNodeCommunicationMessageSerialiser.Serialise(msg)); } } }); } }
private void RemoveFromQueue(bool localOnly, string id) { if (String.IsNullOrEmpty(id)) { throw new ArgumentException("The specified entry is not valid", "id"); } ProcessQueue.Remove(x => x.Tag == id); if (!localOnly) { Task.Run(() => { InterNodeCommunicationMessage msg = new InterNodeCommunicationMessage { Tag = id, IsLocalOnly = true, MessageType = InterNodeMessageType.RemoveFromQueue }; // not sure this is valid as its non deterministic lock (KnownNodes) { foreach (var el in KnownNodes) { SendToNode(el, InterNodeCommunicationMessageSerialiser.Serialise(msg)); } } }); } }
private void internode_OnDataReceived(object sender, SocketDataEventArgs e) { InterNodeCommunicationMessage msg = InterNodeCommunicationMessageSerialiser.Deserialise(e.Data); Console.WriteLine("INCM :: " + msg.MessageType.ToString()); switch (msg.MessageType) { case InterNodeMessageType.AddToCache: // typeof ClientResultMessage AddToCache(msg.IsLocalOnly, msg.Tag, msg.Data); break; case InterNodeMessageType.AddToQueue: // typeof ClientMessage AddToQueue(msg.IsLocalOnly, msg.Data); break; case InterNodeMessageType.RemoveFromCache: RemoveFromCache(msg.IsLocalOnly, msg.Tag); break; case InterNodeMessageType.RemoveFromQueue: RemoveFromQueue(msg.IsLocalOnly, msg.Tag); break; case InterNodeMessageType.NewNodeDiscovered: NewNodeDiscoveredOnNetwork(e.Source); break; case InterNodeMessageType.ChangeMessageStateInQueue: // typeof QueuedProcessState QueueMessageStateChanged(msg.Tag, msg.Data); break; case InterNodeMessageType.FullCacheUpdateSent: SendFullCacheUpdate(e.Source); break; case InterNodeMessageType.FullQueueUpdateSent: SendFullQueueUpdate(e.Source); break; case InterNodeMessageType.FullCacheUpdateReceived: UpdateCacheWithFullUpdate(msg.Data); GetDataFromNode(e.Source, InterNodeMessageType.FullQueueUpdateSent); break; case InterNodeMessageType.FullQueueUpdateReceived: UpdateQueueWithFullUpdate(msg.Data); break; case InterNodeMessageType.StartUp: GetDataFromNode(e.Source, InterNodeMessageType.FullCacheUpdateSent); break; default: Console.WriteLine("Internode message received. Default."); break; } }
public void OnNext(ClientResultMessage value) { Task.Run(() => { // Get the list of nodes List <IPAddress> nodes = ParentNode.KnownNodes; // if there are no nodes, there is no point serialising the message if (nodes != null) { // setup the message before sending var msg = new InterNodeCommunicationMessage { Data = ParentNode.ClientResultMessageSerialiser.Serialise(value), IsLocalOnly = true, MessageType = InterNodeMessageType.AddToCache }; byte[] data = ParentNode.InterNodeCommunicationMessageSerialiser.Serialise(msg); // need to send this to all the other known nodes foreach (var node in nodes) { ParentNode.SendToNode(node, data); } // be a good boy and clean up after ourselves nodes.Clear(); } }); }
private void internode_OnClientConnectCompleted(object sender, IPEndPoint e) { if (e != null) { Console.WriteLine("Client connected..."); InterNodeCommunicationMessage msg = new InterNodeCommunicationMessage { IsLocalOnly = true, MessageType = InterNodeMessageType.StartUp, Tag = e.Address.ToString() }; SendToNode(e.Address, InterNodeCommunicationMessageSerialiser.Serialise(msg)); } }
private void GetDataFromNode(IPAddress ip, InterNodeMessageType msgType) { if (ip == null) { throw new ArgumentException("ip cannot be null"); } if (msgType == InterNodeMessageType.FullQueueUpdateSent || msgType == InterNodeMessageType.FullCacheUpdateSent) { InterNodeCommunicationMessage msg = new InterNodeCommunicationMessage { MessageType = msgType, IsLocalOnly = true }; SendToNode(ip, InterNodeCommunicationMessageSerialiser.Serialise(msg)); } }
private void SendFullQueueUpdate(IPAddress ip) { if (ip == null) { throw new ArgumentException("The specified ip is not valid", "ip"); } IObservableQueue <ClientProcess> temp = ProcessQueue; var msg = new InterNodeCommunicationMessage { Data = PrioritisedQueueSerialiser.Serialise(temp), IsLocalOnly = true, MessageType = InterNodeMessageType.FullQueueUpdateReceived }; SendToNode(ip, InterNodeCommunicationMessageSerialiser.Serialise(msg)); }
public async void OnNext(ClientProcess value) { await Task.Run(() => { // Get the list of nodes List <IPAddress> nodes = ParentNode.KnownNodes; // if there are no nodes, there is no point serialising the message if (nodes != null && !value.LocalOnly) { // setup the message before sending InterNodeCommunicationMessage msg = new InterNodeCommunicationMessage { Data = ParentNode.ClientProcessSerialiser.Serialise(value), IsLocalOnly = true, MessageType = InterNodeMessageType.AddToQueue }; byte[] data = ParentNode.InterNodeCommunicationMessageSerialiser.Serialise(msg); // need to send this to all the other known nodes foreach (var node in nodes) { ParentNode.SendToNode(node, data); } // be a good boy and clean up after ourselves nodes.Clear(); } }); if (!value.LocalOnly) { // start the task ParentNode.ProcessQueue.ChangeState(value.Tag, QueuedProcessState.Running, false); Task <byte[]> task = ParentNode.NodeTask.Execute(value.Data); var result = await task; ParentNode.AddToCache(false, value.Tag, result); ClientResultMessage msg = new ClientResultMessage { Data = result, Key = value.Tag, Source = value.Source }; ParentNode.SendToNode(value.Source, ParentNode.DependancyInjector.Resolve <ISerialiser <ClientResultMessage, byte[]> >().Serialise(msg)); } }
private void SendFullCacheUpdate(IPAddress ip) { if (ip == null) { throw new ArgumentException("The specified IP address is not valid", "ip"); } if (!KnownNodes.Contains(ip)) { throw new ArgumentException("The ip address specified is not known.", "ip"); } // grab a copy of the cache CacheProvider <string, ClientResultMessage> temp = ResultCache; temp.ClearSubscribersNoNotify(); InterNodeCommunicationMessage msg = new InterNodeCommunicationMessage { Data = CacheSerialiser.Serialise(temp), IsLocalOnly = true, MessageType = InterNodeMessageType.FullCacheUpdateReceived }; SendToNode(ip, InterNodeCommunicationMessageSerialiser.Serialise(msg)); }