public static String uplink, password;
public static int port;
- private static final EventLoopGroup eventLoop = new NioEventLoopGroup(1);
+ protected static final EventLoopGroup eventLoop = new NioEventLoopGroup(1);
private static io.netty.channel.Channel channel;
public static void start(String server, int port, String name, String description, String password, String SID, boolean ssl)
// b.close();
// }
- Timer.processTimers();
-
Message m = Message.findMessage(message.getCommand());
if (m == null)
{
package net.rizon.acid.core;
+import io.netty.util.concurrent.ScheduledFuture;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
+import java.util.concurrent.TimeUnit;
import net.rizon.acid.conf.Client;
import net.rizon.acid.conf.Config;
import net.rizon.acid.sql.SQL;
au.introduce();
}
}
+
+ public static ScheduledFuture scheduleWithFixedDelay(Runnable r, long t, TimeUnit unit)
+ {
+ ScheduledFuture future = eventLoop.scheduleWithFixedDelay(r, t, t, unit);
+ FutureExceptionListener listener = new FutureExceptionListener();
+
+ listener.setRescheduleParameters(r, t, t, unit, FutureExceptionListener.ScheduleType.FIXED_DELAY);
+
+ future.addListener(listener);
+
+ return future;
+ }
+
+ public static ScheduledFuture scheduleAtFixedRate(Runnable r, long t, TimeUnit unit)
+ {
+ ScheduledFuture future = eventLoop.scheduleAtFixedRate(r, t, t, unit);
+ FutureExceptionListener listener = new FutureExceptionListener();
+
+ listener.setRescheduleParameters(r, t, t, unit, FutureExceptionListener.ScheduleType.FIXED_RATE);
+
+ future.addListener(listener);
+
+ return future;
+ }
+
+ public static ScheduledFuture schedule(Runnable r, long t, TimeUnit unit)
+ {
+ ScheduledFuture future = eventLoop.schedule(r, t, unit);
+ FutureExceptionListener listener = new FutureExceptionListener();
+
+ future.addListener(listener);
+
+ return future;
+ }
}
--- /dev/null
+package net.rizon.acid.core;
+
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.FutureListener;
+import java.util.concurrent.TimeUnit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Reports any errors when a future operation is completed and is not completed
+ * successfully or canceled.
+ * <p>
+ * @author Orillion {@literal <orillion@rizon.net>}
+ */
+public class FutureExceptionListener<T> implements FutureListener<T>
+{
+ private static final Logger logger = LoggerFactory.getLogger(FutureExceptionListener.class);
+
+ public enum ScheduleType
+ {
+ FIXED_DELAY,
+ FIXED_RATE
+ }
+
+ private boolean reschedule = false;
+ private Runnable runnable;
+ private long delay;
+ private long timer;
+ private TimeUnit timeUnit;
+ private ScheduleType scheduleType;
+
+ @Override
+ public void operationComplete(Future<T> future) throws Exception
+ {
+ if (future.isSuccess() || future.isCancelled())
+ {
+ return;
+ }
+
+ Throwable t = future.cause();
+
+ logger.error("Scheduled operation failed: " + t.toString(), t);
+
+ if (this.reschedule)
+ {
+ switch (this.scheduleType)
+ {
+ case FIXED_DELAY:
+ Acidictive.scheduleWithFixedDelay(this.runnable, this.timer, this.timeUnit);
+ break;
+ case FIXED_RATE:
+ Acidictive.scheduleAtFixedRate(this.runnable, this.timer, this.timeUnit);
+ break;
+ }
+ }
+ }
+
+ /**
+ * Sets the parameters in case this future needs to reschedule itself when
+ * it throws an exception.
+ * <p>
+ * @param r Runnable to run
+ * @param delay Initial delay
+ * @param timer Period between timings
+ * @param unit TimeUnit
+ * @param type Type of schedule to use
+ */
+ public void setRescheduleParameters(Runnable r, long delay, long timer, TimeUnit unit, ScheduleType type)
+ {
+ this.reschedule = true;
+ this.runnable = r;
+ this.delay = delay;
+ this.timer = timer;
+ this.timeUnit = unit;
+ this.scheduleType = type;
+ }
+}
+++ /dev/null
-package net.rizon.acid.core;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.Iterator;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public abstract class Timer
-{
- private static final Logger log = LoggerFactory.getLogger(Timer.class);
-
- private static ArrayList<Timer> timers = new ArrayList<Timer>();
-
- public Date creation;
- public Date tick;
- public long time_from_now;
- public boolean repeating;
- private boolean dead;
-
- public Timer(long time_from_now, boolean repeating)
- {
- this.creation = new Date();
- this.tick = new Date(System.currentTimeMillis() + (time_from_now * 1000));
- this.time_from_now = time_from_now;
- this.repeating = repeating;
- this.dead = false;
- }
-
- public void start()
- {
- timers.add(this);
- }
-
- public void stop()
- {
- this.dead = true;
- }
-
- public abstract void run(final Date now);
-
- public static void processTimers()
- {
- Date now = new Date();
-
- // Timers can be added from Timer;:run, which will modify this list.
- // Iterate by indexes instead going backward.
- for (int i = timers.size(); i > 0; --i)
- {
- Timer t = timers.get(i - 1);
-
- if (t.dead == false && now.after(t.tick))
- {
- try
- {
- t.run(now);
- }
- catch (Exception ex)
- {
- log.error("Error running timer", ex);
- }
- if (t.repeating == false)
- t.stop();
- else
- t.tick = new Date(System.currentTimeMillis() + (t.time_from_now * 1000));
- }
- }
-
- for (Iterator<Timer> it = timers.iterator(); it.hasNext();)
- {
- Timer t = it.next();
-
- if (t.dead == true)
- it.remove();
- }
- }
-}
\ No newline at end of file
package net.rizon.acid.plugins.pyva.pyva;
+import io.netty.util.concurrent.ScheduledFuture;
import java.util.Arrays;
-import java.util.Date;
+import java.util.concurrent.TimeUnit;
import net.rizon.acid.core.Acidictive;
import net.rizon.acid.core.Channel;
import net.rizon.acid.core.Event;
import net.rizon.acid.core.Plugin;
-import net.rizon.acid.core.Timer;
import net.rizon.acid.core.User;
import net.rizon.acid.plugins.pyva.pyva.conf.Config;
import net.rizon.pyva.Pyva;
}
}
-class GCTimer extends Timer
+class GCTimer implements Runnable
{
- public GCTimer()
- {
- super(60, true);
- }
-
@Override
- public void run(Date now)
+ public void run()
{
System.gc();
pyva.pyva.gc();
protected static final Logger log = LoggerFactory.getLogger(pyva.class);
private Config conf;
- private Timer t;
+ private ScheduledFuture gcTimer;
public static Pyva pyva;
private Event e;
e = new PyvaEvent();
- t = new GCTimer();
- t.start();
+ Acidictive.scheduleWithFixedDelay(new GCTimer(), 1, TimeUnit.MINUTES);
}
@Override
public void stop()
{
- t.stop();
+ gcTimer.cancel(true);
e.remove();
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
-import net.rizon.acid.core.Timer;
-class ExpireTimer extends Timer
+class ExpireTimer implements Runnable
{
- ExpireTimer()
- {
- // one day
- super(60 * 60 * 24, true);
- }
-
@Override
- public void run(Date now)
+ public void run()
{
+ Date now = new Date();
+
for (Iterator<Map.Entry<String, TrappedUser>> it = trapbot.users.entrySet().iterator(); it.hasNext();)
{
Map.Entry<String, TrappedUser> e = it.next();
package net.rizon.acid.plugins.trapbot;
-import java.util.Date;
-import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import net.rizon.acid.core.Acidictive;
import net.rizon.acid.core.Message;
import net.rizon.acid.core.Protocol;
-import net.rizon.acid.core.Timer;
-class ReleaseTimer extends Timer
+class ReleaseTimer implements Runnable
{
- ReleaseTimer()
- {
- // XXX magic?
- super(new Random().nextInt(2700) + 2700, false);
- }
-
@Override
- public void run(final Date now)
+ public void run()
{
Protocol.privmsg(trapbot.trapbot.getUID(), trapbot.getTrapChanName(), Message.BOLD + "YOU HAVE 1 MINUTE TO PART THE CHANNEL, GET OUT WHILE YOU CAN!" + Message.BOLD);
trapbot.enforce = false;
trapbot.releaseTimer = null;
- trapbot.retrapTimer = new RetrapTimer(60, false);
- trapbot.retrapTimer.start();
+ trapbot.retrapTimer = Acidictive.schedule(new RetrapTimer(), 1, TimeUnit.MINUTES);
}
}
package net.rizon.acid.plugins.trapbot;
-import java.util.Date;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import net.rizon.acid.core.Acidictive;
import net.rizon.acid.core.Message;
import net.rizon.acid.core.Protocol;
-import net.rizon.acid.core.Timer;
-class RetrapTimer extends Timer
+class RetrapTimer implements Runnable
{
- RetrapTimer(long time_from_now, boolean repeating)
- {
- super(time_from_now, repeating);
- }
-
/*
* release users
*/
@Override
- public void run(final Date now)
+ public void run()
{
/* start enforcing again and announce it */
trapbot.enforce = true;
Protocol.privmsg(trapbot.trapbot.getUID(), trapbot.getTrapChanName(), Message.BOLD + "HAHA TRAPPED AGAIN!" + Message.BOLD);
- trapbot.releaseTimer = new ReleaseTimer();
- trapbot.releaseTimer.start();
+ // magic here, too
+ trapbot.releaseTimer = Acidictive.schedule(new ReleaseTimer(), new Random().nextInt(2700) + 2700, TimeUnit.SECONDS);
}
}
package net.rizon.acid.plugins.trapbot;
+import io.netty.util.concurrent.ScheduledFuture;
import java.util.HashMap;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
import net.rizon.acid.core.AcidCore;
import net.rizon.acid.core.AcidUser;
import net.rizon.acid.core.Acidictive;
import net.rizon.acid.core.Event;
import net.rizon.acid.core.Plugin;
-import net.rizon.acid.core.Timer;
import net.rizon.acid.core.User;
import net.rizon.acid.plugins.trapbot.conf.Config;
import org.slf4j.Logger;
private Event ev;
public static AcidUser trapbot;
public static Config conf;
- private Timer expireTimer;
- public static Timer retrapTimer;
- public static Timer releaseTimer;
+ private ScheduledFuture expireTimer;
+ public static ScheduledFuture retrapTimer;
+ public static ScheduledFuture releaseTimer;
// ip -> user
public static HashMap<String, TrappedUser> users = new HashMap<String, TrappedUser>();
public static boolean enforce = true;
reload();
- expireTimer = new ExpireTimer();
- expireTimer.start();
+ expireTimer = Acidictive.scheduleWithFixedDelay(new ExpireTimer(), 1, TimeUnit.DAYS);
- releaseTimer = new ReleaseTimer();
- releaseTimer.start();
+ // magic!
+ releaseTimer = Acidictive.schedule(new ReleaseTimer(), new Random().nextInt(2700) + 2700, TimeUnit.SECONDS);
}
@Override
ev.remove();
if (expireTimer != null)
- expireTimer.stop();
+ expireTimer.cancel(true);
if (releaseTimer != null)
- releaseTimer.stop();
+ releaseTimer.cancel(true);
if (retrapTimer != null)
- retrapTimer.stop();
+ retrapTimer.cancel(true);
}
}