Пример #1
0
        /// <summary>
        /// Runs the agent
        /// </summary>
        public void Run()
        {
            try
            {
                // Check we have an address to connect to.
                if (string.IsNullOrEmpty(ApplicationSettings.Default.XmrNetworkAddress))
                {
                    throw new Exception("Empty XMR Network Address");
                }

                // Get the Private Key
                AsymmetricCipherKeyPair rsaKey = _hardwareKey.getXmrKey();

                // Connect to XMR
                using (SubscriberSocket socket = new SubscriberSocket())
                {
                    // Bind
                    socket.Connect(ApplicationSettings.Default.XmrNetworkAddress);
                    socket.Subscribe("H");
                    socket.Subscribe(_hardwareKey.Channel);

                    // Notify
                    _clientInfoForm.XmrSubscriberStatus = "Connected to " + ApplicationSettings.Default.XmrNetworkAddress;

                    while (!_forceStop)
                    {
                        lock (_locker)
                        {
                            try
                            {
                                NetMQMessage message = socket.ReceiveMultipartMessage();

                                // Update status
                                string statusMessage = "Connected (" + ApplicationSettings.Default.XmrNetworkAddress + "), last activity: " + DateTime.Now.ToString();

                                // Write this out to a log
                                _clientInfoForm.XmrSubscriberStatus = statusMessage;
                                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", statusMessage), LogType.Audit.ToString());

                                // Deal with heart beat
                                if (message[0].ConvertToString() == "H")
                                {
                                    LastHeartBeat = DateTime.Now;
                                    continue;
                                }

                                // Decrypt the message
                                string opened = OpenSslInterop.decrypt(message[2].ConvertToString(), message[1].ConvertToString(), rsaKey.Private);

                                // Decode into a JSON string
                                PlayerAction action = JsonConvert.DeserializeObject <PlayerAction>(opened);

                                // Make sure the TTL hasn't expired
                                if (DateTime.Now > action.createdDt.AddSeconds(action.ttl))
                                {
                                    Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Expired Message: " + action.action), LogType.Info.ToString());
                                    continue;
                                }

                                // Decide what to do with the message, probably raise events according to the type of message we have
                                switch (action.action)
                                {
                                case "commandAction":

                                    // Create a schedule command out of the message
                                    Dictionary <string, string> obj     = JsonConvert.DeserializeObject <Dictionary <string, string> >(opened);
                                    ScheduleCommand             command = new ScheduleCommand();
                                    string code;
                                    obj.TryGetValue("commandCode", out code);
                                    command.Code = code;

                                    new Thread(new ThreadStart(command.Run)).Start();
                                    break;

                                case "collectNow":
                                case RevertToSchedulePlayerAction.Name:
                                    if (OnAction != null)
                                    {
                                        OnAction(action);
                                    }
                                    break;

                                case LayoutChangePlayerAction.Name:

                                    LayoutChangePlayerAction changeLayout = JsonConvert.DeserializeObject <LayoutChangePlayerAction>(opened);

                                    if (OnAction != null)
                                    {
                                        OnAction(changeLayout);
                                    }

                                    break;

                                case OverlayLayoutPlayerAction.Name:
                                    OverlayLayoutPlayerAction overlayLayout = JsonConvert.DeserializeObject <OverlayLayoutPlayerAction>(opened);

                                    if (OnAction != null)
                                    {
                                        OnAction(overlayLayout);
                                    }
                                    break;

                                case "screenShot":
                                    ScreenShot.TakeAndSend();
                                    _clientInfoForm.notifyStatusToXmds();
                                    break;

                                default:
                                    Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unknown Message: " + action.action), LogType.Info.ToString());
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                // Log this message, but dont abort the thread
                                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Exception in Run: " + ex.Message), LogType.Error.ToString());
                                _clientInfoForm.XmrSubscriberStatus = "Error. " + ex.Message;
                            }
                        }
                    }
                }

                // Update status
                _clientInfoForm.XmrSubscriberStatus = "Not Running, last activity: " + LastHeartBeat.ToString();

                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Subscriber Stopped"), LogType.Info.ToString());
            }
            catch (Exception e)
            {
                Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unable to Subscribe to XMR: " + e.Message), LogType.Info.ToString());
                _clientInfoForm.XmrSubscriberStatus = e.Message;
            }
        }
Пример #2
0
        /// <summary>
        /// Runs the agent
        /// </summary>
        public void Run()
        {
            Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Thread Started"), LogType.Info.ToString());

            while (!_forceStop)
            {
                lock (_locker)
                {
                    try
                    {
                        // If we are restarting, reset
                        _manualReset.Reset();

                        // Check we have an address to connect to.
                        if (string.IsNullOrEmpty(ApplicationSettings.Default.XmrNetworkAddress))
                        {
                            throw new Exception("Empty XMR Network Address");
                        }

                        // Cache the address for this socket (the setting may change outside).
                        _address = ApplicationSettings.Default.XmrNetworkAddress;

                        // Get the Private Key
                        AsymmetricCipherKeyPair rsaKey = _hardwareKey.getXmrKey();

                        // Connect to XMR
                        try
                        {
                            // Create a Poller
                            _poller = new NetMQPoller();

                            // Create a Socket
                            using (SubscriberSocket socket = new SubscriberSocket())
                            {
                                // Options
                                socket.Options.ReconnectInterval = TimeSpan.FromSeconds(5);
                                socket.Options.Linger            = TimeSpan.FromSeconds(0);

                                // Bind
                                socket.Connect(ApplicationSettings.Default.XmrNetworkAddress);
                                socket.Subscribe("H");
                                socket.Subscribe(_hardwareKey.Channel);

                                // Add Socket to Poller
                                _poller.Add(socket);

                                // Bind to the receive ready event
                                socket.ReceiveReady += _socket_ReceiveReady;

                                // Notify
                                _clientInfoForm.XmrSubscriberStatus = "Connected to " + ApplicationSettings.Default.XmrNetworkAddress + ". Waiting for messages.";

                                // Sit and wait, processing messages, indefinitely or until we are interrupted.
                                _poller.Run();
                            }
                        }
                        finally
                        {
                            _poller.Dispose();
                        }

                        Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Socket Disconnected, waiting to reconnect."), LogType.Info.ToString());
                    }
                    catch (TerminatingException terminatingEx)
                    {
                        Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "NetMQ terminating: " + terminatingEx.Message), LogType.Audit.ToString());
                    }
                    catch (Exception e)
                    {
                        Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Unable to Subscribe: " + e.Message), LogType.Info.ToString());
                        _clientInfoForm.XmrSubscriberStatus = e.Message;
                    }

                    // Update status
                    _clientInfoForm.XmrSubscriberStatus = "Disconnected, waiting to reconnect, last activity: " + LastHeartBeat.ToString();

                    // Sleep for 60 seconds.
                    _manualReset.WaitOne(60 * 1000);
                }
            }

            Trace.WriteLine(new LogMessage("XmrSubscriber - Run", "Subscriber Stopped"), LogType.Info.ToString());
        }