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.3"
00136
00137 #define DASHEL_VERSION_INT 10003
00138
00140
00143 class DashelException: public std::runtime_error
00144 {
00145 public:
00147 typedef enum {
00148 Unknown,
00149 SyncError,
00150 InvalidTarget,
00151 InvalidOperation,
00152 ConnectionLost,
00153 IOError,
00154 ConnectionFailed,
00155 EnumerationError,
00156 PreviousIncomingDataNotRead
00157 } Source;
00158
00160 Source source;
00162 int sysError;
00164 Stream *stream;
00165
00166 public:
00168
00173 DashelException(Source s, int se, const char *reason, Stream* stream = NULL);
00174 };
00175
00177
00179 class SerialPortEnumerator
00180 {
00181 public:
00183
00189 static std::map<int, std::pair<std::string, std::string> > getPorts();
00190 };
00191
00193 class IPV4Address
00194 {
00195 public:
00196 unsigned address;
00197 unsigned short port;
00198
00199 public:
00201 IPV4Address(unsigned addr = 0, unsigned short prt = 0);
00202
00204 IPV4Address(const std::string& name, unsigned short port);
00205
00207 bool operator==(const IPV4Address& o) const;
00208
00210 bool operator<(const IPV4Address& o) const;
00211
00213
00216 std::string format(const bool resolveName = true) const;
00217
00219 std::string hostname() const;
00220
00222
00223 };
00224
00226 class ParameterSet
00227 {
00228 private:
00229 std::map<std::string, std::string> values;
00230 std::vector<std::string> params;
00231
00232 public:
00234 void add(const char *line);
00235
00237
00242 void addParam(const char *param, const char *value = NULL, bool atStart = false);
00243
00245 bool isSet(const char *key) const;
00246
00248 template<typename T> T get(const char *key) const;
00249
00251 const std::string& get(const char *key) const;
00252
00254 std::string getString() const;
00255
00257 void erase(const char *key);
00258 };
00259
00261 class Stream
00262 {
00263 private:
00265 bool failedFlag;
00267 std::string failReason;
00268
00269 protected:
00271 ParameterSet target;
00273 std::string protocolName;
00274
00275 protected:
00276
00277 friend class Hub;
00278
00280 Stream(const std::string& protocolName) : failedFlag(false), protocolName(protocolName) {}
00281
00283 virtual ~Stream() {}
00284
00285 public:
00287
00291 void fail(DashelException::Source s, int se, const char* reason);
00292
00294
00296 bool failed() const { return failedFlag; }
00297
00299
00301 const std::string &getFailReason() const { return failReason; }
00302
00304 const std::string &getProtocolName() const { return protocolName; }
00305
00307
00311 std::string getTargetName() const { return protocolName + ":" + target.getString(); }
00312
00314
00317 const std::string &getTargetParameter(const char *param) const { return target.get(param); }
00318
00320
00328 virtual void write(const void *data, const size_t size) = 0;
00329
00331
00334 template<typename T> void write(T v)
00335 {
00336 write(&v, sizeof(T));
00337 }
00338
00340
00344 virtual void flush() = 0;
00345
00347
00354 virtual void read(void *data, size_t size) = 0;
00355
00357
00361 template<typename T> T read()
00362 {
00363 T v;
00364 read(&v, sizeof(T));
00365 return v;
00366 }
00367 };
00368
00370
00378 class PacketStream: virtual public Stream
00379 {
00380 public:
00382 PacketStream(const std::string& protocolName) : Stream(protocolName) { }
00383
00385
00388 virtual void send(const IPV4Address& dest) = 0;
00389
00391
00396 virtual void receive(IPV4Address& source) = 0;
00397 };
00398
00404 class Hub
00405 {
00406 public:
00408 typedef std::set<Stream*> StreamsSet;
00409
00410 private:
00411 void *hTerminate;
00412 void *streamsLock;
00413 StreamsSet streams;
00414
00415 protected:
00416 StreamsSet dataStreams;
00417 const bool resolveIncomingNames;
00418
00419 public:
00423 Hub(const bool resolveIncomingNames = true);
00424
00426 virtual ~Hub();
00427
00438 Stream* connect(const std::string &target);
00439
00449 void closeStream(Stream* stream);
00450
00453 void run();
00454
00462 bool step(const int timeout = 0);
00463
00465 void stop();
00466
00470 void lock();
00471
00475 void unlock();
00476
00477 protected:
00478
00489 virtual void connectionCreated(Stream * ) { }
00490
00501 virtual void incomingData(Stream * ) { }
00502
00514 virtual void connectionClosed(Stream * , bool ) { }
00515 };
00516 }
00517
00518 #endif