001: package examples.prack;
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 Shootist implements SipListener {
017:
018: private static SipProvider sipProvider;
019:
020: private static AddressFactory addressFactory;
021:
022: private static MessageFactory messageFactory;
023:
024: private static HeaderFactory headerFactory;
025:
026: private static SipStack sipStack;
027:
028: private ContactHeader contactHeader;
029:
030: private ListeningPoint udpListeningPoint;
031:
032: private ClientTransaction inviteTid;
033:
034: private Dialog dialog;
035:
036: protected static final String usageString = "java "
037: + "examples.shootist.Shootist \n"
038: + ">>>> is your class path set to the root?";
039:
040: private static void usage() {
041: System.out.println(usageString);
042: System.exit(0);
043:
044: }
045:
046: public void processRequest(RequestEvent requestReceivedEvent) {
047: Request request = requestReceivedEvent.getRequest();
048: ServerTransaction serverTransactionId = requestReceivedEvent
049: .getServerTransaction();
050:
051: System.out.println("\n\nRequest " + request.getMethod()
052: + " received at " + sipStack.getStackName()
053: + " with server transaction id " + serverTransactionId);
054:
055: // We are the UAC so the only request we get is the BYE.
056: if (request.getMethod().equals(Request.BYE))
057: processBye(request, serverTransactionId);
058:
059: }
060:
061: public void processBye(Request request,
062: ServerTransaction serverTransactionId) {
063: try {
064: System.out.println("shootist: got a bye .");
065: if (serverTransactionId == null) {
066: System.out.println("shootist: null TID.");
067: return;
068: }
069: Dialog dialog = serverTransactionId.getDialog();
070: System.out.println("Dialog State = " + dialog.getState());
071: Response response = messageFactory.createResponse(200,
072: request);
073: serverTransactionId.sendResponse(response);
074: System.out.println("shootist: Sending OK.");
075: System.out.println("Dialog State = " + dialog.getState());
076:
077: } catch (Exception ex) {
078: ex.printStackTrace();
079: System.exit(0);
080:
081: }
082: }
083:
084: public void processResponse(ResponseEvent responseReceivedEvent) {
085: System.out.println("Got a response");
086: Response response = (Response) responseReceivedEvent
087: .getResponse();
088: ClientTransaction tid = responseReceivedEvent
089: .getClientTransaction();
090: CSeqHeader cseq = (CSeqHeader) response
091: .getHeader(CSeqHeader.NAME);
092:
093: System.out.println("Response received : Status Code = "
094: + response.getStatusCode() + " " + cseq);
095: if (tid == null) {
096: System.out.println("Stray response -- dropping ");
097: return;
098: }
099: System.out.println("transaction state is " + tid.getState());
100: System.out.println("Dialog = " + tid.getDialog());
101: System.out.println("Dialog State is "
102: + tid.getDialog().getState());
103: SipProvider provider = (SipProvider) responseReceivedEvent
104: .getSource();
105:
106: try {
107: if (response.getStatusCode() == Response.OK) {
108: if (cseq.getMethod().equals(Request.INVITE)) {
109: Request ackRequest = dialog
110: .createRequest(Request.ACK);
111: System.out.println("Sending ACK");
112: dialog.sendAck(ackRequest);
113: } else if (cseq.getMethod().equals(Request.CANCEL)) {
114: if (dialog.getState() == DialogState.CONFIRMED) {
115: // oops cancel went in too late. Need to hang up the
116: // dialog.
117: System.out
118: .println("Sending BYE -- cancel went in too late !!");
119: Request byeRequest = dialog
120: .createRequest(Request.BYE);
121: ClientTransaction ct = sipProvider
122: .getNewClientTransaction(byeRequest);
123: dialog.sendRequest(ct);
124:
125: }
126:
127: }
128: } else if (response.getStatusCode() == 183) {
129: RequireHeader requireHeader = (RequireHeader) response
130: .getHeader(RequireHeader.NAME);
131: if (requireHeader.getOptionTag().equalsIgnoreCase(
132: "100rel")) {
133: Dialog dialog = tid.getDialog();
134: Request prackRequest = dialog.createPrack(response);
135: ClientTransaction ct = provider
136: .getNewClientTransaction(prackRequest);
137: dialog.sendRequest(ct);
138:
139: }
140:
141: }
142: } catch (Exception ex) {
143: ex.printStackTrace();
144: System.exit(0);
145: }
146:
147: }
148:
149: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
150:
151: System.out.println("Transaction Time out");
152: }
153:
154: public void sendCancel() {
155: try {
156: System.out.println("Sending cancel");
157: Request cancelRequest = inviteTid.createCancel();
158: ClientTransaction cancelTid = sipProvider
159: .getNewClientTransaction(cancelRequest);
160: cancelTid.sendRequest();
161: } catch (Exception ex) {
162: ex.printStackTrace();
163: }
164: }
165:
166: public void init() {
167: SipFactory sipFactory = null;
168: sipStack = null;
169: sipFactory = SipFactory.getInstance();
170: sipFactory.setPathName("gov.nist");
171: Properties properties = new Properties();
172: // If you want to try TCP transport change the following to
173: String transport = "udp";
174: String peerHostPort = "127.0.0.1:5070";
175: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
176: + "/" + transport);
177: // If you want to use UDP then uncomment this.
178: properties.setProperty("javax.sip.STACK_NAME", "shootist");
179:
180: // The following properties are specific to nist-sip
181: // and are not necessarily part of any other jain-sip
182: // implementation.
183: // You can set a max message size for tcp transport to
184: // guard against denial of service attack.
185: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
186: "shootistdebug.txt");
187: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
188: "shootistlog.txt");
189:
190: // Set to 0 in your production code for max speed.
191: // You need 16 for logging traces. 32 for debug + traces.
192: // Your code will limp at 32 but it is best for debugging.
193: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
194:
195: try {
196: // Create SipStack object
197: sipStack = sipFactory.createSipStack(properties);
198: System.out.println("createSipStack " + sipStack);
199: } catch (PeerUnavailableException e) {
200: // could not find
201: // gov.nist.jain.protocol.ip.sip.SipStackImpl
202: // in the classpath
203: e.printStackTrace();
204: System.err.println(e.getMessage());
205: System.exit(0);
206: }
207:
208: try {
209: headerFactory = sipFactory.createHeaderFactory();
210: addressFactory = sipFactory.createAddressFactory();
211: messageFactory = sipFactory.createMessageFactory();
212: udpListeningPoint = sipStack.createListeningPoint(
213: "127.0.0.1", 5060, "udp");
214: sipProvider = sipStack.createSipProvider(udpListeningPoint);
215: Shootist listener = this ;
216: sipProvider.addSipListener(listener);
217:
218: String fromName = "BigGuy";
219: String fromSipAddress = "here.com";
220: String fromDisplayName = "The Master Blaster";
221:
222: String toSipAddress = "there.com";
223: String toUser = "LittleGuy";
224: String toDisplayName = "The Little Blister";
225:
226: // create >From Header
227: SipURI fromAddress = addressFactory.createSipURI(fromName,
228: fromSipAddress);
229:
230: Address fromNameAddress = addressFactory
231: .createAddress(fromAddress);
232: fromNameAddress.setDisplayName(fromDisplayName);
233: FromHeader fromHeader = headerFactory.createFromHeader(
234: fromNameAddress, "12345");
235:
236: // create To Header
237: SipURI toAddress = addressFactory.createSipURI(toUser,
238: toSipAddress);
239: Address toNameAddress = addressFactory
240: .createAddress(toAddress);
241: toNameAddress.setDisplayName(toDisplayName);
242: ToHeader toHeader = headerFactory.createToHeader(
243: toNameAddress, null);
244:
245: // create Request URI
246: SipURI requestURI = addressFactory.createSipURI(toUser,
247: peerHostPort);
248:
249: // Create ViaHeaders
250:
251: ArrayList viaHeaders = new ArrayList();
252: ViaHeader viaHeader = headerFactory.createViaHeader(
253: "127.0.0.1", sipProvider.getListeningPoint(
254: transport).getPort(), transport, null);
255:
256: // add via headers
257: viaHeaders.add(viaHeader);
258:
259: // Create ContentTypeHeader
260: ContentTypeHeader contentTypeHeader = headerFactory
261: .createContentTypeHeader("application", "sdp");
262:
263: // Create a new CallId header
264: CallIdHeader callIdHeader = sipProvider.getNewCallId();
265:
266: // Create a new Cseq header
267: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
268: Request.INVITE);
269:
270: // Create a new MaxForwardsHeader
271: MaxForwardsHeader maxForwards = headerFactory
272: .createMaxForwardsHeader(70);
273:
274: // Create the request.
275: Request request = messageFactory.createRequest(requestURI,
276: Request.INVITE, callIdHeader, cSeqHeader,
277: fromHeader, toHeader, viaHeaders, maxForwards);
278: // Create contact headers
279: String host = "127.0.0.1";
280:
281: SipURI contactUrl = addressFactory.createSipURI(fromName,
282: host);
283: contactUrl.setPort(udpListeningPoint.getPort());
284:
285: // Create the contact name address.
286: SipURI contactURI = addressFactory.createSipURI(fromName,
287: host);
288: contactURI.setPort(sipProvider.getListeningPoint(transport)
289: .getPort());
290:
291: Address contactAddress = addressFactory
292: .createAddress(contactURI);
293:
294: // Add the contact address.
295: contactAddress.setDisplayName(fromName);
296:
297: contactHeader = headerFactory
298: .createContactHeader(contactAddress);
299: request.addHeader(contactHeader);
300:
301: /*
302: * When the UAC creates a new request, it can insist on reliable
303: * delivery of provisional responses for that request. To do that,
304: * it inserts a Require header field with the option tag 100rel into
305: * the request.
306: */
307:
308: RequireHeader requireHeader = headerFactory
309: .createRequireHeader("100rel");
310: request.addHeader(requireHeader);
311: // Create the client transaction.
312: inviteTid = sipProvider.getNewClientTransaction(request);
313:
314: // send the request out.
315: inviteTid.sendRequest();
316:
317: dialog = inviteTid.getDialog();
318:
319: } catch (Exception ex) {
320: System.out.println(ex.getMessage());
321: ex.printStackTrace();
322: usage();
323: }
324: }
325:
326: public static void main(String args[]) {
327: new Shootist().init();
328:
329: }
330:
331: public void processIOException(IOExceptionEvent exceptionEvent) {
332: System.out.println("IOException happened for "
333: + exceptionEvent.getHost() + " port = "
334: + exceptionEvent.getPort());
335:
336: }
337:
338: public void processTransactionTerminated(
339: TransactionTerminatedEvent transactionTerminatedEvent) {
340: System.out.println("Transaction terminated event recieved");
341: }
342:
343: public void processDialogTerminated(
344: DialogTerminatedEvent dialogTerminatedEvent) {
345: System.out.println("dialogTerminatedEvent");
346:
347: }
348: }
|