dashel  1.1.0
dashel.h
Go to the documentation of this file.
1 /*
2  Dashel
3  A cross-platform DAta Stream Helper Encapsulation Library
4  Copyright (C) 2007 -- 2015:
5 
6  Stephane Magnenat <stephane at magnenat dot net>
7  (http://stephane.magnenat.net)
8  Mobots group - Laboratory of Robotics Systems, EPFL, Lausanne
9  (http://mobots.epfl.ch)
10 
11  Sebastian Gerlach
12  Kenzan Technologies
13  (http://www.kenzantech.com)
14 
15  All rights reserved.
16 
17  Redistribution and use in source and binary forms, with or without
18  modification, are permitted provided that the following conditions are met:
19  * Redistributions of source code must retain the above copyright
20  notice, this list of conditions and the following disclaimer.
21  * Redistributions in binary form must reproduce the above copyright
22  notice, this list of conditions and the following disclaimer in the
23  documentation and/or other materials provided with the distribution.
24  * Neither the names of "Mobots", "Laboratory of Robotics Systems", "EPFL",
25  "Kenzan Technologies" nor the names of the contributors may be used to
26  endorse or promote products derived from this software without specific
27  prior written permission.
28 
29  THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY
30  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 */
40 
41 #ifndef __Dashel_H
42 #define __Dashel_H
43 
44 #include <string>
45 #include <set>
46 #include <map>
47 #include <vector>
48 #include <deque>
49 #include <stdexcept>
50 
131 namespace Dashel
133 {
134  class Stream;
135 
137  #define DASHEL_VERSION "1.1.0"
138  #define DASHEL_VERSION_INT 10100
140 
142 
145  class DashelException: public std::runtime_error
146  {
147  public:
149  typedef enum {
159  } Source;
160 
162  Source source;
164  int sysError;
167 
168  public:
170 
175  DashelException(Source s, int se, const char *reason, Stream* stream = NULL);
176  };
177 
179 
182  {
183  public:
185 
191  static std::map<int, std::pair<std::string, std::string> > getPorts();
192  };
193 
196  {
197  public:
198  unsigned address;
199  unsigned short port;
200 
201  public:
203  IPV4Address(unsigned addr = 0, unsigned short prt = 0);
204 
206  IPV4Address(const std::string& name, unsigned short port);
207 
209  bool operator==(const IPV4Address& o) const;
210 
212  bool operator<(const IPV4Address& o) const;
213 
215 
218  std::string format(const bool resolveName = true) const;
219 
221  std::string hostname() const;
222 
224  //bool isValid() const;
225  };
226 
229  {
230  private:
231  std::map<std::string, std::string> values;
232  std::vector<std::string> params;
233 
234  public:
236  void add(const char *line);
237 
239 
244  void addParam(const char *param, const char *value = NULL, bool atStart = false);
245 
247  bool isSet(const char *key) const;
248 
251  template<typename T> T get(const char *key) const;
252 
254  const std::string& get(const char *key) const;
255 
257  std::string getString() const;
258 
260  void erase(const char *key);
261  };
262 
264  class Stream
265  {
266  private:
268  bool failedFlag;
270  std::string failReason;
271 
272  protected:
276  std::string protocolName;
277 
278  protected:
279 
280  friend class Hub;
281 
283  Stream(const std::string& protocolName) : failedFlag(false), protocolName(protocolName) {}
284 
286  virtual ~Stream() {}
287 
288  public:
290 
294  void fail(DashelException::Source s, int se, const char* reason);
295 
297 
299  bool failed() const { return failedFlag; }
300 
302 
304  const std::string &getFailReason() const { return failReason; }
305 
307  const std::string &getProtocolName() const { return protocolName; }
308 
310 
314  std::string getTargetName() const { return protocolName + ":" + target.getString(); }
315 
317 
320  const std::string &getTargetParameter(const char *param) const { return target.get(param); }
321 
323 
331  virtual void write(const void *data, const size_t size) = 0;
332 
334 
337  template<typename T> void write(T v)
338  {
339  write(&v, sizeof(T));
340  }
341 
343 
347  virtual void flush() = 0;
348 
350 
357  virtual void read(void *data, size_t size) = 0;
358 
360 
364  template<typename T> T read()
365  {
366  T v;
367  read(&v, sizeof(T));
368  return v;
369  }
370  };
371 
373 
381  class PacketStream: virtual public Stream
382  {
383  public:
385  PacketStream(const std::string& protocolName) : Stream(protocolName) { }
386 
388 
391  virtual void send(const IPV4Address& dest) = 0;
392 
394 
399  virtual void receive(IPV4Address& source) = 0;
400  };
401 
407  class Hub
408  {
409  public:
411  typedef std::set<Stream*> StreamsSet;
412 
413  private:
414  void *hTerminate;
415  void *streamsLock;
416  StreamsSet streams;
417 
418  protected:
419  StreamsSet dataStreams;
420 
421  public:
422  const bool resolveIncomingNames;
423 
424  public:
428  Hub(const bool resolveIncomingNames = true);
429 
431  virtual ~Hub();
432 
443  Stream* connect(const std::string &target);
444 
454  void closeStream(Stream* stream);
455 
458  void run();
459 
467  bool step(const int timeout = 0);
468 
470  void stop();
471 
474  void lock();
475 
478  void unlock();
479 
480  protected:
481 
492  virtual void connectionCreated(Stream * /* stream */) { }
493 
504  virtual void incomingData(Stream * /* stream */) { }
505 
517  virtual void connectionClosed(Stream * /* stream */, bool /* abnormal */) { }
518  };
519 
522  {
524  typedef Stream* (*CreatorFunc)(const std::string& target, const Hub& hub);
525 
528 
530  void reg(const std::string& proto, const CreatorFunc func);
531 
533  Stream* create(const std::string& proto, const std::string& target, const Hub& hub) const;
534 
536  std::string list() const;
537 
538  protected:
540  typedef std::map<std::string, CreatorFunc> CreatorMap;
542  CreatorMap creators;
543  };
544 
546  template<typename C>
547  Stream* createInstance(const std::string& target, const Hub& hub)
548  {
549  return new C(target);
550  }
551 
553  template<typename C>
554  Stream* createInstanceWithHub(const std::string& target, const Hub& hub)
555  {
556  return new C(target, hub);
557  }
558 
561 }
562 
563 #endif
A data stream, that can be later send data as at UDP packet or read data from an UDP packet...
Definition: dashel.h:381
Some I/O error.
Definition: dashel.h:155
Stream(const std::string &protocolName)
Constructor.
Definition: dashel.h:283
const std::string & getTargetParameter(const char *param) const
Returns the value of a parameter extracted from the target.
Definition: dashel.h:320
The central place where to create, destroy, and synchronize streams.
Definition: dashel.h:407
virtual void connectionCreated(Stream *)
Called when any data connection is created.
Definition: dashel.h:492
bool failed() const
Query failed state of stream.
Definition: dashel.h:299
The target string was bad.
Definition: dashel.h:152
const bool resolveIncomingNames
Whether Dashel should try to resolve the peer&#39;s hostname of incoming TCP connections.
Definition: dashel.h:422
A IP version 4 address.
Definition: dashel.h:195
Parameter set.
Definition: dashel.h:228
virtual void connectionClosed(Stream *, bool)
Called when target closes connection.
Definition: dashel.h:517
void write(T v)
Write a variable of basic type to the stream.
Definition: dashel.h:337
A data stream, with low-level (not-endian safe) read/write functions.
Definition: dashel.h:264
Source source
The exception cause.
Definition: dashel.h:162
The connection was lost.
Definition: dashel.h:154
ParameterSet target
The target description.
Definition: dashel.h:274
Serial port enumerator class.
Definition: dashel.h:181
CreatorMap creators
streams that can be created
Definition: dashel.h:542
The one size fits all exception for streams.
Definition: dashel.h:145
StreamTypeRegistry streamTypeRegistry
The registry of all known stream types.
Dashel, a cross-platform stream abstraction library.
Definition: dashel.h:132
The operation is not valid on this stream.
Definition: dashel.h:153
virtual void incomingData(Stream *)
Called when data is available for reading on the stream.
Definition: dashel.h:504
DashelException(Source s, int se, const char *reason, Stream *stream=NULL)
Construct an stream exception with everything.
The incoming data was not read by the Hub subclass.
Definition: dashel.h:158
unsigned address
IP host address. Stored in local byte order.
Definition: dashel.h:198
T read()
Read a variable of basic type from the stream.
Definition: dashel.h:364
Well, hopefully never used.
Definition: dashel.h:150
unsigned short port
IP port. Stored in local byte order.
Definition: dashel.h:199
StreamsSet dataStreams
All our streams that transfer data (in opposition to streams that just listen for data)...
Definition: dashel.h:419
const std::string & getFailReason() const
Returns the reason the stream has failed.
Definition: dashel.h:304
Stream * createInstanceWithHub(const std::string &target, const Hub &hub)
Create an instance of stream type C, passing target to its constructor.
Definition: dashel.h:554
Some synchronisation error.
Definition: dashel.h:151
std::set< Stream * > StreamsSet
A list of streams.
Definition: dashel.h:411
Stream * createInstance(const std::string &target, const Hub &hub)
Create an instance of stream type C, passing target to its constructor.
Definition: dashel.h:547
virtual ~Stream()
Virtual destructor, to ensure calls to destructors of sub-classes.
Definition: dashel.h:286
int sysError
The reason as an OS error code.
Definition: dashel.h:164
T get(const char *key) const
Get a parameter value.
const std::string & getProtocolName() const
Returns the protocol name of the stream.
Definition: dashel.h:307
std::map< std::string, CreatorFunc > CreatorMap
a map of stream type names to constructors and arguments
Definition: dashel.h:540
Registry of constructors to a stream, to add new stream types dynamically.
Definition: dashel.h:521
Some serial enumeration error.
Definition: dashel.h:157
std::string getTargetName() const
Returns the name of the target.
Definition: dashel.h:314
Stream * stream
The stream that caused the exception to be thrown.
Definition: dashel.h:166
Source
The different exception causes.
Definition: dashel.h:149
PacketStream(const std::string &protocolName)
Constructor.
Definition: dashel.h:385
std::string protocolName
The protocol name.
Definition: dashel.h:276
The connection could not be established.
Definition: dashel.h:156
std::string getString() const
Get the parameters as string.