Source code: com/opencloud/slee/services/sip/registrar/RegistrarSbb.java
1 package com.opencloud.slee.services.sip.registrar;
2
3 import org.mobicents.slee.container.SleeContainer;
4 import org.mobicents.slee.resource.sip.SipFactoryProvider;
5 //import org.mobicents.slee.test.SleeUnitTest;
6 //import org.mobicents.slee.test.TestResult;
7
8 import javax.naming.Context;
9 import javax.naming.InitialContext;
10 import javax.naming.NamingException;
11 import javax.sip.RequestEvent;
12 import javax.sip.ServerTransaction;
13 import javax.sip.SipProvider;
14 import javax.sip.TimeoutEvent;
15 import javax.sip.address.Address;
16 import javax.sip.address.AddressFactory;
17 import javax.sip.address.SipURI;
18 import javax.sip.header.ContactHeader;
19 import javax.sip.header.HeaderFactory;
20 import javax.sip.message.MessageFactory;
21 import javax.sip.message.Request;
22 import javax.sip.message.Response;
23 import javax.slee.ActivityContextInterface;
24 import javax.slee.CreateException;
25 import javax.slee.RolledBackContext;
26 import javax.slee.Sbb;
27 import javax.slee.SbbContext;
28 import javax.slee.SbbID;
29 import javax.slee.SbbLocalObject;
30 import javax.slee.facilities.AlarmFacility;
31 import javax.slee.facilities.Level;
32 import javax.slee.facilities.TimerEvent;
33 import javax.slee.facilities.TimerFacility;
34 import javax.slee.facilities.TimerID;
35 import javax.slee.facilities.TimerOptions;
36 import javax.slee.facilities.TraceFacility;
37 import javax.slee.nullactivity.NullActivity;
38 import javax.slee.nullactivity.NullActivityContextInterfaceFactory;
39 import javax.slee.nullactivity.NullActivityFactory;
40 import javax.slee.profile.ProfileFacility;
41
42 import org.jboss.logging.Logger;
43
44
45
46
47
48
49
50 /**
51 *
52 * this is the abstract class that will be deployed
53 *
54 * Sbb abstract class provided by the Sbb developer
55 *
56 * @author F.Moggia
57 */
58 public abstract class RegistrarSbb implements Sbb {
59 //private static Logger log;
60
61 // For test only -- to test post conditions.
62 //private SleeContainer myServiceContainer;
63
64 private String errorMessage;
65
66 private Address contactAddress;
67
68 private SipURI requestURI;
69
70
71 /*static {
72 log = Logger.getLogger(RegistrarSbb.class);
73 }*/
74
75 private void processRequest(ServerTransaction serverTransaction, Request request) {
76 trace(Level.FINEST, "processRequest: request = >>>>>>>>>\n" + request.toString() + "\n<<<<<<<<");
77 try {
78 // Create a worker to process this event
79 MyRegistrationHandler handler = new MyRegistrationHandler(this);
80 // Go
81 handler.processRequest(serverTransaction, request);
82
83 } catch (Exception e) {
84 // Send error response so client can deal with it
85 //e.printStackTrace();
86 trace(Level.WARNING, "Exception during processRequest", e);
87 try {
88 serverTransaction.sendResponse(getMessageFactory().createResponse(Response.SERVER_INTERNAL_ERROR, request));
89 } catch (Exception ex) {
90 ex.printStackTrace();
91 }
92 }
93 }
94
95 public void onRegisterEvent(RequestEvent event, ActivityContextInterface ac) {
96 trace(Level.INFO, "SipRegistrarSBB: " + this + ": got register event");
97 try {
98 ServerTransaction serverTransactionId = (ServerTransaction)ac.getActivity();
99 Request request = event.getRequest();
100 processRequest(serverTransactionId, request);
101
102 } catch (Exception e) {
103 // Send error response so client can deal with it
104 //e.printStackTrace();
105 trace(Level.WARNING, "Exception during onRegisterEvent", e);
106 }
107 }
108
109 public void onTimerEvent( TimerEvent event, ActivityContextInterface ac ) {
110 trace(Level.INFO, "Received timer event");
111
112 try {
113 RegistrarActivityContextInterface regACI = asSbbActivityContextInterface(ac);
114
115 MyRegistrationHandler handler = new MyRegistrationHandler(this);
116
117 handler.expireRegistration(regACI.getSipAddress(), regACI.getSipContactAddress(), regACI.getCallId(), regACI.getCSeq());
118
119 // detach if no more events to come
120 if (event.getRemainingRepetitions() == 0) regACI.detach(getSbbLocalObject());
121
122 } catch (Exception e) {
123 trace(Level.WARNING, "Exception during onTimerEvent", e);
124 }
125 }
126 /*
127 public void onRegister(RequestEvent event, ActivityContextInterface aci) {
128 try {
129 RegistrarActivityContextInterface regACI = asSbbActivityContextInterface(aci);
130 String result = regACI.getUserName();
131 ServerTransaction serverTransactionId = event
132 .getServerTransaction();
133 Request request = event.getRequest();
134
135 ServerTransaction st = (ServerTransaction)aci.getActivity();
136 Response response = messageFactory.createResponse(Response.OK, request);
137 testResult = new TestResult(request,response);
138 if (this.errorMessage != null){
139 testResult.appendMessage(this.errorMessage);
140 }
141 st.sendResponse(response);
142 contactAddress = ((ContactHeader) request.getHeader(ContactHeader.NAME)).getAddress();
143
144 this.requestURI =(SipURI) contactAddress.getURI();
145
146
147 } catch (Exception e) {
148
149
150 e.printStackTrace();
151 log.error("Error sending response");
152 }
153
154 }*/
155
156 /*public void onTransactionTimeout(TimeoutEvent event,
157 ActivityContextInterface aci) {
158 //log.error("Received transaction timeout event");
159 }
160
161 public void onTransactionExpired(TimeoutEvent event,
162 ActivityContextInterface aci) {
163 //log.debug("Received transaction expired event");
164 }*/
165
166 /*
167 * this is implemented by bytecode rewriting.
168 * See ConcreteRegisrarSbb.java
169 */
170 public abstract RegistrarActivityContextInterface asSbbActivityContextInterface(
171 ActivityContextInterface aci);
172
173 public void setSbbContext(SbbContext context) {
174 this.context = context;
175 try {
176 sbbEnv = (Context) new InitialContext().lookup("java:comp/env");
177 id = context.getSbb();
178 traceFacility = (TraceFacility) sbbEnv.lookup("slee/facilities/trace");
179 timerFacility = (TimerFacility) sbbEnv.lookup("slee/facilities/timer");
180 alarmFacility = (AlarmFacility) sbbEnv.lookup("slee/facilities/alarm");
181 //profileFacility = (ProfileFacility) sbbEnv.lookup("slee/facilities/profile");
182 nullACIFactory = (NullActivityContextInterfaceFactory)sbbEnv.lookup("slee/nullactivity/activitycontextinterfacefactory");
183 nullActivityFactory = (NullActivityFactory)sbbEnv.lookup("slee/nullactivity/factory");
184 fp = (SipFactoryProvider) sbbEnv.lookup("slee/resources/jainsip/1.1/provider");
185 provider = fp.getSipProvider();
186 addressFactory = fp.getAddressFactory();
187 headerFactory = fp.getHeaderFactory();
188 messageFactory = fp.getMessageFactory();
189 } catch (NamingException ne) {
190 System.out.println("Could not set SBB context: " + ne.toString());
191 }
192
193 }
194
195 public void unsetSbbContext() {
196 context = null;
197 }
198
199 public void sbbCreate() throws CreateException {
200 //log.info("called sbb create");
201 }
202
203 public void sbbPostCreate() throws CreateException {
204 //log.info("called sbb post create");
205 }
206
207 public void sbbRemove() {
208 // this is called when the object is returned to the pool
209 //log.info("called sbbRemove " + this.myServiceContainer.getActivityContextCount());
210 //this.testResult.startPostProcessingTimer(this);
211 }
212
213 public void sbbPassivate() {
214 // this is called when the object is returned to the pool
215 //log.info("called sbbPassivate");
216
217 }
218
219 public void sbbActivate() {
220 //log.info("called sbb activiate");
221 }
222
223 public void sbbLoad() {
224 }
225
226 public void sbbStore() {
227 }
228
229 public void sbbExceptionThrown(Exception exception, Object event,
230 ActivityContextInterface aci) {
231 }
232
233 public void sbbRolledBack(RolledBackContext context) {
234 }
235
236 /*public void doPostProcessingConsistencyCheck() {
237 if ( this.myServiceContainer.getActivityContextCount() != 0 ) {
238 testResult.appendMessage("activity context count is " + this.myServiceContainer.getActivityContextCount());
239 } else {
240 log.debug("activityContextCount = " + this.myServiceContainer.getActivityContextCount()) ;
241
242 }
243 testResult.testFinished();
244 }*/
245
246
247
248 protected String getTraceMessageType() { return "RegistrarSbb"; }
249
250 protected final void trace(Level level, String message) {
251 try {
252 traceFacility.createTrace(id, level, getTraceMessageType(), message, System.currentTimeMillis());
253 } catch (Exception e) { }
254 }
255
256 protected final void trace(Level level, String message, Throwable t) {
257 try {
258 traceFacility.createTrace(id, level, getTraceMessageType(), message, t, System.currentTimeMillis());
259 } catch (Exception e) { }
260 }
261
262 // Inner class for SLEE-specific SIP registration stuff
263 class MyRegistrationHandler extends Registrar {
264
265 public MyRegistrationHandler(RegistrarSbb sbb) {
266 super(sbb);
267 }
268
269
270 // override set timer method so we can use SLEE TimerFacility
271 void setRegistrationTimer(String sipAddress, String sipContactAddress, long timeout, String callId, long cseq) {
272 // first find out if we already have a timer set for this registration, and if so, cancel it.
273 System.out.println("setRegistrationTimer(" + sipAddress + ", " + sipContactAddress + ", " + timeout + ", " + callId + ", " + cseq + ")");
274 try {
275 // set a one-shot timer. when it fires we expire the registration
276 long expireTime = System.currentTimeMillis() + (timeout * 1000);
277
278 // Create new ACI for this timer
279 NullActivity nullAC = getNullActivityFactory().createNullActivity();
280 ActivityContextInterface nullACI = getNullACIFactory().getActivityContextInterface(nullAC);
281 RegistrarActivityContextInterface regACI = asSbbActivityContextInterface(nullACI);
282 regACI.setSipAddress(sipAddress);
283 regACI.setSipContactAddress(sipContactAddress);
284 // callId and cseq used to identify a particular registration
285 regACI.setCallId(callId);
286 regACI.setCSeq(cseq);
287
288 // attach so we will receive the timer event...
289 regACI.attach(getSbbLocalObject());
290
291 TimerID newTimer = getTimerFacility().setTimer(regACI, null, expireTime, new TimerOptions());
292
293 System.out.println("set new timer for registration: " + sipAddress + " -> " + sipContactAddress + ", expires in " + timeout + "s");
294
295 } catch (Exception e) {
296 e.printStackTrace();
297 }
298
299 }
300
301 }
302
303 private SipFactoryProvider factoryProvider;
304
305 public SipProvider getSipProvider() { return provider; }
306 public AddressFactory getAddressFactory() { return addressFactory; }
307 public HeaderFactory getHeaderFactory() { return headerFactory; }
308 public NullActivityFactory getNullActivityFactory() { return nullActivityFactory; }
309 protected final TimerFacility getTimerFacility() { return timerFacility; }
310 public NullActivityContextInterfaceFactory getNullACIFactory() { return nullACIFactory; }
311 public MessageFactory getMessageFactory() { return messageFactory; }
312 protected final SbbLocalObject getSbbLocalObject() { return context.getSbbLocalObject(); }
313
314
315 private SipFactoryProvider fp;
316 private SipProvider provider;
317 private AddressFactory addressFactory;
318 private HeaderFactory headerFactory;
319 private MessageFactory messageFactory;
320
321 private SbbContext context;
322 private TraceFacility traceFacility;
323 private TimerFacility timerFacility;
324 private AlarmFacility alarmFacility;
325 // Not implelemented yet
326 //private ProfileFacility profileFacility;
327 private SbbID id;
328 private NullActivityFactory nullActivityFactory;
329 private NullActivityContextInterfaceFactory nullACIFactory;
330 private Context sbbEnv;
331
332 private Context ctx;
333
334 }