正在显示
1 个修改的文件
包含
243 行增加
和
0 行删除
src/server/spserver/spresponse.cpp
0 → 100644
1 | +/* | ||
2 | + * Copyright 2007 Stephen Liu | ||
3 | + * For license terms, see the file COPYING along with this library. | ||
4 | + */ | ||
5 | + | ||
6 | +#include <stdlib.h> | ||
7 | + | ||
8 | +#include "spresponse.hpp" | ||
9 | +#include "spbuffer.hpp" | ||
10 | +#include "sputils.hpp" | ||
11 | +#include "spmsgblock.hpp" | ||
12 | + | ||
13 | +//------------------------------------------------------------------- | ||
14 | + | ||
15 | +SP_SidList :: SP_SidList() | ||
16 | +{ | ||
17 | + mList = new SP_ArrayList(); | ||
18 | +} | ||
19 | + | ||
20 | +SP_SidList :: ~SP_SidList() | ||
21 | +{ | ||
22 | + for( int i = 0; i < mList->getCount(); i++ ) { | ||
23 | + free( (void*)mList->getItem( i ) ); | ||
24 | + } | ||
25 | + | ||
26 | + delete mList; | ||
27 | + mList = NULL; | ||
28 | +} | ||
29 | + | ||
30 | +void SP_SidList :: reset() | ||
31 | +{ | ||
32 | + for( ; mList->getCount() > 0; ) { | ||
33 | + free( (void*)mList->takeItem( SP_ArrayList::LAST_INDEX ) ); | ||
34 | + } | ||
35 | +} | ||
36 | + | ||
37 | +int SP_SidList :: getCount() const | ||
38 | +{ | ||
39 | + return mList->getCount(); | ||
40 | +} | ||
41 | + | ||
42 | +void SP_SidList :: add( SP_Sid_t sid ) | ||
43 | +{ | ||
44 | + SP_Sid_t * p = (SP_Sid_t*)malloc( sizeof( SP_Sid_t ) ); | ||
45 | + *p = sid; | ||
46 | + | ||
47 | + mList->append( p ); | ||
48 | +} | ||
49 | + | ||
50 | +SP_Sid_t SP_SidList :: get( int index ) const | ||
51 | +{ | ||
52 | + SP_Sid_t ret = { 0, 0 }; | ||
53 | + | ||
54 | + SP_Sid_t * p = (SP_Sid_t*)mList->getItem( index ); | ||
55 | + if( NULL != p ) ret = *p; | ||
56 | + | ||
57 | + return ret; | ||
58 | +} | ||
59 | + | ||
60 | +SP_Sid_t SP_SidList :: take( int index ) | ||
61 | +{ | ||
62 | + SP_Sid_t ret = get( index ); | ||
63 | + | ||
64 | + void * p = mList->takeItem( index ); | ||
65 | + if( NULL != p ) free( p ); | ||
66 | + | ||
67 | + return ret; | ||
68 | +} | ||
69 | + | ||
70 | +int SP_SidList :: find( SP_Sid_t sid ) const | ||
71 | +{ | ||
72 | + for( int i = 0; i < mList->getCount(); i++ ) { | ||
73 | + SP_Sid_t * p = (SP_Sid_t*)mList->getItem( i ); | ||
74 | + | ||
75 | + if( p->mKey == sid.mKey && p->mSeq == sid.mSeq ) return i; | ||
76 | + } | ||
77 | + | ||
78 | + return -1; | ||
79 | +} | ||
80 | + | ||
81 | +//------------------------------------------------------------------- | ||
82 | + | ||
83 | +SP_Message :: SP_Message( int completionKey ) | ||
84 | +{ | ||
85 | + mCompletionKey = completionKey; | ||
86 | + | ||
87 | + mMsg = NULL; | ||
88 | + mFollowBlockList = NULL; | ||
89 | + | ||
90 | + mToList = mSuccess = mFailure = NULL; | ||
91 | +} | ||
92 | + | ||
93 | +SP_Message :: ~SP_Message() | ||
94 | +{ | ||
95 | + if( NULL != mMsg ) delete mMsg; | ||
96 | + mMsg = NULL; | ||
97 | + | ||
98 | + if( NULL != mFollowBlockList ) delete mFollowBlockList; | ||
99 | + mFollowBlockList = NULL; | ||
100 | + | ||
101 | + if( NULL != mToList ) delete mToList; | ||
102 | + mToList = NULL; | ||
103 | + | ||
104 | + if( NULL != mSuccess ) delete mSuccess; | ||
105 | + mSuccess = NULL; | ||
106 | + | ||
107 | + if( NULL != mFailure ) delete mFailure; | ||
108 | + mFailure = NULL; | ||
109 | +} | ||
110 | + | ||
111 | +void SP_Message :: reset() | ||
112 | +{ | ||
113 | + if( NULL != mMsg ) mMsg->reset(); | ||
114 | + | ||
115 | + if( NULL != mFollowBlockList ) mFollowBlockList->reset(); | ||
116 | + | ||
117 | + if( NULL != mToList ) mToList->reset(); | ||
118 | + | ||
119 | + if( NULL != mSuccess ) mSuccess->reset(); | ||
120 | + | ||
121 | + if( NULL != mFailure ) mFailure->reset(); | ||
122 | +} | ||
123 | + | ||
124 | +SP_SidList * SP_Message :: getToList() | ||
125 | +{ | ||
126 | + if( NULL == mToList ) mToList = new SP_SidList(); | ||
127 | + | ||
128 | + return mToList; | ||
129 | +} | ||
130 | + | ||
131 | +size_t SP_Message :: getTotalSize() | ||
132 | +{ | ||
133 | + size_t totalSize = 0; | ||
134 | + | ||
135 | + if( NULL != mMsg ) totalSize += mMsg->getSize(); | ||
136 | + if( NULL != mFollowBlockList ) totalSize += mFollowBlockList->getTotalSize(); | ||
137 | + | ||
138 | + return totalSize; | ||
139 | +} | ||
140 | + | ||
141 | +SP_Buffer * SP_Message :: getMsg() | ||
142 | +{ | ||
143 | + if( NULL == mMsg ) mMsg = new SP_Buffer(); | ||
144 | + | ||
145 | + return mMsg; | ||
146 | +} | ||
147 | + | ||
148 | +SP_MsgBlockList * SP_Message :: getFollowBlockList() | ||
149 | +{ | ||
150 | + if( NULL == mFollowBlockList ) mFollowBlockList = new SP_MsgBlockList(); | ||
151 | + | ||
152 | + return mFollowBlockList; | ||
153 | +} | ||
154 | + | ||
155 | +SP_SidList * SP_Message :: getSuccess() | ||
156 | +{ | ||
157 | + if( NULL == mSuccess ) mSuccess = new SP_SidList(); | ||
158 | + | ||
159 | + return mSuccess; | ||
160 | +} | ||
161 | + | ||
162 | +SP_SidList * SP_Message :: getFailure() | ||
163 | +{ | ||
164 | + if( NULL == mFailure ) mFailure = new SP_SidList(); | ||
165 | + | ||
166 | + return mFailure; | ||
167 | +} | ||
168 | + | ||
169 | +void SP_Message :: setCompletionKey( int completionKey ) | ||
170 | +{ | ||
171 | + mCompletionKey = completionKey; | ||
172 | +} | ||
173 | + | ||
174 | +int SP_Message :: getCompletionKey() | ||
175 | +{ | ||
176 | + return mCompletionKey; | ||
177 | +} | ||
178 | + | ||
179 | +//------------------------------------------------------------------- | ||
180 | + | ||
181 | +SP_Response :: SP_Response( SP_Sid_t fromSid ) | ||
182 | +{ | ||
183 | + mFromSid = fromSid; | ||
184 | + | ||
185 | + mReply = NULL; | ||
186 | + | ||
187 | + mList = new SP_ArrayList(); | ||
188 | + | ||
189 | + mToCloseList = NULL; | ||
190 | +} | ||
191 | + | ||
192 | +SP_Response :: ~SP_Response() | ||
193 | +{ | ||
194 | + for( int i = 0; i < mList->getCount(); i++ ) { | ||
195 | + delete (SP_Message*)mList->getItem( i ); | ||
196 | + } | ||
197 | + | ||
198 | + delete mList; | ||
199 | + mList = NULL; | ||
200 | + | ||
201 | + mReply = NULL; | ||
202 | + | ||
203 | + if( NULL != mToCloseList ) delete mToCloseList; | ||
204 | + mToCloseList = NULL; | ||
205 | +} | ||
206 | + | ||
207 | +SP_Sid_t SP_Response :: getFromSid() const | ||
208 | +{ | ||
209 | + return mFromSid; | ||
210 | +} | ||
211 | + | ||
212 | +SP_Message * SP_Response :: getReply() | ||
213 | +{ | ||
214 | + if( NULL == mReply ) { | ||
215 | + mReply = new SP_Message(); | ||
216 | + mReply->getToList()->add( mFromSid ); | ||
217 | + mList->append( mReply ); | ||
218 | + } | ||
219 | + | ||
220 | + return mReply; | ||
221 | +} | ||
222 | + | ||
223 | +void SP_Response :: addMessage( SP_Message * msg ) | ||
224 | +{ | ||
225 | + mList->append( msg ); | ||
226 | +} | ||
227 | + | ||
228 | +SP_Message * SP_Response :: peekMessage() | ||
229 | +{ | ||
230 | + return ( SP_Message * ) mList->getItem( 0 ); | ||
231 | +} | ||
232 | + | ||
233 | +SP_Message * SP_Response :: takeMessage() | ||
234 | +{ | ||
235 | + return ( SP_Message * ) mList->takeItem( 0 ); | ||
236 | +} | ||
237 | + | ||
238 | +SP_SidList * SP_Response :: getToCloseList() | ||
239 | +{ | ||
240 | + if( NULL == mToCloseList ) mToCloseList = new SP_SidList(); | ||
241 | + return mToCloseList; | ||
242 | +} | ||
243 | + |
请
注册
或
登录
后发表评论