00001 /* 00002 * Copyright (c) 2003 International Computer Science Institute 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. All advertising materials mentioning features or use of this software 00014 * must display the following acknowledgement: 00015 * This product includes software developed by ICIR, the ICSI 00016 * Center for Internet Research (ICSI: the International Computer 00017 * Science Institute). 00018 * 4. Neither the name of ICIR nor of ICSI may be used 00019 * to endorse or promote products derived from this software without 00020 * specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY ICSI AND CONTRIBUTORS ``AS IS'' AND 00023 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00025 * ARE DISCLAIMED. IN NO EVENT SHALL ICSI OR CONTRIBUTORS BE LIABLE 00026 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00027 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00028 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00029 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00031 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00032 * SUCH DAMAGE. 00033 * 00034 */ 00035 00036 00037 #include "connector.h" 00038 #include "random.h" 00039 #include "timer-handler.h" 00040 #include "ranvar.h" 00041 00042 class Delayer; 00043 00044 /* handler for Delayer's target (link) */ 00045 class TargetHandler : public Handler { 00046 public: 00047 inline TargetHandler(Delayer& d) : delayer_(d) {} 00048 inline void handle(Event*); 00049 private: 00050 Delayer& delayer_; 00051 }; 00052 00053 /* timer for channel allocation delay */ 00054 class AllocTimer : public TimerHandler { 00055 public: 00056 inline AllocTimer(Delayer& d) : delayer_(d) {} 00057 inline void expire(Event*); 00058 private: 00059 Delayer& delayer_; 00060 }; 00061 00062 /* timer for delay spikes */ 00063 class SpikeTimer : public TimerHandler { 00064 public: 00065 inline SpikeTimer(Delayer& d) : delayer_(d) {} 00066 inline void expire(Event*); 00067 private: 00068 Delayer& delayer_; 00069 }; 00070 00071 class Delayer : public Connector { 00072 public: 00073 Delayer(); 00074 int command(int argc, const char*const* argv); 00075 void recv(Packet* p, Handler* h); 00076 void try_send(); 00077 inline void freeTarget() {target_free = 1;} 00078 inline void freeAlloc() {alloc_free = 1;} 00079 inline void freeSpike() {spike_free = 1;} 00080 inline void takeSpike() {spike_free = 0;} 00081 int getSpike() {return spike_free;} 00082 double getNextSpikeLen () {return spike_len->value(); } 00083 double getNextSpikeInt () {return spike_int->value(); } 00084 00085 private: 00086 double last_sent; /* time a packet has last left the queue */ 00087 RandomVariable *alloc_int; /* time of keeping channel without data */ 00088 RandomVariable *alloc_len; /* delay to allocate a new channel */ 00089 int alloc_free; /* are we in channel allocation delay? */ 00090 AllocTimer at_; 00091 00092 RandomVariable *spike_int; /* interval between delay spikes */ 00093 RandomVariable *spike_len; /* delay spike length */ 00094 int spike_free; /* are we in a delay spike? */ 00095 SpikeTimer st_; 00096 00097 int target_free; /* is the link free? */ 00098 TargetHandler th_; 00099 Handler *prev_h_; /* downstream handler */ 00100 Packet *pkt_; /* packet we store */ 00101 Event e; /* something to give to prev_h_ */ 00102 }; 00103 00104 static class DelayerClass : public TclClass { 00105 public: 00106 DelayerClass() : TclClass("Delayer") {} 00107 TclObject* create(int, const char*const*) { 00108 return (new Delayer()); 00109 } 00110 } delayer_class;