/[resiprocate]/main/sip/resiprocate/dum/ClientSubscription.cxx
ViewVC logotype

Contents of /main/sip/resiprocate/dum/ClientSubscription.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3493 - (show annotations) (download)
Fri Nov 19 19:43:26 2004 UTC (15 years, 2 months ago) by sgodin
File size: 8643 byte(s)
-safety checks for existance of Event header. 
- make session timer helper methods private
1 #include "resiprocate/Helper.hxx"
2 #include "resiprocate/os/Logger.hxx"
3 #include "resiprocate/SipFrag.hxx"
4 #include "resiprocate/SipMessage.hxx"
5 #include "resiprocate/dum/ClientSubscription.hxx"
6 #include "resiprocate/dum/Dialog.hxx"
7 #include "resiprocate/dum/DialogUsageManager.hxx"
8 #include "resiprocate/dum/SubscriptionHandler.hxx"
9 #include "resiprocate/dum/SubscriptionCreator.hxx"
10
11
12 using namespace resip;
13
14 #define RESIPROCATE_SUBSYSTEM Subsystem::DUM
15
16
17 ClientSubscription::ClientSubscription(DialogUsageManager& dum, Dialog& dialog, const SipMessage& request)
18 : BaseSubscription(dum, dialog, request),
19 mOnNewSubscriptionCalled(false)
20 {
21 mLastRequest = request;
22 mDialog.makeRequest(mLastRequest, SUBSCRIBE);
23 }
24
25 ClientSubscription::~ClientSubscription()
26 {
27 mDialog.mClientSubscriptions.remove(this);
28 }
29
30 ClientSubscriptionHandle
31 ClientSubscription::getHandle()
32 {
33 return ClientSubscriptionHandle(mDum, getBaseHandle().getId());
34 }
35
36 void
37 ClientSubscription::dispatch(const SipMessage& msg)
38 {
39 ClientSubscriptionHandler* handler = mDum.getClientSubscriptionHandler(mEventType);
40 assert(handler);
41
42 // asserts are checks the correctness of Dialog::dispatch
43 if (msg.isRequest() )
44 {
45 assert( msg.header(h_RequestLine).getMethod() == NOTIFY );
46 mDialog.makeResponse(mLastResponse, msg, 200);
47 send(mLastResponse);
48
49 if (!mOnNewSubscriptionCalled)
50 {
51 InfoLog (<< "[ClientSubscription] " << mLastRequest.header(h_To) << "Terminated");
52 handler->onNewSubscription(getHandle(), msg);
53 mOnNewSubscriptionCalled = true;
54 }
55 int expires = 0;
56 //default to 60 seconds so non-compliant endpoints don't result in leaked usages
57 if (msg.exists(h_SubscriptionState) && msg.header(h_SubscriptionState).exists(p_expires))
58 {
59 expires = msg.header(h_SubscriptionState).param(p_expires);
60 }
61 else
62 {
63 expires = 60;
64 }
65 if (!mLastRequest.exists(h_Expires))
66 {
67 mLastRequest.header(h_Expires).value() = expires;
68 }
69
70 //if no subscription state header, treat as an extension. Only allow for
71 //refer to handle non-compliant implementations
72 if (!msg.exists(h_SubscriptionState))
73 {
74 if (msg.exists(h_Event) && msg.header(h_Event).value() == "refer")
75 {
76 SipFrag* frag = dynamic_cast<SipFrag*>(msg.getContents());
77 if (frag)
78 {
79 if (frag->message().isResponse())
80 {
81 int code = frag->message().header(h_StatusLine).statusCode();
82 if (code < 200)
83 {
84 handler->onUpdateExtension(getHandle(), msg);
85 }
86 else
87 {
88 handler->onTerminated(getHandle(), msg);
89 delete this;
90 }
91 }
92 }
93 else
94 {
95 handler->onTerminated(getHandle(), msg);
96 delete this;
97 }
98 }
99 else
100 {
101 handler->onTerminated(getHandle(), msg);
102 delete this;
103 }
104 return;
105 }
106
107 SubscriptionCreator* creator = dynamic_cast<SubscriptionCreator*> (mDialog.mDialogSet.getCreator());
108
109 int refreshInterval = 0;
110 if (expires)
111 {
112 if (creator && creator->hasRefreshInterval() && creator->getRefreshInterval() < expires)
113 {
114 refreshInterval = creator->getRefreshInterval();
115 }
116 else
117 {
118 refreshInterval = Helper::aBitSmallerThan((unsigned long)expires);
119 }
120 }
121
122 if (msg.header(h_SubscriptionState).value() == "active")
123 {
124 if (refreshInterval)
125 {
126 unsigned long t = refreshInterval;
127 mDum.addTimer(DumTimeout::Subscription, t, getBaseHandle(), ++mTimerSeq);
128 DebugLog (<< "[ClientSubscription] reSUBSCRIBE in " << t);
129 }
130
131 handler->onUpdateActive(getHandle(), msg);
132 }
133 else if (msg.header(h_SubscriptionState).value() == "pending")
134 {
135 if (refreshInterval)
136 {
137 unsigned long t = refreshInterval;
138 mDum.addTimer(DumTimeout::Subscription, t, getBaseHandle(), ++mTimerSeq);
139 DebugLog (<< "[ClientSubscription] reSUBSCRIBE in " << t);
140 }
141
142 handler->onUpdatePending(getHandle(), msg);
143 }
144 else if (msg.header(h_SubscriptionState).value() == "terminated")
145 {
146 handler->onTerminated(getHandle(), msg);
147 DebugLog (<< "[ClientSubscription] " << mLastRequest.header(h_To) << "[ClientSubscription] Terminated");
148 delete this;
149 return;
150 }
151 else
152 {
153 handler->onUpdateExtension(getHandle(), msg);
154 }
155 }
156 else
157 {
158 // !jf! might get an expiration in the 202 but not in the NOTIFY - we're going
159 // to ignore this case
160 if (msg.header(h_StatusLine).statusCode() >= 300)
161 {
162 handler->onTerminated(getHandle(), msg);
163 delete this;
164 return;
165 }
166 }
167 }
168
169 void
170 ClientSubscription::dispatch(const DumTimeout& timer)
171 {
172 if (timer.seq() == mTimerSeq)
173 {
174 requestRefresh();
175 }
176 }
177
178 void
179 ClientSubscription::requestRefresh()
180 {
181 mDialog.makeRequest(mLastRequest, SUBSCRIBE);
182 //!dcm! -- need a mechanism to retrieve this for the event package...part of
183 //the map that stores the handlers, or part of the handler API
184 //mLastRequest.header(h_Expires).value() = 300;
185 InfoLog (<< "Refresh subscription: " << mLastRequest.header(h_Contacts).front());
186 send(mLastRequest);
187 }
188
189 void
190 ClientSubscription::end()
191 {
192 InfoLog (<< "End subscription: " << mLastRequest.header(h_RequestLine).uri());
193
194 mDialog.makeRequest(mLastRequest, SUBSCRIBE);
195 mLastRequest.header(h_Expires).value() = 0;
196 send(mLastRequest);
197 }
198
199
200 /* ====================================================================
201 * The Vovida Software License, Version 1.0
202 *
203 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
204 *
205 * Redistribution and use in source and binary forms, with or without
206 * modification, are permitted provided that the following conditions
207 * are met:
208 *
209 * 1. Redistributions of source code must retain the above copyright
210 * notice, this list of conditions and the following disclaimer.
211 *
212 * 2. Redistributions in binary form must reproduce the above copyright
213 * notice, this list of conditions and the following disclaimer in
214 * the documentation and/or other materials provided with the
215 * distribution.
216 *
217 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
218 * and "Vovida Open Communication Application Library (VOCAL)" must
219 * not be used to endorse or promote products derived from this
220 * software without prior written permission. For written
221 * permission, please contact vocal@vovida.org.
222 *
223 * 4. Products derived from this software may not be called "VOCAL", nor
224 * may "VOCAL" appear in their name, without prior written
225 * permission of Vovida Networks, Inc.
226 *
227 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
228 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
229 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
230 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
231 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
232 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
233 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
234 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
235 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
236 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
237 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
238 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
239 * DAMAGE.
240 *
241 * ====================================================================
242 *
243 * This software consists of voluntary contributions made by Vovida
244 * Networks, Inc. and many individuals on behalf of Vovida Networks,
245 * Inc. For more information on Vovida Networks, Inc., please see
246 * <http://www.vovida.org/>.
247 *
248 */

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27