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 |
|
|
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(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
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; |
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 |
|
|
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 |
|
|
153 |
– |
//---ACCESSOR/MUTATOR METHODS--- |
154 |
– |
|
165 |
|
//---ATTRIBUTES--- |
166 |
|
|
167 |
|
/** |
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 |
|
|
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; |