message.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000
4  * Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 /********************************************************************
23  *
24  * This library is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with this library; if not, write to the Free Software
36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37  *
38  ********************************************************************/
39 
40 /*
41  * Desc: Message class and message queues
42  * CVS: $Id$
43  * Author: Toby Collett - Jan 2005
44  */
45 
46 #ifndef MESSAGE_H
47 #define MESSAGE_H
48 
49 #if defined (WIN32)
50  #if defined (PLAYER_STATIC)
51  #define PLAYERCORE_EXPORT
52  #elif defined (playercore_EXPORTS)
53  #define PLAYERCORE_EXPORT __declspec (dllexport)
54  #else
55  #define PLAYERCORE_EXPORT __declspec (dllimport)
56  #endif
57 #else
58  #define PLAYERCORE_EXPORT
59 #endif
60 
61 #include <pthread.h>
62 
63 #include <libplayerinterface/player.h>
64 
65 class MessageQueue;
66 
73 class PLAYERCORE_EXPORT QueuePointer
74 {
75  public:
77  QueuePointer();
79  QueuePointer(bool _Replace, size_t _Maxlen);
81  ~QueuePointer();
83  QueuePointer(const QueuePointer & CopyFrom);
84 
86  QueuePointer & operator = (const QueuePointer & rhs);
88  MessageQueue * operator -> ();
90  MessageQueue * get() const;
92  MessageQueue & operator * ();
94  bool operator == (const QueuePointer & rhs);
96  bool operator == (void * pointer);
98  bool operator != (const QueuePointer & rhs);
100  bool operator != (void * pointer);
101 
102  private:
104  void DecRef();
105 
107  MessageQueue * Queue;
108 
110  unsigned int * RefCount;
111 
113  pthread_mutex_t * Lock;
114 };
115 
116 
117 
132 class PLAYERCORE_EXPORT Message
133 {
134  public:
137  Message(const struct player_msghdr & Header,
138  void* data,
139  bool copy = true);
140 
143  Message(const struct player_msghdr & Header,
144  void* data,
145  QueuePointer &_queue,
146  bool copy = true);
147 
149  Message(const Message & rhs);
150 
152  ~Message();
153 
159  static bool MatchMessage(player_msghdr_t* hdr,
160  int type,
161  int subtype,
162  player_devaddr_t addr)
163  {
164  return(((type < 0) || (hdr->type == (uint8_t)type)) &&
165  ((subtype < 0) || (hdr->subtype == (uint8_t)subtype)) &&
166  (hdr->addr.host == addr.host) &&
167  (hdr->addr.robot == addr.robot) &&
168  (hdr->addr.interf == addr.interf) &&
169  (hdr->addr.index == addr.index));
170  }
171 
177  static bool MatchMessage(player_msghdr_t* hdr,
178  int type,
179  int subtype)
180  {
181  return(((type < 0) || (hdr->type == (uint8_t)type)) &&
182  ((subtype < 0) || (hdr->subtype == (uint8_t)subtype)));
183  }
184 
186  player_msghdr_t * GetHeader() {return &Header;};
188  void* GetPayload() {return (void*)Data;};
190  unsigned int GetDataSize() {return Header.size;};
192  bool Compare(Message &other);
194  void DecRef();
195 
197  QueuePointer Queue;
198 
200  unsigned int * RefCount;
201 
202  private:
203  void CreateMessage(const struct player_msghdr & Header,
204  void* data,
205  bool copy = true);
206 
208  player_msghdr_t Header;
210  uint8_t * Data;
212  pthread_mutex_t * Lock;
213 };
214 
218 class PLAYERCORE_EXPORT MessageQueueElement
219 {
220  public:
225 
227  Message* msg;
228  private:
230  MessageQueueElement * prev;
232  MessageQueueElement * next;
233 
234  friend class MessageQueue;
235 };
236 
243 class PLAYERCORE_EXPORT MessageReplaceRule
244 {
245  private:
246  // The address to match (not using a player_devaddr_t so that we can
247  // use -1 to indicate don't care)
248  int host, robot, interf, index;
249  // The type and subtype to match (-1 is don't care)
250  int type, subtype;
251  public:
252  MessageReplaceRule(int _host, int _robot, int _interf, int _index,
253  int _type, int _subtype, int _replace) :
254  host(_host), robot(_robot), interf(_interf), index(_index),
255  type(_type), subtype(_subtype), replace(_replace), next(NULL) {}
256 
257  bool Match(player_msghdr_t* hdr)
258  {
259  return(((this->host < 0) ||
260  ((uint32_t)this->host == hdr->addr.host)) &&
261  ((this->robot < 0) ||
262  ((uint32_t)this->robot == hdr->addr.robot)) &&
263  ((this->interf < 0) ||
264  ((uint16_t)this->interf == hdr->addr.interf)) &&
265  ((this->index < 0) ||
266  ((uint16_t)this->index == hdr->addr.index)) &&
267  ((this->type < 0) ||
268  ((uint8_t)this->type == hdr->type)) &&
269  ((this->subtype < 0) ||
270  ((uint8_t)this->subtype == hdr->subtype)));
271  }
272 
273  bool Equivalent (int _host, int _robot, int _interf, int _index, int _type, int _subtype)
274  {
275  return (host == _host && robot == _robot && interf ==_interf && index == _index &&
276  type == _type && subtype == _subtype);
277  }
278 
279  // To replace, or not to replace
280  // That is the question
281  int replace;
282  // Next rule in the list
283  MessageReplaceRule* next;
284 };
285 
337 class PLAYERCORE_EXPORT MessageQueue
338 {
339  public:
341  MessageQueue(bool _Replace, size_t _Maxlen);
343  ~MessageQueue();
345  bool Empty() { return(this->head == NULL); }
348  bool Push(Message& msg);
349 
354  void PushFront(Message & msg, bool haveLock);
355 
360  void PushBack(Message & msg, bool haveLock);
361 
365  Message* Pop();
369  void SetReplace(bool _Replace) { this->Replace = _Replace; };
376  void AddReplaceRule(int _host, int _robot, int _interf, int _index,
377  int _type, int _subtype, int _replace);
383  void AddReplaceRule(const player_devaddr_t &device,
384  int _type, int _subtype, int _replace);
387  int CheckReplace(player_msghdr_t* hdr);
394  bool Wait(double TimeOut=0.0);
397  void DataAvailable(void);
399  bool Filter(Message& msg);
401  void ClearFilter(void);
403  void SetFilter(int host, int robot, int interf, int index,
404  int type, int subtype);
406  void SetPull (bool _pull) { this->pull = _pull; }
407 
409  size_t GetLength(void);
410 
412  void SetDataRequested(bool d, bool haveLock);
413 
414  private:
416  void Lock() {pthread_mutex_lock(&lock);};
418  void Unlock() {pthread_mutex_unlock(&lock);};
421  void Remove(MessageQueueElement* el);
423  MessageQueueElement* head;
425  MessageQueueElement* tail;
427  pthread_mutex_t lock;
429  size_t Maxlen;
431  MessageReplaceRule* replaceRules;
434  bool Replace;
436  size_t Length;
440  pthread_cond_t cond;
442  pthread_mutex_t condMutex;
444  bool filter_on;
445  int filter_host, filter_robot, filter_interf,
446  filter_index, filter_type, filter_subtype;
449  bool pull;
452  bool data_requested;
454  bool data_delivered;
456  bool drop_count;
457 };
458 
459 
460 
461 
462 #endif
#define PLAYER_WARN1(msg, a)
Error message macros.
Definition: error.h:89
position 2d velocity command
Definition: player_interfaces.h:617
player_pose2d_t vel
translational velocities [m/s,m/s,rad/s] (x, y, yaw)
Definition: player_interfaces.h:620
uint32_t host
The "host" on which the device resides.
Definition: player.h:147
static bool MatchMessage(player_msghdr_t *hdr, int type, int subtype, player_devaddr_t addr)
Helper for message processing.
Definition: message.h:158
double ReadFloat(int section, const char *name, double value)
Read a floating point (double) value.
A pose in the plane.
Definition: player.h:216
#define PLAYER_LASER_DATA_SCANPOSE
Data subtype: pose-stamped scan.
Definition: player_interfaces.h:848
Generic message header.
Definition: player.h:160
float max_angle
Start and end angles for the laser scan [rad].
Definition: player_interfaces.h:888
virtual int MainSetup(void)
Sets up the resources needed by the driver thread.
Definition: driver.h:657
virtual void MainQuit(void)
Cleanup method for driver thread (called when main exits)
Definition: driver.h:663
uint8_t type
Message type; must be one of PLAYER_MSGTYPE_*.
Definition: player.h:165
Encapsulates a device (i.e., a driver bound to an interface)
Definition: device.h:73
const char * ReadString(int section, const char *name, const char *value)
Read a string value.
double px
X [m].
Definition: player.h:219
We keep a singly-linked list of (addr,type,subtype,replace) tuples.
Definition: message.h:242
#define PLAYER_POSITION2D_CMD_VEL
Command: velocity (PLAYER_POSITION2D_CMD_VEL)
Definition: player_interfaces.h:581
uint8_t subtype
Message subtype; interface specific.
Definition: player.h:167
uint32_t robot
The "robot" or device collection in which the device resides.
Definition: player.h:150
virtual void Main(void)=0
Main method for driver thread.
const char * ReadTupleString(int section, const char *name, int index, const char *value)
Read a string from a tuple field.
int ReadInt(int section, const char *name, int value)
Read an integer value.
void * GetPayload()
Get pointer to payload.
Definition: message.h:187
player_pose2d_t pose
The global pose of the laser at the time the scan was acquired.
Definition: player_interfaces.h:914
#define PLAYER_MSGTYPE_DATA
A data message.
Definition: player.h:94
double py
Y [m].
Definition: player.h:221
player_laser_data_t scan
The scan data.
Definition: player_interfaces.h:912
uint32_t id
A unique, increasing, ID for the scan.
Definition: player_interfaces.h:902
float * ranges
Range readings [m].
Definition: player_interfaces.h:896
virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr *hdr, void *data)
Message handler.
#define PLAYER_MSGTYPE_REQ
A request message.
Definition: player.h:105
int ReadDeviceAddr(player_devaddr_t *addr, int section, const char *name, int code, int index, const char *key)
Read a device id.
Class for loading configuration file information.
Definition: configfile.h:195
A device address.
Definition: player.h:144
An autopointer for the message queue.
Definition: message.h:72
position2d data
Definition: player_interfaces.h:606
A doubly-linked queue of messages.
Definition: message.h:336
This class is a helper for maintaining doubly-linked queues of Messages.
Definition: message.h:217
#define PLAYER_ERROR(msg)
Error message macros.
Definition: error.h:80
float min_angle
Start and end angles for the laser scan [rad].
Definition: player_interfaces.h:886
Base class for drivers which oeprate with a thread.
Definition: driver.h:551
#define PLAYER_POSITION2D_DATA_STATE
Data: state (PLAYER_POSITION2D_DATA_STATE)
Definition: player_interfaces.h:568
float max_range
Maximum range [m].
Definition: player_interfaces.h:892
double timestamp
Time associated with message contents (seconds since epoch)
Definition: player.h:169
player_pose2d_t pos
position [m,m,rad] (x, y, yaw)
Definition: player_interfaces.h:609
uint32_t size
Size in bytes of the payload to follow.
Definition: player.h:173
Reference-counted message objects.
Definition: message.h:131
#define PLAYER_MSGTYPE_CMD
A command message.
Definition: player.h:98
double pa
yaw [rad]
Definition: player.h:223
Base class for all drivers.
Definition: driver.h:107
uint32_t intensity_count
Number of intensity readings.
Definition: player_interfaces.h:898
#define PLAYER_POSITION2D_DATA_GEOM
Data: geometry.
Definition: player_interfaces.h:574
Definition: TData.h:57
Data: pose-stamped scan (PLAYER_LASER_DATA_SCANPOSE)
Definition: player_interfaces.h:909
float resolution
Angular resolution [rad].
Definition: player_interfaces.h:890
uint32_t ranges_count
Number of range readings.
Definition: player_interfaces.h:894
Definition: TData.h:47
Data: scan (PLAYER_LASER_DATA_SCAN)
Definition: player_interfaces.h:883
player_msghdr_t * GetHeader()
Get pointer to header.
Definition: message.h:185
uint16_t index
Which device of that interface.
Definition: player.h:154
player_devaddr_t addr
Device to which this message pertains.
Definition: player.h:163
uint16_t interf
The interface provided by the device; must be one of PLAYER_*_CODE.
Definition: player.h:152
#define PLAYER_MSGQUEUE_DEFAULT_MAXLEN
Default maximum length for a message queue.
Definition: player.h:75
position2d position command
Definition: player_interfaces.h:626
uint8_t * intensity
Intensity readings.
Definition: player_interfaces.h:900