zoo_driver.h

Go to the documentation of this file.
00001 #ifndef _STAGE_ZOO_DRIVER_H
00002 #define _STAGE_ZOO_DRIVER_H
00003 
00004 #include "p_driver.h"
00005 #include <limits.h>
00006 #include <vector>
00007 #include <map>
00008 #include <sys/types.h>
00009 #include <stdio.h>
00010 
00011 #define ZOO_SCORE_BUFFER_SIZE   256
00012 #define ZOO_SCORE_PROPERTY_NAME "zoo_score"
00013 #define ZOO_SCORE_LABEL "score from Zoo"
00014 
00015 /* some forward declarations here */
00016 class ZooSpecies;
00017 class ZooController;
00018 class ZooDriver;
00019 class ZooReferee;
00020 
00021 /* referee stuff */
00022 typedef int (*zooref_init_t)(ConfigFile *, int, ZooDriver *);
00023 typedef int (*zooref_score_draw_t)(stg_model_t *, const void *sdata, size_t,
00024                                    void *, int mindex);
00025 
00026 /* for registering the driver */
00027 void ZooDriver_Register(DriverTable *);
00028 
00029 /* general stuff */
00030 int zoo_err(const char *, int ...);
00031 
00032 /* maps */
00033 typedef struct {
00034         const char *model_name;
00035         int port;
00036         ZooController *controller;
00037 } rmap_t;
00038 
00039 class ZooDriver : public Driver
00040 {
00041 public:
00042         ZooDriver(ConfigFile *cf, int section);
00043         ~ZooDriver();
00044 
00045         ZooSpecies *GetSpeciesByName(const char *);
00046 
00047         /* Player calls these */
00048         int Setup(void);
00049         int Shutdown(void);
00050         void Prepare(void);
00051 
00052         /* Functions for starting and stopping controllers */
00053         void Run(int);
00054         void Run(const char *);   // accepts a model name
00055         void RunAll(void);
00056         void Kill(int);
00057         void Kill(const char *);  // accepts a model name
00058         void KillAll(void);
00059 
00060         /* Model access functions.  Provide a variety of ways of getting a
00061          * variety of representations of a robot: a stg_model_t object, a string
00062          * holding the name, the index in a list kept by Zoo, or port. */
00063         int GetModelCount(void);
00064         const char *GetModelNameByIndex(int);
00065         const char *GetModelNameByPort(int);
00066         stg_model_t *GetModelByName(const char *);
00067         stg_model_t *GetModelByIndex(int);
00068         stg_model_t *GetModelByPort(int);
00069         int GetModelPortByName(const char *);
00070 
00071         /* Functions for storing and retrieving arbitrary score data */
00072         int GetScore(const char *, void *);
00073         int GetScoreSize(const char *);
00074         int SetScore(const char *, void *, size_t);
00075         int ClearScore(const char *);
00076         void SetScoreDrawCB(zooref_score_draw_t, void *userdata);
00077                 // for every species
00078 
00079         rmap_t *FindRobot(const char *modelName);
00080         rmap_t *FindRobot(int port);
00081         rmap_t *FindRobot(ZooController *);
00082 
00083 private:
00084         int species_count;
00085         ZooSpecies **species;
00086 
00087         int robot_count; /* could be model_count or port_count */
00088         rmap_t *robotMap; // port --> ZooController
00089         std::vector<const char *> modelList;
00090 
00091         void *zooref_handle; // for dynamically linked ref
00092         ZooReferee *referee;
00093 };
00094 
00095 class ZooSpecies
00096 {
00097 public:
00098         ZooSpecies(void);
00099         ZooSpecies(ConfigFile *cf, int section, ZooDriver *);
00100         ~ZooSpecies();
00101         ZooController *Run(int);
00102         void RunAll(void);
00103 #if 0
00104         void Kill(int);
00105 #endif
00106         void KillAll(void);
00107         ZooController *SelectController(void);
00108         bool Hosts(int);
00109         void print(void);
00110 
00111         void SetScoreDrawCB(zooref_score_draw_t, void *userdata);
00112         zooref_score_draw_t score_draw_cb;
00113         void *score_draw_user_data;
00114 
00115         const char *name;
00116 private:
00117         int population_size;
00118         int *port_list;
00119         char **model_list;
00120         std::vector<ZooController> controller;
00121 
00122         /* used by SelectController */
00123         int next_controller;
00124         int controller_instance; // for frequency > 1
00125 
00126         ZooDriver *zoo;
00127 };
00128 
00129 class ZooController
00130 {
00131 public:
00132         ZooController(ConfigFile *cf, int section, ZooSpecies *sp);
00133         ~ZooController();
00134 
00135         void Run(int port);
00136         void Kill(void);
00137         inline int GetFrequency(void) { return frequency; }
00138         inline const char *GetCommand(void) { return command; }
00139 
00140 #if 0
00141         /* A controller can be a member of more than one species, and each
00142          * species may have its own way of keeping score, so each controller
00143          * should maintain a map from species names to scores */
00144         std::map< const char *, void * > scoreMap;
00145         std::map< const char *, size_t > scoreSizeMap;
00146 #endif
00147         void *score_data;
00148         int score_size;
00149 
00150         static const char *path;
00151         ZooSpecies *species;
00152 private:
00153         pid_t pid;
00154         int frequency;
00155         const char *command;
00156 };
00157 
00158 class ZooReferee
00159 {
00160 public:
00161         ZooReferee(ConfigFile *, int, ZooDriver *);
00162         void Startup(void);
00163 private:
00164         ZooDriver *zoo;
00165 };
00166 
00167 #endif

Generated on Thu Aug 11 13:08:10 2005 for Stage by  doxygen 1.4.0