/** * Calls {@link #add(int)} or {@link #remove} with the parameters defined by the configuration. * * @return always false */ public final boolean execute() { if (add == 0) return false; if (!substitute) { if ((maxsize <= Network.size() && add > 0) || (minsize >= Network.size() && add < 0)) return false; } int toadd = 0; int toremove = 0; if (add > 0) { toadd = (int) Math.round(add < 1 ? add * Network.size() : add); if (!substitute && toadd > maxsize - Network.size()) toadd = maxsize - Network.size(); if (substitute) toremove = toadd; } else if (add < 0) { toremove = (int) Math.round(add > -1 ? -add * Network.size() : -add); if (!substitute && toremove > Network.size() - minsize) toremove = Network.size() - minsize; if (substitute) toadd = toremove; } remove(toremove); add(toadd); return false; }
/** * Schedules the protocol at given node for the first execution adding it to the priority queue of * the event driven simulation. The time of the first execution is determined by a reference point * in time and {@link #firstDelay}, which defines the delay from the reference point. The * reference point is the maximum of the current time, and the value of parameter {@value * peersim.core.Scheduler#PAR_FROM} of the protocol being scheduled. If the calculated time of the * first execution is not valid according to the schedule of the protocol then no execution is * scheduled for that protocol. * * <p>A final note: for performance reasons, the recommended practice is not to use parameter * {@value peersim.core.Scheduler#PAR_FROM} in protocols, but to schedule {@link CDScheduler} * itself for the desired time, whenever possible (e.g., it is not possible if {@link CDScheduler} * is used as a {@link NodeInitializer}). */ public void initialize(Node n) { /*XXX * If "from" is not the current time and this is used as a control (not node * initializer) then we dump _lots_ of events in the queue * that are just stored there until "from" comes. This reduces performance, * and should be fixed. When fixed, the final comment can be removed from the * docs. */ final long time = CommonState.getTime(); for (int i = 0; i < pid.length; ++i) { Object nceclone = null; try { nceclone = nce[i].clone(); } catch (CloneNotSupportedException e) { } // cannot possibly happen final long delay = firstDelay(sch[pid[i]].step); final long nexttime = Math.max(time, sch[pid[i]].from) + delay; if (nexttime < sch[pid[i]].until) EDSimulator.add(nexttime - time, nceclone, n, pid[i]); } }