PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/stable/src/drcl/inet/core/queue/PriorityQueue.java

http://j-sim.googlecode.com/
Java | 351 lines | 233 code | 36 blank | 82 comment | 74 complexity | 3f52463291a4280bd2f2afa5d18192ef MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // @(#)PriorityQueue.java 12/2003
  2. // Copyright (c) 1998-2003, Distributed Real-time Computing Lab (DRCL)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // 1. Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // 2. Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // 3. Neither the name of "DRCL" nor the names of its contributors may be used
  14. // to endorse or promote products derived from this software without specific
  15. // prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  21. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. //
  28. package drcl.inet.core.queue;
  29. import java.util.Vector;
  30. import drcl.data.*;
  31. import drcl.util.queue.*;
  32. import drcl.comp.*;
  33. import drcl.comp.queue.QLogic;
  34. import drcl.net.*;
  35. import drcl.inet.InetPacketInterface;
  36. /**
  37. * PriorityQueue is an <i>m</i>-level queue. <i>m</i> is configurable.
  38. * Specifically, it consists of <i>m</i> FIFO queues, each associated with
  39. * a queue logic.
  40. * The level-0 queue has the highest priority
  41. * while the level-<code>(<i>m</i>-1)</code> queue has the lowest.
  42. * It uses a {@link drcl.net.PktClassifier} to
  43. * classify the incoming packets into <i>m</i> levels. When a packet arrives,
  44. * the packet is classified into one of the <i>m</i> level and is put in that
  45. * FIFO queue if the corresponding queue logic permits.
  46. *
  47. * The component treats a packet as lowest priority if no level of queue
  48. * is found for the packet.
  49. */
  50. public class PriorityQueue extends drcl.inet.core.Queue
  51. implements drcl.net.PktClassifier
  52. {
  53. /** Name of the port that exports the instant queue size change events. */
  54. public static final String EVENT_QSIZE_PORT_ID = ".q";
  55. public static final String EVENT_QLEN = "Instant Q Length";
  56. protected Port qLenPort = addEventPort(".q");
  57. /** The real queue that holds packets. */
  58. VSFIFOQueue[] qq = null;
  59. /** Levels of queue logics. */
  60. QLogic[] qlogics;
  61. PktClassifier classifier = this;
  62. int capacity = Integer.MAX_VALUE;
  63. int currentSize = 0;
  64. public PriorityQueue()
  65. { super(); }
  66. public PriorityQueue(String id_)
  67. { super(id_); }
  68. /**
  69. * Implements {@link drcl.net.PktClassifier#classify(drcl.net.Packet)}.
  70. * It simply return (1 - the first bit of the ToS field of the INET header)
  71. * so CONTROL packets have higher priority than DATA packets.
  72. */
  73. public int classify(Packet p_)
  74. {
  75. if (p_ instanceof InetPacketInterface)
  76. return 1 - (int) ((InetPacketInterface)p_).getToS() & 01;
  77. else
  78. return 0;
  79. }
  80. public void setClassifier(PktClassifier pc_)
  81. { classifier = pc_; }
  82. public void reset()
  83. {
  84. super.reset();
  85. if (qq != null)
  86. for (int i=0; i<qq.length; i++)
  87. qq[i].reset();
  88. if (qlogics != null) {
  89. for (int i=0; i<qlogics.length; i++)
  90. if (qlogics[i] != null)
  91. qlogics[i].reset();
  92. }
  93. currentSize = 0;
  94. }
  95. public void duplicate(Object source_)
  96. {
  97. super.duplicate(source_);
  98. PriorityQueue that_ = (PriorityQueue)source_;
  99. classifier = that_.classifier; // FIXME:??
  100. capacity = that_.capacity;
  101. if (that_.qq != null) setLevels(that_.qq.length);
  102. if (that_.qlogics == null) qlogics = null;
  103. else {
  104. try {
  105. qlogics = new QLogic[that_.qlogics.length];
  106. for (int i=0; i<qlogics.length; i++) {
  107. QLogic thatqlogic_ = that_.qlogics[i];
  108. if (thatqlogic_ == null) {
  109. setQLogic(i, null); continue;
  110. }
  111. QLogic qlogic_ = (QLogic)thatqlogic_.getClass().newInstance();
  112. // must sethost() before calling qlogic.duplicate() so that
  113. // the event ports are also duplicated
  114. qlogic_.setHost(this);
  115. qlogic_.duplicate(thatqlogic_);
  116. setQLogic(i, qlogic_);
  117. }
  118. } catch (Exception e_) {
  119. drcl.Debug.error(e_);
  120. e_.printStackTrace();
  121. }
  122. }
  123. }
  124. public String info(String prefix_)
  125. { return info(prefix_, false); }
  126. public String info(String prefix_, boolean listContent_)
  127. {
  128. try {
  129. StringBuffer sb_ = new StringBuffer(super.info(prefix_) + prefix_
  130. + "Classifier:" + classifier + "\n");
  131. if (qlogics == null)
  132. sb_.append(prefix_ + "No queue logic is installed.\n");
  133. else {
  134. sb_.append(prefix_ + "QueueLogics: # of levels=" + qlogics.length
  135. + "\n");
  136. for (int i=0; i<qlogics.length; i++) {
  137. if (qlogics[i] == null) continue;
  138. sb_.append(prefix_ + " Level" + i + ":\n"
  139. + qlogics[i].info(prefix_ + " "));
  140. }
  141. }
  142. if (qq == null || qq.length == 0)
  143. sb_.append(prefix_ + "No queue is installed.\n");
  144. else {
  145. for (int i=0; i<qq.length; i++)
  146. sb_.append(prefix_ + "Queue" + i + ": "
  147. + qq[i].info("", listContent_) + "\n");
  148. }
  149. return sb_.toString();
  150. }
  151. catch (Exception e_) {
  152. e_.printStackTrace();
  153. return null;
  154. }
  155. }
  156. public Queue getQueue(int level_)
  157. {
  158. if (qq == null || level_ < 0 || qq.length <= level_)
  159. return null;
  160. else
  161. return qq[level_];
  162. }
  163. // make sure array has a spot at the level_
  164. void _assureLogics(int level_)
  165. {
  166. if (level_ < 0) throw new IndexOutOfBoundsException("negative index:" + level_);
  167. if (qlogics == null) qlogics = new QLogic[level_ + 1];
  168. if (qlogics.length > level_) return;
  169. // create a larger array
  170. QLogic[] tmp_ = new QLogic[level_ + 1];
  171. System.arraycopy(qlogics, 0, tmp_, 0, qlogics.length);
  172. qlogics = tmp_;
  173. if (qq.length <= level_) {
  174. VSFIFOQueue[] tmp2_ = new VSFIFOQueue[level_ + 1];
  175. System.arraycopy(qq, 0, tmp2_, 0, qq.length);
  176. for (int i=qq.length; i<=level_; i++)
  177. qq[i] = new VSFIFOQueue();
  178. qq = tmp2_;
  179. }
  180. }
  181. /**
  182. * @param level_ level index of the queue logic.
  183. */
  184. public void setQLogic(int level_, QLogic qlogic_)
  185. {
  186. _assureLogics(level_);
  187. qlogics[level_] = qlogic_;
  188. }
  189. public void setLevels(int nlevels_)
  190. {
  191. qq = new VSFIFOQueue[nlevels_];
  192. for (int i=0; i<qq.length; i++)
  193. qq[i] = new VSFIFOQueue();
  194. }
  195. public QLogic getQLogic(int level_)
  196. { return qlogics[level_]; }
  197. /**
  198. * Enqueues the object at the end of the queue
  199. * @return the object being dropped due to the enqueue; null otherwise.
  200. */
  201. public Object enqueue(Object obj_)
  202. {
  203. Packet p_ = (Packet)obj_;
  204. int psize_ = isByteMode()? p_.size: 1;
  205. int level_ = classifier.classify(p_);
  206. if (level_ < 0 || level_ >= qq.length)
  207. level_ = qq.length-1; // lowest priority
  208. if (currentSize + psize_ > capacity) {
  209. if (isGarbageEnabled()) drop(p_, "buffer overflow");
  210. // do we need to do this?
  211. if (qlogics != null && qlogics.length > level_
  212. && qlogics[level_] != null)
  213. qlogics[level_].dropHandler(p_, psize_);
  214. return null;
  215. }
  216. if (qlogics != null && qlogics.length > level_
  217. && qlogics[level_] != null) {
  218. String advice_ = qlogics[level_].adviceOn(p_, psize_);
  219. if (advice_ != null) {
  220. if (isGarbageEnabled()) drop(p_, advice_);
  221. qlogics[level_].dropHandler(p_, psize_);
  222. return null;
  223. }
  224. }
  225. qq[level_].enqueue(p_);
  226. currentSize += psize_;
  227. if (qLenPort._isEventExportEnabled())
  228. qLenPort.exportEvent(EVENT_QLEN, (double)currentSize, null);
  229. return null;
  230. }
  231. /**
  232. * Dequeues and returns the first object in the queue.
  233. * @return the object dequeued; null if queue is empty.
  234. */
  235. public Object dequeue()
  236. {
  237. for (int i=0; i<qq.length; i++) {
  238. VSFIFOQueue q = qq[i];
  239. if (q.isEmpty()) continue;
  240. Packet p_ = (Packet) q.dequeue();
  241. int psize_ = isByteMode()? p_.size: 1;
  242. if (qlogics != null && qlogics.length > i && qlogics[i] != null)
  243. qlogics[i].dequeueHandler(p_, psize_);
  244. currentSize -= psize_;
  245. if (qLenPort._isEventExportEnabled())
  246. qLenPort.exportEvent(EVENT_QLEN, (double)currentSize, null);
  247. return p_;
  248. }
  249. return null;
  250. }
  251. /**
  252. * Retrieves but not remove the object at the position specified.
  253. * @return the object; null if position is not valid.
  254. */
  255. public Object peekAt(int pos_)
  256. {
  257. for (int i=0; i<qq.length; i++) {
  258. VSFIFOQueue q = qq[i];
  259. if (q.getSize() <= pos_) {
  260. pos_ -= q.getSize();
  261. continue;
  262. }
  263. else
  264. return q.retrieveAt(pos_);
  265. }
  266. return null;
  267. }
  268. /**
  269. * Retrieves but not dequeue the first object in the queue.
  270. * @return the object; null if queue is empty.
  271. */
  272. public Object firstElement()
  273. {
  274. for (int i=0; i<qq.length; i++) {
  275. VSFIFOQueue q = qq[i];
  276. if (q.isEmpty()) continue;
  277. return q.firstElement();
  278. }
  279. return null;
  280. }
  281. /**
  282. * Retrieves but not remove the last object in the queue.
  283. * @return the object; null if queue is empty.
  284. */
  285. public Object lastElement()
  286. {
  287. for (int i=qq.length-1; i>=0; i--) {
  288. VSFIFOQueue q = qq[i];
  289. if (q.isEmpty()) continue;
  290. return q.lastElement();
  291. }
  292. return null;
  293. }
  294. /** Return true if the queue is full. */
  295. public boolean isFull()
  296. { return currentSize >= capacity; }
  297. /** Return true if the queue is empty. */
  298. public boolean isEmpty()
  299. {
  300. for (int i=0; i<qq.length; i++) {
  301. VSFIFOQueue q = qq[i];
  302. if (!q.isEmpty()) return false;
  303. }
  304. return true;
  305. }
  306. /** Sets the capacity of the queue. */
  307. public void setCapacity(int capacity_)
  308. { capacity = capacity_; }
  309. /** Returns the capacity of the queue. */
  310. public int getCapacity()
  311. { return capacity; }
  312. /** Returns the current size of the queue. */
  313. public int getSize()
  314. { return currentSize; }
  315. }