Exemple #1
0
 public Init(DistributedObject distributedObject, string[] conditionals)
 {
     DistributedObject = distributedObject;
     Conditionals      = conditionals;
 }
Exemple #2
0
        private void HandleInternalMessage(InternalMessages.IInternalMessage message)
        {
            switch (message)
            {
            case InternalMessages.Init init:
                _obj = init.DistributedObject;
                if (bool.Parse(Environment.GetEnvironmentVariable("IS_AKKA_SEED")))
                {
                    if (_token == null)
                    {
                        _token = new Token(new Dictionary <Address, int>(), new Queue <Address>());
                    }

                    _token.Conditionals = new Dictionary <string, Queue <Address> >();
                    foreach (var cond in init.Conditionals ?? Enumerable.Empty <string>())
                    {
                        _token.Conditionals.Add(cond, new Queue <Address>());
                    }
                }
                break;

            case InternalMessages.AskWait wait:
                //_log.Info($"Now I'm waiting [{_address}] in [{wait.Conditional}]");

                _token.Conditionals[wait.Conditional].Enqueue(_address);
                _askWaitResponseActors.Add(wait.Conditional, Sender);
                Self.Tell(new InternalMessages.AskUnlock());
                break;

            case InternalMessages.AskPulse pulse:

                var q = _token.Conditionals[pulse.Conditional];
                if (q.Count > 0)
                {
                    //_log.Info($"Pulsing [{pulse.Conditional}]");
                    ActorForAddress(q.Dequeue()).Tell(new ExternalMessages.Pulse(pulse.Conditional));
                }
                Sender.Tell(Empty.Default, Self);
                break;

            case InternalMessages.AskPulseAll pulse:
                var qu = _token.Conditionals[pulse.Conditional];
                if (qu.Count > 0)
                {
                    foreach (var queuer in qu)
                    {
                        ActorForAddress(queuer).Tell(new ExternalMessages.Pulse(pulse.Conditional));
                    }
                }
                Sender.Tell(Empty.Default, Self);
                break;

            case InternalMessages.AskLock l:
                if (_token != null)
                {
                    _inCriticalSection = true;
                    //_log.Info("Entering cs");
                    //if (l == null)
                    //{
                    //  Sender.Tell(Empty.Default, Self);
                    //} else
                    //{
                    //  _askWaitResponseActors[l.Conditional].Tell(Empty.Default, Self);
                    //}
                    Sender.Tell(Empty.Default, Self);
                }
                else
                {
                    var sn = ++_requestNumber[_address];

                    foreach (var node in _nodes)
                    {
                        if (_address == node.Address)
                        {
                            continue;
                        }

                        //_log.Info("Requesting cs with sn: " + sn);
                        //_log.Info(_requestNumber.Select(pair => $"[{pair.Key}, {pair.Value}]").Aggregate((a, b) => a + " " + b));
                        ActorForAddress(node.Address).Tell(new ExternalMessages.RequestCS(sn));
                    }

                    //if (l == null)
                    //{
                    //  _askLockResponseActor = Sender;
                    //}
                    _askLockResponseActor = Sender;
                }
                break;

            case InternalMessages.AskUnlock _:
                _token.LastRequestNumber[_address] = _requestNumber[_address];
                //_log.Info("Unlocking");
                //_log.Info(_requestNumber.Select(pair => $"[{pair.Value}]").Aggregate((a, b) => a + " " + b));
                //_log.Info(_token.LastRequestNumber.Select(pair => $"[{pair.Value}]").Aggregate((a, b) => a + " " + b));
                foreach (var reqNo in _requestNumber)
                {
                    if (!_token.Queue.Contains(reqNo.Key) && reqNo.Value > _token.LastRequestNumber[reqNo.Key])
                    {
                        //_log.Info("Adding to queue");
                        _token.Queue.Enqueue(reqNo.Key);
                    }
                }
                if (_token.Queue.Count > 0)
                {
                    //_log.Info("Sending token");
                    ActorForAddress(_token.Queue.Dequeue())
                    .Tell(_token.ToTokenMessage());
                    _token = null;
                }
                _inCriticalSection = false;
                //_log.Info("Leaving cs");
                Sender.Tell(Empty.Default);
                break;

            case InternalMessages.AskUpdateObject update:
                var tasks = new List <Task>();
                foreach (var node in _nodes)
                {
                    if (_address == node.Address)
                    {
                        continue;
                    }

                    tasks.Add(ActorForAddress(node.Address).Ask(new ExternalMessages.UpdateData(update.JsonData)));
                }
                Task.WhenAll(tasks).Wait();
                Sender.Tell(Empty.Default);
                break;

            default:
                Unhandled(message);
                break;
            }
        }