/// <summary> /// Notifies the event. /// </summary> /// <param name="url">The URL.</param> /// <param name="action">The action.</param> /// <returns></returns> public static EventResponse NotifyEvent(string url, EventAction action) { try { if (log.IsDebugEnabled) log.Debug(string.Format("Sending event [{0}]", action.Notification)); EventInvocation invoker = Activator.GetObject(typeof(EventInvocation), url) as EventInvocation; EventResponse response = invoker.Invoke(action); if (log.IsDebugEnabled) log.Debug("Event is successfully sent"); return response; } catch (Exception ex) { log.Error(string.Format("Invocation error: {0}", ex.Message), ex); } EventResponse defaultResponse = new EventResponse(); defaultResponse.Status = StringEnum.GetStringValue(EventNotificationResponse.Failed); return defaultResponse; }
/// <summary> /// Called when [event received]. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse OnEventReceived(EventAction action) { // Put the event in a queue and respond immediately lock (eventLock) { // Queue the event and trigger the processing eventQueue.Enqueue(action, EventPriority.Normal); eventTrigger.Set(); // Send back the processing EventResponse response = new EventResponse(); response.Status = StringEnum.GetStringValue(EventNotificationResponse.OK); return response; } }
/// <summary> /// Verifies the response. /// </summary> /// <param name="response">The response.</param> /// <returns></returns> public static bool VerifyResponse(EventResponse response) { return(!string.IsNullOrEmpty(response.Status) && !response.Status.Equals(StringEnum.GetStringValue(EventNotificationResponse.Failed), StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Starts the gateway. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse StartGateway(EventAction action) { string gatewayId = action.Values[EventParameter.GatewayId]; if (log.IsDebugEnabled) log.Debug(string.Format("Start gateway. ID is [{0}]", gatewayId)); EventResponse response = new EventResponse(); IGateway gateway; if (messageGatewayService.Find(gatewayId, out gateway)) { // Check if the status is Stopped/Failed/Restart, if yes, remove and restart if (gateway.Status == GatewayStatus.Failed || gateway.Status == GatewayStatus.Stopped || gateway.Status == GatewayStatus.Restart) { messageGatewayService.Remove(gatewayId); ConnectGateway(gatewayId); } } else { ConnectGateway(gatewayId); } return response; }
/// <summary> /// Stops the messenger. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse StopMessenger(EventAction action) { string name = action.Values[EventParameter.MessengerName]; if (log.IsDebugEnabled) log.DebugFormat("Stopping messenger. Name is [{0}]", name); EventResponse response = new EventResponse(); DbMessenger messenger = DbMessenger.SingleOrDefault(m => m.Name.ToLower() == name.ToLower()); if (messenger != null) { if (pollers == null) return response; // Check if the poller is already started if (pollers.Exists(p => p.Name.ToLower() == messenger.Name.ToLower())) { BasePoller poller = pollers.Find(p => p.Name.ToLower() == messenger.Name.ToLower()); pollers.Remove(poller); if (workerThreads != null) { Thread t = workerThreads.Find(w => messenger.Name.ToLower() == w.Name.ToLower()); if (t != null) { try { t.Join(ThreadWaitInterval); } catch (Exception ex) { log.Info(string.Format("Aborting thread [{0}]", t.Name), ex); try { t.Abort(); } catch (Exception) { } } } } log.InfoFormat("Messenger [{0}] is stopped", name); } else { log.InfoFormat("Unable to find messenger [{0}]", name); } } return response; }
/// <summary> /// Starts the messenger. /// </summary> /// <param name="action">The action.</param> private EventResponse StartMessenger(EventAction action) { string name = action.Values[EventParameter.MessengerName]; log.InfoFormat("Starting messenger. Name is [{0}]", name); EventResponse response = new EventResponse(); DbMessenger messenger = DbMessenger.SingleOrDefault(m =>m.Name.ToLower() == name.ToLower()); if (messenger != null) { // Start to poll incoming message if (workerThreads == null) workerThreads = new List<Thread>(1); if (pollers == null) pollers = new List<BasePoller>(1); // Check if the poller is already started if (pollers.Exists(p => p.Name.ToLower() == messenger.Name.ToLower())) { log.InfoFormat("Database messenger [{0}] is already started", messenger.Name); return response; } DatabasePoller poller = new DatabasePoller(messenger, this.messageGatewayService); poller.Name = messenger.Name; pollers.Add(poller); Thread worker = new Thread(new ThreadStart(poller.StartTimer)); worker.IsBackground = true; worker.Name = messenger.Name; workerThreads.Add(worker); worker.Start(); log.InfoFormat("Messenger [{0}] is started", name); } return response; }
/// <summary> /// Checks the messenger status. /// </summary> /// <param name="action">The action.</param> private EventResponse CheckMessengerStatus(EventAction action) { string name = action.Values[EventParameter.MessengerName]; log.DebugFormat("Check messenger status. Name is [{0}]", name); EventResponse response = new EventResponse(); if (pollers == null || !pollers.Exists(p=> p.Name.ToLower() == name.ToLower())) { response.Status = StringEnum.GetStringValue(EventNotificationResponse.Failed); response.Results.Add(EventParameter.GatewayStatus, StringEnum.GetStringValue(GatewayStatus.Stopped)); } else { response.Results.Add(EventParameter.MessengerStatus, StringEnum.GetStringValue(MessengerStatus.Started)); } return response; }
/// <summary> /// Processes synchronous events /// </summary> /// <param name="action">The action.</param> /// <returns>Event response</returns> private EventResponse ProcessSyncEvents(EventAction action) { EventNotificationType notificationType = (EventNotificationType)StringEnum.Parse(typeof(EventNotificationType), action.Notification); if (notificationType == EventNotificationType.QueryGatewayStatus) { return CheckGatewayStatus(action); } else if (notificationType == EventNotificationType.QueryWebServerStatus) { return CheckWebServerStatus(action); } else if (notificationType == EventNotificationType.QueryMessengerStatus) { return CheckMessengerStatus(action); } EventResponse response = new EventResponse(); response.Status = StringEnum.GetStringValue(EventNotificationResponse.Failed); return response; }
/// <summary> /// Called when [event received]. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse OnEventReceived(EventAction action) { // Put the event in a queue and respond immediately lock (eventLock) { try { if (action.ActionType == EventAction.ASynchronous) { // Queue the event and trigger the processing eventQueue.Enqueue(action, EventPriority.Normal); eventTrigger.Set(); // Send back the processing EventResponse response = new EventResponse(); response.Status = StringEnum.GetStringValue(EventNotificationResponse.OK); return response; } else { return ProcessSyncEvents(action); } } catch (Exception ex) { log.Error(string.Format("Error processing incoming event - [{0}]", action.ToString())); log.Error(ex.Message, ex); } } return new EventResponse(); }
/// <summary> /// Checks the web server status. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse CheckWebServerStatus(EventAction action) { EventResponse response = new EventResponse(); response.Status = StringEnum.GetStringValue(EventNotificationResponse.OK); if (webServer != null) { response.Results.Add(EventParameter.WebServerStatus, StringEnum.GetStringValue(WebServerStatus.Started)); response.Results.Add(EventParameter.WebServerUrl, webServer.RootUrl); } else { response.Results.Add(EventParameter.WebServerStatus, StringEnum.GetStringValue(WebServerStatus.Stopped)); response.Results.Add(EventParameter.WebServerUrl, string.Empty); } return response; }
/// <summary> /// Checks the gateway status. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse CheckGatewayStatus(EventAction action) { string gatewayId = action.Values[EventParameter.GatewayId]; log.Debug(string.Format("Check gateway status. ID is [{0}]", gatewayId)); EventResponse response = new EventResponse(); IGateway gateway; if (messageGatewayService.Find(gatewayId, out gateway)) { // Gateway found response.Status = StringEnum.GetStringValue(EventNotificationResponse.OK); response.Results.Add(EventParameter.GatewayStatus, StringEnum.GetStringValue(gateway.Status)); if (gateway.Status == GatewayStatus.Started) { IMobileGateway mobileGateway = gateway as IMobileGateway; response.Results.Add(EventParameter.GatewayOperator, mobileGateway.NetworkOperator.OperatorInfo); response.Results.Add(EventParameter.GatewaySignalStrength, Convert.ToString(mobileGateway.SignalQuality.SignalStrengthPercent)); } } else { response.Status = StringEnum.GetStringValue(EventNotificationResponse.Failed); response.Results.Add(EventParameter.GatewayStatus, StringEnum.GetStringValue(GatewayStatus.Stopped)); } return response; }
/// <summary> /// Stops the gateway. /// </summary> /// <param name="action">The action.</param> /// <returns></returns> private EventResponse StopGateway(EventAction action) { string gatewayId = action.Values[EventParameter.GatewayId]; log.Debug(string.Format("Stop gateway. ID is [{0}]", gatewayId)); EventResponse response = new EventResponse(); IGateway gateway; if (messageGatewayService.Find(gatewayId, out gateway)) { messageGatewayService.Remove(gatewayId); } return response; }
/// <summary> /// Verifies the response. /// </summary> /// <param name="response">The response.</param> /// <returns></returns> public static bool VerifyResponse(EventResponse response) { return (!string.IsNullOrEmpty(response.Status) && !response.Status.Equals(StringEnum.GetStringValue(EventNotificationResponse.Failed), StringComparison.OrdinalIgnoreCase)); }