Example usage for org.joda.time DateTime now

List of usage examples for org.joda.time DateTime now

Introduction

In this page you can find the example usage for org.joda.time DateTime now.

Prototype

public static DateTime now() 

Source Link

Document

Obtains a DateTime set to the current system millisecond time using ISOChronology in the default time zone.

Usage

From source file:com.almende.eve.algorithms.test.agents.DAAAgent.java

License:Apache License

/**
 * Sets the initial DAA value.// w w w  .ja va2 s.  c  o  m
 *
 * @param value
 *            the new initial value
 */
public void start(final double value) {
    final ObjectNode config = JOM.createObjectNode();
    config.put("width", 1000);
    config.put("initialTTL", 10);
    config.put("evictionFactor", 20);

    config.put("intervalFactor", 5);
    config.put("intervalMin", 200);
    config.put("redundancyFactor", 9999);

    daa.configure(config);
    daa.setNewValue(value);

    trickle = new TrickleRPC(config, getScheduler(), new Runnable() {
        @Override
        public void run() {
            daa.getLocalValue().setTTL(DateTime.now().plus((long) (trickle.getDelay() * 3.2)).getMillis());
        }
    }, new Runnable() {
        @Override
        public void run() {
            final Params params = new Params();
            params.add("value", daa.getCurrentEstimate());
            for (final Edge agent : getGraph().getByTag("daa")) {
                try {
                    call(agent.getAddress(), "daaReceive", params);
                } catch (IOException e) {
                }
            }
        }
    });

}

From source file:com.almende.eve.algorithms.TrickleRPC.java

License:Apache License

private void reschedule(final long[] intervals) {
    if (intervals != null && intervals.length > 0 && intervals[0] >= 0 && intervals[1] >= 0) {
        final DateTime nextSend = DateTime.now().plus(intervals[0]);
        final DateTime nextInterval = DateTime.now().plus(intervals[1]);

        final String oldSendTaskId = sendTaskId;
        if (oldSendTaskId != null) {
            scheduler.cancel(oldSendTaskId);
        }//  w ww .j  a  v  a 2  s  . co  m
        sendTaskId = scheduler.schedule(null, REQUESTS.get(namespace + "send"), nextSend);

        final String oldIntTaskId = intTaskId;
        if (oldIntTaskId != null) {
            scheduler.cancel(oldIntTaskId);
        }
        intTaskId = scheduler.schedule(null, REQUESTS.get(namespace + "nextInterval"), nextInterval);
    }
}

From source file:com.almende.eve.demo.TipsNTricksAgent.java

License:Apache License

/**
 * Schedule parallel./* ww w  .  jav a2  s  . co  m*/
 */
public void scheduleSpecific() {
    schedule("scheduleSpecific", null, DateTime.now().plus(1000 - DateTime.now().millisOfSecond().get()));
    onInterval();
}

From source file:com.almende.eve.ggdemo.DAALampAgent.java

License:Apache License

@Override
public void create(List<String> neighbours, Integer stepSize) throws JSONRPCException, IOException {
    super.create(neighbours, stepSize);
    final ObjectNode config = JOM.createObjectNode();
    config.put("width", 1000);
    config.put("initialTTL", 10);
    config.put("evictionFactor", 20);

    config.put("intervalFactor", 5);
    config.put("intervalMin", 200);
    config.put("redundancyFactor", 999);

    lampCnt.configure(config);//ww  w.j  a v  a2s  .  c  o  m
    lampCnt.setNewValue(1.0);

    onCnt.configure(config);
    onCnt.setNewValue(super.isOn() ? 1.0 : 0.0);

    final List<String> nbs = neighbours;
    trickle = new TrickleRPC(config, getScheduler(), new Runnable() {
        @Override
        public void run() {
            //checkLamps();
            final long delay = (long) (trickle.getDelay() * 3.2);
            lampCnt.getLocalValue().setTTL(DateTime.now().plus(delay).getMillis());
            onCnt.getLocalValue().setTTL(DateTime.now().plus(delay).getMillis());
        }
    }, new Runnable() {
        @Override
        public void run() {
            for (final String neighbour : nbs) {
                Params params = new Params();
                params.add("nofLamps", lampCnt.getCurrentEstimate());
                params.add("nofOn", onCnt.getCurrentEstimate());
                try {
                    call(URIUtil.create(neighbour), "daaReceive", params);
                } catch (IOException e) {
                }
            }
        }
    });
    scheduleLamps();
}

From source file:com.almende.eve.monitor.Cache.java

License:Apache License

/**
 * Store value and set storage time to now();.
 * /*  ww  w . j  ava 2 s  . c  om*/
 * @param value
 *            the value
 */
public void store(final Object value) {
    stored = DateTime.now();
    this.value = value;
}

From source file:com.almende.eve.scheduler.clock.RunnableClock.java

License:Apache License

@Override
public void run() {
    synchronized (TIMELINE) {
        while (!TIMELINE.isEmpty()) {
            final ClockEntry ce = TIMELINE.firstEntry().getValue();
            if (future != null) {
                future.cancel(false);/*from w  w  w. j a  v  a2  s. c  om*/
                future = null;
            }
            final DateTime now = DateTime.now();
            if (ce.getDue().isBefore(now)) {
                TIMELINE.remove(ce);
                POOL.execute(ce.getCallback());
                continue;
            }
            final long interval = new Interval(now, ce.getDue()).toDurationMillis();
            future = POOL.schedule(this, interval, TimeUnit.MILLISECONDS);
            break;
        }
    }
}

