1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
package uk.ac.ukc.iscream.client.alerters; |
2 |
> |
package uk.org.iscream.client.alerters; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
< |
import uk.ac.ukc.iscream.client.*; |
6 |
< |
import uk.ac.ukc.iscream.core.*; |
7 |
< |
import uk.ac.ukc.iscream.util.*; |
8 |
< |
import uk.ac.ukc.iscream.componentmanager.*; |
5 |
> |
import uk.org.iscream.client.*; |
6 |
> |
import uk.org.iscream.core.*; |
7 |
> |
import uk.org.iscream.util.*; |
8 |
> |
import uk.org.iscream.componentmanager.*; |
9 |
|
|
10 |
|
import java.io.*; |
11 |
|
import java.net.*; |
21 |
|
* @author $Author$ |
22 |
|
* @version $Id$ |
23 |
|
*/ |
24 |
< |
public class IRC__Alerter implements PluginAlerter { |
24 |
> |
public class IRC__Alerter extends Thread implements PluginAlerter { |
25 |
|
|
26 |
|
//---FINAL ATTRIBUTES--- |
27 |
|
|
52 |
|
|
53 |
|
// connect to the IRC server |
54 |
|
_ircbot = new IRCBot(); |
55 |
+ |
// set it's name and start it |
56 |
+ |
_ircbot.setName("client.IRC__Alerter$IRCBot"); |
57 |
|
_ircbot.start(); |
58 |
|
_startTime = System.currentTimeMillis(); |
59 |
< |
|
59 |
> |
this.start(); |
60 |
|
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
61 |
|
} |
62 |
|
|
63 |
|
//---PUBLIC METHODS--- |
64 |
|
|
65 |
+ |
public void run() { |
66 |
+ |
while(_running) { |
67 |
+ |
try { |
68 |
+ |
sendAlert((Alert) getQueue().get(getQueueId())); |
69 |
+ |
} catch (InvalidQueueException e) { |
70 |
+ |
_logger.write(this.toString(), Logger.ERROR, "Unable to get queue."); |
71 |
+ |
} |
72 |
+ |
} |
73 |
+ |
} |
74 |
+ |
|
75 |
|
public void sendAlert(Alert alert) { |
76 |
|
// only send alerts if we're active |
77 |
|
if(_active) { |
89 |
|
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
90 |
|
String alertType = Alert.alertLevels[alert.getLevel()]; |
91 |
|
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
92 |
+ |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
93 |
+ |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
94 |
|
// sort out the message |
95 |
|
String message; |
96 |
|
try { |
106 |
|
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
107 |
|
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
108 |
|
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
109 |
< |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
109 |
> |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
110 |
> |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
111 |
> |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
112 |
|
|
113 |
|
// send the message |
114 |
|
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
118 |
|
_alertCount ++; |
119 |
|
} |
120 |
|
} |
121 |
+ |
else { |
122 |
+ |
_ignoredCount ++; |
123 |
+ |
} |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
128 |
|
* method to provide clean logging (every class should have this). |
129 |
|
* |
130 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
130 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
131 |
|
* to format the toString() |
132 |
|
* |
133 |
|
* @return the name of this class and its CVS revision |
148 |
|
|
149 |
|
//---PRIVATE METHODS--- |
150 |
|
|
151 |
< |
private String getTimeString(long time) { |
152 |
< |
String timeString = null; |
153 |
< |
if (time >= 60) { |
154 |
< |
timeString = (time / 60) + " minute(s)"; |
155 |
< |
} else if (time >= 3600) { |
156 |
< |
timeString = ((time/60) / 60) + " hour(s)"; |
157 |
< |
} else { |
158 |
< |
timeString = time + " second(s)"; |
151 |
> |
//---ACCESSOR/MUTATOR METHODS--- |
152 |
> |
|
153 |
> |
protected Queue getQueue() { |
154 |
> |
return AlerterManager.getInstance().getQueue(); |
155 |
> |
} |
156 |
> |
|
157 |
> |
protected int getQueueId() { |
158 |
> |
if (_qID == -1) { |
159 |
> |
_qID = getQueue().getQueue(); |
160 |
> |
_logger.write(toString(), Logger.DEBUG, "Assigned Queue - " + _qID); |
161 |
|
} |
162 |
< |
return timeString; |
162 |
> |
return _qID; |
163 |
|
} |
164 |
|
|
144 |
– |
//---ACCESSOR/MUTATOR METHODS--- |
145 |
– |
|
165 |
|
//---ATTRIBUTES--- |
166 |
|
|
167 |
|
/** |
187 |
|
/** |
188 |
|
* Number of alerts sent |
189 |
|
*/ |
190 |
< |
private long _alertCount = 0; |
190 |
> |
private int _alertCount = 0; |
191 |
|
|
192 |
|
/** |
193 |
+ |
* Number of alerts ignored when in "stopped" mode |
194 |
+ |
*/ |
195 |
+ |
private int _ignoredCount = 0; |
196 |
+ |
|
197 |
+ |
/** |
198 |
|
* Time of IRCBot startup |
199 |
|
*/ |
200 |
|
private long _startTime; |
201 |
|
|
202 |
|
/** |
203 |
+ |
* The running status of the alerter |
204 |
+ |
*/ |
205 |
+ |
private boolean _running = true; |
206 |
+ |
|
207 |
+ |
/** |
208 |
|
* This is the friendly identifier of the |
209 |
|
* component this class is running in. |
210 |
|
* eg, a Filter may be called "filter1", |
220 |
|
* system logger that is being used. |
221 |
|
*/ |
222 |
|
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
223 |
+ |
|
224 |
+ |
/** |
225 |
+ |
* The queue id for this alerters queue in the alert queue |
226 |
+ |
*/ |
227 |
+ |
private int _qID = -1; |
228 |
|
|
229 |
|
//---STATIC ATTRIBUTES--- |
230 |
|
|
464 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
465 |
|
* method to provide clean logging (every class should have this). |
466 |
|
* |
467 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
467 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
468 |
|
* to format the toString() |
469 |
|
* |
470 |
|
* @return the name of this class and its CVS revision |
551 |
|
endOfChan = newChan.length(); |
552 |
|
} |
553 |
|
newChan = newChan.substring(0, endOfChan); |
554 |
< |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
555 |
< |
_socketOut.println("PART "+_channel); |
556 |
< |
_socketOut.println("JOIN "+newChan); |
557 |
< |
_channel = newChan; |
554 |
> |
if(newChan.equals(_channel)) { |
555 |
> |
sendMsg(getMsgSender(line)+", I'm already on "+newChan+"!"); |
556 |
> |
} else { |
557 |
> |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
558 |
> |
_socketOut.println("PART "+_channel); |
559 |
> |
_socketOut.println("JOIN "+newChan); |
560 |
> |
_channel = newChan; |
561 |
> |
} |
562 |
|
} |
563 |
|
else if(message.indexOf(nickChangeCommand)!=-1) { |
564 |
|
String nickChangeCmd = nickChangeCommand; |
589 |
|
sendPrivMsg(getMsgSender(line), helpCommand); |
590 |
|
} |
591 |
|
else if(message.indexOf(statCommand)!=-1) { |
592 |
< |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts!"); |
592 |
> |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
593 |
|
} |
594 |
|
else if(message.indexOf(uptimeCommand)!=-1) { |
595 |
|
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
596 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
597 |
|
sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); |
598 |
+ |
} |
599 |
+ |
else if(message.indexOf("ping")!=-1) { |
600 |
+ |
sendMsg("pong"); |
601 |
|
} |
602 |
|
else if(message.indexOf("do a jibble dance")!=-1) { |
603 |
|
// little joke :) |