001: package examples.busy;
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:
104: try {
105: if (response.getStatusCode() == Response.OK) {
106: if (cseq.getMethod().equals(Request.INVITE)) {
107: Request ackRequest = dialog
108: .createRequest(Request.ACK);
109: System.out.println("Sending ACK");
110: dialog.sendAck(ackRequest);
111: } else if (cseq.getMethod().equals(Request.CANCEL)) {
112: if (dialog.getState() == DialogState.CONFIRMED) {
113: // oops cancel went in too late. Need to hang up the
114: // dialog.
115: System.out
116: .println("Sending BYE -- cancel went in too late !!");
117: Request byeRequest = dialog
118: .createRequest(Request.BYE);
119: ClientTransaction ct = sipProvider
120: .getNewClientTransaction(byeRequest);
121: dialog.sendRequest(ct);
122:
123: }
124:
125: }
126: }
127: } catch (Exception ex) {
128: ex.printStackTrace();
129: System.exit(0);
130: }
131:
132: }
133:
134: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
135:
136: System.out.println("Transaction Time out");
137: }
138:
139: public void sendCancel() {
140: try {
141: System.out.println("Sending cancel");
142: Request cancelRequest = inviteTid.createCancel();
143: ClientTransaction cancelTid = sipProvider
144: .getNewClientTransaction(cancelRequest);
145: cancelTid.sendRequest();
146: } catch (Exception ex) {
147: ex.printStackTrace();
148: }
149: }
150:
151: public void init() {
152: SipFactory sipFactory = null;
153: sipStack = null;
154: sipFactory = SipFactory.getInstance();
155: sipFactory.setPathName("gov.nist");
156: Properties properties = new Properties();
157: // If you want to try TCP transport change the following to
158: String transport = "udp";
159: String peerHostPort = "127.0.0.1:5070";
160: properties.setProperty("javax.sip.OUTBOUND_PROXY", peerHostPort
161: + "/" + transport);
162: // If you want to use UDP then uncomment this.
163: properties.setProperty("javax.sip.STACK_NAME", "shootist");
164:
165: // The following properties are specific to nist-sip
166: // and are not necessarily part of any other jain-sip
167: // implementation.
168: // You can set a max message size for tcp transport to
169: // guard against denial of service attack.
170: properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
171: "1048576");
172: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
173: "shootistdebuglog.txt");
174: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
175: "shootistlog.txt");
176:
177: // Drop the client connection after we are done with the transaction.
178: properties.setProperty(
179: "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
180: // Set to 0 in your production code for max speed.
181: // You need 16 for logging traces. 32 for debug + traces.
182: // Your code will limp at 32 but it is best for debugging.
183: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL",
184: "DEBUG");
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: udpListeningPoint = sipStack.createListeningPoint(
204: "127.0.0.1", 5060, "udp");
205: sipProvider = sipStack.createSipProvider(udpListeningPoint);
206: Shootist listener = this ;
207: sipProvider.addSipListener(listener);
208:
209: String fromName = "BigGuy";
210: String fromSipAddress = "here.com";
211: String fromDisplayName = "The Master Blaster";
212:
213: String toSipAddress = "there.com";
214: String toUser = "LittleGuy";
215: String toDisplayName = "The Little Blister";
216:
217: // create >From Header
218: SipURI fromAddress = addressFactory.createSipURI(fromName,
219: fromSipAddress);
220:
221: Address fromNameAddress = addressFactory
222: .createAddress(fromAddress);
223: fromNameAddress.setDisplayName(fromDisplayName);
224: FromHeader fromHeader = headerFactory.createFromHeader(
225: fromNameAddress, "12345");
226:
227: // create To Header
228: SipURI toAddress = addressFactory.createSipURI(toUser,
229: toSipAddress);
230: Address toNameAddress = addressFactory
231: .createAddress(toAddress);
232: toNameAddress.setDisplayName(toDisplayName);
233: ToHeader toHeader = headerFactory.createToHeader(
234: toNameAddress, null);
235:
236: // create Request URI
237: SipURI requestURI = addressFactory.createSipURI(toUser,
238: peerHostPort);
239:
240: // Create ViaHeaders
241:
242: ArrayList viaHeaders = new ArrayList();
243: ViaHeader viaHeader = headerFactory.createViaHeader(
244: "127.0.0.1", sipProvider.getListeningPoint(
245: transport).getPort(), transport, null);
246:
247: // add via headers
248: viaHeaders.add(viaHeader);
249:
250: // Create ContentTypeHeader
251: ContentTypeHeader contentTypeHeader = headerFactory
252: .createContentTypeHeader("application", "sdp");
253:
254: // Create a new CallId header
255: CallIdHeader callIdHeader = sipProvider.getNewCallId();
256:
257: // Create a new Cseq header
258: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
259: Request.INVITE);
260:
261: // Create a new MaxForwardsHeader
262: MaxForwardsHeader maxForwards = headerFactory
263: .createMaxForwardsHeader(70);
264:
265: // Create the request.
266: Request request = messageFactory.createRequest(requestURI,
267: Request.INVITE, callIdHeader, cSeqHeader,
268: fromHeader, toHeader, viaHeaders, maxForwards);
269: // Create contact headers
270: String host = "127.0.0.1";
271:
272: SipURI contactUrl = addressFactory.createSipURI(fromName,
273: host);
274: contactUrl.setPort(udpListeningPoint.getPort());
275:
276: // Create the contact name address.
277: SipURI contactURI = addressFactory.createSipURI(fromName,
278: host);
279: contactURI.setPort(sipProvider.getListeningPoint(transport)
280: .getPort());
281:
282: Address contactAddress = addressFactory
283: .createAddress(contactURI);
284:
285: // Add the contact address.
286: contactAddress.setDisplayName(fromName);
287:
288: contactHeader = headerFactory
289: .createContactHeader(contactAddress);
290: request.addHeader(contactHeader);
291:
292: // Add the extension header.
293: Header extensionHeader = headerFactory.createHeader(
294: "My-Header", "my header value");
295: request.addHeader(extensionHeader);
296:
297: String sdpData = "v=0\r\n"
298: + "o=4855 13760799956958020 13760799956958020"
299: + " IN IP4 129.6.55.78\r\n"
300: + "s=mysession session\r\n"
301: + "p=+46 8 52018010\r\n"
302: + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n"
303: + "m=audio 6022 RTP/AVP 0 4 18\r\n"
304: + "a=rtpmap:0 PCMU/8000\r\n"
305: + "a=rtpmap:4 G723/8000\r\n"
306: + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
307: byte[] contents = sdpData.getBytes();
308:
309: request.setContent(contents, contentTypeHeader);
310:
311: extensionHeader = headerFactory.createHeader(
312: "My-Other-Header", "my new header value ");
313: request.addHeader(extensionHeader);
314:
315: Header callInfoHeader = headerFactory.createHeader(
316: "Call-Info", "<http://www.antd.nist.gov>");
317: request.addHeader(callInfoHeader);
318:
319: // Create the client transaction.
320: inviteTid = sipProvider.getNewClientTransaction(request);
321:
322: // send the request out.
323: inviteTid.sendRequest();
324:
325: dialog = inviteTid.getDialog();
326:
327: } catch (Exception ex) {
328: System.out.println(ex.getMessage());
329: ex.printStackTrace();
330: usage();
331: }
332: }
333:
334: public static void main(String args[]) {
335: new Shootist().init();
336:
337: }
338:
339: public void processIOException(IOExceptionEvent exceptionEvent) {
340: System.out.println("IOException happened for "
341: + exceptionEvent.getHost() + " port = "
342: + exceptionEvent.getPort());
343:
344: }
345:
346: public void processTransactionTerminated(
347: TransactionTerminatedEvent transactionTerminatedEvent) {
348: System.out.println("Transaction terminated event recieved");
349: }
350:
351: public void processDialogTerminated(
352: DialogTerminatedEvent dialogTerminatedEvent) {
353: System.out.println("dialogTerminatedEvent");
354:
355: }
356: }
|