Robot Devastation
YarpNetworkManager.hpp
1 // Authors: see AUTHORS.md at project root.
2 // CopyPolicy: released under the terms of the LGPLv2.1, see LICENSE at project root.
3 // URL: https://github.com/asrob-uc3m/robotDevastation
4 
5 #ifndef __RD_YARP_NETWORK_MANAGER_HPP__
6 #define __RD_YARP_NETWORK_MANAGER_HPP__
7 
8 #include <string>
9 
10 #include <yarp/os/PeriodicThread.h>
11 #include <yarp/os/PortReaderBuffer.h>
12 #include <yarp/os/Bottle.h>
13 #include <yarp/os/RpcClient.h>
14 #include <yarp/os/BufferedPort.h>
15 
16 #include "NetworkManager.hpp"
17 #include "Player.hpp"
18 
19 namespace rd{
20 
38  private yarp::os::PeriodicThread,
39  public yarp::os::TypedReaderCallback<yarp::os::Bottle>
40 {
41  public:
42  //------------------ Creation and configuration --------------------------------------------------------------//
48  static bool RegisterManager();
49 
50  virtual ~YarpNetworkManager();
51 
53  static const std::string id;
54 
55  //------------------ Manager Startup & Halting ---------------------------------------------------------------//
56  virtual bool start();
57  virtual bool stop();
58  virtual bool isStopped() const;
59 
60  //------------------------------ Configuration & Listeners ----------------------------------------------------//
62  virtual bool configure(const std::string & parameter, const Player & value);
63 
64  //------------------ Server API ----------------------------------------------------------------------------//
65  virtual bool sendPlayerHit(const Player & player, int damage);
66  virtual bool login();
67  virtual bool logout();
68  virtual bool keepAlive();
69 
70  protected:
79 
81  void onRead(yarp::os::Bottle& b);
82 
83  void run();
84 
85  private:
87  YarpNetworkManager & operator=(const YarpNetworkManager &);
88 
91 
93  static const int KEEPALIVE_RATE_MS;
94 
95 
96  yarp::os::RpcClient rpcClient;
97  yarp::os::BufferedPort<yarp::os::Bottle> callbackPort;
98 
101 
102  bool started;
103 };
104 
105 }
106 
107 #endif //-- __RD_NETWORK_MANAGER_HPP__
rd::YarpNetworkManager::configure
virtual bool configure(const std::string &parameter, const Player &value)
Configures a parameter with a value.
Definition: YarpNetworkManager.cpp:170
rd::YarpNetworkManager::keepAlive
virtual bool keepAlive()
Keeps the user connected to the server (requires to be called periodically)
Definition: YarpNetworkManager.cpp:261
rd::YarpNetworkManager::player
Player player
Player.
Definition: YarpNetworkManager.hpp:100
rd::YarpNetworkManager::onRead
void onRead(yarp::os::Bottle &b)
Yarp callback for incoming messages.
Definition: YarpNetworkManager.cpp:114
rd::YarpNetworkManager::start
virtual bool start()
Start the network manager.
Definition: YarpNetworkManager.cpp:45
rd::YarpNetworkManager::logout
virtual bool logout()
Log the user out of the network.
Definition: YarpNetworkManager.cpp:236
rd::Player
Class that represents a player of this game, with a 'unique' id, name, health, team id and score.
Definition: Player.hpp:21
rd
The main, catch-all namespace for Robot Devastation.
Definition: groups.dox:4
rd::YarpNetworkManager::RegisterManager
static bool RegisterManager()
Register this manager in the NetworkManager registry so that can be used.
Definition: YarpNetworkManager.cpp:20
rd::YarpNetworkManager::YarpNetworkManager
YarpNetworkManager()
Constructor.
Definition: YarpNetworkManager.cpp:30
rd::YarpNetworkManager::isStopped
virtual bool isStopped() const
Returns true if manager is stopped (obviously)
Definition: YarpNetworkManager.cpp:165
rd::YarpNetworkManager::login
virtual bool login()
Log the user into the network.
Definition: YarpNetworkManager.cpp:204
rd::YarpNetworkManager::stop
virtual bool stop()
Stop the network manager.
Definition: YarpNetworkManager.cpp:143
rd::YarpNetworkManager::uniqueInstance
static YarpNetworkManager * uniqueInstance
Reference to this manager (unique instance)
Definition: YarpNetworkManager.hpp:90
rd::YarpNetworkManager::id
static const std::string id
String that identifies this manager.
Definition: YarpNetworkManager.hpp:53
rd::YarpNetworkManager
Manages the communications between the user and the Server using YARP.
Definition: YarpNetworkManager.hpp:37
rd::NetworkManager
Manages the communications between the user and the RobotDevastation network.
Definition: NetworkManager.hpp:37
rd::YarpNetworkManager::KEEPALIVE_RATE_MS
static const int KEEPALIVE_RATE_MS
Period of the keep alive (called by run() )
Definition: YarpNetworkManager.hpp:93
rd::YarpNetworkManager::sendPlayerHit
virtual bool sendPlayerHit(const Player &player, int damage)
Communicate to the network that a player has been hit by the user.
Definition: YarpNetworkManager.cpp:181