wvstreams.cc

00001 #include "wvistreamlist.h"
00002 #include "wvlog.h"
00003 #include "wvmoniker.h"
00004 #include "wvstreamclone.h"
00005 #include "wvlinkerhack.h"
00006 #include <signal.h>
00007 
00008 WV_LINK_TO(WvConStream);
00009 WV_LINK_TO(WvTCPConn);
00010 
00011 volatile bool want_to_die = false;
00012 
00013 static void signalhandler(int sig)
00014 {
00015     fprintf(stderr, "Caught signal %d.  Exiting...\n", sig);
00016     want_to_die = true;
00017     signal(sig, SIG_DFL);
00018 }
00019 
00020 
00021 static void bounce_to_list(WvStream &s, void *userdata)
00022 {
00023     WvIStreamList *list = (WvIStreamList *)userdata;
00024     char buf[4096];
00025     size_t len;
00026     
00027     for (int i = 0; i < 1000; i++)
00028     {
00029         len = s.read(buf, sizeof(buf));
00030         if (!len) break;
00031         
00032         WvIStreamList::Iter i(*list);
00033         for (i.rewind(); i.next(); )
00034         {
00035             if (&s != i.ptr())
00036             {
00037                 // you might think this assumes IWvStream has a buffer; but in
00038                 // fact, we already know that everything in the list is a
00039                 // WvStreamClone, and WvStreamClone *does* have an output
00040                 // buffer, so this is safe.
00041                 i->write(buf, len);
00042             }
00043         }
00044     }
00045 }
00046 
00047 
00048 int main(int argc, char **argv)
00049 {
00050     WvIStreamList list;
00051     WvLog log(argv[0], WvLog::Debug);
00052     
00053     signal(SIGTERM, signalhandler);
00054     signal(SIGINT, signalhandler);
00055 
00056     if (argc <= 1)
00057     {
00058         fprintf(stderr, "Usage: %s <stream1> [stream2 [stream3...]]\n",
00059                 argv[0]);
00060         return 1;
00061     }
00062     
00063     for (int count = 1; count < argc; count++)
00064     {
00065         log("Creating stream: '%s'\n", argv[count]);
00066         IWvStream *s = wvcreate<IWvStream>(argv[count]);
00067         if (!s)
00068         {
00069             fprintf(stderr, "Can't create stream %s: no moniker!\n",
00070                     argv[count]);
00071             return 2;
00072         }
00073         
00074         if (!s->isok())
00075         {
00076             fprintf(stderr, "Stream %s: %s\n",
00077                     argv[count], s->errstr().cstr());
00078             return 3;
00079         }
00080         
00081         WvStream *s2 = new WvStreamClone(s);
00082         
00083         s2->setcallback(bounce_to_list, &list);
00084         list.append(s2, true, argv[count]);
00085     }
00086     
00087     while (!want_to_die && list.count() >= 2)
00088         list.runonce();
00089 }

Generated on Thu May 25 21:51:04 2006 for WvStreams by  doxygen 1.4.6