001: package examples.redirect;
002:
003: import javax.sip.*;
004: import javax.sip.address.*;
005: import javax.sip.header.*;
006: import javax.sip.message.*;
007:
008: import org.apache.log4j.ConsoleAppender;
009: import org.apache.log4j.Logger;
010: import org.apache.log4j.SimpleLayout;
011:
012: import java.util.*;
013:
014: import junit.framework.TestCase;
015:
016: /**
017: * This class is a UAC template. Shootist is the guy that shoots and shootme is
018: * the guy that gets shot.
019: *
020: * @author M. Ranganathan
021: */
022:
023: public class Shootme extends TestCase implements SipListener {
024:
025: private ProtocolObjects protocolObjects;
026:
027: private static final String myAddress = "127.0.0.1";
028:
029: public static final int myPort = 5070;
030:
031: protected ServerTransaction inviteTid;
032:
033: private Response okResponse;
034:
035: private Request inviteRequest;
036:
037: private Dialog dialog;
038:
039: private SipProvider sipProvider;
040:
041: private int inviteCount = 0;
042:
043: private int dialogTerminationCount = 0;
044:
045: private int dialogCount;
046:
047: private static Logger logger = Logger.getLogger(Shootme.class);
048:
049: class MyTimerTask extends TimerTask {
050: Shootme shootme;
051:
052: public MyTimerTask(Shootme shootme) {
053: this .shootme = shootme;
054:
055: }
056:
057: public void run() {
058: shootme.sendInviteOK();
059: }
060:
061: }
062:
063: protected static final String usageString = "java "
064: + "examples.shootist.Shootist \n"
065: + ">>>> is your class path set to the root?";
066:
067: public void processRequest(RequestEvent requestEvent) {
068: Request request = requestEvent.getRequest();
069: ServerTransaction serverTransactionId = requestEvent
070: .getServerTransaction();
071:
072: logger.info("\n\nRequest " + request.getMethod()
073: + " received at "
074: + protocolObjects.sipStack.getStackName()
075: + " with server transaction id " + serverTransactionId);
076:
077: if (request.getMethod().equals(Request.INVITE)) {
078: processInvite(requestEvent, serverTransactionId);
079: } else if (request.getMethod().equals(Request.ACK)) {
080: processAck(requestEvent, serverTransactionId);
081: } else {
082: fail("unexpected request recieved");
083: }
084: }
085:
086: public void processResponse(ResponseEvent responseEvent) {
087:
088: Response r = responseEvent.getResponse();
089: logger.info("\n\nResponse " + r.getStatusCode()
090: + " received at "
091: + protocolObjects.sipStack.getStackName());
092:
093: ClientTransaction ct = responseEvent.getClientTransaction();
094: assertNotNull(ct);
095: assertNotNull(ct.getDialog());
096: assertEquals(DialogState.TERMINATED, ct.getDialog().getState());
097: }
098:
099: /**
100: * Process the ACK request. Send the bye and complete the call flow.
101: */
102: public void processAck(RequestEvent requestEvent,
103: ServerTransaction serverTransaction) {
104: try {
105: logger.info("shootme: got an ACK! "
106: + requestEvent.getRequest());
107: logger.info("Dialog State = " + dialog.getState()
108: + " sending BYE ");
109: assertTrue(dialog.getState() == DialogState.CONFIRMED);
110: Request bye = dialog.createRequest(Request.BYE);
111: logger.info("bye request = " + bye);
112:
113: ClientTransaction ct = this .sipProvider
114: .getNewClientTransaction(bye);
115: dialog.sendRequest(ct);
116:
117: // JvB: not yet, set upon receiving BYE OK response
118: // assertEquals( DialogState.TERMINATED, dialog.getState() );
119: } catch (Exception ex) {
120: logger.error("unexpected exception", ex);
121: fail("unexpected exception sending bye");
122: }
123: }
124:
125: /**
126: * Process the invite request.
127: */
128: public void processInvite(RequestEvent requestEvent,
129: ServerTransaction serverTransaction) {
130: SipProvider sipProvider = (SipProvider) requestEvent
131: .getSource();
132: Request request = requestEvent.getRequest();
133: try {
134: this .inviteCount++;
135: logger.info("shootme: got an Invite " + request);
136: assertTrue(request.getHeader(ContactHeader.NAME) != null);
137: Response response = protocolObjects.messageFactory
138: .createResponse(Response.TRYING, request);
139: ToHeader toHeader = (ToHeader) response
140: .getHeader(ToHeader.NAME);
141: Address address = protocolObjects.addressFactory
142: .createAddress("Shootme <sip:" + myAddress + ":"
143: + myPort + ";transport="
144: + protocolObjects.transport + ">");
145: ServerTransaction st = requestEvent.getServerTransaction();
146:
147: if (st == null) {
148: st = sipProvider.getNewServerTransaction(request);
149: }
150: Dialog dialog = st.getDialog();
151:
152: assertTrue(this .dialog != dialog);
153: this .dialogCount++;
154: this .dialog = dialog;
155:
156: logger.info("Shootme: dialog = " + dialog);
157:
158: st.sendResponse(response);
159: ContactHeader contactHeader = protocolObjects.headerFactory
160: .createContactHeader(address);
161:
162: /**
163: * We distinguish here after the display header in the Request URI
164: * to create a final response
165: */
166: if (((SipURI) (request.getRequestURI()))
167: .getParameter("redirection") == null) {
168: Response moved = protocolObjects.messageFactory
169: .createResponse(Response.MOVED_TEMPORARILY,
170: request);
171: moved.addHeader(contactHeader);
172: toHeader = (ToHeader) moved.getHeader(ToHeader.NAME);
173: toHeader.setTag("4321"); // Application is supposed to set.
174: st.sendResponse(moved);
175: // Check that the stack is assigning the right state to the
176: // dialog.
177: assertTrue("dialog state should be terminated", dialog
178: .getState() == DialogState.TERMINATED);
179:
180: } else {
181: Response ringing = protocolObjects.messageFactory
182: .createResponse(Response.RINGING, request);
183: toHeader = (ToHeader) ringing.getHeader(ToHeader.NAME);
184: toHeader.setTag("5432"); // Application is supposed to set.
185: st.sendResponse(ringing);
186: assertTrue("server tx state should be proceeding", st
187: .getState() == TransactionState.PROCEEDING);
188:
189: this .okResponse = protocolObjects.messageFactory
190: .createResponse(Response.OK, request);
191: toHeader = (ToHeader) okResponse
192: .getHeader(ToHeader.NAME);
193: toHeader.setTag("5432"); // Application is supposed to set.
194: okResponse.addHeader(contactHeader);
195: this .inviteTid = st;
196: // Defer sending the OK to simulate the phone ringing.
197: this .inviteRequest = request;
198:
199: new Timer().schedule(new MyTimerTask(this ), 1000);
200:
201: }
202: } catch (Exception ex) {
203: ex.printStackTrace();
204: fail("Error sending response to INVITE");
205: }
206: }
207:
208: private void sendInviteOK() {
209: try {
210: assertTrue(inviteTid.getState() == TransactionState.PROCEEDING);
211:
212: inviteTid.sendResponse(okResponse);
213: logger.info("Dialog = " + inviteTid.getDialog());
214: logger.info("shootme: Dialog state after response: "
215: + okResponse.getStatusCode() + " "
216: + inviteTid.getDialog().getState());
217:
218: assertTrue(inviteTid.getState() == TransactionState.TERMINATED);
219:
220: } catch (SipException ex) {
221: logger.error("unexpected exception", ex);
222: fail("unexpected exception");
223:
224: } catch (InvalidArgumentException ex) {
225: logger.error("unexpceted exception", ex);
226: fail("unexpected exception");
227: }
228: }
229:
230: /**
231: * Process the bye request.
232: */
233: public void processBye(RequestEvent requestEvent,
234: ServerTransaction serverTransactionId) {
235: SipProvider sipProvider = (SipProvider) requestEvent
236: .getSource();
237: Request request = requestEvent.getRequest();
238: try {
239: logger.info("shootme: got a bye sending OK.");
240: Response response = protocolObjects.messageFactory
241: .createResponse(200, request);
242: serverTransactionId.sendResponse(response);
243: logger.info("Dialog State is "
244: + serverTransactionId.getDialog().getState());
245:
246: } catch (Exception ex) {
247: ex.printStackTrace();
248: System.exit(0);
249:
250: }
251: }
252:
253: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
254: Transaction transaction;
255: if (timeoutEvent.isServerTransaction()) {
256: transaction = timeoutEvent.getServerTransaction();
257: } else {
258: transaction = timeoutEvent.getClientTransaction();
259: }
260: logger.info("state = " + transaction.getState());
261: logger.info("dialog = " + transaction.getDialog());
262: logger.info("dialogState = "
263: + transaction.getDialog().getState());
264: logger.info("Transaction Time out");
265: fail("unexpected timeout occured");
266: }
267:
268: public SipProvider createProvider() throws Exception {
269: ListeningPoint lp = protocolObjects.sipStack
270: .createListeningPoint("127.0.0.1", myPort,
271: protocolObjects.transport);
272: this .sipProvider = protocolObjects.sipStack
273: .createSipProvider(lp);
274: return this .sipProvider;
275: }
276:
277: public Shootme(ProtocolObjects protocolObjects) {
278: this .protocolObjects = protocolObjects;
279: }
280:
281: public static void main(String args[]) throws Exception {
282: Shootme shootme = new Shootme(new ProtocolObjects("shootme",
283: true, "udp", ""));
284: logger.addAppender(new ConsoleAppender(new SimpleLayout()));
285: shootme.createProvider();
286: shootme.sipProvider.addSipListener(shootme);
287:
288: }
289:
290: public void processIOException(IOExceptionEvent exceptionEvent) {
291: logger.info("IOException");
292: fail("unexpected exception");
293:
294: }
295:
296: public void processTransactionTerminated(
297: TransactionTerminatedEvent transactionTerminatedEvent) {
298: logger.info("Transaction terminated event recieved");
299:
300: }
301:
302: public void processDialogTerminated(
303: DialogTerminatedEvent dialogTerminatedEvent) {
304: logger.info("Dialog terminated event recieved dialog = "
305: + dialogTerminatedEvent.getDialog());
306: this .dialogTerminationCount++;
307:
308: }
309:
310: public void checkState() {
311: assertTrue(this.dialogTerminationCount == this.dialogCount);
312:
313: }
314:
315: }
|