00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
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