001: package examples.publish;
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: //ifdef SIMULATION
010: /*
011: import sim.java.*;
012: //endif
013: */
014:
015: /**
016: * This class is a UAC template. Shootist is the guy that shoots and shootme
017: * is the guy that gets shot.
018: *
019: *@author M. Ranganathan
020: */
021:
022: public class Subscriber implements SipListener {
023:
024: private static SipProvider sipProvider;
025: private static AddressFactory addressFactory;
026: private static MessageFactory messageFactory;
027: private static HeaderFactory headerFactory;
028: private static SipStack sipStack;
029: private ContactHeader contactHeader;
030:
031: protected ClientTransaction subscribeTid;
032:
033: protected static final String usageString = "java "
034: + "examples.subsnotify.Subscriber \n"
035: + ">>>> is your class path set to the root?";
036:
037: private static void usage() {
038: System.out.println(usageString);
039: System.exit(0);
040:
041: }
042:
043: public void processRequest(RequestEvent requestReceivedEvent) {
044: Request request = requestReceivedEvent.getRequest();
045: ServerTransaction serverTransactionId = requestReceivedEvent
046: .getServerTransaction();
047:
048: System.out.println("\n\nRequest " + request.getMethod()
049: + " received at " + sipStack.getStackName()
050: + " with server transaction id " + serverTransactionId);
051:
052: if (request.getMethod().equals(Request.NOTIFY))
053: processNotify(request, serverTransactionId);
054:
055: }
056:
057: public void processNotify(Request request,
058: ServerTransaction serverTransactionId) {
059: try {
060: System.out.println("subscriber: got a notify .");
061: if (serverTransactionId == null) {
062: System.out.println("subscriber: null TID.");
063: return;
064: }
065: Dialog dialog = serverTransactionId.getDialog();
066: System.out.println("Dialog State = " + dialog.getState());
067: Response response = messageFactory.createResponse(200,
068: request);
069: serverTransactionId.sendResponse(response);
070: SubscriptionStateHeader subscriptionState = (SubscriptionStateHeader) request
071: .getHeader(SubscriptionStateHeader.NAME);
072: // Subscription is terminated.
073: if (subscriptionState.getState().equals(
074: SubscriptionStateHeader.TERMINATED)) {
075: dialog.delete();
076: }
077:
078: } catch (Exception ex) {
079: ex.printStackTrace();
080: System.exit(0);
081:
082: }
083: }
084:
085: public void processResponse(ResponseEvent responseReceivedEvent) {
086: System.out.println("Got a response");
087: Response response = (Response) responseReceivedEvent
088: .getResponse();
089: Transaction tid = responseReceivedEvent.getClientTransaction();
090:
091: System.out
092: .println("Response received with client transaction id "
093: + tid + ":\n" + response);
094: if (tid == null) {
095: System.out.println("Stray response -- dropping ");
096: return;
097: }
098: System.out.println("transaction state is " + tid.getState());
099: System.out.println("Dialog = " + tid.getDialog());
100: System.out.println("Dialog State is "
101: + tid.getDialog().getState());
102:
103: }
104:
105: public void init() {
106: SipFactory sipFactory = null;
107: sipStack = null;
108: sipProvider = null;
109: sipFactory = SipFactory.getInstance();
110: sipFactory.setPathName("gov.nist");
111: Properties properties = new Properties();
112: String transport = "udp";
113: properties.setProperty("javax.sip.OUTBOUND_PROXY",
114: "127.0.0.1:5070/" + transport);
115: properties.setProperty("javax.sip.STACK_NAME", "subscriber");
116: properties.setProperty("javax.sip.MAX_MESSAGE_SIZE", "1048576");
117: properties.setProperty("gov.nist.javax.sip.DEBUG_LOG",
118: "subscriberdebug.txt");
119: properties.setProperty("gov.nist.javax.sip.SERVER_LOG",
120: "subscriberlog.txt");
121: // Drop the client connection after we are done with the transaction.
122: properties.setProperty(
123: "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS", "false");
124: // Set to 0 in your production code for max speed.
125: // You need 16 for logging traces. 32 for debug + traces.
126: // Your code will limp at 32 but it is best for debugging.
127: properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "32");
128:
129: try {
130: // Create SipStack object
131: sipStack = sipFactory.createSipStack(properties);
132: System.out.println("createSipStack " + sipStack);
133: } catch (PeerUnavailableException e) {
134: // could not find
135: // gov.nist.jain.protocol.ip.sip.SipStackImpl
136: // in the classpath
137: e.printStackTrace();
138: System.err.println(e.getMessage());
139: System.exit(0);
140: }
141:
142: try {
143: headerFactory = sipFactory.createHeaderFactory();
144: addressFactory = sipFactory.createAddressFactory();
145: messageFactory = sipFactory.createMessageFactory();
146: ListeningPoint lp = sipStack.createListeningPoint(
147: "127.0.0.1", 5060, "udp");
148: sipProvider = sipStack.createSipProvider(lp);
149: Subscriber listener = this ;
150: sipProvider.addSipListener(listener);
151: lp = sipStack
152: .createListeningPoint("127.0.0.1", 5060, "tcp");
153: SipProvider sipProvider1 = sipStack.createSipProvider(lp);
154: sipProvider1.addSipListener(listener);
155: String fromName = "BigGuy";
156: String fromSipAddress = "here.com";
157: String fromDisplayName = "The Master Blaster";
158:
159: String toSipAddress = "there.com";
160: String toUser = "LittleGuy";
161: String toDisplayName = "The Little Blister";
162:
163: // create >From Header
164: SipURI fromAddress = addressFactory.createSipURI(fromName,
165: fromSipAddress);
166:
167: Address fromNameAddress = addressFactory
168: .createAddress(fromAddress);
169: fromNameAddress.setDisplayName(fromDisplayName);
170: FromHeader fromHeader = headerFactory.createFromHeader(
171: fromNameAddress, "12345");
172:
173: // create To Header
174: SipURI toAddress = addressFactory.createSipURI(toUser,
175: toSipAddress);
176: Address toNameAddress = addressFactory
177: .createAddress(toAddress);
178: toNameAddress.setDisplayName(toDisplayName);
179: ToHeader toHeader = headerFactory.createToHeader(
180: toNameAddress, null);
181:
182: // create Request URI
183: SipURI requestURI = addressFactory.createSipURI(toUser,
184: toSipAddress);
185:
186: // Create ViaHeaders
187:
188: ArrayList viaHeaders = new ArrayList();
189: int port = sipProvider.getListeningPoint("udp").getPort();
190: ViaHeader viaHeader = headerFactory.createViaHeader(
191: "127.0.0.1", port, transport, null);
192:
193: // add via headers
194: viaHeaders.add(viaHeader);
195:
196: // Create a new CallId header
197: CallIdHeader callIdHeader = sipProvider.getNewCallId();
198:
199: // Create a new Cseq header
200: CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L,
201: Request.SUBSCRIBE);
202:
203: // Create a new MaxForwardsHeader
204: MaxForwardsHeader maxForwards = headerFactory
205: .createMaxForwardsHeader(70);
206:
207: // Create the request.
208: Request request = messageFactory.createRequest(requestURI,
209: Request.SUBSCRIBE, callIdHeader, cSeqHeader,
210: fromHeader, toHeader, viaHeaders, maxForwards);
211: // Create contact headers
212: String host = lp.getIPAddress();
213:
214: SipURI contactUrl = addressFactory.createSipURI(fromName,
215: host);
216: contactUrl.setPort(lp.getPort());
217:
218: // Create the contact name address.
219: SipURI contactURI = addressFactory.createSipURI(fromName,
220: host);
221: contactURI.setPort(sipProvider.getListeningPoint("udp")
222: .getPort());
223:
224: Address contactAddress = addressFactory
225: .createAddress(contactURI);
226:
227: // Add the contact address.
228: contactAddress.setDisplayName(fromName);
229:
230: contactHeader = headerFactory
231: .createContactHeader(contactAddress);
232: request.addHeader(contactHeader);
233:
234: // Create the client transaction.
235: listener.subscribeTid = sipProvider
236: .getNewClientTransaction(request);
237:
238: // send the request out.
239: listener.subscribeTid.sendRequest();
240:
241: } catch (Exception ex) {
242: System.out.println(ex.getMessage());
243: ex.printStackTrace();
244: usage();
245: }
246: }
247:
248: public static void main(String args[]) {
249: new Subscriber().init();
250:
251: }
252:
253: public void processIOException(IOExceptionEvent exceptionEvent) {
254: System.out.println("io exception event recieved");
255: }
256:
257: public void processTransactionTerminated(
258: TransactionTerminatedEvent transactionTerminatedEvent) {
259: System.out.println("transaction terminated");
260:
261: }
262:
263: public void processDialogTerminated(
264: DialogTerminatedEvent dialogTerminatedEvent) {
265: System.out.println("dialog terminated event recieved");
266: }
267:
268: public void processTimeout(javax.sip.TimeoutEvent timeoutEvent) {
269:
270: System.out.println("Transaction Time out");
271: }
272: }
|