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:
008: import java.text.ParseException;
009: import java.util.*;
010:
011: /**
012: * Concurrent calls test. The client creates 20 concurrent dialogs on the server.
013: * The server replies to each one.
014: *
015: * @author M. Ranganathan
016: */
017:
018: public class Shootist implements SipListener {
019:
020: private static SipProvider tcpProvider;
021:
022: private static SipProvider udpProvider;
023:
024: private static AddressFactory addressFactory;
025:
026: private static MessageFactory messageFactory;
027:
028: private static HeaderFactory headerFactory;
029:
030: private static SipStack sipStack;
031:
032: private ContactHeader contactHeader;
033:
034: private ListeningPoint tcpListeningPoint;
035:
036: private ListeningPoint udpListeningPoint;
037:
038: /* move variables as class variables from init() */
039: private SipURI requestURI;
040:
041: private CSeqHeader cSeqHeader;
042:
043: private FromHeader fromHeader;
044:
045: private ToHeader toHeader;
046:
047: private MaxForwardsHeader maxForwards;
048:
049: private Shootist listener;
050:
051: private SipProvider sipProvider;
052:
053: private Address fromNameAddress;
054:
055: private ContentTypeHeader contentTypeHeader;
056:
057: private int terminatedCount;
058:
059: private int confirmationCount;
060: // If you want to try TCP transport change the following to
061: // String transport = "tcp";
062: String transport = "udp";
063:
064: /* remote peer host */
065: static String peerHostPort = "127.0.0.1:5070";
066:
067: static String localHost = "127.0.0.1";
068:
069: protected static final String usageString = "java "
070: + "examples.multi.Shootist \n"
071: + ">>>> is your class path set to the root?";
072:
073: private static void usage() {
074: System.out.println(usageString);
075: System.exit(0);
076:
077: }
078:
079: public void processRequest(RequestEvent requestReceivedEvent) {
080: Request request = requestReceivedEvent.getRequest();
081: ServerTransaction serverTransactionId = requestReceivedEvent
082: .getServerTransaction();
083:
084: // We are the UAC so the only request we get is the BYE.
085: if (request.getMethod().equals(Request.BYE))
086: processBye(request, serverTransactionId);
087:
088: }
089:
090: public void processBye(Request request,
091: ServerTransaction serverTransactionId) {
092: try {
093: //System.out.println("shootist: got a bye .");
094: if (serverTransactionId == null) {
095: //System.out.println("shootist: null TID.");
096: return;
097: }
098: //System.out.println("Dialog State = " + dialog.getState());
099: Response response = messageFactory.createResponse(200,
100: request);
101: serverTransactionId.sendResponse(response);
102: //System.out.println("shootist: Sending OK.");
103: //System.out.println("Dialog State = " + dialog.getState());
104:
105: } catch (Exception ex) {
106: ex.printStackTrace();
107: System.exit(0);
108:
109: }
110: }
111:
112: public void processResponse(ResponseEvent responseReceivedEvent) {
113: //System.out.println("Got a response");
114: Response response = (Response) responseReceivedEvent
115: .getResponse();
116: Transaction tid = responseReceivedEvent.getClientTransaction();
117:
118: try {
119: if (response.getStatusCode() == Response.OK
120: && ((CSeqHeader) response
121: .getHeader(CSeqHeader.NAME)).getMethod()
122: .equals(Request.INVITE)) {
123: Dialog dialog = responseReceivedEvent.getDialog();
124: CSeqHeader cseq = (CSeqHeader) response
125: .getHeader(CSeqHeader.NAME);
126: Request ackRequest = dialog.createAck(cseq
127: .getSeqNumber());
128: //System.out.println("Sending ACK");
129: dialog.sendAck(ackRequest);
130: System.out.println("Dialog Confirmed: Count = "
131: + ++this .confirmationCount + " dialogID = "
132: + dialog.getDialogId() + " dialogState = "
133: + dialog.getState());
134: if (tid == null)
135: System.out.println("null txID");
136:
137: }
138: } catch (Exception ex) {
139: ex.printStackTrace();
140: System.exit(0);
141: }
142:
143: }
144:
145: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
146:
147: //System.out.println("Transaction Time out");
148: }
149:
150: public void init() {
151: SipFactory sipFactory = null;
152: sipStack = null;
153: sipFactory = SipFactory.getInstance();
154: sipFactory.setPathName("gov.nist");
155: Properties properties = new Properties();
156:
157: /* remote peer host */
158: String peerHostPort = "127.0.0.1:5070";
159: String localHost = "127.0.0.1";
160:
161: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
162: + "/" + transport);
163:
164: properties.setProperty("javax.sip.STACK_NAME", "shootist");
165:
166: // The following properties are specific to nist-sip
167: // and are not necessarily part of any other jain-sip
168: // implementation.
169: // You can set a max message size for tcp transport to
170: // guard against denial of service attack.
171: properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
172: "1048576");
173: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
174: "shootistdebug.txt");
175: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
176: "shootistlog.txt");
177:
178: // Drop the client connection after we are done with the transaction.
179: properties.setProperty(
180: "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
181: // Set to 0 in your production code for max speed.
182: // You need 16 for logging traces. 32 for debug + traces.
183: // Your code will limp at 32 but it is best for debugging.
184: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
185:
186: try {
187: // Create SipStack object
188: sipStack = sipFactory.createSipStack(properties);
189: System.out.println("createSipStack " + sipStack);
190: } catch (PeerUnavailableException e) {
191: // could not find
192: // gov.nist.jain.protocol.ip.sip.SipStackImpl
193: // in the classpath
194: e.printStackTrace();
195: System.err.println(e.getMessage());
196: System.exit(0);
197: }
198:
199: try {
200: headerFactory = sipFactory.createHeaderFactory();
201: addressFactory = sipFactory.createAddressFactory();
202: messageFactory = sipFactory.createMessageFactory();
203:
204: /* udp ListeningPoint */
205: udpListeningPoint = sipStack.createListeningPoint(
206: localHost, 5060, "udp");
207: udpProvider = sipStack.createSipProvider(udpListeningPoint);
208: listener = this ;
209: udpProvider.addSipListener(listener);
210:
211: /* tcp ListeningPoint */
212: tcpListeningPoint = sipStack.createListeningPoint(
213: localHost, 5060, "tcp");
214: tcpProvider = sipStack.createSipProvider(tcpListeningPoint);
215: tcpProvider.addSipListener(listener);
216:
217: sipProvider = transport.equalsIgnoreCase("udp") ? udpProvider
218: : tcpProvider;
219:
220: String fromName = "BigGuy";
221: String fromSipAddress = "here.com";
222: String fromDisplayName = "The Master Blaster";
223:
224: String toSipAddress = "there.com";
225: String toUser = "LittleGuy";
226: String toDisplayName = "The Little Blister";
227:
228: // create >From Header
229: SipURI fromAddress = addressFactory.createSipURI(fromName,
230: fromSipAddress);
231:
232: fromNameAddress = addressFactory.createAddress(fromAddress);
233: fromNameAddress.setDisplayName(fromDisplayName);
234:
235: // create To Header
236: SipURI toAddress = addressFactory.createSipURI(toUser,
237: toSipAddress);
238: Address toNameAddress = addressFactory
239: .createAddress(toAddress);
240: toNameAddress.setDisplayName(toDisplayName);
241: toHeader = headerFactory
242: .createToHeader(toNameAddress, null);
243:
244: // create Request URI
245: requestURI = addressFactory.createSipURI(toUser,
246: peerHostPort);
247:
248: // Create ContentTypeHeader
249: contentTypeHeader = headerFactory.createContentTypeHeader(
250: "application", "sdp");
251:
252: // Create a new MaxForwardsHeader
253: maxForwards = headerFactory.createMaxForwardsHeader(70);
254:
255: // Create contact headers
256: String host = localHost;
257:
258: SipURI contactUrl = addressFactory.createSipURI(fromName,
259: host);
260: contactUrl.setPort(tcpListeningPoint.getPort());
261:
262: // Create the contact name address.
263: SipURI contactURI = addressFactory.createSipURI(fromName,
264: host);
265: contactURI.setPort(sipProvider.getListeningPoint(transport)
266: .getPort());
267:
268: Address contactAddress = addressFactory
269: .createAddress(contactURI);
270:
271: // Add the contact address.
272: contactAddress.setDisplayName(fromName);
273:
274: contactHeader = headerFactory
275: .createContactHeader(contactAddress);
276:
277: } catch (Exception ex) {
278: System.out.println(ex.getMessage());
279: ex.printStackTrace();
280: usage();
281: }
282: }
283:
284: /* extract something about Request as a seperated function from init() */
285: void sendInviteRequest(int cmdSeq) {
286: System.out.println("====Send INVITE at times: " + cmdSeq);
287: try {
288: cSeqHeader = headerFactory.createCSeqHeader(1L,
289: Request.INVITE);
290:
291: // Create a new CallId header
292: CallIdHeader callIdHeader = sipProvider.getNewCallId();
293:
294: int fromTag = 1000 + cmdSeq;
295:
296: fromHeader = headerFactory.createFromHeader(
297: fromNameAddress, new Integer(fromTag).toString());
298: String sdpData = "v=0\r\n"
299: + "o=4855 13760799956958020 13760799956958020"
300: + " IN IP4 129.6.55.78\r\n"
301: + "s=mysession session\r\n"
302: + "p=+46 8 52018010\r\n"
303: + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n"
304: + "m=audio 6022 RTP/AVP 0 4 18\r\n"
305: + "a=rtpmap:0 PCMU/8000\r\n"
306: + "a=rtpmap:4 G723/8000\r\n"
307: + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
308: // Create ViaHeaders
309: ArrayList viaHeaders = new ArrayList();
310: ViaHeader viaHeader = headerFactory.createViaHeader(
311: localHost, sipProvider.getListeningPoint(transport)
312: .getPort(), transport, null);
313:
314: // add via headers
315: viaHeaders.add(viaHeader);
316: Request request = messageFactory.createRequest(requestURI,
317: Request.INVITE, callIdHeader, cSeqHeader,
318: fromHeader, toHeader, viaHeaders, maxForwards);
319:
320: request.setHeader(contactHeader);
321: request.setContent(sdpData, contentTypeHeader);
322: // Create the client transaction.
323: ClientTransaction inviteTid = sipProvider
324: .getNewClientTransaction(request);
325: System.out.println("inviteTid = " + inviteTid
326: + " sipDialog = " + inviteTid.getDialog());
327:
328: // send the request out.
329: inviteTid.sendRequest();
330: } catch (Exception ex) {
331: System.out
332: .println("Fail to sendInviteRequest with SipException:\n"
333: + ex.getMessage());
334: }
335: return;
336: }
337:
338: public static void main(String args[]) throws Exception {
339:
340: System.out.println("\n***localHost=<" + localHost
341: + ">, peerHostPort=<" + peerHostPort + ">.");
342:
343: int loops = 500;
344: int cmdSeq = 1;
345:
346: Shootist shootist = new Shootist();
347: shootist.init();
348:
349: /* simply send multiple INVITEs, no multi-thread */
350: for (int i = 0; i < loops; i++) {
351: Thread.sleep(10);
352: shootist.sendInviteRequest(cmdSeq++);
353: }
354: }
355:
356: public void processIOException(IOExceptionEvent exceptionEvent) {
357: System.out.println("An IO Exception occured!");
358: }
359:
360: public void processTransactionTerminated(
361: TransactionTerminatedEvent transactionTerminatedEvent) {
362: // System.out.println("TransactionTerminated event notification");
363: }
364:
365: public void processDialogTerminated(
366: DialogTerminatedEvent dialogTerminatedEvent) {
367: this .terminatedCount++;
368: System.out.println("DialogTerminatedEvent notification "
369: + this .terminatedCount + " dialog ID = "
370: + dialogTerminatedEvent.getDialog().getDialogId());
371: }
372: }
|