001: package examples.tpcc;
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 shootmeA is
011: * the guy that gets shot.
012: *
013: * @author M. Ranganathan
014: */
015:
016: /*
017: * KMC - SHOOTMEA IS EXACTLY THE SAME TO THE CLASIC SHOOTME , ONLY LISTENING ON
018: * PORT 5070
019: */
020: public class Phone implements SipListener {
021:
022: private static AddressFactory addressFactory;
023:
024: private static MessageFactory messageFactory;
025:
026: private static HeaderFactory headerFactory;
027:
028: private static SipStack sipStack;
029:
030: SipProvider sipProvider;
031:
032: private static final String myAddress = "127.0.0.1";
033:
034: private static int myPort;
035:
036: protected ServerTransaction inviteTid;
037:
038: private Response okResponse;
039:
040: private Request inviteRequest;
041:
042: private Dialog dialog;
043:
044: private String transport = "udp";
045:
046: class MyTimerTask extends TimerTask {
047: Phone shootmeA;
048: boolean byebye;
049:
050: public MyTimerTask(Phone shootmeA, boolean flag) {
051: this .shootmeA = shootmeA;
052: this .byebye = flag;
053:
054: }
055:
056: public void run() {
057: if (byebye) {
058: shootmeA.sendBye();// create a bye
059:
060: } else {
061: shootmeA.sendInviteOK();
062: }
063: }
064:
065: }
066:
067: protected static final String usageString = "java "
068: + "examples.shootist.Shootist \n"
069: + ">>>> is your class path set to the root?";
070:
071: private static void usage() {
072: System.out.println(usageString);
073: System.exit(0);
074:
075: }
076:
077: public void processRequest(RequestEvent requestEvent) {
078: Request request = requestEvent.getRequest();
079: ServerTransaction serverTransactionId = requestEvent
080: .getServerTransaction();
081:
082: System.out.println("\n\nRequest " + request.getMethod()
083: + " received at " + sipStack.getStackName()
084: + " with server transaction id " + serverTransactionId);
085:
086: if (request.getMethod().equals(Request.INVITE)) {
087: processInvite(requestEvent, serverTransactionId);
088: } else if (request.getMethod().equals(Request.ACK)) {
089: processAck(requestEvent, serverTransactionId);
090: } else if (request.getMethod().equals(Request.BYE)) {
091: processBye(requestEvent, serverTransactionId);
092: } else if (request.getMethod().equals(Request.CANCEL)) {
093: processCancel(requestEvent, serverTransactionId);
094: }
095:
096: }
097:
098: public void processResponse(ResponseEvent responseEvent) {
099: }
100:
101: /**
102: * Process the ACK request. Send the bye and complete the call flow.
103: */
104: public void processAck(RequestEvent requestEvent,
105: ServerTransaction serverTransaction) {
106: System.out.println("shootmeA: got an ACK! ");
107: System.out.println("Dialog State = " + dialog.getState());
108: new Timer().schedule(new MyTimerTask(this , true), 4000);
109:
110: }
111:
112: /**
113: * Process the invite request.
114: */
115: public void processInvite(RequestEvent requestEvent,
116: ServerTransaction serverTransaction) {
117: SipProvider sipProvider = (SipProvider) requestEvent
118: .getSource();
119: Request request = requestEvent.getRequest();
120: try {
121: System.out
122: .println("shootmeA: got an Invite sending Trying");
123: // System.out.println("shootmeA: " + request);
124: Response response = messageFactory.createResponse(
125: Response.TRYING, request);
126: ServerTransaction st = requestEvent.getServerTransaction();
127:
128: if (st == null) {
129: st = sipProvider.getNewServerTransaction(request);
130: }
131: dialog = st.getDialog();
132:
133: st.sendResponse(response);
134:
135: this .okResponse = messageFactory.createResponse(
136: Response.OK, request);
137: Address address = addressFactory
138: .createAddress("ShootmeA <sip:" + myAddress + ":"
139: + myPort + ";lr" + ">");
140: ContactHeader contactHeader = headerFactory
141: .createContactHeader(address);
142: response.addHeader(contactHeader);
143: ToHeader toHeader = (ToHeader) okResponse
144: .getHeader(ToHeader.NAME);
145: if (toHeader.getTag() == null) {
146: toHeader.setTag(new Integer(
147: (int) (Math.random() * 10000)).toString()); // Application is supposed to set.
148: } else {
149: System.out.println("Re-INVITE processing");
150: }
151: okResponse.addHeader(contactHeader);
152:
153: // Create ContentTypeHeader
154: ContentTypeHeader contentTypeHeader = headerFactory
155: .createContentTypeHeader("application", "sdp");
156: String sdpData = "v=0\r\n"
157: + "o=4855 13760799956958020 13760799956958020"
158: + " IN IP4 129.6.55.78\r\n"
159: + "s=mysession session\r\n"
160: + "p=+46 8 52018010\r\n"
161: + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n"
162: + "m=audio 6022 RTP/AVP 0 4 18\r\n"
163: + "a=rtpmap:0 PCMU/8000\r\n"
164: + "a=rtpmap:4 G723/8000\r\n"
165: + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
166: byte[] contents = sdpData.getBytes();
167: okResponse.setContent(contents, contentTypeHeader);
168:
169: this .inviteTid = st;
170: this .inviteRequest = request;
171:
172: new Timer().schedule(new MyTimerTask(this , false), 1000);
173: } catch (Exception ex) {
174: ex.printStackTrace();
175: System.exit(0);
176: }
177: }
178:
179: private void sendBye() {
180: try {
181: dialog = this .inviteTid.getDialog();
182: if (dialog.getState() == DialogState.TERMINATED) {
183: System.out.println("Dialog already terminated!");
184: return;
185: }
186: System.out.println("Sending BYE");
187: Request byeRequest = dialog.createRequest(Request.BYE);
188: ClientTransaction ct = sipProvider
189: .getNewClientTransaction(byeRequest);
190: dialog.sendRequest(ct);
191: } catch (SipException ex) {
192: ex.printStackTrace();
193: }
194: }
195:
196: private void sendInviteOK() {
197: try {
198: if (inviteTid.getState() != TransactionState.COMPLETED) {
199: System.out
200: .println("shootmeA: Dialog state before 200: "
201: + inviteTid.getDialog().getState());
202: inviteTid.sendResponse(okResponse);
203: System.out.println(myPort + " Dialog state after 200: "
204: + inviteTid.getDialog().getState());
205: }
206: } catch (SipException ex) {
207: ex.printStackTrace();
208: } catch (InvalidArgumentException ex) {
209: ex.printStackTrace();
210: }
211: }
212:
213: /**
214: * Process the bye request.
215: */
216: public void processBye(RequestEvent requestEvent,
217: ServerTransaction serverTransactionId) {
218: SipProvider sipProvider = (SipProvider) requestEvent
219: .getSource();
220: Request request = requestEvent.getRequest();
221: try {
222: System.out.println(myPort + " got a bye sending OK.");
223: Response response = messageFactory.createResponse(200,
224: request);
225: serverTransactionId.sendResponse(response);
226: System.out.println("Dialog State is "
227: + serverTransactionId.getDialog().getState());
228:
229: } catch (Exception ex) {
230: ex.printStackTrace();
231: System.exit(0);
232:
233: }
234: }
235:
236: public void processCancel(RequestEvent requestEvent,
237: ServerTransaction serverTransactionId) {
238: SipProvider sipProvider = (SipProvider) requestEvent
239: .getSource();
240: Request request = requestEvent.getRequest();
241: try {
242: System.out.println("shootmeA: got a cancel.");
243: if (serverTransactionId == null) {
244: System.out.println("shootmeA: null tid.");
245: return;
246: }
247: Response response = messageFactory.createResponse(200,
248: request);
249: serverTransactionId.sendResponse(response);
250: if (dialog.getState() != DialogState.CONFIRMED) {
251: response = messageFactory.createResponse(
252: Response.REQUEST_TERMINATED, inviteRequest);
253: inviteTid.sendResponse(response);
254: }
255:
256: } catch (Exception ex) {
257: ex.printStackTrace();
258: System.exit(0);
259:
260: }
261: }
262:
263: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
264: Transaction transaction;
265: if (timeoutEvent.isServerTransaction()) {
266: transaction = timeoutEvent.getServerTransaction();
267: } else {
268: transaction = timeoutEvent.getClientTransaction();
269: }
270: System.out.println("state = " + transaction.getState());
271: System.out.println("dialog = " + transaction.getDialog());
272: System.out.println("dialogState = "
273: + transaction.getDialog().getState());
274: System.out.println("Transaction Time out");
275: }
276:
277: public void init() {
278: SipFactory sipFactory = null;
279: sipStack = null;
280: sipFactory = SipFactory.getInstance();
281: sipFactory.setPathName("gov.nist");
282: Properties properties = new Properties();
283: properties
284: .setProperty("javax.sip.STACK_NAME", "phone" + myPort);
285: // You need 16 for logging traces. 32 for debug + traces.
286: // Your code will limp at 32 but it is best for debugging.
287: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",
288: "TRACE");
289: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "phone"
290: + myPort + "debuglog.txt");
291: properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "phone"
292: + myPort + "log.txt");
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: "127.0.0.1", myPort, transport);
315:
316: Phone listener = this ;
317:
318: sipProvider = sipStack.createSipProvider(lp);
319: System.out.println("provider " + sipProvider);
320: sipProvider.addSipListener(listener);
321:
322: } catch (Exception ex) {
323: System.out.println(ex.getMessage());
324: ex.printStackTrace();
325: usage();
326: }
327:
328: }
329:
330: public static void main(String args[]) throws Exception {
331: myPort = Integer.parseInt(args[0]);
332: new Phone().init();
333: }
334:
335: public void processIOException(IOExceptionEvent exceptionEvent) {
336: System.out.println("IOException");
337:
338: }
339:
340: public void processTransactionTerminated(
341: TransactionTerminatedEvent transactionTerminatedEvent) {
342: System.out.println("Transaction terminated event recieved");
343:
344: }
345:
346: public void processDialogTerminated(
347: DialogTerminatedEvent dialogTerminatedEvent) {
348: System.out.println("Dialog terminated event recieved");
349:
350: }
351:
352: }
|