001: package test.tck.msgflow.callflows;
002:
003: import java.util.HashSet;
004: import java.util.Iterator;
005: import java.util.Properties;
006:
007: import javax.sip.ObjectInUseException;
008: import javax.sip.SipException;
009: import javax.sip.SipFactory;
010: import javax.sip.SipProvider;
011: import javax.sip.SipStack;
012: import javax.sip.address.AddressFactory;
013: import javax.sip.header.HeaderFactory;
014: import javax.sip.message.MessageFactory;
015:
016: /**
017: * @author M. Ranganathan
018: *
019: */
020: public class ProtocolObjects {
021: public final AddressFactory addressFactory;
022:
023: public final MessageFactory messageFactory;
024:
025: public final HeaderFactory headerFactory;
026:
027: public final SipStack sipStack;
028:
029: public int logLevel = 0;
030:
031: String logFileDirectory = "logs/";
032:
033: public final String transport;
034:
035: private boolean isStarted;
036:
037: public ProtocolObjects(String stackname, String pathname,
038: String transport, boolean autoDialog) {
039:
040: this .transport = transport;
041: SipFactory sipFactory = SipFactory.getInstance();
042: sipFactory.resetFactory();
043: sipFactory.setPathName(pathname);
044: Properties properties = new Properties();
045: properties.setProperty("javax.sip.STACK_NAME", stackname);
046:
047: // The following properties are specific to nist-sip
048: // and are not necessarily part of any other jain-sip
049: // implementation.
050: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
051: logFileDirectory + stackname + "debuglog.txt");
052: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
053: logFileDirectory + stackname + "log.txt");
054:
055: properties.setProperty("javax.sip.AUTOMATIC_DIALOG_SUPPORT",
056: (autoDialog ? "on" : "off"));
057:
058: // For the forked subscribe notify test
059: properties.setProperty("javax.sip.FORKABLE_EVENTS", "foo");
060:
061: //For the TelUrlRouter test.
062: properties.setProperty("javax.sip.ROUTER_PATH",
063: NonSipUriRouter.class.getName());
064:
065: // Dont use the router for all requests.
066: properties.setProperty("javax.sip.USE_ROUTER_FOR_ALL_URIS",
067: "false");
068:
069: properties.setProperty("gov.nist.javax.sip.THREAD_POOL_SIZE",
070: "1");
071:
072: // Set to 0 in your production code for max speed.
073: // You need 16 for logging traces. 32 for debug + traces.
074: // Your code will limp at 32 but it is best for debugging.
075: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",
076: new Integer(logLevel).toString());
077:
078: try {
079: // Create SipStack object
080: sipStack = sipFactory.createSipStack(properties);
081:
082: NonSipUriRouter router = (NonSipUriRouter) sipStack
083: .getRouter();
084:
085: router.setMyPort(5080);
086:
087: System.out.println("createSipStack " + sipStack);
088: } catch (Exception e) {
089: // could not find
090: // gov.nist.jain.protocol.ip.sip.SipStackImpl
091: // in the classpath
092: e.printStackTrace();
093: System.err.println(e.getMessage());
094: throw new RuntimeException("Stack failed to initialize");
095: }
096:
097: try {
098: headerFactory = sipFactory.createHeaderFactory();
099: addressFactory = sipFactory.createAddressFactory();
100: messageFactory = sipFactory.createMessageFactory();
101: } catch (SipException ex) {
102: ex.printStackTrace();
103: throw new RuntimeException(ex);
104: }
105: }
106:
107: public synchronized void destroy() {
108:
109: HashSet hashSet = new HashSet();
110:
111: for (Iterator it = sipStack.getSipProviders(); it.hasNext();) {
112:
113: SipProvider sipProvider = (SipProvider) it.next();
114: hashSet.add(sipProvider);
115: }
116:
117: for (Iterator it = hashSet.iterator(); it.hasNext();) {
118: SipProvider sipProvider = (SipProvider) it.next();
119:
120: for (int j = 0; j < 5; j++) {
121: try {
122: sipStack.deleteSipProvider(sipProvider);
123: } catch (ObjectInUseException ex) {
124: try {
125: Thread.sleep(1000);
126: } catch (Exception e) {
127: }
128:
129: }
130: }
131: }
132:
133: sipStack.stop();
134: }
135:
136: public void start() throws Exception {
137: if (this .isStarted)
138: return;
139: sipStack.start();
140: this .isStarted = true;
141:
142: }
143: }
|