001: /*
002: * Copyright (c) 2000 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.lib.aSocket;
026:
027: import seda.sandStorm.api.*;
028: import seda.sandStorm.api.internal.*;
029: import seda.sandStorm.core.*;
030: import seda.sandStorm.internal.*;
031: import seda.sandStorm.main.*;
032:
033: import java.net.*;
034: import java.io.*;
035: import java.util.*;
036:
037: /**
038: * The aSocketMgr is an internal class used to provide an interface between
039: * the Sandstorm runtime and the aSocket library. Applications should not
040: * make use of this class.
041: *
042: * @author Matt Welsh
043: */
044: public class aSocketMgr {
045:
046: private static final boolean DEBUG = false;
047:
048: private static ThreadManagerIF aSocketTM, aSocketRCTM;
049: private static SinkIF read_sink;
050: private static SinkIF listen_sink;
051: private static SinkIF write_sink;
052:
053: private static Object init_lock = new Object();
054: private static boolean initialized = false;
055:
056: static boolean USE_NIO = false;
057: private static aSocketImplFactory factory;
058:
059: /**
060: * Called at startup time by the Sandstorm runtime.
061: */
062: public static void initialize(ManagerIF mgr, SystemManagerIF sysmgr)
063: throws Exception {
064:
065: synchronized (init_lock) {
066: SandstormConfig cfg = mgr.getConfig();
067:
068: String provider = cfg.getString("global.aSocket.provider");
069: if (provider == null) {
070: throw new RuntimeException(
071: "aSocketMgr: Must specify either "
072: + "'NIO' or 'NBIO' for global.aSocket.provider");
073: }
074:
075: if (provider.equals("NIO")) {
076: USE_NIO = true;
077: System.err
078: .println("aSocket layer using JDK1.4 java.nio package");
079: } else if (provider.equals("NBIO")) {
080: USE_NIO = false;
081: System.err.println("aSocket layer using NBIO package");
082: } else {
083: throw new RuntimeException(
084: "aSocketMgr: Must specify either "
085: + "'NIO' or 'NBIO' for global.aSocket.provider");
086: }
087:
088: try {
089: factory = aSocketImplFactory.getFactory();
090: } catch (Exception e) {
091: throw new RuntimeException(
092: "aSocketMgr: Cannot create aSocketImplFactory: "
093: + e);
094: }
095:
096: aSocketTM = new aSocketThreadManager(mgr);
097: sysmgr.addThreadManager("aSocket", aSocketTM);
098:
099: ReadEventHandler revh = new ReadEventHandler();
100: aSocketStageWrapper rsw;
101: if (cfg.getBoolean("global.aSocket.governor.enable")) {
102: aSocketRCTM = new aSocketRCTMSleep(mgr);
103: sysmgr.addThreadManager("aSocketRCTM", aSocketRCTM);
104: rsw = new aSocketStageWrapper("aSocket ReadStage",
105: revh, new ConfigData(mgr), aSocketRCTM);
106: } else {
107: rsw = new aSocketStageWrapper("aSocket ReadStage",
108: revh, new ConfigData(mgr), aSocketTM);
109: }
110:
111: StageIF readStage = sysmgr.createStage(rsw, true);
112: read_sink = readStage.getSink();
113:
114: ListenEventHandler levh = new ListenEventHandler();
115: aSocketStageWrapper lsw = new aSocketStageWrapper(
116: "aSocket ListenStage", levh, new ConfigData(mgr),
117: aSocketTM);
118: StageIF listenStage = sysmgr.createStage(lsw, true);
119: listen_sink = listenStage.getSink();
120:
121: WriteEventHandler wevh = new WriteEventHandler();
122: aSocketStageWrapper wsw = new aSocketStageWrapper(
123: "aSocket WriteStage", wevh, new ConfigData(mgr),
124: aSocketTM);
125: StageIF writeStage = sysmgr.createStage(wsw, true);
126: write_sink = writeStage.getSink();
127:
128: System.out.println("Write Sink : "
129: + write_sink.getClass().getName());
130: initialized = true;
131: }
132: }
133:
134: /**
135: * Ensure that the aSocket layer is initialized, in case the library
136: * is being used in standalone mode.
137: */
138: static void init() {
139: synchronized (init_lock) {
140: // When invoked in standalone mode
141: if (!initialized) {
142: try {
143: Sandstorm ss = Sandstorm.getSandstorm();
144: if (ss != null) {
145: initialize(ss.getManager(), ss
146: .getSystemManager());
147: } else {
148: SandstormConfig cfg = new SandstormConfig();
149: ss = new Sandstorm(cfg);
150: }
151: } catch (Exception e) {
152: System.err
153: .println("aSocketMgr: Warning: Initialization failed: "
154: + e);
155: e.printStackTrace();
156: return;
157: }
158: }
159: }
160: }
161:
162: static aSocketImplFactory getFactory() {
163: return factory;
164: }
165:
166: static public void enqueueRequest(aSocketRequest req) {
167: init();
168:
169: if ((req instanceof ATcpWriteRequest)
170: || (req instanceof ATcpConnectRequest)
171: || (req instanceof ATcpFlushRequest)
172: || (req instanceof ATcpCloseRequest)
173: || (req instanceof AUdpWriteRequest)
174: || (req instanceof AUdpCloseRequest)
175: || (req instanceof AUdpFlushRequest)
176: || (req instanceof AUdpConnectRequest)
177: || (req instanceof AUdpDisconnectRequest)) {
178:
179: try {
180: write_sink.enqueue(req);
181: } catch (SinkException se) {
182: System.err
183: .println("aSocketMgr.enqueueRequest: Warning: Got SinkException "
184: + se);
185: System.err
186: .println("aSocketMgr.enqueueRequest: This is a bug - contact <mdw@cs.berkeley.edu>");
187: }
188:
189: } else if ((req instanceof ATcpStartReadRequest)
190: || (req instanceof AUdpStartReadRequest)) {
191:
192: try {
193: read_sink.enqueue(req);
194: } catch (SinkException se) {
195: System.err
196: .println("aSocketMgr.enqueueRequest: Warning: Got SinkException "
197: + se);
198: System.err
199: .println("aSocketMgr.enqueueRequest: This is a bug - contact <mdw@cs.berkeley.edu>");
200: }
201:
202: } else if ((req instanceof ATcpListenRequest)
203: || (req instanceof ATcpSuspendAcceptRequest)
204: || (req instanceof ATcpResumeAcceptRequest)
205: || (req instanceof ATcpCloseServerRequest)) {
206:
207: try {
208: listen_sink.enqueue(req);
209: } catch (SinkException se) {
210: System.err
211: .println("aSocketMgr.enqueueRequest: Warning: Got SinkException "
212: + se);
213: System.err
214: .println("aSocketMgr.enqueueRequest: This is a bug - contact <mdw@cs.berkeley.edu>");
215: }
216:
217: } else {
218: throw new IllegalArgumentException("Bad request type "
219: + req);
220: }
221: }
222: }
|