Ejemplo n.º 1
0
        public void RequestToken()
        {
            TokenMutex.WaitOne();
            if (CurrentToken == null)
            {
                Console.WriteLine("Requesting Token");
                RequestNumberList[RemoteServerGroup.Instance.NodeAddress]++;
                while (CurrentToken == null)
                {
                    var rsg = RemoteServerGroup.Instance;
                    var msg = new Message
                    {
                        Data          = RequestNumberList[RemoteServerGroup.Instance.NodeAddress].ToString(),
                        MonitorId     = Id,
                        MsgType       = MessageType.TokenRequest,
                        SendingServer = rsg.NodeAddress
                    };

                    foreach (var remoteAddress in rsg.RemoteAddressesList)
                    {
                        var remoteDispatch = new RemoteDispatch(msg, remoteAddress);
                        rsg.SendQueue.Add(remoteDispatch);
                    }

                    TokenMutex.ReleaseMutex();
                    CurrentToken = ReceivedToken.Take();
                    TokenMutex.WaitOne();
                }
            }
        }
Ejemplo n.º 2
0
        public void NotifyAll()
        {
            ConditionMutex.WaitOne();
            var msg = new Message
            {
                MsgType       = MessageType.NotifyAllResponse,
                Data          = Id,
                SendingServer = RemoteServerGroup.Instance.NodeAddress
            };

            while (RemoteNodesWaitingQueue.Count > 0)
            {
                if (RemoteNodesWaitingQueue[0] == RemoteServerGroup.Instance.NodeAddress)
                {
                    foreach (var queue in ConditionalQueues)
                    {
                        queue.Add(RemoteServerGroup.Instance.NodeAddress);
                    }

                    ConditionalQueues.Clear();
                }
                else
                {
                    var remoteAddress  = RemoteNodesWaitingQueue[0];
                    var remoteDispatch = new RemoteDispatch(msg, remoteAddress);
                    RemoteServerGroup.Instance.SendQueue.Add(remoteDispatch);
                }
                RemoteNodesWaitingQueue.RemoveAt(0);
            }
            ConditionMutex.ReleaseMutex();
        }
Ejemplo n.º 3
0
        private void InformSenderAboutFinish()
        {
            var msg = new Message
            {
                MsgType       = MessageType.EndingProgram,
                SendingServer = this.NodeAddress
            };
            var messageWrapper = new RemoteDispatch(msg, null);

            SendQueue.Add(messageWrapper);
        }
Ejemplo n.º 4
0
        private void BarrierWithTimeout()
        {
            var msg = new Message
            {
                MsgType       = MessageType.BarrierResponse,
                SendingServer = NodeAddress,
            };

            foreach (var remoteAddress in RemoteAddressesList)
            {
                var respond = new RemoteDispatch(msg, remoteAddress);
                SendQueue.Add(respond);
            }

            Console.WriteLine("Waiting on barrier");
            if (!SyncBarrier.Wait(TimeSpan.FromSeconds(Timeout)))
            {
                throw new Exception("Barrier timeout");
            }
        }
Ejemplo n.º 5
0
        public void Wait()
        {
            ConditionMutex.WaitOne();
            var waitForSignal = new BlockingCollection <string>();

            ConditionalQueues.Add(waitForSignal);
            var remoteServerGroup = RemoteServerGroup.Instance;

            if (!RemoteNodesWaitingQueue.Contains(remoteServerGroup.NodeAddress))
            {
                RemoteNodesWaitingQueue.Add(remoteServerGroup.NodeAddress);
            }
            var msg = new Message
            {
                MsgType       = MessageType.WaitRequest,
                Data          = Id,
                SendingServer = remoteServerGroup.NodeAddress,
            };

            foreach (var remoteAddress in remoteServerGroup.RemoteAddressesList)
            {
                var remoteDispatch = new RemoteDispatch(msg, remoteAddress);
                remoteServerGroup.SendQueue.Add(remoteDispatch);
            }

            for (var i = AssignedMonitors.Count - 1; i >= 0; i--)
            {
                AssignedMonitors[i].ReleaseLock();
            }
            ConditionMutex.ReleaseMutex();
            Console.WriteLine("Waiting to be notified");
            var notifiedBy = waitForSignal.Take();

            Console.WriteLine($"Notified by {notifiedBy}");
            for (var i = 0; i < AssignedMonitors.Count; i++)
            {
                AssignedMonitors[i].AcquireLock();
            }
        }
Ejemplo n.º 6
0
        public void SendToken(string destination)
        {
            Console.WriteLine($"Sending Token to: {destination}");
            JsonSerializerSettings settings = new JsonSerializerSettings {
                TypeNameHandling = TypeNameHandling.All
            };

            CurrentToken.ReceivedData = JsonConvert.SerializeObject(this, settings);
            var tokenJson = JsonConvert.SerializeObject(CurrentToken, settings);

            CurrentToken = null;

            var msg = new Message
            {
                Data          = tokenJson,
                MonitorId     = Id,
                MsgType       = MessageType.TokenRespond,
                SendingServer = RemoteServerGroup.Instance.NodeAddress
            };
            var remoteDispatch = new RemoteDispatch(msg, destination);

            RemoteServerGroup.Instance.SendQueue.Add(remoteDispatch);
        }