/[projects]/dao/DaoMqPump2/DaoMqPump2/PumpService.cs
ViewVC logotype

Annotation of /dao/DaoMqPump2/DaoMqPump2/PumpService.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2002 - (hide annotations) (download)
Mon Jul 8 14:33:50 2013 UTC (10 years, 10 months ago) by torben
File size: 6441 byte(s)
load remoteControl services before starting worker thread
1 torben 1986 using System;
2     using System.Threading;
3    
4     using System.Diagnostics;
5    
6     using System.ServiceModel;
7     using System.ServiceModel.Description;
8    
9    
10     namespace DaoMqPump2
11     {
12    
13 torben 1988 [System.ComponentModel.DesignerCategory("Code")]
14 torben 1986 class PumpService : System.ServiceProcess.ServiceBase
15     {
16     const int INTERVAL = 5;
17     TimeSpan m_delay = new TimeSpan(0, 0, 0, INTERVAL, 0);
18    
19     private Thread m_thread = null;
20     private static ManualResetEvent m_shutdownEvent = new ManualResetEvent(false);
21    
22    
23    
24    
25    
26     TransportController transportController = null;
27    
28    
29     ServiceHost selfHost;
30    
31    
32     protected void InitializeComponent()
33     {
34    
35     // Initialize the operating properties for the service.
36     this.CanPauseAndContinue = false;
37     this.CanShutdown = true;
38     this.CanHandleSessionChangeEvent = true;
39     this.ServiceName = "DaoMqPump2";
40     }
41    
42     // Start the service.
43     protected override void OnStart(string[] args)
44     {
45     try
46     {
47 torben 2002 //first load transports
48 torben 1986 transportController = TransportController.getInstance();
49    
50 torben 2002 //then load remoteControl
51     startRemoteControl();
52    
53     //finally start the worker thread
54 torben 1999 // create our threadstart object to wrap our delegate method
55     ThreadStart ts = new ThreadStart(this.ServiceWorkerMethod);
56 torben 1986
57 torben 1999 // create the manual reset event and
58     // set it to an initial state of unsignaled
59     m_shutdownEvent = new ManualResetEvent(false);
60 torben 1986
61 torben 1999 // create the worker thread
62     m_thread = new Thread(ts);
63 torben 1986
64 torben 1999 // go ahead and start the worker thread
65 torben 2002 m_thread.Start();
66 torben 1986
67 torben 1999 }
68     catch (Exception e)
69     {
70     EventLog.WriteEntry("DaoMqPump2", "Error starting DaoMqPump2: " + e.Message, EventLogEntryType.Error);
71     throw e;
72     }
73 torben 1986 }
74    
75     // Stop this service.
76     protected override void OnStop()
77     {
78     // New in .NET Framework version 2.0.
79     this.RequestAdditionalTime(10000);
80    
81     // signal the event to shutdown
82     m_shutdownEvent.Set();
83    
84     // wait for the thread to stop giving it 10 seconds
85     m_thread.Join(10000);
86    
87     // call the base class
88     base.OnStop();
89    
90     stopRemoteControl();
91    
92     this.ExitCode = 0;
93     }
94    
95    
96     void startRemoteControl()
97     {
98     // Step 1 Create a URI to serve as the base address.
99     Uri baseAddress = new Uri("http://localhost:8000/RemoteControl/");
100    
101     // Step 2 Create a ServiceHost instance
102     selfHost = new ServiceHost(typeof(RemoteControl), baseAddress);
103    
104     try
105     {
106     /*BasicHttpBinding
107     WSHttpBinding binding = new WSHttpBinding();
108     binding.Security.Mode = SecurityMode.None;//no security*/
109    
110    
111     // Step 3 Add a service endpoint.
112     selfHost.AddServiceEndpoint(typeof(IRemoteControl), new BasicHttpBinding(), "RemoteControl");
113    
114     // Step 4 Enable metadata exchange.
115     ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
116     smb.HttpGetEnabled = true;
117     selfHost.Description.Behaviors.Add(smb);
118     //selfHost.Authorization.ServiceAuthorizationManager = new GrantningAuthorizationManager();
119    
120     // Step 5 Start the service.
121     selfHost.Open();
122     Console.WriteLine("The service is ready.");
123    
124    
125     }
126     catch (CommunicationException ce)
127     {
128     Console.WriteLine("An exception occurred: {0}", ce.Message);
129     EventLog.WriteEntry("DaoMqPump2", "startRemoteControl(): " + ce.Message, EventLogEntryType.Warning);
130     selfHost.Abort();
131     }
132     }
133    
134     void stopRemoteControl()
135     {
136     try
137     {
138     // Close the ServiceHostBase to shutdown the service.
139     selfHost.Close();
140     }
141     catch (CommunicationException ce)
142     {
143     Console.WriteLine("An exception occurred: {0}", ce.Message);
144     EventLog.WriteEntry("DaoMqPump2", "stopRemoteControl(): " + ce.Message, EventLogEntryType.Warning);
145     }
146    
147     }
148    
149     public void ServiceWorkerMethod()
150     {
151     bool bSignaled = false;
152     int count = 20; //count starts high so we get to pump during the first iteration
153     try
154     {
155     do
156     {
157     // Block if the service is paused or is shutting down.
158     bSignaled = m_shutdownEvent.WaitOne(m_delay, true);
159    
160     // if we were signaled to shutdow, exit the loop
161     if (bSignaled == true)
162     break;
163    
164     count ++;
165    
166     int elapsed = INTERVAL * count;
167     if (elapsed >= 30) // only run every 30th second
168     {
169     transportController.transportAllMessages();
170     count = 0;//reset counter
171     }
172    
173     }
174     while (true);
175     }
176     catch (ThreadAbortException)
177     {
178     // Another thread has signalled that this worker
179     // thread must terminate. Typically, this occurs when
180     // the main service thread receives a service stop
181     // command.
182    
183     // Write a trace line indicating that the worker thread
184     // is exiting. Notice that this simple thread does
185     // not have any local objects or data to clean up.
186     }
187     }
188    
189    
190     }
191    
192     public class GrantningAuthorizationManager : ServiceAuthorizationManager
193     {
194     protected override bool CheckAccessCore(OperationContext operationContext)
195     {
196     return true;
197     }
198     }
199     }

  ViewVC Help
Powered by ViewVC 1.1.20