/[resiprocate]/branches/b-directory-reorg/resip/dum/test/limpc.cxx
ViewVC logotype

Contents of /branches/b-directory-reorg/resip/dum/test/limpc.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4076 - (show annotations) (download)
Wed Mar 30 07:54:54 2005 UTC (14 years, 9 months ago) by jason
Original Path: main/sip/resiprocate/dum/test/limpc.cxx
File size: 16599 byte(s)
added skeleton of a dum-based version of limpc

1 #if defined(HAVE_CONFIG_HXX)
2 #include "resiprocate/config.hxx"
3 #endif
4
5 #include <cstring>
6 #include <cassert>
7
8 //#define USE_CURSES
9
10 #if defined (HAVE_POPT_H)
11 #include <popt.h>
12 #endif
13
14 #ifdef USE_CURSES
15 #include <ncurses.h>
16 #else
17 #include <iostream>
18 #include <cstdio>
19
20 #ifdef WIN32
21 #include <io.h>
22 #else
23 #include <unistd.h>
24 #endif
25
26 typedef void WINDOW;
27
28 #ifndef __APPLE__
29 bool TRUE=true;
30 bool FALSE=false;
31 #endif
32
33 char ACS_HLINE=1;
34 char ACS_VLINE=2;
35 WINDOW* stdscr=0;
36 WINDOW* newwin(...) { return NULL; };
37 void waddstr(WINDOW*, const char* text) { std::clog << text; };
38 char getch()
39 {
40 char buf[1];
41 int r = read(fileno(stdin),&buf,1);
42 if ( r ==1 )
43 {
44 return buf[0];
45 }
46 return 0;
47 };
48
49 void werase(WINDOW*) {};
50 void wrefresh(...) {};
51 void mvhline(...) {};
52 void refresh(...) {};
53 void getmaxyx(...) {};
54 void clearok(...) {};
55 void waddch(...) {};
56 void initscr(...) {};
57 void cbreak(...) {};
58 void noecho(...) {};
59 void nonl(...) {};
60 void intrflush(...) {};
61 void keypad(...) {};
62 void scrollok(...) {};
63 void wmove(...) {};
64 void mvvline(...) {};
65 #endif
66
67
68
69 #ifndef WIN32
70 #include <sys/time.h>
71 #include <sys/types.h>
72 #include <unistd.h>
73 #include <stdlib.h>
74 #endif
75
76 #include "resiprocate/os/Socket.hxx"
77 #include "resiprocate/os/Logger.hxx"
78 #include "resiprocate/SipStack.hxx"
79 #include "resiprocate/Uri.hxx"
80 #include "resiprocate/TuIM.hxx"
81 #include "resiprocate/Security.hxx"
82
83 static int myMain(int argc, char* argv[]);
84
85 using namespace resip;
86 using namespace std;
87
88 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
89
90 static WINDOW* commandWin=0;
91 static WINDOW* textWin=0;
92 static WINDOW* statusWin=0;
93
94 static TuIM* tuIM;
95 static Uri dest;
96
97 void
98 displayPres()
99 {
100 werase(statusWin);
101
102 for( int i=0; i<tuIM->getNumBuddies();i++)
103 {
104 Uri uri = tuIM->getBuddyUri(i);
105 Data status;
106 bool online = tuIM->getBuddyStatus(i,&status);
107 const char* stat = (online)?"online":"offline";
108
109 waddstr(statusWin,uri.getAor().c_str());
110 waddstr(statusWin," ");
111 waddstr(statusWin,stat);
112 waddstr(statusWin," ");
113 waddstr(statusWin,status.c_str());
114 waddstr(statusWin,"\n");
115 }
116
117 wrefresh(statusWin);
118 }
119
120
121 class TestCallback: public TuIM::Callback
122 {
123 public:
124 virtual void presenceUpdate(const Uri& dest, bool open, const Data& status );
125 virtual void receivedPage( const Data& msg, const Uri& from ,
126 const Data& signedBy, SignatureStatus sigStatus,
127 bool wasEncryped );
128 virtual void sendPageFailed( const Uri& dest,int respNumber );
129 virtual void registrationFailed(const resip::Uri&, int respNumber);
130 virtual void registrationWorked(const Uri& dest );
131 virtual void receivePageFailed(const Uri& sender);
132 };
133
134
135 void
136 TestCallback::presenceUpdate(const Uri& from, bool open, const Data& status )
137 {
138 const char* stat = (open)?"online":"offline";
139 //cout << from << " set presence to " << stat << " " << status.c_str() << endl;
140
141 waddstr(textWin,"Status: ");
142 waddstr(textWin, from.getAor().c_str());
143 waddstr(textWin," is ");
144 waddstr(textWin,stat);
145 waddstr(textWin," ");
146 waddstr(textWin,status.c_str());
147 waddstr(textWin,"\n");
148
149 wrefresh(textWin);
150
151 displayPres();
152 }
153
154 void
155 TestCallback::receivedPage( const Data& msg, const Uri& from,
156 const Data& signedBy, SignatureStatus sigStatus,
157 bool wasEncryped )
158 {
159 //DebugLog(<< "In TestPageCallback");
160
161 if ( dest != from )
162 {
163 dest = from;
164 //cerr << "Set destination to <" << *mDest << ">" << endl;
165 waddstr(textWin,"Set destination to ");
166 waddstr(textWin, Data::from(dest).c_str());
167 waddstr(textWin,"\n");
168 }
169
170 //cout << from;
171
172 waddstr(textWin,"From: ");
173 waddstr(textWin,from.getAor().c_str());
174
175 if ( !wasEncryped )
176 {
177 //cout << " -NOT SECURE- ";
178 waddstr(textWin," -NOT SECURE-");
179 }
180 else
181 {
182 waddstr(textWin," -secure-");
183 }
184 switch ( sigStatus )
185 {
186 case SignatureIsBad:
187 //cout << " -bad signature- ";
188 waddstr(textWin,"bad signature");
189 break;
190 case SignatureNone:
191 //cout << " -no signature- ";
192 waddstr(textWin,"no signature");
193 break;
194 case SignatureTrusted:
195 //cout << " <signed " << signedBy << " > ";
196 waddstr(textWin,"signed ");
197 waddstr(textWin,signedBy.c_str());
198 break;
199 case SignatureCATrusted:
200 //cout << " <ca signed " << signedBy << " > ";
201 waddstr(textWin,"ca signed " );
202 waddstr(textWin,signedBy.c_str());
203 break;
204 case SignatureNotTrusted:
205 //cout << " <signed " << signedBy << " NOT TRUSTED > ";
206 waddstr(textWin,"untrusted signature ");
207 waddstr(textWin,signedBy.c_str());
208 break;
209 }
210
211 //cout << " says:" << endl;
212 //cout << msg.escaped() << endl;
213 waddstr(textWin, " says: ");
214 waddstr(textWin, msg.escaped().c_str() );
215 waddstr(textWin, "\n");
216
217 wrefresh(textWin);
218 }
219
220
221 void
222 TestCallback::sendPageFailed( const Uri& target, int respNum )
223 {
224 //InfoLog(<< "In TestErrCallback");
225 // cerr << "Message to " << dest << " failed" << endl;
226 Data num(respNum);
227
228 waddstr(textWin,"Message to ");
229 waddstr(textWin, Data::from(target).c_str());
230 waddstr(textWin," failed (");
231 waddstr(textWin,num.c_str());
232 waddstr(textWin," response)\n");
233 wrefresh(textWin);
234 }
235
236
237 void
238 TestCallback::receivePageFailed( const Uri& target )
239 {
240 //InfoLog(<< "In TestErrCallback");
241 // cerr << "Message to " << dest << " failed" << endl;
242
243 waddstr(textWin,"Can not understand messager from ");
244 waddstr(textWin, Data::from(target).c_str());
245 waddstr(textWin,"\n");
246 wrefresh(textWin);
247 }
248
249
250 void
251 TestCallback::registrationFailed(const resip::Uri& target, int respNum )
252 {
253 Data num(respNum);
254
255 waddstr(textWin,"Registration to ");
256 waddstr(textWin, Data::from(target).c_str());
257 waddstr(textWin," failed (");
258 waddstr(textWin,num.c_str());
259 waddstr(textWin," response)\n");
260 wrefresh(textWin);
261 }
262
263
264 void
265 TestCallback::registrationWorked(const resip::Uri& target)
266 {
267 waddstr(textWin,"Registration to ");
268 waddstr(textWin, Data::from(target).c_str());
269 waddstr(textWin," worked");
270 wrefresh(textWin);
271 }
272
273
274 bool
275 processStdin( Uri* dest, bool sign, bool encryp )
276 {
277 static unsigned int num=0;
278 static char buf[1024];
279
280 char c = getch();
281
282 if ( c == 0 )
283 {
284 return true;
285 }
286
287 if ( c == '\f' )
288 {
289 clearok(textWin,TRUE);
290 clearok(statusWin,TRUE);
291 clearok(commandWin,TRUE);
292
293 assert( num < sizeof(buf) );
294 buf[num] = 0;
295 werase(commandWin);
296 waddstr(commandWin,buf);
297
298 wrefresh(textWin);
299 wrefresh(statusWin);
300 wrefresh(commandWin);
301
302 return true;
303 }
304
305 if ( (c == '\a') || (c == '\b') || (c == 4 ) || (c == 0x7F) )
306 {
307 if ( num > 0 )
308 {
309 num--;
310 }
311 buf[num]=0;
312
313 werase(commandWin);
314 waddstr(commandWin,buf);
315 wrefresh(commandWin);
316
317 return true;
318 }
319
320 if ( (c == '\r') || (c == '\n') || (num+2>=sizeof(buf)) )
321 {
322 buf[num] =0;
323
324 if ( (num>3) && (!strncmp("to:",buf,3)) )
325 {
326 buf[num] = 0;
327 *dest = Uri(Data(buf+3));
328
329 //cerr << "Set destination to <" << *dest << ">";
330 waddstr(textWin,"Set destination to ");
331 waddstr(textWin, Data::from(*dest).c_str());
332 waddstr(textWin,"\n");
333 wrefresh(textWin);
334 }
335 else if ( (num>4) && (!strncmp("add:",buf,4)) )
336 {
337 buf[num] = 0;
338 Uri uri(Data(buf+4));
339
340 //cerr << "Subscribing to buddy <" << uri << ">";
341 waddstr(textWin, "Subscribing to ");
342 waddstr(textWin, Data::from(uri).c_str());
343 waddstr(textWin, "\n");
344 wrefresh(textWin);
345
346 tuIM->addBuddy( uri, Data::Empty );
347 displayPres();
348 }
349 else if ( (num>=7) && (!strncmp("status:",buf,7)) )
350 {
351 buf[num] = 0;
352 Data stat(buf+7);
353
354 //cerr << "setting presence status to <" << stat << ">";
355 waddstr(textWin,"Set presece status to <");
356 waddstr(textWin,stat.c_str());
357 waddstr(textWin,">\n");
358 wrefresh(textWin);
359
360 tuIM->setMyPresence( !stat.empty(), stat );
361 }
362 else if ( (num==1) && (!strncmp(".",buf,1)) )
363 {
364 //DebugLog( << "Got a period - end program" );
365 return false;
366 }
367 else
368 {
369 if ( num >= 1 )
370 {
371 assert( num < sizeof(buf) );
372 buf[num] = 0;
373 Data text(buf);
374
375 Data destValue = dest->getAor();
376
377 DebugLog( << "Destination is " << destValue );
378
379 Data encFor = Data::Empty;
380 if (encryp)
381 {
382 encFor = dest->getAorNoPort();
383 }
384
385 DebugLog( << "Destination encrypt for is " << encFor );
386
387 if ( tuIM->haveCerts(sign,encFor) )
388 {
389 waddstr(textWin,"To: ");
390 waddstr(textWin,destValue.c_str());
391 waddstr(textWin," ");
392 waddstr(textWin,text.c_str());
393 waddstr(textWin,"\n");
394 wrefresh(textWin);
395
396 tuIM->sendPage( text , *dest, sign , encFor );
397 }
398 else
399 {
400 waddstr(textWin,"Don't have aproperate certificates to sign and encrypt a message to ");
401 waddstr(textWin,destValue.c_str());
402 waddstr(textWin,"\n");
403 wrefresh(textWin);
404 }
405 }
406 }
407
408 num = 0;
409
410 werase(commandWin);
411 wrefresh(commandWin);
412 }
413 else
414 {
415 buf[num++] = c;
416 assert( num < sizeof(buf) );
417
418 waddch(commandWin,c);
419 wrefresh(commandWin);
420 }
421
422 return true;
423 }
424
425
426 int
427 main(int argc, char* argv[])
428 {
429 try
430 {
431 UserAgent ua(argc, argv);
432
433 InfoLog(<< argv[0] << " starting");
434 while(1)
435 {
436 ua.process();
437 }
438 }
439 catch (BaseSecurity::Exception& e)
440 {
441 WarningLog (<< "Couldn't set up security object");
442 exit(-1);
443 }
444 catch (BaseException& e)
445 {
446 ErrLog (<< "Caught: " << e);
447 exit(-1);
448 }
449 catch( ... )
450 {
451 ErrLog( << "Caught non-resip exception" );
452 exit(-1);
453 }
454
455 return 0;
456 }
457
458 static int
459 myMain(int argc, char* argv[])
460 {
461 DebugLog( << "Done adding the transports " );
462
463
464 InfoLog( << "aor is " << aor );
465 InfoLog( << "contact is " << contact );
466 TestCallback callback;
467 tuIM = new TuIM(&sipStack,aor,contact,&callback);
468
469 Data name("SIPimp.org/0.2.5 (curses)");
470 tuIM->setUAName( name );
471
472 if ( !outbound.host().empty() )
473 {
474 tuIM->setOutboundProxy( outbound );
475 }
476
477 // setup prefered outbound transport
478 if ( prefUdp )
479 {
480 tuIM->setDefaultProtocol( UDP );
481 }
482 if ( prefTcp )
483 {
484 tuIM->setDefaultProtocol( TCP );
485 }
486 if ( prefTls )
487 {
488 tuIM->setDefaultProtocol( TLS );
489 }
490 if ( prefDtls )
491 {
492 tuIM->setDefaultProtocol( DTLS );
493 }
494
495 if ( haveAor )
496 {
497 if ( !noRegister )
498 {
499 tuIM->registerAor( aor, aorPassword );
500 }
501 }
502
503 initscr();
504 cbreak();
505 noecho();
506 nonl();
507 intrflush(stdscr, FALSE);
508 keypad(stdscr, TRUE);
509
510 int rows=0;
511 int cols=0;
512 getmaxyx(stdscr,rows,cols); /* get the number of rows and columns */
513
514 commandWin = newwin(2,cols,rows-2,0);
515 scrollok(commandWin, TRUE);
516 wmove(commandWin,0,0);
517
518 textWin = newwin(rows-3,cols*3/4,0,0);
519 scrollok(textWin, TRUE);
520 wmove(textWin,0,0);
521
522 statusWin = newwin(rows-3,cols-(cols*3/4)-1,0,1+cols*3/4);
523 scrollok(statusWin, FALSE);
524 wmove(statusWin,0,0);
525
526 mvhline(rows-3,0,ACS_HLINE,cols);
527 mvvline(0,(cols*3/4),ACS_VLINE,rows-3);
528 refresh();
529
530 for ( int i=0; i<numAdd; i++ )
531 {
532 Uri uri(addList[i]);
533 tuIM->addBuddy( uri, Data::Empty );
534 }
535
536 for ( int i=0; i<numPub; i++ )
537 {
538 Uri uri(pubList[i]);
539 tuIM->addStateAgent( uri );
540 }
541
542 displayPres();
543
544 waddstr(textWin,"Use -help on the command line to view options\n");
545 waddstr(textWin,"To set where your messages will get sent type\n");
546 waddstr(textWin," to: sip:alice@example.com \n");
547 waddstr(textWin,"To monitores someeone presence type\n");
548 waddstr(textWin," add: sip:buddy@example.com \n");
549 waddstr(textWin,"To change you online status type\n");
550 waddstr(textWin," status: in meeting\n");
551 waddstr(textWin,"To set yourself to offline type\n");
552 waddstr(textWin," status:\n");
553 waddstr(textWin,"To exit type a single period\n");
554 waddstr(textWin,"\n");
555 wrefresh(textWin);
556
557 while (1)
558 {
559 FdSet fdset;
560 sipStack.buildFdSet(fdset);
561 int time = sipStack.getTimeTillNextProcessMS();
562
563 fdset.setRead( fileno(stdin) );
564
565 //cerr << time << endl;
566
567 int err = fdset.selectMilliSeconds( time );
568 if ( err == -1 )
569 {
570 int e = errno;
571 switch (e)
572 {
573 case 0:
574 break;
575 default:
576 //InfoLog(<< "Error " << e << " " << strerror(e) << " in select");
577 break;
578 }
579 }
580 if ( err == 0 )
581 {
582 //cerr << "select timed out" << endl;
583 }
584 if ( err > 0 )
585 {
586 //cerr << "select has " << err << " fd ready" << endl;
587 }
588
589 ////InfoLog(<< "Select returned");
590
591 if ( fdset.readyToRead( fileno(stdin) ) )
592 {
593 bool keepGoing = processStdin(&dest,sign,encryp);
594 if (!keepGoing)
595 {
596 break;
597 }
598 }
599
600 // //DebugLog ( << "Try TO PROCESS " );
601 try
602 {
603 sipStack.process(fdset);
604 }
605 catch (...)
606 {
607 ErrLog( << "Got a exception from sipStack::process" );
608 }
609
610 try
611 {
612 tuIM->process();
613 }
614 catch (...)
615 {
616 ErrLog( << "Got a exception passed from TuIM::process" );
617 }
618 }
619
620 return 0;
621 }
622 /* ====================================================================
623 * The Vovida Software License, Version 1.0
624 *
625 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
626 *
627 * Redistribution and use in source and binary forms, with or without
628 * modification, are permitted provided that the following conditions
629 * are met:
630 *
631 * 1. Redistributions of source code must retain the above copyright
632 * notice, this list of conditions and the following disclaimer.
633 *
634 * 2. Redistributions in binary form must reproduce the above copyright
635 * notice, this list of conditions and the following disclaimer in
636 * the documentation and/or other materials provided with the
637 * distribution.
638 *
639 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
640 * and "Vovida Open Communication Application Library (VOCAL)" must
641 * not be used to endorse or promote products derived from this
642 * software without prior written permission. For written
643 * permission, please contact vocal@vovida.org.
644 *
645 * 4. Products derived from this software may not be called "VOCAL", nor
646 * may "VOCAL" appear in their name, without prior written
647 * permission of Vovida Networks, Inc.
648 *
649 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
650 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
651 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
652 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
653 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
654 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
655 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
656 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
657 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
658 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
659 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
660 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
661 * DAMAGE.
662 *
663 * ====================================================================
664 *
665 * This software consists of voluntary contributions made by Vovida
666 * Networks, Inc. and many individuals on behalf of Vovida Networks,
667 * Inc. For more information on Vovida Networks, Inc., please see
668 * <http://www.vovida.org/>.
669 *
670 */

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27