001: package test.load.multidialog;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007: import java.util.*;
008:
009: /**
010: * This class is a UAC template. Shootist is the guy that shoots and shootme is
011: * the guy that gets shot.
012: *
013: * @author M. Ranganathan
014: */
015:
016: public class Shootme implements SipListener {
017:
018: private static AddressFactory addressFactory;
019:
020: private static MessageFactory messageFactory;
021:
022: private static HeaderFactory headerFactory;
023:
024: private static SipStack sipStack;
025:
026: private static String myAddress = "127.0.0.1";
027:
028: private static int myPort = 5070;
029:
030: int numInvite = 0;
031:
032: private int terminatedCount;
033:
034: private int createdCount;
035:
036: private HashSet dialogIds;
037:
038: private HashMap transactionIDs;
039:
040: class TTask extends TimerTask {
041:
042: RequestEvent requestEvent;
043:
044: ServerTransaction st;
045:
046: public TTask(RequestEvent requestEvent, ServerTransaction st) {
047: this .requestEvent = requestEvent;
048: this .st = st;
049: }
050:
051: public void run() {
052: Request request = requestEvent.getRequest();
053: try {
054: //System.out.println("shootme: got an Invite sending OK");
055: Response response = messageFactory.createResponse(180,
056: request);
057: ToHeader toHeader = (ToHeader) response
058: .getHeader(ToHeader.NAME);
059: Address address = addressFactory
060: .createAddress("Shootme <sip:" + myAddress
061: + ":" + myPort + ">");
062: ContactHeader contactHeader = headerFactory
063: .createContactHeader(address);
064: response.addHeader(contactHeader);
065:
066: //System.out.println("got a server tranasaction " + st);
067: Dialog dialog = st.getDialog();
068: /*
069: * if (dialog != null) { System.out.println("Dialog " + dialog);
070: * System.out.println("Dialog state " + dialog.getState()); }
071: */
072: st.sendResponse(response); // send 180(RING)
073: response = messageFactory.createResponse(200, request);
074: toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
075: String toTag = new Integer((int) (Math.random() * 1000))
076: .toString();
077: toHeader.setTag(toTag); // Application is supposed to set.
078: response.addHeader(contactHeader);
079:
080: st.sendResponse(response);// send 200(OK)
081:
082: } catch (Exception ex) {
083: ex.printStackTrace();
084: System.exit(0);
085: }
086:
087: }
088:
089: }
090:
091: protected static final String usageString = "java "
092: + "examples.shootist.Shootist \n"
093: + ">>>> is your class path set to the root?";
094:
095: private static void usage() {
096: System.out.println(usageString);
097: System.exit(0);
098:
099: }
100:
101: public void processRequest(RequestEvent requestEvent) {
102: Request request = requestEvent.getRequest();
103: ServerTransaction serverTransactionId = requestEvent
104: .getServerTransaction();
105:
106: /*
107: * System.out.println("\n\nRequest " + request.getMethod() + "
108: * received at " + sipStack.getStackName() + " with server
109: * transaction id " + serverTransactionId);
110: */
111:
112: if (request.getMethod().equals(Request.INVITE)) {
113: processInvite(requestEvent, serverTransactionId);
114: } else if (request.getMethod().equals(Request.ACK)) {
115: processAck(requestEvent, serverTransactionId);
116: } else if (request.getMethod().equals(Request.BYE)) {
117: processBye(requestEvent, serverTransactionId);
118: }
119:
120: }
121:
122: /**
123: * Process the ACK request. Send the bye and complete the call flow.
124: */
125: public void processAck(RequestEvent requestEvent,
126: ServerTransaction serverTransaction) {
127: SipProvider sipProvider = (SipProvider) requestEvent
128: .getSource();
129: try {
130: // System.out.println("*** shootme: got an ACK "
131: // + requestEvent.getRequest());
132: if (serverTransaction == null) {
133: System.out
134: .println("null server transaction -- ignoring the ACK!");
135: return;
136: }
137: Dialog dialog = serverTransaction.getDialog();
138: this .createdCount++;
139: System.out.println("Dialog Created = "
140: + dialog.getDialogId() + " createdCount "
141: + this .createdCount + " Dialog State = "
142: + dialog.getState());
143:
144: if (this .dialogIds.contains(dialog.getDialogId())) {
145: System.out.println("OOPS ! I already saw "
146: + dialog.getDialogId());
147: } else {
148: this .dialogIds.add(dialog.getDialogId());
149: }
150:
151: Request byeRequest = dialog.createRequest(Request.BYE);
152: ClientTransaction tr = sipProvider
153: .getNewClientTransaction(byeRequest);
154: // System.out.println("shootme: got an ACK -- sending bye! ");
155: dialog.sendRequest(tr);
156:
157: } catch (Exception ex) {
158: ex.printStackTrace();
159: System.exit(0);
160: }
161: }
162:
163: /**
164: * Process the invite request.
165: */
166: public void processInvite(RequestEvent requestEvent,
167: ServerTransaction serverTransaction) {
168: try {
169: //System.out.println("ProcessInvite");
170: Request request = requestEvent.getRequest();
171: SipProvider sipProvider = (SipProvider) requestEvent
172: .getSource();
173: // Note you need to create the Server Transaction
174: // before the listener returns but you can delay sending the response
175:
176: ServerTransaction st = sipProvider
177: .getNewServerTransaction(request);
178: if (transactionIDs.containsKey(st.getBranchId())) {
179: System.out
180: .println("OOOPS -- seen this guy before!! This must be a late guy "
181: + st.getBranchId()
182: + " st = "
183: + transactionIDs.get(st.getBranchId()));
184: return;
185: } else {
186: transactionIDs.put(st.getBranchId(), st);
187: }
188:
189: TTask ttask = new TTask(requestEvent, st);
190: int ttime;
191: if ((numInvite % 4) == 0)
192: ttime = 5000;
193: else if ((numInvite % 4) == 1)
194: ttime = 1000;
195: else
196: ttime = 300;
197: numInvite++;
198: new Timer().schedule(ttask, ttime);
199: } catch (Exception ex) {
200: ex.printStackTrace();
201: }
202: }
203:
204: /**
205: * Process the bye request.
206: */
207: public void processBye(RequestEvent requestEvent,
208: ServerTransaction serverTransactionId) {
209: Request request = requestEvent.getRequest();
210: try {
211: // System.out.println("shootme: got a bye sending OK.");
212: Response response = messageFactory.createResponse(200,
213: request, null, null);
214: serverTransactionId.sendResponse(response);
215: // System.out.println("Dialog State is "
216: // + serverTransactionId.getDialog().getState());
217:
218: } catch (Exception ex) {
219: ex.printStackTrace();
220: System.exit(0);
221:
222: }
223: }
224:
225: public void processResponse(ResponseEvent responseReceivedEvent) {
226: // System.out.println("Got a response");
227: Response response = (Response) responseReceivedEvent
228: .getResponse();
229: Transaction tid = responseReceivedEvent.getClientTransaction();
230:
231: // System.out.println("Response received with client transaction id "
232: // + tid + ":\n" + response);
233:
234: try {
235: if (response.getStatusCode() == Response.OK
236: && ((CSeqHeader) response
237: .getHeader(CSeqHeader.NAME)).getMethod()
238: .equals(Request.INVITE)) {
239:
240: Dialog dialog = tid.getDialog();
241: Request request = tid.getRequest();
242: dialog.sendAck(request);
243: }
244:
245: } catch (Exception ex) {
246: ex.printStackTrace();
247: System.exit(0);
248: }
249:
250: }
251:
252: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
253: Transaction transaction;
254: if (timeoutEvent.isServerTransaction()) {
255: transaction = timeoutEvent.getServerTransaction();
256: } else {
257: transaction = timeoutEvent.getClientTransaction();
258: }
259: /*
260: * System.out.println("state = " + transaction.getState());
261: * System.out.println("dialog = " + transaction.getDialog());
262: * System.out.println("dialogState = " +
263: * transaction.getDialog().getState());
264: * System.out.println("Transaction Time out" + transaction.getBranchId());
265: */
266:
267: }
268:
269: public void init() {
270: this .dialogIds = new HashSet();
271: this .transactionIDs = new HashMap();
272: SipFactory sipFactory = null;
273: sipStack = null;
274: sipFactory = SipFactory.getInstance();
275: sipFactory.setPathName("gov.nist");
276: Properties properties = new Properties();
277:
278: properties.setProperty("javax.sip.STACK_NAME", "shootme");
279: // You need 16 for logging traces. 32 for debug + traces.
280: // Your code will limp at 32 but it is best for debugging.
281: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
282: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
283: "shootmedebuglog.txt");
284: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
285: "shootmelog.txt");
286: // Guard against starvation.
287: properties.setProperty("gov.nist.javax.sip.READ_TIMEOUT",
288: "1000");
289: // properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
290: // "4096");
291: properties.setProperty(
292: "gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS", "false");
293:
294: try {
295: // Create SipStack object
296: sipStack = sipFactory.createSipStack(properties);
297: System.out.println("sipStack = " + sipStack);
298: } catch (PeerUnavailableException e) {
299: // could not find
300: // gov.nist.jain.protocol.ip.sip.SipStackImpl
301: // in the classpath
302: e.printStackTrace();
303: System.err.println(e.getMessage());
304: if (e.getCause() != null)
305: e.getCause().printStackTrace();
306: System.exit(0);
307: }
308:
309: try {
310: headerFactory = sipFactory.createHeaderFactory();
311: addressFactory = sipFactory.createAddressFactory();
312: messageFactory = sipFactory.createMessageFactory();
313: ListeningPoint lp = sipStack.createListeningPoint(
314: myAddress, 5070, "udp");
315: ListeningPoint lp1 = sipStack.createListeningPoint(
316: myAddress, 5070, "tcp");
317:
318: Shootme listener = this ;
319:
320: SipProvider sipProvider = sipStack.createSipProvider(lp);
321: System.out.println("udp provider " + sipProvider);
322: sipProvider.addSipListener(listener);
323: sipProvider = sipStack.createSipProvider(lp1);
324: System.out.println("tcp provider " + sipProvider);
325: sipProvider.addSipListener(listener);
326:
327: } catch (Exception ex) {
328: System.out.println(ex.getMessage());
329: ex.printStackTrace();
330: usage();
331: }
332:
333: }
334:
335: public static void main(String args[]) {
336: /* pass dynamic parameters in *.bat file(command line) */
337: if (args.length >= 1)
338: myAddress = args[0];
339: if (args.length >= 2)
340: myPort = Integer.parseInt(args[1]);
341:
342: System.out.println("\n***Address=<" + myAddress + ">, Port=<"
343: + myPort + ">.");
344: new Shootme().init();
345: }
346:
347: public void processIOException(IOExceptionEvent exceptionEvent) {
348: System.out.println("IOException event");
349: }
350:
351: public void processTransactionTerminated(
352: TransactionTerminatedEvent transactionTerminatedEvent) {
353: //System.out.println("TransactionTerminatedEvent");
354: }
355:
356: public void processDialogTerminated(
357: DialogTerminatedEvent dialogTerminatedEvent) {
358: Dialog dialog = dialogTerminatedEvent.getDialog();
359: this .terminatedCount++;
360: System.out.println("Dialog Terminated Event "
361: + dialog.getDialogId() + " terminatedCount = "
362: + terminatedCount);
363: if (!this .dialogIds.contains(dialog.getDialogId())) {
364: System.out
365: .println("Saw a terminated event for an unknown dialog id");
366: } else {
367: this.dialogIds.remove(dialog.getDialogId());
368: }
369: }
370:
371: }
|