1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.metricshub.wbem.sblim.slp.internal.sa;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import java.io.IOException;
44 import org.metricshub.wbem.sblim.slp.ServiceLocationException;
45 import org.metricshub.wbem.sblim.slp.internal.TRC;
46
47
48
49
50
51 public abstract class RecieverThread implements Runnable {
52 private Thread iThread;
53
54 private volatile boolean iStop;
55
56 private boolean iInited;
57
58 private Object iInitLock = new Object();
59
60 protected ServiceAgent iSrvAgent;
61
62
63
64
65
66
67
68 public RecieverThread(String pName, ServiceAgent pSrvAgent) {
69 this.iThread = new Thread(this, pName);
70 this.iSrvAgent = pSrvAgent;
71 }
72
73
74
75
76 public void start() {
77 this.iThread.start();
78 }
79
80
81
82
83 public void wait4init() {
84 synchronized (this.iInitLock) {
85 try {
86 if (this.iInited) return;
87 this.iInitLock.wait();
88 return;
89 } catch (InterruptedException e) {
90 TRC.error(e);
91 }
92 }
93 }
94
95
96
97
98 public void stop() {
99 stop(true);
100 }
101
102
103
104
105
106
107 public void stop(boolean pWait) {
108 this.iStop = true;
109 if (pWait) join();
110 }
111
112 public void run() {
113
114 synchronized (this.iInitLock) {
115 TRC.debug("initing");
116 initialize();
117 this.iInited = true;
118 TRC.debug("inited");
119 try {
120 this.iInitLock.notifyAll();
121 } catch (IllegalMonitorStateException e) {
122 TRC.error(e);
123 }
124 }
125 while (!this.iStop) {
126 try {
127 mainLoop();
128 } catch (Exception e) {
129 TRC.error(e);
130 sleep(100);
131 initialize();
132 }
133 }
134 close();
135 this.iStop = false;
136 TRC.debug("STOPPED");
137 }
138
139
140
141 protected abstract void init() throws ServiceLocationException, IOException;
142
143 protected abstract void mainLoop() throws IOException;
144
145 protected abstract void close();
146
147 private void join() {
148 try {
149 this.iThread.join();
150 } catch (InterruptedException e) {
151 TRC.error(e);
152 }
153 }
154
155 private void initialize() {
156 try {
157 init();
158 } catch (ServiceLocationException e) {
159 TRC.error(e);
160 } catch (IOException e) {
161 TRC.error(e);
162 }
163 }
164
165 private static void sleep(int pMillis) {
166 try {
167 Thread.sleep(pMillis);
168 } catch (InterruptedException e) {
169 TRC.error(e);
170 }
171 }
172 }