transceiver.h

00001 
00002 /***************************************************************************
00003  *  transceiver.h - World Info Transceiver
00004  *
00005  *  Created: Sun Jan 14 17:56:54 2007
00006  *  Copyright  2006-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __NETCOMM_WORLDINFO_TRANSCEIVER_H_
00025 #define __NETCOMM_WORLDINFO_TRANSCEIVER_H_
00026 
00027 #include <core/exception.h>
00028 #include <core/utils/lock_list.h>
00029 
00030 #include <netcomm/worldinfo/handler.h>
00031 #include <netcomm/worldinfo/defs.h>
00032 #include <netcomm/worldinfo/messages.h>
00033 
00034 #include <map>
00035 #include <string>
00036 #include <cstddef>
00037 #include <ctime>
00038 
00039 namespace fawkes {
00040 
00041 class Socket;
00042 class WorldInfoMessageEncryptor;
00043 class WorldInfoMessageDecryptor;
00044 class NetworkNameResolver;
00045 
00046 class WorldInfoException : public Exception
00047 {
00048  public:
00049   WorldInfoException(const char *msg);
00050 };
00051 
00052 class WorldInfoTransceiver
00053 {
00054  public:
00055   /** Socket type */
00056   enum SocketType {
00057     MULTICAST,  /**< Use multicast socket for communication */
00058     BROADCAST   /**< Use broadcase socket for communication */
00059   };
00060 
00061   WorldInfoTransceiver(SocketType socket_type,
00062                        const char *addr, unsigned short port,
00063                        const char *key, const char *iv,
00064                        NetworkNameResolver *resolver = NULL);
00065   ~WorldInfoTransceiver();
00066 
00067   void set_fatmsg_enabled(bool fatmsg_enabled);
00068 
00069   void add_handler(WorldInfoHandler *h);
00070   void rem_handler(WorldInfoHandler *h);
00071 
00072   void set_pose(float x, float y, float theta, float *covariance);
00073   void set_velocity(float vel_x, float vel_y, float vel_theta, float *covariance);
00074 
00075   void set_rel_ball_pos(float dist, float bearing, float slope, float *covariance);
00076   void set_rel_ball_visible(bool visible, int visibility_history);
00077   void set_rel_ball_velocity(float vel_x, float vel_y, float vel_z, float *covariance);
00078 
00079   void set_glob_ball_pos(float x, float y, float z, float *covariance);
00080   void set_glob_ball_visible(bool visible, int visibility_history);
00081   void set_glob_ball_velocity(float vel_x, float vel_y, float vel_z, float *covariance);
00082 
00083   void set_gamestate(int gamestate, worldinfo_gamestate_team_t state_team);
00084   void set_score(unsigned int score_cyan, unsigned int score_magenta);
00085   void set_team_goal(worldinfo_gamestate_team_t our_color,
00086                      worldinfo_gamestate_goalcolor_t goal_color);
00087   void set_half(worldinfo_gamestate_half_t half);
00088   void add_penalty(unsigned int player, unsigned int penalty,
00089                    unsigned int seconds_remaining);
00090 
00091   void clear_opponents();
00092   void add_opponent(unsigned int uid, float distance, float bearing, float *covariance);
00093   void add_disappeared_opponent(unsigned int uid);
00094 
00095   void send();
00096   void recv(bool block = false, unsigned int max_num_msgs = 0);
00097 
00098   void set_loop(bool loop);
00099   void flush_sequence_numbers(unsigned int sec);
00100 
00101   void *  last_sent_plain_buffer();
00102   size_t  last_sent_plain_buffer_size();
00103   void *  last_sent_crypted_buffer();
00104   size_t  last_sent_crypted_buffer_size();
00105 
00106  private:
00107   void reset_outbound();
00108   void crypt_outbound();
00109   void append_outbound(uint16_t msg_type, void *msg, uint16_t msg_size);
00110 
00111   Socket *s;
00112   bool    loop;
00113 
00114   WorldInfoMessageEncryptor *encryptor;
00115   WorldInfoMessageDecryptor *decryptor;
00116 
00117   NetworkNameResolver       *resolver;
00118   bool                       resolver_delete;
00119 
00120   void  *in_buffer;
00121   void  *out_buffer;
00122   void  *crypted_out_buffer;
00123   void  *crypted_in_buffer;
00124   size_t crypt_buffer_size;
00125 
00126   size_t crypted_out_bytes;
00127   size_t crypted_in_bytes;
00128   char * __key;
00129   char * __iv;
00130 
00131   bool   fatmsg_enabled;
00132   void  *fatmsg_buf;
00133   size_t fatmsg_bufsize;
00134   worldinfo_header_t *fatmsg_header;
00135   worldinfo_message_header_t *fatmsg_msgheader;
00136   worldinfo_fat_message_t *fatmsg;
00137 
00138   unsigned int out_seq;
00139   unsigned int in_seq;
00140 
00141   unsigned char *outbound_buffer;
00142   unsigned int outbound_bytes;
00143   unsigned int outbound_num_msgs;
00144 
00145   unsigned char *inbound_buffer;
00146   size_t         inbound_bytes;
00147 
00148   bool   pose_changed;
00149   float  pose_x;
00150   float  pose_y;
00151   float  pose_theta;
00152   float *pose_covariance;
00153 
00154   bool   vel_changed;
00155   float  vel_x;
00156   float  vel_y;
00157   float  vel_theta;
00158   float *vel_covariance;
00159 
00160   bool   rel_ball_changed;
00161   bool   rel_ball_visible;
00162   int    rel_ball_visibility_history;
00163   float  rel_ball_dist;
00164   float  rel_ball_bearing;
00165   float  rel_ball_slope;
00166   float *rel_ball_covariance;
00167 
00168   bool   rel_ball_vel_changed;
00169   float  rel_ball_vel_x;
00170   float  rel_ball_vel_y;
00171   float  rel_ball_vel_z;
00172   float *rel_ball_vel_covariance;
00173 
00174   bool   glob_ball_changed;
00175   bool   glob_ball_visible;
00176   int    glob_ball_visibility_history;
00177   float  glob_ball_x;
00178   float  glob_ball_y;
00179   float  glob_ball_z;
00180   float *glob_ball_covariance;
00181 
00182   bool   glob_ball_vel_changed;
00183   float  glob_ball_vel_x;
00184   float  glob_ball_vel_y;
00185   float  glob_ball_vel_z;
00186   float *glob_ball_vel_covariance;
00187 
00188   bool gamestate_changed;
00189   worldinfo_gamestate_message_t gamestate_msg;
00190 
00191   typedef struct {
00192     uint32_t uid;
00193     float  distance;
00194     float  bearing;
00195     float *covariance;
00196   } opponent_t;
00197   std::list<opponent_t> opponents;
00198   std::list<opponent_t>::iterator oppit;
00199 
00200   std::list<unsigned int>  disappeared_opponents;
00201   std::list<unsigned int>::iterator  doppit;
00202 
00203   std::map<unsigned int, worldinfo_penalty_message_t> penalties;
00204   std::map<unsigned int, worldinfo_penalty_message_t>::iterator penit;
00205 
00206   LockList<WorldInfoHandler *> handlers;
00207   LockList<WorldInfoHandler *>::iterator hit;
00208 
00209   // Currently we only support IPv4
00210   std::map<uint32_t, unsigned int>       sequence_numbers;
00211   std::map<uint32_t, time_t>             last_received_time;
00212   std::map<uint32_t, time_t>::iterator   lrtit;
00213 };
00214 
00215 } // end namespace fawkes
00216 
00217 
00218 #endif

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1