001: /*
002: * Copyright (c) 2001 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.sandStorm.internal;
026:
027: import seda.sandStorm.api.*;
028: import seda.sandStorm.api.internal.*;
029: import seda.sandStorm.core.*;
030: import java.util.*;
031:
032: /**
033: * A StageWrapper is a basic implementation of StageWrapperIF for
034: * application-level stages.
035: *
036: * @author Matt Welsh
037: */
038:
039: class StageWrapper implements StageWrapperIF {
040:
041: private String name;
042: private StageIF stage;
043: private EventHandlerIF handler;
044: private ConfigDataIF config;
045: private FiniteQueue eventQ;
046: private ThreadManagerIF threadmgr;
047: private StageStatsIF stats;
048: private ResponseTimeControllerIF rtcon;
049:
050: /**
051: * Create a StageWrapper with the given name, handler, config data, and
052: * thread manager.
053: */
054: StageWrapper(ManagerIF mgr, String name, EventHandlerIF handler,
055: ConfigDataIF config, ThreadManagerIF threadmgr) {
056: this .name = name;
057: this .handler = handler;
058: this .config = config;
059: this .threadmgr = threadmgr;
060: eventQ = new FiniteQueue(name);
061: this .stats = new StageStats(this );
062: this .stage = new Stage(name, this , (SinkIF) eventQ, config);
063: config.setStage(this .stage);
064: createRTController(mgr);
065: }
066:
067: /**
068: * Create a StageWrapper with the given name, handler, config data, thread
069: * manager, and queue threshold.
070: */
071: StageWrapper(ManagerIF mgr, String name, EventHandlerIF handler,
072: ConfigDataIF config, ThreadManagerIF threadmgr,
073: int queueThreshold) {
074: this .name = name;
075: this .handler = handler;
076: this .config = config;
077: this .threadmgr = threadmgr;
078: this .stats = new StageStats(this );
079: this .rtcon = null;
080:
081: eventQ = new FiniteQueue(name);
082: QueueThresholdPredicate pred = new QueueThresholdPredicate(
083: eventQ, queueThreshold);
084: eventQ.setEnqueuePredicate(pred);
085:
086: this .stage = new Stage(name, this , (SinkIF) eventQ, config);
087: config.setStage(this .stage);
088: createRTController(mgr);
089: }
090:
091: private void createRTController(ManagerIF mgr) {
092: boolean rtControllerEnabled = mgr.getConfig().getBoolean(
093: "global.rtController.enable");
094: String deftype = mgr.getConfig().getString(
095: "global.rtController.type");
096: if (mgr.getConfig().getBoolean(
097: "stages." + name + ".rtController.enable",
098: rtControllerEnabled)) {
099: String contype = mgr.getConfig().getString(
100: "stages." + name + ".rtController.type", deftype);
101: if (contype == null) {
102: this .rtcon = new ResponseTimeControllerDirect(mgr, this );
103: } else if (contype.equals("direct")) {
104: this .rtcon = new ResponseTimeControllerDirect(mgr, this );
105: } else if (contype.equals("mm1")) {
106: this .rtcon = new ResponseTimeControllerMM1(mgr, this );
107: } else if (contype.equals("pid")) {
108: this .rtcon = new ResponseTimeControllerPID(mgr, this );
109: } else if (contype.equals("multiclass")) {
110: this .rtcon = new ResponseTimeControllerMulticlass(mgr,
111: this );
112: } else {
113: throw new RuntimeException("StageWrapper <" + name
114: + ">: Bad response time controller type "
115: + contype);
116: }
117: }
118: }
119:
120: /**
121: * Initialize this stage.
122: */
123: public void init() throws Exception {
124: handler.init(config);
125: threadmgr.register(this );
126: }
127:
128: /**
129: * Destroy this stage.
130: */
131: public void destroy() throws Exception {
132: threadmgr.deregister(this );
133: handler.destroy();
134: }
135:
136: /**
137: * Return the event handler associated with this stage.
138: */
139: public EventHandlerIF getEventHandler() {
140: return handler;
141: }
142:
143: /**
144: * Return the stage handle for this stage.
145: */
146: public StageIF getStage() {
147: return stage;
148: }
149:
150: /**
151: * Return the set of sources from which events should be pulled to
152: * pass to this EventHandlerIF.
153: */
154: public SourceIF getSource() {
155: return (SourceIF) eventQ;
156: }
157:
158: /**
159: * Return the thread manager which will run this stage.
160: */
161: public ThreadManagerIF getThreadManager() {
162: return threadmgr;
163: }
164:
165: /**
166: * Return execution statistics for this stage.
167: */
168: public StageStatsIF getStats() {
169: return stats;
170: }
171:
172: /**
173: * Return the response time controller, if any.
174: */
175: public ResponseTimeControllerIF getResponseTimeController() {
176: return rtcon;
177: }
178:
179: public String toString() {
180: return "SW[" + stage.getName() + "]";
181: }
182:
183: }
|