1 /*
2  * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
3  * California. All rights reserved.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation for any purpose, without fee, and without written agreement is
7  * hereby granted, provided that the above copyright notice and the following
8  * two paragraphs appear in all copies of this software.
9  *
10  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
11  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
12  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
13  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20  *
21  * Author: Matt Welsh <mdw@cs.berkeley.edu>
22  *
23  */
24 
25 package seda.sandStorm.internal;
26 
27 import seda.sandStorm.api.*;
28 import seda.sandStorm.api.internal.*;
29 import seda.sandStorm.core.*;
30 import java.util.*;
31 
32 /**
33  * A StageWrapper is a basic implementation of StageWrapperIF for
34  * application-level stages.
35  *
36  * @author   Matt Welsh
37  */
38 
39 class StageWrapper implements StageWrapperIF {
40 
41   private String name;
42   private StageIF stage;
43   private EventHandlerIF handler;
44   private ConfigDataIF config;
45   private FiniteQueue eventQ;
46   private ThreadManagerIF threadmgr;
47   private StageStatsIF stats;
48   private ResponseTimeControllerIF rtcon;
49 
50   /**
51    * Create a StageWrapper with the given name, handler, config data, and
52    * thread manager.
53    */
StageWrapper(ManagerIF mgr, String name, EventHandlerIF handler, ConfigDataIF config, ThreadManagerIF threadmgr)54   StageWrapper(ManagerIF mgr, String name, EventHandlerIF handler, ConfigDataIF config, ThreadManagerIF threadmgr) {
55     this.name = name;
56     this.handler = handler;
57     this.config = config;
58     this.threadmgr = threadmgr;
59     eventQ = new FiniteQueue(name);
60     this.stats = new StageStats(this);
61     this.stage = new Stage(name, this, (SinkIF)eventQ, config);
62     config.setStage(this.stage);
63     createRTController(mgr);
64   }
65 
66   /**
67    * Create a StageWrapper with the given name, handler, config data, thread
68    * manager, and queue threshold.
69    */
StageWrapper(ManagerIF mgr, String name, EventHandlerIF handler, ConfigDataIF config, ThreadManagerIF threadmgr, int queueThreshold)70   StageWrapper(ManagerIF mgr, String name, EventHandlerIF handler, ConfigDataIF config, ThreadManagerIF threadmgr, int queueThreshold) {
71     this.name = name;
72     this.handler = handler;
73     this.config = config;
74     this.threadmgr = threadmgr;
75     this.stats = new StageStats(this);
76     this.rtcon = null;
77 
78     eventQ = new FiniteQueue(name);
79     QueueThresholdPredicate pred = new QueueThresholdPredicate(eventQ, queueThreshold);
80     eventQ.setEnqueuePredicate(pred);
81 
82     this.stage = new Stage(name, this, (SinkIF)eventQ, config);
83     config.setStage(this.stage);
84     createRTController(mgr);
85   }
86 
createRTController(ManagerIF mgr)87   private void createRTController(ManagerIF mgr) {
88     boolean rtControllerEnabled = mgr.getConfig().getBoolean("global.rtController.enable");
89     String deftype = mgr.getConfig().getString("global.rtController.type");
90     if (mgr.getConfig().getBoolean("stages."+name+".rtController.enable", rtControllerEnabled)) {
91       String contype = mgr.getConfig().getString("stages."+name+".rtController.type", deftype);
92       if (contype == null) {
93 	this.rtcon = new ResponseTimeControllerDirect(mgr, this);
94       } else if (contype.equals("direct")) {
95 	this.rtcon = new ResponseTimeControllerDirect(mgr, this);
96       } else if (contype.equals("mm1")) {
97 	this.rtcon = new ResponseTimeControllerMM1(mgr, this);
98       } else if (contype.equals("pid")) {
99 	this.rtcon = new ResponseTimeControllerPID(mgr, this);
100       } else if (contype.equals("multiclass")) {
101 	this.rtcon = new ResponseTimeControllerMulticlass(mgr, this);
102       } else {
103 	throw new RuntimeException("StageWrapper <"+name+">: Bad response time controller type "+contype);
104       }
105     }
106   }
107 
108   /**
109    * Initialize this stage.
110    */
init()111   public void init() throws Exception {
112     handler.init(config);
113     threadmgr.register(this);
114   }
115 
116   /**
117    * Destroy this stage.
118    */
destroy()119   public void destroy() throws Exception {
120     threadmgr.deregister(this);
121     handler.destroy();
122   }
123 
124   /**
125    * Return the event handler associated with this stage.
126    */
getEventHandler()127   public EventHandlerIF getEventHandler() {
128     return handler;
129   }
130 
131   /**
132    * Return the stage handle for this stage.
133    */
getStage()134   public StageIF getStage() {
135     return stage;
136   }
137 
138   /**
139    * Return the set of sources from which events should be pulled to
140    * pass to this EventHandlerIF.
141    */
getSource()142   public SourceIF getSource() {
143     return (SourceIF)eventQ;
144   }
145 
146   /**
147    * Return the thread manager which will run this stage.
148    */
getThreadManager()149   public ThreadManagerIF getThreadManager() {
150     return threadmgr;
151   }
152 
153   /**
154    * Return execution statistics for this stage.
155    */
getStats()156   public StageStatsIF getStats() {
157     return stats;
158   }
159 
160   /**
161    * Return the response time controller, if any.
162    */
getResponseTimeController()163   public ResponseTimeControllerIF getResponseTimeController() {
164     return rtcon;
165   }
166 
toString()167   public String toString() {
168     return "SW["+stage.getName()+"]";
169   }
170 
171 }
172 
173