001: // %Z%%M% %I% "%W% %E% Sun Microsystems"
002:
003: package com.sun.portal.netlet.client.common;
004:
005: import java.util.Hashtable;
006: import java.util.Vector;
007: import java.util.Enumeration;
008:
009: public class SClientMgr implements Runnable {
010:
011: private SClient allproxies[];
012:
013: // could be another object, but this is temporary anyway!
014: private int numConfig = 0;
015: private String ruleNames[];
016: private int srcPort[];
017: private String serverPort[];
018: private String serverHost[];
019: private String threadname[];
020: private int keyLength[];
021: private String cipherName[];
022: private int numProxies;
023:
024: private Thread t = null;
025:
026: private Hashtable clientPorts; // Mapping of clientPort to ruleName.
027: private boolean started = false;
028:
029: public SClientMgr(int numProxies) {
030: this .numProxies = numProxies;
031: ruleNames = new String[numProxies];
032: srcPort = new int[numProxies];
033: serverPort = new String[numProxies];
034: serverHost = new String[numProxies];
035: threadname = new String[numProxies];
036: allproxies = new SClient[numProxies];
037: keyLength = new int[numProxies];
038: cipherName = new String[numProxies];
039: clientPorts = new Hashtable();
040:
041: }
042:
043: public void start() {
044: if (t == null) {
045: t = new Thread(this );
046: t.start();
047: }
048: }
049:
050: public void run() {
051: for (int i = 0; i < numConfig; i++) {
052: addproxy(ruleNames[i], srcPort[i], serverPort[i],
053: serverHost[i], threadname[i], false, cipherName[i],
054: keyLength[i]);
055: }
056: System.out.println("Local port configuration -> "
057: + clientPorts.toString());
058: synchronized (this ) {
059: started = true;
060: }
061: notifyStart();
062: }
063:
064: /**
065: * Wait until all the SClients are started
066: */
067: synchronized public void waitToStart() {
068: if (started)
069: return;
070: try {
071: wait();
072: } catch (InterruptedException e) {
073: e.printStackTrace();
074: }
075: }
076:
077: /**
078: * Notify any events to the waiting threads.
079: */
080: synchronized public void notifyStart() {
081: notify();
082: }
083:
084: /**
085: * Create a Map with ruleName as key, set of ClientPorts as value.
086: */
087: public Hashtable getClientPorts() {
088: return clientPorts;
089: }
090:
091: public String getFormattedClientPorts() {
092: StringBuffer clientPortSB = new StringBuffer();
093: Enumeration enum = clientPorts.keys();
094: int htSize = clientPorts.size();
095: while (enum.hasMoreElements()) {
096: String ruleName = (String) enum.nextElement();
097: clientPortSB.append(ruleName).append("->");
098: Vector v = (Vector) clientPorts.get(ruleName);
099: int vSize = v.size();
100: Enumeration ports = v.elements();
101: while (ports.hasMoreElements()) {
102: String port = (String) ports.nextElement();
103: clientPortSB.append(port);
104: if (--vSize > 0) {
105: clientPortSB.append(",");
106: }
107: }
108: if (--htSize > 0) {
109: clientPortSB.append("|");
110: }
111: }
112: return clientPortSB.toString();
113: }
114:
115: public void stop() {
116: if (t != null) {
117: t.stop();
118: t = null;
119: }
120: }
121:
122: public void addconfig(String ruleName, int srcp, String srvp,
123: String srvh, String tname, String ciphername) {
124: ruleNames[numConfig] = ruleName;
125: srcPort[numConfig] = srcp;
126: serverPort[numConfig] = new String(srvp);
127: serverHost[numConfig] = new String(srvh);
128: threadname[numConfig] = new String(tname);
129: cipherName[numConfig] = new String(ciphername);
130: numConfig++;
131: }
132:
133: // pretty heavyweight, but leave like this just in case we an a UI!
134: // then we'll put back ActionListener code...
135:
136: public void addproxy(String ruleName, int srcp, String srvp,
137: String srvh, String tname, boolean trans,
138: String ciphername, int keylength) {
139: // see if this thread already exists, if not, then create it
140:
141: boolean foundthread = false;
142: int firstthread = -1;
143: int i = 0;
144: while (i < numProxies) {
145: if (allproxies[i] != null && !allproxies[i].stopped) {
146: if (allproxies[i].getName().equals(tname)) {
147: foundthread = true;
148: }
149: } else {
150: if (firstthread == -1) {
151: firstthread = i;
152: }
153: }
154: i++;
155: }
156: if (!foundthread && (firstthread != -1)) {
157: // have a free slot and no match, create thread
158: SClient sc = new SClient(srcp, srvp, srvh, tname, this ,
159: trans, ciphername, keylength);
160: allproxies[firstthread] = sc;
161: allproxies[firstthread].start();
162: Object obj = clientPorts.get(ruleNames[firstthread]);
163: Vector v = null;
164: if (obj != null) {
165: v = (Vector) obj;
166: } else {
167: v = new Vector();
168: }
169: v.addElement(allproxies[firstthread].getSrcPortInRule()
170: + ":" + allproxies[firstthread].getSrcPort());
171: clientPorts.put(ruleNames[firstthread], v);
172:
173: } else {
174: if (firstthread == -1) {
175: System.out
176: .println("Netlet: SCM Required number of proxies already started");
177: }
178: }
179: }
180:
181: public void stopProcessing() {
182: for (int i = 0; i < numConfig; i++) {
183: if (allproxies[i] != null) {
184: allproxies[i].stop();
185: allproxies[i] = null;
186: }
187: }
188: }
189: }
|