001: package test.load.leakcheck.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 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 final String myAddress = "127.0.0.1";
027:
028: private static final int myPort = 5070;
029:
030: protected ServerTransaction inviteTid;
031:
032: private int counter;
033:
034: class Ttask extends TimerTask {
035:
036: public Ttask() {
037: new Timer().schedule(this , 0, 20000);
038: }
039:
040: /*
041: * (non-Javadoc)
042: *
043: * @see java.util.TimerTask#run()
044: */
045: public void run() {
046: System.gc();
047: long freemem = Runtime.getRuntime().totalMemory();
048: System.out.println("mem = " + freemem);
049: }
050:
051: }
052:
053: class ApplicationData {
054: protected int ackCount;
055: }
056:
057: protected static final String usageString = "java "
058: + "examples.shootist.Shootist \n"
059: + ">>>> is your class path set to the root?";
060:
061: private static void usage() {
062: System.out.println(usageString);
063: System.exit(0);
064:
065: }
066:
067: public void processRequest(RequestEvent requestEvent) {
068: Request request = requestEvent.getRequest();
069: ServerTransaction serverTransactionId = requestEvent
070: .getServerTransaction();
071:
072: /**
073: * System.out.println("\n\nRequest " + request.getMethod() + " received
074: * at " + sipStack.getStackName() + " with server transaction id " +
075: * serverTransactionId);
076: */
077:
078: if (request.getMethod().equals(Request.INVITE)) {
079: processInvite(requestEvent, serverTransactionId);
080: } else if (request.getMethod().equals(Request.ACK)) {
081: processAck(requestEvent, serverTransactionId);
082: } else if (request.getMethod().equals(Request.BYE)) {
083: processBye(requestEvent, serverTransactionId);
084: }
085:
086: }
087:
088: /**
089: * Process the ACK request. Send the bye and complete the call flow.
090: */
091: public void processAck(RequestEvent requestEvent,
092: ServerTransaction serverTransaction) {
093: SipProvider sipProvider = (SipProvider) requestEvent
094: .getSource();
095: System.out.println("sip provider: " + sipProvider);
096: try {
097: System.out.println("shootme: got an ACK "
098: + requestEvent.getRequest());
099: } catch (Exception ex) {
100: ex.printStackTrace();
101: System.exit(0);
102: }
103: }
104:
105: /**
106: * Process the invite request.
107: */
108: public void processInvite(RequestEvent requestEvent,
109: ServerTransaction serverTransaction) {
110: SipProvider sipProvider = (SipProvider) requestEvent
111: .getSource();
112: Request request = requestEvent.getRequest();
113:
114: if (counter == 99)
115: new Ttask();
116: this .counter++;
117:
118: try {
119:
120: Response response = messageFactory.createResponse(
121: Response.BUSY_HERE, request);
122: ToHeader toHeader = (ToHeader) response
123: .getHeader(ToHeader.NAME);
124: toHeader.setTag("4321"); // Application is supposed to set.
125: Address address = addressFactory
126: .createAddress("Shootme <sip:" + myAddress + ":"
127: + myPort + ">");
128: ContactHeader contactHeader = headerFactory
129: .createContactHeader(address);
130: response.addHeader(contactHeader);
131: if (serverTransaction == null) {
132: serverTransaction = sipProvider
133: .getNewServerTransaction(request);
134: }
135: serverTransaction.sendResponse(response);
136:
137: } catch (Exception ex) {
138: ex.printStackTrace();
139: System.exit(0);
140: }
141: }
142:
143: /**
144: * Process the bye request.
145: */
146: public void processBye(RequestEvent requestEvent,
147: ServerTransaction serverTransactionId) {
148: SipProvider sipProvider = (SipProvider) requestEvent
149: .getSource();
150: System.out.println("sip provider: " + sipProvider);
151: Request request = requestEvent.getRequest();
152: try {
153: System.out.println("shootme: got a bye sending OK.");
154: Response response = messageFactory.createResponse(200,
155: request, null, null);
156: serverTransactionId.sendResponse(response);
157: System.out.println("Dialog State is "
158: + serverTransactionId.getDialog().getState());
159:
160: } catch (Exception ex) {
161: ex.printStackTrace();
162: System.exit(0);
163:
164: }
165: }
166:
167: public void processResponse(ResponseEvent responseReceivedEvent) {
168: System.out.println("Got a response");
169: Response response = (Response) responseReceivedEvent
170: .getResponse();
171: Transaction tid = responseReceivedEvent.getClientTransaction();
172:
173: System.out
174: .println("Response received with client transaction id "
175: + tid + ":\n" + response);
176: try {
177: if (response.getStatusCode() == Response.BUSY_HERE
178: && ((CSeqHeader) response
179: .getHeader(CSeqHeader.NAME)).getMethod()
180: .equals(Request.INVITE)) {
181: if (tid != this .inviteTid) {
182: new Exception().printStackTrace();
183: System.exit(0);
184: }
185: Dialog dialog = tid.getDialog();
186: // Save the tags for the dialog here.
187: Request request = tid.getRequest();
188: dialog.sendAck(request);
189: }
190: Dialog dialog = tid.getDialog();
191: System.out.println("Dalog State = " + dialog.getState());
192: } catch (Exception ex) {
193: ex.printStackTrace();
194: System.exit(0);
195: }
196:
197: }
198:
199: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
200: Transaction transaction;
201: if (timeoutEvent.isServerTransaction()) {
202: transaction = timeoutEvent.getServerTransaction();
203: } else {
204: transaction = timeoutEvent.getClientTransaction();
205: }
206: System.out.println("state = " + transaction.getState());
207: System.out.println("dialog = " + transaction.getDialog());
208: System.out.println("dialogState = "
209: + transaction.getDialog().getState());
210: System.out.println("Transaction Time out");
211: }
212:
213: public void init() {
214: SipFactory sipFactory = null;
215: sipStack = null;
216: sipFactory = SipFactory.getInstance();
217: sipFactory.setPathName("gov.nist");
218: Properties properties = new Properties();
219: //ifdef SIMULATION
220: /*
221: * properties.setProperty("javax.sip.IP_ADDRESS","129.6.55.62"); //else
222: */
223: properties.setProperty("javax.sip.IP_ADDRESS", myAddress);
224: //endif
225: //
226: properties.setProperty("javax.sip.RETRANSMISSION_FILTER",
227: "true");
228: properties.setProperty("javax.sip.STACK_NAME", "shootme");
229: // You need 16 for logging traces. 32 for debug + traces.
230: // Your code will limp at 32 but it is best for debugging.
231: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "0");
232: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
233: "shootmedebug.txt");
234: // Guard against starvation.
235: properties.setProperty("gov.nist.javax.sip.READ_TIMEOUT",
236: "1000");
237: // properties.setProperty("gov.nist.javax.sip.MAX_MESSAGE_SIZE",
238: // "4096");
239: properties.setProperty(
240: "gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS", "false");
241:
242: try {
243: // Create SipStack object
244: sipStack = sipFactory.createSipStack(properties);
245: System.out.println("sipStack = " + sipStack);
246: } catch (PeerUnavailableException e) {
247: // could not find
248: // gov.nist.jain.protocol.ip.sip.SipStackImpl
249: // in the classpath
250: e.printStackTrace();
251: System.err.println(e.getMessage());
252: if (e.getCause() != null)
253: e.getCause().printStackTrace();
254: System.exit(0);
255: }
256:
257: try {
258: headerFactory = sipFactory.createHeaderFactory();
259: addressFactory = sipFactory.createAddressFactory();
260: messageFactory = sipFactory.createMessageFactory();
261: ListeningPoint lp = sipStack.createListeningPoint(sipStack
262: .getIPAddress(), 5070, "udp");
263: ListeningPoint lp1 = sipStack.createListeningPoint(sipStack
264: .getIPAddress(), 5070, "tcp");
265:
266: Shootme listener = this ;
267:
268: SipProvider sipProvider = sipStack.createSipProvider(lp);
269: System.out.println("udp provider " + sipProvider);
270: sipProvider.addSipListener(listener);
271: sipProvider = sipStack.createSipProvider(lp1);
272: System.out.println("tcp provider " + sipProvider);
273: sipProvider.addSipListener(listener);
274:
275: System.gc();
276: long freemem = Runtime.getRuntime().totalMemory();
277: System.out.println("Current memory" + freemem);
278:
279: } catch (Exception ex) {
280: System.out.println(ex.getMessage());
281: ex.printStackTrace();
282: usage();
283: }
284:
285: }
286:
287: public void processIOException(IOExceptionEvent exceptionEvent) {
288: System.out
289: .println("IOException occured while retransmitting requests:"
290: + exceptionEvent);
291: }
292:
293: public void processTransactionTerminated(
294: TransactionTerminatedEvent transactionTerminatedEvent) {
295: System.out.println("Transaction Terminated event: "
296: + transactionTerminatedEvent);
297: }
298:
299: public void processDialogTerminated(
300: DialogTerminatedEvent dialogTerminatedEvent) {
301: System.out.println("Dialog Terminated event: "
302: + dialogTerminatedEvent);
303: }
304:
305: public static void main(String args[]) {
306: new Shootme().init();
307: }
308:
309: }
|