Browse code

allow for custom AmSipDialog implementation in session classes

Raphael Coeffic authored on 02/02/2013 20:27:29
Showing 14 changed files
... ...
@@ -49,8 +49,8 @@ ReliableB2BEvent::~ReliableB2BEvent()
49 49
 ////////////////////////////////////////////////////////////////////////////////
50 50
 
51 51
 // callee
52
-CallLeg::CallLeg(const CallLeg* caller):
53
-  AmB2BSession(caller->getLocalTag()),
52
+CallLeg::CallLeg(const CallLeg* caller, AmSipDialog* p_dlg):
53
+  AmB2BSession(caller->getLocalTag(),p_dlg),
54 54
   call_status(Disconnected),
55 55
   hold_request_cseq(0), hold_status(NotHeld)
56 56
 {
... ...
@@ -92,8 +92,8 @@ CallLeg::CallLeg(const CallLeg* caller):
92 92
 }
93 93
 
94 94
 // caller
95
-CallLeg::CallLeg(): 
96
-  AmB2BSession(),
95
+CallLeg::CallLeg(AmSipDialog* p_dlg): 
96
+  AmB2BSession("",p_dlg),
97 97
   call_status(Disconnected),
98 98
   hold_request_cseq(0), hold_status(NotHeld)
99 99
 {
... ...
@@ -350,10 +350,10 @@ class CallLeg: public AmB2BSession
350 350
 
351 351
   public:
352 352
     /** creates A leg */
353
-    CallLeg();
353
+    CallLeg(AmSipDialog* dlg=NULL);
354 354
 
355 355
     /** creates B leg using given session as A leg */
356
-    CallLeg(const CallLeg* caller);
356
+    CallLeg(const CallLeg* caller, AmSipDialog* dlg=NULL);
357 357
 };
358 358
 
359 359
 
