Siena C++ API documentation (v. 0.4.3b5)

subscriber.cc

This is an example of how to program a simple subscriber application. To compile this example on a GNU/Linux system or on other Unix-like systems, assuming Siena has been installed in $prefix, execute:

  c++ subscriber.cc -o subscriber -I$prefix/include -L$prefix/lib -lsiena
  

On some platforms (notably Solaris), you will have to link additional libraris (e.g., -lsocket -lnsl).

To compile this example on a win32 system, using Visual C++, assuming Siena has been installed in %prefix%, execute:

  cl -I%prefix%
-GX -c subscriber.cc link -out:subscriber subscriber.obj %prefix%.lib wsock32.lib

00001 // -*- C++ -*-
00002 //
00003 //  This file is part of Siena, a wide-area event notification system.
00004 //  See http://www.cs.colorado.edu/serl/siena/
00005 //
00006 //  Author: Antonio Carzaniga <[email protected]>
00007 //  See the file AUTHORS for full details. 
00008 //
00009 //  Copyright (C) 1998-2001 University of Colorado
00010 //
00011 //  This program is free software; you can redistribute it and/or
00012 //  modify it under the terms of the GNU General Public License
00013 //  as published by the Free Software Foundation; either version 2
00014 //  of the License, or (at your option) any later version.
00015 //
00016 //  This program is distributed in the hope that it will be useful,
00017 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License
00022 //  along with this program; if not, write to the Free Software
00023 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
00024 //  USA, or send email to [email protected].
00025 //
00026 // $Id: subscriber.cc,v 1.1 2003/07/09 20:15:55 carzanig Exp $
00027 //
00028 #include <string.h>
00029 #include <stdlib.h>
00030 
00031 #include <iostream>
00032 #include <exception>
00033                                                 // include Siena headers
00034 #include <siena/Siena.h>
00035 #include <siena/ThinClient.h>
00036 
00037 static void print_usage(const char * progname) {
00038     cerr << "usage: " << progname 
00039          << " [-udp] [-port <num>] [-host <host>] <uri-master>" << endl;
00040 }
00041 
00042 int main (int argc, char *argv[])
00043 {
00044     bool udp = false;
00045     unsigned short port = 1969;
00046     const char * master = NULL;
00047     const char * thishost = NULL;
00048     
00049     try {
00050         int i;
00051         for(i=1; i < argc; ++i) {               // parse cmd-line params
00052             if(strcmp(argv[i], "-udp")==0) {
00053                 udp = true;
00054             } else if (strcmp(argv[i], "-port")==0) {
00055                 if (++i < argc) {
00056                     port = atoi(argv[i]);
00057                 } else {
00058                     print_usage(argv[0]);
00059                     return 1;
00060                 }
00061             } else if (strcmp(argv[i], "-host")==0) {
00062                 if (++i < argc) {
00063                     thishost = argv[i];
00064                 } else {
00065                     print_usage(argv[0]);
00066                     return 1;
00067                 }
00068             } else {
00069                 master = argv[i];
00070             }
00071         }
00072         if (master == NULL) {
00073             print_usage(argv[0]);
00074             return 1;
00075         }
00076 
00077         ThinClient siena(master);               // create interface to
00078         Receiver * r;                           // given master server
00079         if (udp) {
00080             r = new UDPReceiver(port, thishost);// create receiver for 
00081         } else {                                // this interface
00082             r = new TCPReceiver(port, thishost);
00083         }
00084         siena.set_receiver(r);                  // set receiver
00085 
00086         Filter f;                               // create subscription filter
00087         f.add_constraint("stock", SX_eq, "XYZ");
00088         f.add_constraint("price", SX_lt, (long)100);
00089         siena.subscribe(f);                     // subscribe
00090 
00091         Notification * n;
00092 
00093         for (i = 1; i < 20; ++i) {              // read incoming notifications
00094             n = siena.get_notification();
00095             if (n != NULL)  {
00096                 cout << "stock=" << (*n)["stock"].string_value()
00097                      << " price=" << (*n)["price"].int_value() << endl;
00098                 delete(n);
00099             }
00100         }
00101         siena.unsubscribe();                    // unsubscribe and shutdown 
00102         siena.shutdown();                       // interface
00103         delete(r);
00104     } catch (exception &ex) {
00105         cout << "error: " << ex.what() << endl;
00106     }
00107     return 0;
00108 }

Copyright © 2001 University of Colorado.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". This documentation is authored and maintained by Antonio Carzaniga