View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2009
3    
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE 
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE 
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7    
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10   
11    @author : Endre Bak, IBM, ebak@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1804402    2007-09-28  ebak         IPv6 ready SLP
17   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
18   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
19   */
20  
21  package org.metricshub.wbem.sblim.slp.internal.sa;
22  
23  /*-
24   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
25   * WBEM Java Client
26   * ჻჻჻჻჻჻
27   * Copyright 2023 - 2025 MetricsHub
28   * ჻჻჻჻჻჻
29   * Licensed under the Apache License, Version 2.0 (the "License");
30   * you may not use this file except in compliance with the License.
31   * You may obtain a copy of the License at
32   *
33   *      http://www.apache.org/licenses/LICENSE-2.0
34   *
35   * Unless required by applicable law or agreed to in writing, software
36   * distributed under the License is distributed on an "AS IS" BASIS,
37   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38   * See the License for the specific language governing permissions and
39   * limitations under the License.
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   * RecieverThread
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  	 * Ctor.
64  	 *
65  	 * @param pName
66  	 * @param pSrvAgent
67  	 */
68  	public RecieverThread(String pName, ServiceAgent pSrvAgent) {
69  		this.iThread = new Thread(this, pName);
70  		this.iSrvAgent = pSrvAgent;
71  	}
72  
73  	/**
74  	 * start
75  	 */
76  	public void start() {
77  		this.iThread.start();
78  	}
79  
80  	/**
81  	 * wait4init
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  	 * stop
97  	 */
98  	public void stop() {
99  		stop(true);
100 	}
101 
102 	/**
103 	 * stop
104 	 *
105 	 * @param pWait
106 	 */
107 	public void stop(boolean pWait) {
108 		this.iStop = true;
109 		if (pWait) join();
110 	}
111 
112 	public void run() {
113 		// 1st init
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 	// exception of init shoud be reachable for ServiceTable
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 }