... ...
@@ -112,15 +112,21 @@ void assertEndCRLF(string& s) {
112 112
 
113 113
 ///////////////////////////////////////////////////////////////////////////////////////////
114 114
 
115
-static SBCCallLeg* _default_createCallLeg(const SBCCallProfile& call_profile)
115
+SBCCallLeg* CallLegCreator::create(const SBCCallProfile& call_profile)
116 116
 {
117
-  return new SBCCallLeg(call_profile);
117
+  return new SBCCallLeg(call_profile, new AmSipDialog());
118
+}
119
+
120
+SBCCallLeg* CallLegCreator::create(SBCCallLeg* caller)
121
+{
122
+  return new SBCCallLeg(caller);
118 123
 }
119 124
 
120 125
 SBCFactory::SBCFactory(const string& _app_name)
121
-  : AmSessionFactory(_app_name), AmDynInvokeFactory(_app_name)
126
+  : AmSessionFactory(_app_name), 
127
+    AmDynInvokeFactory(_app_name),
128
+    callLegCreator(new CallLegCreator)
122 129
 {
123
-  createCallLeg = &_default_createCallLeg;
124 130
 }
125 131
 
126 132
 SBCFactory::~SBCFactory() {
... ...
@@ -281,7 +287,7 @@ AmSession* SBCFactory::onInvite(const AmSipRequest& req, const string& app_name,
281 287
     return NULL;
282 288
   }
283 289
 
284
-  SBCCallLeg* b2b_dlg = (*createCallLeg)(call_profile);
290
+  SBCCallLeg* b2b_dlg = callLegCreator->create(call_profile);
285 291
 
286 292
   if (call_profile.auth_aleg_enabled) {
287 293
     // adding auth handler
... ...
@@ -45,6 +45,11 @@ using std::string;
45 45
 #define SBC_TIMER_ID_CALL_TIMERS_START   10
46 46
 #define SBC_TIMER_ID_CALL_TIMERS_END     99
47 47
 
48
+struct CallLegCreator {
49
+  virtual SBCCallLeg* create(const SBCCallProfile& call_profile);
50
+  virtual SBCCallLeg* create(SBCCallLeg* caller);
51
+};
52
+
48 53
 class SBCFactory: public AmSessionFactory,
49 54
     public AmDynInvoke,
50 55
     public AmDynInvokeFactory
... ...
@@ -55,8 +60,7 @@ class SBCFactory: public AmSessionFactory,
55 60
   vector<string> active_profile;
56 61
   AmMutex profiles_mut;
57 62
 
58
-  typedef SBCCallLeg* (*CallLegCreator)(const SBCCallProfile& call_profile);
59
-  CallLegCreator createCallLeg;
63
+  auto_ptr<CallLegCreator> callLegCreator;
60 64
 
61 65
   void listProfiles(const AmArg& args, AmArg& ret);
62 66
   void reloadProfiles(const AmArg& args, AmArg& ret);
... ...
@@ -84,7 +88,8 @@ class SBCFactory: public AmSessionFactory,
84 88
 
85 89
   int onLoad();
86 90
 
87
-  void setCallLegCreator(CallLegCreator fp) { createCallLeg = fp; }
91
+  void setCallLegCreator(CallLegCreator* clc) { callLegCreator.reset(clc); }
92
+  CallLegCreator* getCallLegCreator() { return callLegCreator.get(); }
88 93
 
89 94
   AmSession* onInvite(const AmSipRequest& req, const string& app_name,
90 95
 		      const map<string,string>& app_params);
... ...
@@ -143,8 +143,9 @@ void PayloadIdMapping::reset()
143 143
 ///////////////////////////////////////////////////////////////////////////////////////////
144 144
 
145 145
 // A leg constructor (from SBCDialog)
146
-SBCCallLeg::SBCCallLeg(const SBCCallProfile& call_profile)
147
-  : m_state(BB_Init),
146
+SBCCallLeg::SBCCallLeg(const SBCCallProfile& call_profile, AmSipDialog* p_dlg)
147
+  : CallLeg(p_dlg),
148
+    m_state(BB_Init),
148 149
     auth(NULL),
149 150
     call_profile(call_profile),
150 151
     cc_timer_id(SBC_TIMER_ID_CALL_TIMERS_START)
... ...
@@ -161,10 +162,10 @@ SBCCallLeg::SBCCallLeg(const SBCCallProfile& call_profile)
161 162
 }
162 163
 
163 164
 // B leg constructor (from SBCCalleeSession)
164
-SBCCallLeg::SBCCallLeg(SBCCallLeg* caller)
165
+SBCCallLeg::SBCCallLeg(SBCCallLeg* caller, AmSipDialog* p_dlg)
165 166
   : auth(NULL),
166 167
     call_profile(caller->getCallProfile()),
167
-    CallLeg(caller)
168
+    CallLeg(caller,p_dlg)
168 169
 {
169 170
   // FIXME: do we want to inherit cc_vars from caller?
170 171
   // Can be pretty dangerous when caller stored pointer to object - we should
... ...
@@ -794,12 +795,17 @@ void SBCCallLeg::onInvite(const AmSipRequest& req)
794 795
   }
795 796
 }
796 797
 
797
-void SBCCallLeg::connectCallee(const string& remote_party, const string& remote_uri,
798
-    const string &from, const AmSipRequest &original_invite, const AmSipRequest &invite)
798
+void SBCCallLeg::connectCallee(const string& remote_party, 
799
+			       const string& remote_uri,
800
+			       const string &from, 
801
+			       const AmSipRequest &original_invite, 
802
+			       const AmSipRequest &invite)
799 803
 {
800 804
   // FIXME: no fork for now
801 805
 
802
-  SBCCallLeg* callee_session = new SBCCallLeg(this);
806
+  SBCCallLeg* callee_session = SBCFactory::instance()->
807
+    getCallLegCreator()->create(this);
808
+
803 809
   callee_session->setLocalParty(from, from);
804 810
   callee_session->setRemoteParty(remote_party, remote_uri);
805 811
 
... ...
@@ -100,8 +100,8 @@ class SBCCallLeg : public CallLeg, public CredentialHolder
100 100
 
101 101
  public:
102 102
 
103
-  SBCCallLeg(const SBCCallProfile& call_profile);
104
-  SBCCallLeg(SBCCallLeg* caller);
103
+  SBCCallLeg(const SBCCallProfile& call_profile, AmSipDialog* dlg=NULL);
104
+  SBCCallLeg(SBCCallLeg* caller, AmSipDialog* dlg=NULL);
105 105
   ~SBCCallLeg();
106 106
 
107 107
   void process(AmEvent* ev);
... ...
@@ -64,8 +64,9 @@ static void errCode2RelayedReply(AmSipReply &reply, int err_code, unsigned defau
64 64
 // AmB2BSession methods
65 65
 //
66 66
 
67
-AmB2BSession::AmB2BSession(const string& other_local_tag)
68
-  : other_id(other_local_tag),
67
+AmB2BSession::AmB2BSession(const string& other_local_tag, AmSipDialog* dlg)
68
+  : AmSession(dlg),
69
+    other_id(other_local_tag),
69 70
     sip_relay_only(true),
70 71
     rtp_relay_mode(RTP_Direct),
71 72
     rtp_relay_force_symmetric_rtp(false),
... ...
@@ -230,7 +230,7 @@ class AmB2BSession: public AmSession
230 230
    */
231 231
   virtual bool onOtherReply(const AmSipReply& reply);
232 232
 
233
-  AmB2BSession(const string& other_local_tag = "");
233
+  AmB2BSession(const string& other_local_tag = "", AmSipDialog* dlg=NULL);
234 234
 
235 235
   virtual ~AmB2BSession();
236 236
 
... ...
@@ -33,7 +33,7 @@ AmBasicSipDialog::AmBasicSipDialog(AmBasicSipEventHandler* h)
33 33
     nat_handling(false),
34 34
     usages(0)
35 35
 {
36
-  assert(h);
36
+  //assert(h);
37 37
 }
38 38
 
39 39
 AmBasicSipDialog::~AmBasicSipDialog()
... ...
@@ -368,7 +368,7 @@ bool AmBasicSipDialog::onRxReplyStatus(const AmSipReply& reply,
368 368
   case 485:
369 369
   case 502:
370 370
   case 604:
371
-    hdl->onRemoteDisappeared(reply);
371
+    if(hdl) hdl->onRemoteDisappeared(reply);
372 372
     break;
373 373
   }
374 374
   
... ...
@@ -187,8 +187,10 @@ public:
187 187
   bool r_cseq_i;
188 188
   unsigned int r_cseq; // last remote CSeq  
189 189
 
190
-  AmBasicSipDialog(AmBasicSipEventHandler* h=0);
190
+  AmBasicSipDialog(AmBasicSipEventHandler* h=NULL);
191 191
   virtual ~AmBasicSipDialog();
192
+
193
+  void setEventhandler(AmBasicSipEventHandler* h) { hdl = h; }
192 194
   
193 195
   /** @return UAC request coresponding to cseq or NULL */
194 196
   AmSipRequest* getUACTrans(unsigned int t_cseq);
... ...
@@ -70,9 +70,8 @@ struct timeval cps_max_timestamp = avg_last_timestamp;
70 70
 
71 71
 // AmSession methods
72 72
 
73
-AmSession::AmSession()
74
-  : AmEventQueue(this),
75
-    //dlg(this),
73
+AmSession::AmSession(AmSipDialog* p_dlg)
74
+  : AmEventQueue(this), dlg(p_dlg),
76 75
     input(NULL), output(NULL),
77 76
     sess_stopped(false),
78 77
     m_dtmfDetector(this), m_dtmfEventQueue(&m_dtmfDetector),
... ...
@@ -89,7 +88,9 @@ AmSession::AmSession()
89 88
   , _pid(this)
90 89
 #endif
91 90
 {
92
-  dlg = createSipDialog();
91
+  DBG("dlg = %p",dlg);
92
+  if(!dlg) dlg = new AmSipDialog(this);
93
+  else dlg->setEventhandler(this);
93 94
 }
94 95
 
95 96
 AmSession::~AmSession()
... ...
@@ -213,7 +213,7 @@ public:
213 213
   /** 
214 214
    * Session constructor.
215 215
    */
216
-  AmSession();
216
+  AmSession(AmSipDialog* dlg=NULL);
217 217
 
218 218
   virtual ~AmSession();
219 219
 
... ...
@@ -149,6 +149,8 @@ bool AmSipDialog::onRxReqStatus(const AmSipRequest& req)
149 149
 
150 150
 int AmSipDialog::onSdpCompleted()
151 151
 {
152
+  if(!hdl) return 0;
153
+
152 154
   int ret = ((AmSipDialogEventHandler*)hdl)->
153 155
     onSdpCompleted(oa.getLocalSdp(), oa.getRemoteSdp());
154 156
 
... ...
@@ -175,11 +177,13 @@ int AmSipDialog::onSdpCompleted()
175 177
 
176 178
 bool AmSipDialog::getSdpOffer(AmSdp& offer)
177 179
 {
180
+  if(!hdl) return false;
178 181
   return ((AmSipDialogEventHandler*)hdl)->getSdpOffer(offer);
179 182
 }
180 183
 
181 184
 bool AmSipDialog::getSdpAnswer(const AmSdp& offer, AmSdp& answer)
182 185
 {
186
+  if(!hdl) return false;
183 187
   return ((AmSipDialogEventHandler*)hdl)->getSdpAnswer(offer,answer);
184 188
 }
185 189
 
... ...
@@ -428,8 +432,7 @@ bool AmSipDialog::onRxReplyStatus(const AmSipReply& reply,
428 432
   if( (reply.code >= 200) && (reply.code < 300) &&
429 433
       (reply.cseq_method == SIP_METH_INVITE) ) {
430 434
 
431
-    if(hdl)
432
-      ((AmSipDialogEventHandler*)hdl)->onInvite2xx(reply);
435
+    if(hdl) ((AmSipDialogEventHandler*)hdl)->onInvite2xx(reply);
433 436
 
434 437
   } else {
435 438
     cont = AmBasicSipDialog::onRxReplyStatus(reply,t_uac_it);
... ...
@@ -77,7 +77,7 @@ protected:
77 77
 
78 78
  public:
79 79
 
80
-  AmSipDialog(AmSipDialogEventHandler* h);
80
+  AmSipDialog(AmSipDialogEventHandler* h=NULL);
81 81
   ~AmSipDialog();
82 82
 
83 83
   /** @return whether UAC INVITE transaction is pending */