netcomm: documentation fixes
[fawkes.git] / src / libs / netcomm / worldinfo / transceiver.h
1
2 /***************************************************************************
3  *  transceiver.h - World Info Transceiver
4  *
5  *  Created: Sun Jan 14 17:56:54 2007
6  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9
10 /*  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version. A runtime exception applies to
14  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU Library General Public License for more details.
20  *
21  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23
24 #ifndef __NETCOMM_WORLDINFO_TRANSCEIVER_H_
25 #define __NETCOMM_WORLDINFO_TRANSCEIVER_H_
26
27 #include <core/exception.h>
28 #include <core/utils/lock_list.h>
29
30 #include <netcomm/worldinfo/handler.h>
31 #include <netcomm/worldinfo/defs.h>
32 #include <netcomm/worldinfo/messages.h>
33
34 #include <map>
35 #include <string>
36 #include <cstddef>
37 #include <ctime>
38
39 namespace fawkes {
40
41 class Socket;
42 class WorldInfoMessageEncryptor;
43 class WorldInfoMessageDecryptor;
44 class NetworkNameResolver;
45
46 class WorldInfoException : public Exception
47 {
48  public:
49   WorldInfoException(const char *msg);
50 };
51
52 class WorldInfoTransceiver
53 {
54  public:
55   /** Socket type */
56   enum SocketType {
57     MULTICAST,  /**< Use multicast socket for communication */
58     BROADCAST   /**< Use broadcase socket for communication */
59   };
60
61   WorldInfoTransceiver(SocketType socket_type,
62                        const char *addr, unsigned short port,
63                        const char *key, const char *iv,
64                        NetworkNameResolver *resolver = NULL);
65   ~WorldInfoTransceiver();
66
67   void set_fatmsg_enabled(bool fatmsg_enabled);
68
69   void add_handler(WorldInfoHandler *h);
70   void rem_handler(WorldInfoHandler *h);
71
72   void set_pose(float x, float y, float theta, float *covariance);
73   void set_velocity(float vel_x, float vel_y, float vel_theta, float *covariance);
74
75   void set_rel_ball_pos(float dist, float bearing, float slope, float *covariance);
76   void set_rel_ball_visible(bool visible, int visibility_history);
77   void set_rel_ball_velocity(float vel_x, float vel_y, float vel_z, float *covariance);
78
79   void set_glob_ball_pos(float x, float y, float z, float *covariance);
80   void set_glob_ball_visible(bool visible, int visibility_history);
81   void set_glob_ball_velocity(float vel_x, float vel_y, float vel_z, float *covariance);
82
83   void set_gamestate(int gamestate, worldinfo_gamestate_team_t state_team);
84   void set_score(unsigned int score_cyan, unsigned int score_magenta);
85   void set_team_goal(worldinfo_gamestate_team_t our_color,
86                      worldinfo_gamestate_goalcolor_t goal_color);
87   void set_half(worldinfo_gamestate_half_t half);
88   void add_penalty(unsigned int player, unsigned int penalty,
89                    unsigned int seconds_remaining);
90
91   void clear_opponents();
92   void add_opponent(unsigned int uid, float distance, float bearing, float *covariance);
93   void add_disappeared_opponent(unsigned int uid);
94
95   void send();
96   void recv(bool block = false, unsigned int max_num_msgs = 0);
97
98   void set_loop(bool loop);
99   void flush_sequence_numbers(unsigned int sec);
100
101   void *  last_sent_plain_buffer();
102   size_t  last_sent_plain_buffer_size();
103   void *  last_sent_crypted_buffer();
104   size_t  last_sent_crypted_buffer_size();
105
106  private:
107   void reset_outbound();
108   void crypt_outbound();
109   void append_outbound(uint16_t msg_type, void *msg, uint16_t msg_size);
110
111   Socket *s;
112   bool    loop;
113
114   WorldInfoMessageEncryptor *encryptor;
115   WorldInfoMessageDecryptor *decryptor;
116
117   NetworkNameResolver       *resolver;
118   bool                       resolver_delete;
119
120   void  *in_buffer;
121   void  *out_buffer;
122   void  *crypted_out_buffer;
123   void  *crypted_in_buffer;
124   size_t crypt_buffer_size;
125
126   size_t crypted_out_bytes;
127   size_t crypted_in_bytes;
128   char * __key;
129   char * __iv;
130
131   bool   fatmsg_enabled;
132   void  *fatmsg_buf;
133   size_t fatmsg_bufsize;
134   worldinfo_header_t *fatmsg_header;
135   worldinfo_message_header_t *fatmsg_msgheader;
136   worldinfo_fat_message_t *fatmsg;
137
138   unsigned int out_seq;
139   unsigned int in_seq;
140
141   unsigned char *outbound_buffer;
142   unsigned int outbound_bytes;
143   unsigned int outbound_num_msgs;
144
145   unsigned char *inbound_buffer;
146   size_t         inbound_bytes;
147
148   bool   pose_changed;
149   float  pose_x;
150   float  pose_y;
151   float  pose_theta;
152   float *pose_covariance;
153
154   bool   vel_changed;
155   float  vel_x;
156   float  vel_y;
157   float  vel_theta;
158   float *vel_covariance;
159
160   bool   rel_ball_changed;
161   bool   rel_ball_visible;
162   int    rel_ball_visibility_history;
163   float  rel_ball_dist;
164   float  rel_ball_bearing;
165   float  rel_ball_slope;
166   float *rel_ball_covariance;
167
168   bool   rel_ball_vel_changed;
169   float  rel_ball_vel_x;
170   float  rel_ball_vel_y;
171   float  rel_ball_vel_z;
172   float *rel_ball_vel_covariance;
173
174   bool   glob_ball_changed;
175   bool   glob_ball_visible;
176   int    glob_ball_visibility_history;
177   float  glob_ball_x;
178   float  glob_ball_y;
179   float  glob_ball_z;
180   float *glob_ball_covariance;
181
182   bool   glob_ball_vel_changed;
183   float  glob_ball_vel_x;
184   float  glob_ball_vel_y;
185   float  glob_ball_vel_z;
186   float *glob_ball_vel_covariance;
187
188   bool gamestate_changed;
189   worldinfo_gamestate_message_t gamestate_msg;
190
191   typedef struct {
192     uint32_t uid;
193     float  distance;
194     float  bearing;
195     float *covariance;
196   } opponent_t;
197   std::list<opponent_t> opponents;
198   std::list<opponent_t>::iterator oppit;
199
200   std::list<unsigned int>  disappeared_opponents;
201   std::list<unsigned int>::iterator  doppit;
202
203   std::map<unsigned int, worldinfo_penalty_message_t> penalties;
204   std::map<unsigned int, worldinfo_penalty_message_t>::iterator penit;
205
206   LockList<WorldInfoHandler *> handlers;
207   LockList<WorldInfoHandler *>::iterator hit;
208
209   // Currently we only support IPv4
210   std::map<uint32_t, unsigned int>       sequence_numbers;
211   std::map<uint32_t, time_t>             last_received_time;
212   std::map<uint32_t, time_t>::iterator   lrtit;
213 };
214
215 } // end namespace fawkes
216
217
218 #endif