1 package com.ibm.staf.service.timer;
2 
3 /*****************************************************************************/
4 /* Software Testing Automation Framework (STAF)                              */
5 /* (C) Copyright IBM Corp. 2002, 2005                                        */
6 /*                                                                           */
7 /* This software is licensed under the Eclipse Public License (EPL) V1.0.    */
8 /*****************************************************************************/
9 
10 /****************************************************************************/
11 //
12 // Class: WatchObject
13 //
14 // Logical Name: WatchObject.java
15 //
16 // Description: This class contains information about each machine registered
17 //              to be watched.
18 //
19 //
20 // History:
21 //
22 // DATE       DEVELOPER   CHANGE DESCRIPTION
23 // ---------- ----------- -----------------------------------
24 // 02/01/2000 C Alkov     Original Program
25 // 02/12/2000 C Alkov     Register timers for corresponding watches using
26 //                        unregonnopath 0 & unregonnohandle 0 (def:3622)
27 //
28 /****************************************************************************/
29 
30 import java.io.Serializable;
31 import java.util.Date;
32 import com.ibm.staf.STAFException;
33 import com.ibm.staf.STAFResult;
34 import com.ibm.staf.wrapper.STAFLog;
35 import java.util.Map;
36 
37 public class WatchObject implements Serializable
38 {
39     private String machineToWatch;
40     private long frequency;
41     private int margin;
42 
43     /* Do not serialize (transient) this instance variable since
44        reference will be different from run to run */
45     private transient TimerRequestHandler reqHandler;
46 
47     private Date lastTimeStamp = null;
48 
49 /****************************************************************************/
50 //
51 // Method:
52 //   Constructor
53 //
54 // Description:
55 //   Constructor method for TimerObject class.
56 //
57 // Input:
58 //   aMachineToWatch - the name of the machine to be watched
59 //   aFrequency - the frequency at which the machine should send notifications
60 //                to the WatchManager
61 //   aMargin - the allowable error in the response time of the machine being
62 //             watched
63 //   aReqHandler - reference to the TimerRequestHandler object
64 //
65 // Exceptions Thrown:
66 //   STAFException
67 //
68 // Notes:
69 //   none
70 //
71 /****************************************************************************/
72 
WatchObject(String aMachineToWatch, long aFrequency, int aMargin, TimerRequestHandler aReqHandler)73 public WatchObject(String aMachineToWatch, long aFrequency, int aMargin,
74                    TimerRequestHandler aReqHandler) throws STAFException
75 {
76     super();
77 
78     machineToWatch = aMachineToWatch.toUpperCase();
79     frequency = aFrequency;
80     margin = aMargin;
81     reqHandler = aReqHandler;
82 
83     // Register a timer for machineToWatch.  Specify machineToWatch as the
84     // key so that can match a watch with the right registration
85 
86     String request = "REGISTER TYPE " + reqHandler.sWatchType +
87         " KEY " + machineToWatch + " FREQUENCY " + frequency +
88         " UNREGONNOPATH 0 UNREGONNOHANDLE 0 BYNAME";
89 
90     String result = reqHandler.timer.wHandle.submit(
91         machineToWatch, "TIMER", request);
92 
93     /* Set lastTimeStamp to current time */
94 
95     setLastTimeStamp(new Date());
96 
97     /* Add this timer to watchList */
98 
99     reqHandler.watchList.put(machineToWatch, this);
100 }
101 
102 /****************************************************************************/
103 //
104 // Method:
105 //   getProperties
106 //
107 // Description:
108 //   Returns the properties of this watch in a marshalled map for use in the
109 // LIST WATCHES request.
110 //
111 // Input:
112 //   none
113 //
114 // Exceptions Thrown:
115 //   none
116 //
117 // Notes:
118 //   synchronized method
119 //
120 /****************************************************************************/
121 
getProperties()122 public synchronized Map getProperties()
123 {
124     Map properties;
125 
126     properties = reqHandler.fWatchMapClass.createInstance();
127 
128     properties.put("machine", machineToWatch);
129 
130     if (lastNotificationReceived())
131         properties.put("status", "OK");
132     else
133         properties.put("status", "Missed");
134 
135     if (lastTimeStamp != null)
136         properties.put("lastTimestamp", TimerUtils.formatTime(lastTimeStamp));
137 
138     properties.put("frequency", String.valueOf(frequency));
139     properties.put("margin", String.valueOf(margin));
140 
141     return properties;
142 }
143 
144 /****************************************************************************/
145 //
146 // Method:
147 //   lastNotificationReceived
148 //
149 // Description:
150 //   Private method for checking if the last notification was receieved
151 //   from the machine being watched
152 //
153 // Input:
154 //   none
155 //
156 // Exceptions Thrown:
157 //   none
158 //
159 // Notes:
160 //   none
161 //
162 /****************************************************************************/
163 
lastNotificationReceived()164 private boolean lastNotificationReceived()
165 {
166     Date OKTime = new Date(lastTimeStamp.getTime() + frequency + margin);
167     Date currentTime = new Date();
168 
169     if(currentTime.after(OKTime))
170     {
171         return false;
172     }
173     else
174     {
175         return true;
176     }
177 
178 }
179 
180 /****************************************************************************/
181 //
182 // Method:
183 //   setLastTimeStamp
184 //
185 // Description:
186 //   Sets the value of lastTimeStamp (the last time a notification was
187 //   received from the machine being watched)
188 //
189 // Input:
190 //   newLastTimeStamp - Date representing the last time the machine being
191 //                      watched sent a notification message
192 //
193 // Exceptions Thrown:
194 //   none
195 //
196 // Notes:
197 //   synchronized method
198 //
199 /****************************************************************************/
200 
setLastTimeStamp(Date newLastTimeStamp)201 public synchronized void setLastTimeStamp(Date newLastTimeStamp)
202 {
203     lastTimeStamp = newLastTimeStamp;
204 }
205 
206 /****************************************************************************/
207 //
208 // Method:
209 //   setReqHandler
210 //
211 // Description:
212 //   Sets the RequestHandler for the WatchObject.
213 //
214 // Input:
215 //   aReqHandler - Reference to the current RequestHandler
216 //
217 // Exceptions Thrown:
218 //   none
219 //
220 // Notes:
221 //   none
222 //
223 /****************************************************************************/
224 
setReqHandler(TimerRequestHandler aReqHandler)225 public void setReqHandler(TimerRequestHandler aReqHandler)
226 {
227     reqHandler = aReqHandler;
228 }
229 
230 /****************************************************************************/
231 //
232 // Method:
233 //   update
234 //
235 // Description:
236 //   Updates the frequency and margin of the Watch.
237 //
238 // Input:
239 //   newFrequency - The new frequency of the watch.
240 //   newMargin - The new margin of the watch.
241 //
242 // Exceptions Thrown:
243 //   none
244 //
245 // Notes:
246 //   synchronized method
247 //
248 /****************************************************************************/
249 
update(long newFrequency, int newMargin)250 public synchronized STAFResult update(long newFrequency, int newMargin)
251 {
252     frequency = newFrequency;
253     margin = newMargin;
254 
255     /* Update timer information with machineToWatch */
256 
257     String request = "REGISTER TYPE " + reqHandler.sWatchType +
258         " KEY " + machineToWatch + " FREQUENCY " + frequency + " BYNAME";
259 
260     try
261     {
262         reqHandler.sHandle.submit(machineToWatch, "TIMER", request);
263     }
264     catch(STAFException e)
265     {
266         reqHandler.timer.log.log(STAFLog.Error,
267             "Error registering a timer with machine: " + machineToWatch +
268             " RC:" + e.rc);
269         return new STAFResult(e.rc);
270     }
271 
272     return new STAFResult(STAFResult.Ok);
273 }
274 }
275