/// <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> /// Starts the listen. /// </summary> private void StartEventListener() { StopEventListener(); // if there is any channel still open --> close it try { log.Info("Starting event listener"); int port = AppConfigSettings.GetInt(ConfigParameter.ListenerPort, ModuleName.Console); eventListenerChannel = new TcpChannel(port); ChannelServices.RegisterChannel(eventListenerChannel, false); eventInvocation = new EventInvocation(); string serviceName = AppConfigSettings.GetString(ConfigParameter.ListenerServiceName, ModuleName.Console); eventListenerService = RemotingServices.Marshal(eventInvocation, serviceName); //RemotingConfiguration.RegisterWellKnownServiceType(typeof(EventInvocation), // serviceName, WellKnownObjectMode.SingleCall); // define the event which is triggered when the Master calls the CallSlave() function eventInvocation.EventReceived += new EventInvocation.Received(OnEventReceived); // Create the event queue eventLock = new object(); eventQueue = new PriorityQueue<EventAction, EventPriority>(10); eventTrigger = new AutoResetEvent(false); eventProcessor = new Thread(ProcessEvents); eventProcessor.IsBackground = true; eventProcessor.Start(); log.Info(string.Format("Successfully started event listener {0} on port {1}", serviceName, port)); } catch (Exception ex) { log.Error("Error starting event listener: " + ex.Message, ex); StopEventListener(); // calls StopEventListener } }
/// <summary> /// Stops the listener /// </summary> private void StopEventListener() { try { if (eventListenerService != null) RemotingServices.Unmarshal(eventListenerService); if (eventInvocation != null) RemotingServices.Disconnect(eventInvocation); if (eventListenerChannel != null) ChannelServices.UnregisterChannel(eventListenerChannel); if (eventQueue != null) { eventQueue.Clear(); eventQueue = null; } if (eventTrigger != null) { eventTrigger.Close(); eventTrigger = null; } if (eventProcessor != null) { try { if (eventProcessor.IsAlive) eventProcessor.Abort(); } catch (Exception) { } eventProcessor = null; } eventListenerService = null; eventInvocation = null; eventListenerChannel = null; } catch (Exception ex) { log.Error("Error stopping event listener: " + ex.Message, ex); } }
/// <summary> /// Starts the event listener. /// </summary> private void StartEventListener() { /**** try { // Start up the WCF service log.Info("Starting service event listener"); if (serviceHost != null) { serviceHost.Close(); } // Create a ServiceHost for the CalculatorService type and // provide the base address. serviceHost = new ServiceHost(typeof(ServiceCommand)); // Open the ServiceHostBase to create listeners and start // listening for messages. serviceHost.Open(); // Start up the WCF service log.Info("Interprocess communication service started successfully"); } catch (Exception ex) { // Start up the WCF service log.Error("Failed to start interprocess communication service"); log.Error(string.Format("Error: {0}", ex.Message), ex); } ****/ StopEventListener(); // if there is any channel still open --> close it try { log.Info("Starting event listener"); int port = AppConfigSettings.GetInt(ConfigParameter.ListenerPort, ModuleName.Service); eventListenerChannel = new TcpChannel(port); ChannelServices.RegisterChannel(eventListenerChannel, false); eventInvocation = new EventInvocation(); string serviceName = AppConfigSettings.GetString(ConfigParameter.ListenerServiceName, ModuleName.Service); eventListenerService = RemotingServices.Marshal(eventInvocation, serviceName); //RemotingConfiguration.RegisterWellKnownServiceType(typeof(EventInvocation), // serviceName, WellKnownObjectMode.SingleCall); // define the event which is triggered when the Master calls the CallSlave() function eventInvocation.EventReceived += new EventInvocation.Received(OnEventReceived); // Create the event queue eventLock = new object(); eventQueue = new PriorityQueue<EventAction, EventPriority>(10); eventTrigger = new AutoResetEvent(false); eventProcessor = new Thread(ProcessEvents); eventProcessor.IsBackground = true; eventProcessor.Start(); log.Info(string.Format("Successfully started event listener {0} on port {1}", serviceName, port)); } catch (Exception ex) { log.Error(string.Format("Error starting listener: {0}", ex.Message), ex); StopEventListener(); // calls StopEventListener } }