From source file:com.almende.eve.scheduling.SimpleScheduler.java

License:Apache License

@Override
public String schedule(Object msg, int delay) {
    return schedule(msg, DateTime.now().plus(delay));
}

From source file:com.almende.eve.scheduling.SyncScheduler.java

License:Apache License

/**
 * Sync with peer./*from www. java 2  s  . c  o m*/
 *
 * @param peer
 *            the peer
 * @return the long
 */
@Access(AccessType.PUBLIC)
public SyncTupple syncWithPeer(final URI peer) {
    if (caller == null) {
        LOG.warning("Sync requested, but caller is still null, invalid!");
        return null;
    }
    LOG.info("Starting sync with: " + peer + "!");
    final DateTime start = DateTime.now();
    try {
        final Long result = caller.callSync(peer, "syncScheduler.ping", JOM.createObjectNode(), Long.class);
        final long roundtrip = new Duration(start, DateTime.now()).getMillis();
        final long offset = result - now() + (roundtrip / 2);
        LOG.info("Sync resulted in offset:" + offset + " ( " + roundtrip + ":" + start + ":" + result + ")");
        return new SyncTupple(offset, roundtrip);
    } catch (IOException e) {
        LOG.log(Level.WARNING, "failed to send ping", e);
    }
    return null;
}

From source file:com.almende.eve.scheduling.SyncScheduler.java

License:Apache License

/**
 * Sync./*from www.  java2  s  . c o  m*/
 */
@Access(AccessType.PUBLIC)
public void sync() {
    synchronized (active) {
        if (active) {
            return;
        }
        active = true;
    }
    try {
        for (final URI peer : peers) {
            LOG.info("Doing sync with " + peer + "!");

            final ArrayList<SyncTupple> results = new ArrayList<SyncTupple>(5);
            final int[] fail = new int[1];
            fail[0] = 0;
            getClock().requestTrigger(new UUID().toString(), DateTime.now(), new Runnable() {
                @Override
                public void run() {
                    final SyncTupple res = syncWithPeer(peer);
                    if (res != null) {
                        results.add(res);
                    } else {
                        fail[0]++;
                    }
                    if (fail[0] < 5 && results.size() < 5) {
                        getClock().requestTrigger(new UUID().toString(),
                                DateTime.now().plus((long) (4000 * Math.random())), this);
                    }
                }
            });
            while (fail[0] < 5 && results.size() < 5) {
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                }
            }
            long sum = 0;
            for (int i = 0; i < results.size(); i++) {
                sum += results.get(i).roundtrip;
            }
            long mean = sum / results.size();

            sum = 0;
            for (int i = 0; i < results.size(); i++) {
                sum += Math.pow(results.get(i).roundtrip - mean, 2);
            }

            double stdDev = Math.sqrt(sum / results.size());
            double limit = stdDev + mean;
            LOG.warning("Mean:" + mean + " stdDev:" + stdDev + " limit:" + limit);

            sum = 0;
            int count = 0;
            for (SyncTupple tupple : results) {
                if (tupple.roundtrip > limit) {
                    LOG.warning("Skipping tupple:" + tupple);
                    continue;
                }
                LOG.warning("Adding tupple:" + tupple);
                count++;
                sum += tupple.offset;
            }

            offset += sum / count;
            LOG.info("Done sync with " + peer + "! new offset:" + offset + "(" + sum / count + ")");
        }
    } catch (Exception e) {
        LOG.log(Level.WARNING, "TimeSync failed", e);
    }
    synchronized (active) {
        active = false;
    }
    getClock().requestTrigger(new UUID().toString(),
            new DateTime(now()).plus((long) (syncInterval * Math.random())), new Runnable() {
                @Override
                public void run() {
                    try {
                        sync();
                    } catch (Exception e) {
                        LOG.log(Level.WARNING, "sync failed", e);
                    }
                }
            });

}

From source file:com.almende.eve.transport.tokens.TokenStore.java

License:Apache License

/**
 * Creates the./*from w w  w.  j  av  a  2  s .  c  o m*/
 * 
 * @return the token ret
 */
public TokenRet create() {
    TokenRet result;
    if (tokens.size() == 0 || tokens.get(last.toString()) == null || last.plus(3600000).isBeforeNow()) {
        final DateTime now = DateTime.now();
        final String token = new UUID().toString();
        result = new TokenRet(token, now);
        tokens.put(now.toString(), token);
        last = now;

        if (tokens.size() > SIZE + 2) {
            DateTime oldest = last;
            for (final String time : tokens.keySet()) {
                try {
                    if (DateTime.parse(time).isBefore(oldest)) {
                        oldest = DateTime.parse(time);
                    }
                } catch (final Exception e) {
                    LOG.log(Level.WARNING, "Failed in eviction of tokens:", e);
                }
            }
            tokens.remove(oldest.toString());
        }
    } else {
        result = new TokenRet(tokens.get(last.toString()), last);
    }
    return result;
}