private void ProcessMessages()
        {
            lock (_lockSocket)
            {
                if (_stopRequested)
                {
                    return;
                }

                string message;
                if (!_actor.TryReceiveFrameString(TimeSpan.FromMilliseconds(10), Encoding.ASCII, out message))
                {
                    return;
                }
                if (message == TaskSchedulerBusCommands.BroadCast.ToString())
                {
                    // another node has let us know they are here
                    var fromHostAddress = _actor.ReceiveFrameString();
                    var msg             = fromHostAddress + " broadcasting";
                    _log.Debug(() => msg);

                    // send back a welcome message via the Bus publisher
                    var reply = _hostAddress + " received";
                    _actor.SendMoreFrame(TaskSchedulerBusCommands.Publish.ToString()).SendFrame(reply);
                }
                else if (message == TaskSchedulerBusCommands.AddedNode.ToString())
                {
                    var addedAddress = _actor.ReceiveFrameString();
                    _log.Debug(() => $"Added node {addedAddress} to the Bus");
                }
                else if (message == TaskSchedulerBusCommands.RemovedNode.ToString())
                {
                    var  removedAddress = _actor.ReceiveFrameString();
                    var  uri            = new Uri(removedAddress);
                    long temp;
                    _otherProcessorCounts.TryRemove(uri.Port, out temp);
                    _log.Debug(() => $"Removed node {removedAddress} from the Bus; it's processing count was {temp}");
                    RemoteCountChanged?.Invoke(this, EventArgs.Empty);
                }
                else if (message == TaskSchedulerBusCommands.SetCount.ToString())
                {
                    var key   = int.Parse(_actor.ReceiveFrameString());
                    var value = long.Parse(_actor.ReceiveFrameString());
                    if (!_otherProcessorCounts.ContainsKey(key))
                    {
                        _otherProcessorCounts.TryAdd(key, value);
                    }
                    else
                    {
                        _otherProcessorCounts[key] = value;
                    }
                    RemoteCountChanged?.Invoke(this, EventArgs.Empty);
                }
                else
                {
                    //response to broadcast
                    _log.Debug(() => message);
                }
            }
        }
Example #2
0
        public Account GetPayLoad()
        {
            var frameString = _actor.ReceiveFrameString();

            Log.Information("FrameString: {FrameString}", frameString);

            return(JsonConvert.DeserializeObject <Account>(frameString));
        }
        /// <summary>
        /// Starts this instance.
        /// </summary>
        public void Start()
        {
            lock (_lockSocket)
            {
                _actor = _bus.Start();
            }
            _currentTaskCount = 0;
            _running          = true;
            try
            {
                lock (_lockSocket)
                {
                    _actor.SendFrame(TaskSchedulerBusCommands.GetHostAddress.ToString());
                }
                _hostAddress = _actor.ReceiveFrameString();
                _hostPort    = new Uri("http://" + _hostAddress).Port;

                //second beacon time, so we wait to ensure beacon has fired
                Thread.Sleep(1100);

                //let other nodes know we are here
                lock (_lockSocket)
                {
                    _actor.SendMoreFrame(TaskSchedulerBusCommands.Publish.ToString())
                    .SendMoreFrame(TaskSchedulerBusCommands.BroadCast.ToString())
                    .SendFrame(_hostAddress);
                }

                // receive messages from other nodes on the bus
                while (!_stopRequested)
                {
                    try
                    {
                        ProcessMessages();
                    }
                    catch (Exception error)
                    {
                        _log.ErrorException("Failed to handle NetMCQ commands", error);
                    }
                }
            }
            catch (Exception error)
            {
                _log.ErrorException("A fatal error occurred while processing NetMCQ commands", error);
            }
            finally
            {
                _running = false;
            }
        }
Example #4
0
 /// <summary>
 /// Return our node endpoint, after successful initialization.
 /// </summary>
 /// <returns>our node endpoint</returns>
 public string EndPoint()
 {
     _actor.SendFrame("ENDPOINT");
     _endpoint = _actor.ReceiveFrameString();
     return(_endpoint);
 }
Example #5
0
 public Account GetPayLoad()
 {
     Console.WriteLine(" Testing asynch aspect of the Actor");
     return(JsonConvert.DeserializeObject <Account>(actor.ReceiveFrameString()));
 }