/[resiprocate]/main/sip/resiprocate/test/testIM.cxx
ViewVC logotype

Contents of /main/sip/resiprocate/test/testIM.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 991 - (show annotations) (download)
Tue Dec 31 06:10:38 2002 UTC (17 years, 1 month ago) by fluffy
File size: 8077 byte(s)
*** empty log message ***

1
2 #include <iostream>
3
4 #ifndef WIN32
5 #include <sys/time.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #endif
9
10 #include "sip2/util/Socket.hxx"
11 #include "sip2/util/Logger.hxx"
12
13 #include "sip2/sipstack/SipStack.hxx"
14 #include "sip2/sipstack/Uri.hxx"
15 #include "sip2/sipstack/TuIM.hxx"
16 #include "sip2/sipstack/Security.hxx"
17
18 #include "testIM.hxx"
19
20
21 using namespace Vocal2;
22 using namespace std;
23
24 #define VOCAL_SUBSYSTEM Subsystem::SIP
25
26 class TestPresCallback: public TuIM::PresCallback
27 {
28 public:
29 virtual void presenseUpdate(const Uri& dest, bool open, const Data& status );
30 };
31
32
33 void
34 TestPresCallback::presenseUpdate(const Uri& from, bool open, const Data& status )
35 {
36 const char* stat = (open)?"online":"offline";
37 cout << from << " set presence to " << stat << " " << status.c_str() << endl;
38 }
39
40
41 class TestPageCallback: public TuIM::PageCallback
42 {
43 public:
44 virtual void receivedPage( const Data& msg, const Uri& from ,
45 const Data& signedBy, Security::SignatureStatus sigStatus,
46 bool wasEncryped );
47
48 Uri* mDest;
49 };
50
51 class TestErrCallback: public TuIM::ErrCallback
52 {
53 public:
54 virtual void sendPageFailed( const Uri& dest );
55 };
56
57
58 void
59 TestPageCallback::receivedPage( const Data& msg, const Uri& from,
60 const Data& signedBy, Security::SignatureStatus sigStatus,
61 bool wasEncryped )
62 {
63 DebugLog(<< "In TestPageCallback");
64
65 if ( mDest && ( *mDest != from) )
66 {
67 *mDest = from;
68 cerr << "Set destination to <" << *mDest << ">" << endl;
69 }
70
71 cout << from;
72 if ( !wasEncryped )
73 {
74 cout << " -NOT SECURE- ";
75 }
76 switch ( sigStatus )
77 {
78 case Security::isBad:
79 cout << " -bad signature- ";
80 break;
81 case Security::none:
82 cout << " -no signature- ";
83 break;
84 case Security::trusted:
85 cout << " <signed " << signedBy << " > ";
86 break;
87 case Security::caTrusted:
88 cout << " <ca signed " << signedBy << " > ";
89 break;
90 case Security::notTrusted:
91 cout << " <signed " << signedBy << " NOT TRUSTED > ";
92 break;
93 }
94
95 cout << " says:" << endl;
96 cout << msg.escaped() << endl;
97 }
98
99
100 void
101 TestErrCallback::sendPageFailed( const Uri& dest )
102 {
103 InfoLog(<< "In TestErrCallback");
104 cerr << "Message to " << dest << " failed" << endl;
105 }
106
107
108 bool
109 processStdin( TuIM& tuIM, Uri* dest )
110 {
111 char buf[1024];
112
113 #if 0
114 DebugLog( << "eof = " << eof(fileno(stdin)) );
115 if ( eof(fileno(stdin)) )
116 {
117 return true;
118 }
119 #endif
120
121 DebugLog( << "start read " << sizeof(buf) << " charaters from stdin" );
122 int num = read(fileno(stdin),buf,sizeof(buf));
123 DebugLog( << "Read " << num << " charaters from stdin" );
124
125 if ( (num>3) && (!strncmp("to:",buf,3)) )
126 {
127 buf[num-1] = 0;
128 *dest = Uri(Data(buf+3));
129 cerr << "Set destination to <" << *dest << ">";
130 }
131 else if ( (num>4) && (!strncmp("add:",buf,4)) )
132 {
133 buf[num-1] = 0;
134 Uri uri(Data(buf+4));
135 cerr << "Subscribing to buddy <" << uri << ">";
136 tuIM.addBuddy( uri, Data::Empty );
137 }
138 else if ( (num>3) && (!strncmp("go:",buf,3)) )
139 {
140 buf[num-1] = 0;
141 Data stat(buf+3);
142 cerr << "setting presence status to <" << stat << ">";
143 tuIM.setMyPresense( !stat.empty(), stat );
144 }
145 else if ( (num==2) && (!strncmp(".",buf,1)) )
146 {
147 DebugLog( << "Got a period - end program" );
148 return false;
149 }
150 else
151 {
152 if ( num >= 1 )
153 {
154 buf[num-1] = 0;
155 Data text(buf);
156
157 DebugLog( << "Read <" << text << ">" );
158
159 cout << "Send to <" << *dest << ">";
160
161 tuIM.sendPage( text , *dest, false /*sign*/, Data::Empty /*encryptFor*/ );
162 //tuIM.sendPage( text , *dest, false /*sign*/, dest->getAorNoPort() /*encryptFor*/ );
163 //tuIM.sendPage( text , *dest, true /*sign*/, Data::Empty /*encryptFor*/ );
164 //tuIM.sendPage( text , *dest, true /*sign*/, dest->getAorNoPort() /*encryptFor*/ );
165 }
166 }
167
168 return true;
169 }
170
171
172 int
173 main(int argc, char* argv[])
174 {
175 Log::initialize(Log::COUT, Log::ERR, argv[0]);
176
177 //Log::setLevel(Log::DEBUG_STACK);
178
179 InfoLog(<<"Test Driver for IM Starting");
180
181 InfoLog( << "\nType a line like\nto:sip:fluffy@localhost:5060\n"
182 "to control the destination of your messages. "
183 "A line with a singe period on it ends the program\n" );
184
185 int port = 5060;
186 int tlsPort = 0;
187 Uri aor("sip:aor@localhost:5060" );
188 Uri dest("sip:you@localhost:5070");
189 Data aorPassword;
190
191 for ( int i=1; i<argc; i++)
192 {
193 if (!strcmp(argv[i],"-vv"))
194 {
195 Log::setLevel(Log::DEBUG_STACK);
196 }
197 else if (!strcmp(argv[i],"-v"))
198 {
199 Log::setLevel(Log::INFO);
200 }
201 else if (!strcmp(argv[i],"-port"))
202 {
203 i++;
204 assert( i<argc );
205 port = atoi( argv[i] );
206 }
207 else if (!strcmp(argv[i],"-tlsport"))
208 {
209 i++;
210 assert( i<argc );
211 tlsPort = atoi( argv[i] );
212 }
213 else if (!strcmp(argv[i],"-aor"))
214 {
215 i++;
216 assert( i<argc );
217 aor = Uri(Data(argv[i]));
218 }
219 else if (!strcmp(argv[i],"-aorPassword"))
220 {
221 i++;
222 assert( i<argc );
223 aorPassword = Data(argv[i]);
224 }
225 else if (!strcmp(argv[i],"-to"))
226 {
227 i++;
228 assert( i<argc );
229 dest = Uri(Data(argv[i]));
230
231 cout << "Destination is " << dest << endl;
232 }
233 else
234 {
235 ErrLog(<<"Bad command line opion: " << argv[i] );
236 ErrLog(<<"options are: [-v] [-vv] [-port 1234] [-aor sip:flffuy@flouf.com] [-to sip:1@foo.com]" << argv[i] );
237 assert(0);
238 }
239 }
240
241 InfoLog( << "Using port " << port );
242
243 SipStack sipStack;
244
245 #ifdef USE_SSL
246 assert( sipStack.security );
247 bool ok = sipStack.security->loadAllCerts( Data("password") );
248 if ( !ok )
249 {
250 ErrLog( << "Could not load the certificates" );
251 assert( ok );
252 }
253 #endif
254
255 Vocal2::Transport::Type transport = Transport::UDP;
256
257 sipStack.addTransport(Transport::UDP, port); InfoLog("UDP on port " << port );
258 sipStack.addTransport(Transport::TCP, port); InfoLog("TCP on port " << port );
259
260 #if USE_SSL
261 if ( port == 5060 )
262 {
263 if ( tlsPort == 0 )
264 {
265 tlsPort = 5061;
266 }
267 }
268 if ( tlsPort != 0 )
269 {
270 InfoLog("TLS on port " << tlsPort );
271 sipStack.addTransport(Transport::TLS, tlsPort);
272 }
273 #endif
274
275 TestPageCallback pageCallback;
276 pageCallback.mDest = &dest;
277
278 TestErrCallback errCallback;
279
280 TestPresCallback presCallback;
281
282 dest.param(p_transport) = Transport::toData( transport );
283 aor.param(p_transport) = Transport::toData( transport );
284
285 Uri contact = aor;
286 contact.port() = port;
287 contact.param(p_transport) = aor.param(p_transport);
288 contact.host() = "localhost"; // TODO - fix this
289
290 TuIM tuIM(&sipStack,aor,contact,&pageCallback,&errCallback,&presCallback);
291
292 #if 0
293 tuIM.registerAor( aor, aorPassword );
294 #endif
295
296 //Vocal2::makeSocketNonBlocking( fileno(stdin) );
297
298 while (1)
299 {
300 FdSet fdset;
301 sipStack.buildFdSet(fdset);
302
303 fdset.setRead( fileno(stdin) );
304
305 int err = fdset.select( sipStack.getTimeTillNextProcess());
306 if ( err == -1 )
307 {
308 int e = errno;
309 switch (e)
310 {
311 case 0:
312 break;
313 default:
314 InfoLog(<< "Error " << e << " " << strerror(e) << " in select");
315 break;
316 }
317 }
318 //InfoLog(<< "Select returned");
319
320 if ( fdset.readyToRead( fileno(stdin) ) )
321 //if ( !eof( fileno(stdin) ) )
322 {
323 bool keepGoing = processStdin(tuIM,&dest);
324 if (!keepGoing) break;
325 }
326
327 // DebugLog ( << "Try TO PROCESS " );
328 sipStack.process(fdset);
329
330 tuIM.process();
331 }
332 }
333

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27