dashel/dashel.h

Go to the documentation of this file.
00001 /*
00002     Dashel
00003     A cross-platform DAta Stream Helper Encapsulation Library
00004     Copyright (C) 2007 -- 2008:
00005         
00006         Stephane Magnenat <stephane at magnenat dot net>
00007             (http://stephane.magnenat.net)
00008         Mobots group - Laboratory of Robotics Systems, EPFL, Lausanne
00009             (http://mobots.epfl.ch)
00010         
00011         Sebastian Gerlach
00012         Kenzan Technologies
00013             (http://www.kenzantech.com)
00014     
00015     All rights reserved.
00016     
00017     Redistribution and use in source and binary forms, with or without
00018     modification, are permitted provided that the following conditions are met:
00019         * Redistributions of source code must retain the above copyright
00020           notice, this list of conditions and the following disclaimer.
00021         * Redistributions in binary form must reproduce the above copyright
00022           notice, this list of conditions and the following disclaimer in the
00023           documentation and/or other materials provided with the distribution.
00024         * Neither the names of "Mobots", "Laboratory of Robotics Systems", "EPFL",
00025           "Kenzan Technologies" nor the names of the contributors may be used to
00026           endorse or promote products derived from this software without specific
00027           prior written permission.
00028     
00029     THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY
00030     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00031     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00032     DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
00033     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00034     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00036     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00038     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039 */
00040 
00041 #ifndef __Dashel_H
00042 #define __Dashel_H
00043 
00044 #include <string>
00045 #include <set>
00046 #include <map>
00047 #include <vector>
00048 #include <deque>
00049 #include <stdexcept>
00050 
00129 
00130 namespace Dashel
00131 {
00132     class Stream;
00133     
00135     #define DASHEL_VERSION "1.0.0"
00136     
00138 
00141     class DashelException: public std::runtime_error
00142     {
00143     public:
00145         typedef enum {
00146             Unknown,            
00147             SyncError,          
00148             InvalidTarget,      
00149             InvalidOperation,   
00150             ConnectionLost,     
00151             IOError,            
00152             ConnectionFailed,   
00153             EnumerationError,   
00154             PreviousIncomingDataNotRead 
00155         } Source;
00156 
00158         Source source;
00160         int sysError;
00162         Stream *stream;
00163 
00164     public:
00166 
00171         DashelException(Source s, int se, const char *reason, Stream* stream = NULL);
00172     };
00173     
00175 
00177     class SerialPortEnumerator
00178     {
00179     public:
00181 
00187         static std::map<int, std::pair<std::string, std::string> > getPorts();
00188     };
00189     
00191     class IPV4Address
00192     {
00193     public:
00194         unsigned address; 
00195         unsigned short port; 
00196     
00197     public:
00199         IPV4Address(unsigned addr = 0, unsigned short prt = 0);
00200         
00202         IPV4Address(const std::string& name, unsigned short port);
00203     
00205         bool operator==(const IPV4Address& o) const;
00206         
00208         bool operator<(const IPV4Address& o) const;
00209         
00211         std::string format() const;
00212         
00214         std::string hostname() const;
00215         
00217         //bool isValid() const;
00218     };
00219 
00221     class Stream
00222     {
00223     private:
00225         bool failedFlag;
00227         std::string failReason;
00228 
00229     protected:
00231         std::string targetName;
00232         
00233     public:
00235         Stream(const std::string& targetName) { this->targetName = targetName; failedFlag = false; }
00236     
00238         virtual ~Stream() {}
00239         
00241 
00245         void fail(DashelException::Source s, int se, const char* reason);
00246         
00248 
00250         bool failed() const { return failedFlag; }
00251         
00253 
00255         const std::string &getFailReason() const { return failReason; }
00256         
00258 
00262         const std::string &getTargetName() const { return targetName; }
00263         
00265 
00273         virtual void write(const void *data, const size_t size) = 0;
00274         
00276 
00279         template<typename T> void write(T v)
00280         {
00281             write(&v, sizeof(T));
00282         }
00283         
00285 
00289         virtual void flush() = 0;
00290         
00292 
00299         virtual void read(void *data, size_t size) = 0;
00300         
00302 
00306         template<typename T> T read()
00307         {
00308             T v;
00309             read(&v, sizeof(T));
00310             return v;
00311         }
00312     };
00313     
00315 
00323     class PacketStream: virtual public Stream
00324     {
00325     public:
00327         PacketStream(const std::string& targetName) : Stream(targetName) { }
00328     
00330 
00333         virtual void send(const IPV4Address& dest) = 0;
00334         
00336 
00341         virtual void receive(IPV4Address& source) = 0;
00342     };
00343     
00349     class Hub
00350     {
00351     public:
00353         typedef std::set<Stream*> StreamsSet;
00354         
00355     private:
00356         void *hTerminate;           
00357         StreamsSet streams;         
00358     
00359     protected:
00360         StreamsSet dataStreams;     
00361     
00362     public:
00364         Hub();
00365     
00367         virtual ~Hub();
00368         
00379         Stream* connect(const std::string &target);
00380         
00390         void closeStream(Stream* stream);
00391         
00393         void run(void);
00394         
00402         bool step(int timeout = 0);
00403         
00405         void stop();
00406 
00407     protected:
00408         
00418         virtual void connectionCreated(Stream *stream) { }
00419         
00429         virtual void incomingData(Stream *stream) { }
00430         
00441         virtual void connectionClosed(Stream *stream, bool abnormal) { }
00442     };
00443 }
00444 
00445 #endif

Generated on Sun Mar 1 03:10:15 2009 for dashel by  doxygen 1.5.1