| 1 |
/** |
|---|
| 2 |
* Authors: Steve Teale - steve.teale@britseyeview.com |
|---|
| 3 |
* |
|---|
| 4 |
* Date: 2007/05/19 |
|---|
| 5 |
* History: V0.1 |
|---|
| 6 |
* License: Use freely for any purpose. |
|---|
| 7 |
* |
|---|
| 8 |
* This is an example of a service built on class ServiceImplementation. |
|---|
| 9 |
*/ |
|---|
| 10 |
module bevutils.beepservice; |
|---|
| 11 |
|
|---|
| 12 |
import std.stdio; |
|---|
| 13 |
import std.c.windows.windows; |
|---|
| 14 |
import bevutils.log4d; |
|---|
| 15 |
import bevutils.eventlogger; |
|---|
| 16 |
import bevutils.propertyfile; |
|---|
| 17 |
import bevutils.servicebase; |
|---|
| 18 |
import bevutils.serviceimplementation; |
|---|
| 19 |
|
|---|
| 20 |
extern (Windows) |
|---|
| 21 |
{ |
|---|
| 22 |
BOOL Beep(DWORD dwFreq, DWORD dwDuration); |
|---|
| 23 |
} |
|---|
| 24 |
/++ |
|---|
| 25 |
+ To implement the service you must implement one or more classes derived from interface WorkerThread |
|---|
| 26 |
+ and a class derived from ServiceImplementation. |
|---|
| 27 |
+ You must also provide a stereotyped version of main(). |
|---|
| 28 |
+ |
|---|
| 29 |
+ ------------------------------------------------------ |
|---|
| 30 |
class BeepWorker : WorkerThread |
|---|
| 31 |
{ |
|---|
| 32 |
Log4D _log; |
|---|
| 33 |
ServiceImplementation _si; |
|---|
| 34 |
EventLogger _elog; |
|---|
| 35 |
PropertyFile _props; |
|---|
| 36 |
uint _frequency; |
|---|
| 37 |
uint _duration; |
|---|
| 38 |
|
|---|
| 39 |
this(uint freq, uint dur) |
|---|
| 40 |
{ |
|---|
| 41 |
_frequency = freq; |
|---|
| 42 |
_duration = dur; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
void setLog(Log4D log) { _log = log; } |
|---|
| 46 |
void setHost(ServiceImplementation si) { _si = si; } |
|---|
| 47 |
void setProperties(PropertyFile pf) { _props = pf; } |
|---|
| 48 |
void setEventLogger(EventLogger el) { _elog = el; } |
|---|
| 49 |
char[] threadName() { return "Beeper"; } |
|---|
| 50 |
|
|---|
| 51 |
int threadProc() |
|---|
| 52 |
{ |
|---|
| 53 |
for (;;) |
|---|
| 54 |
{ |
|---|
| 55 |
Beep(_frequency, _duration); |
|---|
| 56 |
_log.logMessage("INFO", "Beeped"); |
|---|
| 57 |
for (int i = 0; i < 20; i++) |
|---|
| 58 |
{ |
|---|
| 59 |
Sleep(500); |
|---|
| 60 |
if (_si.StopFlag) |
|---|
| 61 |
{ |
|---|
| 62 |
_log.logMessage("INFO", "Beeper stopping"); |
|---|
| 63 |
return 0; |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
return 0; |
|---|
| 68 |
} |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
class BeepService : ServiceImplementation |
|---|
| 72 |
{ |
|---|
| 73 |
this() |
|---|
| 74 |
{ |
|---|
| 75 |
super("Beeper", "BPR"); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
public WorkerThread getThreadImpl(int n) |
|---|
| 79 |
{ |
|---|
| 80 |
if (n == 0) |
|---|
| 81 |
return new BeepWorker(200, 500); |
|---|
| 82 |
else |
|---|
| 83 |
return new BeepWorker(650, 200); |
|---|
| 84 |
} |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
void main(char[][] args) |
|---|
| 88 |
{ |
|---|
| 89 |
try |
|---|
| 90 |
{ |
|---|
| 91 |
ServiceImplementation si = new BeepService(); |
|---|
| 92 |
ServiceBase.implementMain(args); |
|---|
| 93 |
} |
|---|
| 94 |
catch (Exception ex) |
|---|
| 95 |
{ |
|---|
| 96 |
writefln(ex.toString()); |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
+ ------------------------------------------------------ |
|---|
| 100 |
+/ |
|---|
| 101 |
class BeepWorker : WorkerThread |
|---|
| 102 |
{ |
|---|
| 103 |
Log4D _log; |
|---|
| 104 |
ServiceImplementation _si; |
|---|
| 105 |
EventLogger _elog; |
|---|
| 106 |
PropertyFile _props; |
|---|
| 107 |
uint _frequency; |
|---|
| 108 |
uint _duration; |
|---|
| 109 |
|
|---|
| 110 |
this(uint freq, uint dur) |
|---|
| 111 |
{ |
|---|
| 112 |
_frequency = freq; |
|---|
| 113 |
_duration = dur; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
void setLog(Log4D log) { _log = log; } |
|---|
| 117 |
void setHost(ServiceImplementation si) { _si = si; } |
|---|
| 118 |
void setProperties(PropertyFile pf) { _props = pf; } |
|---|
| 119 |
void setEventLogger(EventLogger el) { _elog = el; } |
|---|
| 120 |
char[] threadName() { return "Beeper"; } |
|---|
| 121 |
|
|---|
| 122 |
int threadProc() |
|---|
| 123 |
{ |
|---|
| 124 |
for (;;) |
|---|
| 125 |
{ |
|---|
| 126 |
Beep(_frequency, _duration); |
|---|
| 127 |
_log.logMessage("INFO", "Beeped"); |
|---|
| 128 |
for (int i = 0; i < 20; i++) |
|---|
| 129 |
{ |
|---|
| 130 |
Sleep(500); |
|---|
| 131 |
if (_si.StopFlag) |
|---|
| 132 |
{ |
|---|
| 133 |
_log.logMessage("INFO", "Beeper stopping"); |
|---|
| 134 |
return 0; |
|---|
| 135 |
} |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
return 0; |
|---|
| 139 |
} |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
/** |
|---|
| 143 |
* You must also override class ServiceImplementation to provide appropriate |
|---|
| 144 |
* information to its constructor, and to provide an implementation for |
|---|
| 145 |
* getThreadImpl. |
|---|
| 146 |
*/ |
|---|
| 147 |
class BeepService : ServiceImplementation |
|---|
| 148 |
{ |
|---|
| 149 |
this() |
|---|
| 150 |
{ |
|---|
| 151 |
super("Beeper", "BPR"); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
public WorkerThread getThreadImpl(int n) |
|---|
| 155 |
{ |
|---|
| 156 |
if (n == 0) |
|---|
| 157 |
return new BeepWorker(200, 500); |
|---|
| 158 |
else |
|---|
| 159 |
return new BeepWorker(650, 200); |
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
/** |
|---|
| 164 |
* Main is completely stereotyped. |
|---|
| 165 |
* |
|---|
| 166 |
* The properties file for this service should be as follows: |
|---|
| 167 |
* --------------------------------------------------------- |
|---|
| 168 |
* <?xml version="1.0" ?> |
|---|
| 169 |
* <Properties layout="1"> |
|---|
| 170 |
* <logpath type="string">d:\logs</logpath> |
|---|
| 171 |
* <numlogs type="int">10</numlogs> |
|---|
| 172 |
* <maxlogsize type="int">1000000</maxlogsize> |
|---|
| 173 |
* <threads type="int">2</threads> |
|---|
| 174 |
* </Properties> |
|---|
| 175 |
* --------------------------------------------------------- |
|---|
| 176 |
* You could of course put the beep frequencies and durations |
|---|
| 177 |
* in an int[] in the properties file. |
|---|
| 178 |
*/ |
|---|
| 179 |
void main(char[][] args) |
|---|
| 180 |
{ |
|---|
| 181 |
try |
|---|
| 182 |
{ |
|---|
| 183 |
ServiceImplementation si = new BeepService(); |
|---|
| 184 |
ServiceBase.implementMain(args); |
|---|
| 185 |
} |
|---|
| 186 |
catch (Exception ex) |
|---|
| 187 |
{ |
|---|
| 188 |
writefln(ex.toString()); |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|