-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from ZaneMODell/serial-experiments-lain
BIG Serialization Update
- Loading branch information
Showing
18 changed files
with
848 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* This file is part of the Fall 2023, CSE 491 course project. | ||
* @brief A networking interface that allows information to be sent across a network | ||
* @note Status: PROTOTYPE | ||
**/ | ||
|
||
#pragma once | ||
#include <map> | ||
#include <sstream> | ||
#include <vector> | ||
#include "Interfaces/NetWorth/NetworkInterface.hpp" | ||
|
||
namespace netWorth{ | ||
using namespace sf; | ||
|
||
/** | ||
* The server that will be running and that allows clients to connect to | ||
*/ | ||
class ClientManager { | ||
private: | ||
sf::UdpSocket *m_socket; /// Socket shared with ClientInterface | ||
std::optional<sf::IpAddress> m_ip; /// Server IP address | ||
unsigned short m_port; /// Server port | ||
std::unordered_map<size_t, size_t> m_action_map; ///Map of agent IDs to most recent action selected | ||
|
||
protected: | ||
|
||
public: | ||
/** | ||
* Default constructor (AgentBase) | ||
* @param id agent ID | ||
* @param name agent name | ||
*/ | ||
ClientManager()= default; | ||
|
||
/** | ||
* Turn packet from server into action map for ControlledAgents | ||
* @param pkt received packet | ||
*/ | ||
void PacketToActionMap(sf::Packet pkt) { | ||
size_t data_size, agent_id, action_id; | ||
pkt >> data_size; | ||
for (size_t i = 0; i < data_size; i++) { | ||
pkt >> agent_id >> action_id; | ||
m_action_map[agent_id] = action_id; | ||
} | ||
} | ||
|
||
/** | ||
* Set receiving socket for action map and IP/port info | ||
* @param socket pointer to ClientInterface's socket | ||
* @param ip server IP | ||
* @param port server port | ||
*/ | ||
void SetupSocket(sf::UdpSocket *socket, std::optional<sf::IpAddress> ip, unsigned short port) { | ||
m_socket = socket; | ||
m_ip = ip; | ||
m_port = port; | ||
} | ||
|
||
/** | ||
* Wait until server sends action map | ||
*/ | ||
void RequestActionMap() { | ||
sf::Packet recv_pkt; | ||
if (m_socket->receive(recv_pkt, m_ip, m_port) != sf::Socket::Status::Done) { | ||
std::cerr << "Failed to receive" << std::endl; | ||
return; | ||
} | ||
PacketToActionMap(recv_pkt); | ||
} | ||
|
||
/** | ||
* Check if Agent ID is present in agent action map | ||
* @param id Agent ID | ||
* @return true if ID is present | ||
*/ | ||
bool IdPresent(size_t id) { | ||
return m_action_map.find(id) == m_action_map.end(); | ||
} | ||
|
||
/** | ||
* Return action ID correspoding to agent ID | ||
* @param id Agent ID | ||
* @return action ID | ||
*/ | ||
size_t GetActionID(size_t id) { | ||
return m_action_map[id]; | ||
} | ||
|
||
/** | ||
* Clear action map after ClientInterface moves | ||
*/ | ||
void ClearActionMap() { | ||
m_action_map.clear(); | ||
} | ||
|
||
}; // End of class ClientManager | ||
} // End of namespace netWorth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* This file is part of the Fall 2023, CSE 491 course project. | ||
* @brief An Agent that will walk back and forth along a row or column. | ||
* @note Status: PROPOSAL | ||
**/ | ||
|
||
#pragma once | ||
|
||
#include <cassert> | ||
|
||
#include "../../../core/AgentBase.hpp" | ||
#include "ClientManager.hpp" | ||
|
||
namespace netWorth { | ||
|
||
class ControlledAgent : public cse491::AgentBase { | ||
private: | ||
ClientManager *m_manager = nullptr; /// Client manager to access agent action map | ||
|
||
protected: | ||
|
||
public: | ||
ControlledAgent(size_t id, const std::string & name) : AgentBase(id, name) { } | ||
~ControlledAgent() = default; | ||
|
||
/// @brief This agent needs a specific set of actions to function. | ||
/// @return Success. | ||
bool Initialize() override { | ||
m_manager = GetProperty<ClientManager *>("manager"); | ||
return HasAction("up") && HasAction("down") && HasAction("left") && HasAction("right"); | ||
} | ||
|
||
/// Choose the action to take a step in the appropriate direction. | ||
size_t SelectAction(const cse491::WorldGrid & /* grid*/, | ||
const cse491::type_options_t & /* type_options*/, | ||
const cse491::item_map_t & /* item_map*/, | ||
const cse491::agent_map_t & /* agent_map*/) override | ||
{ | ||
if (m_manager->IdPresent(id)) { | ||
// wait for server to complete agent movements | ||
m_manager->RequestActionMap(); | ||
} | ||
return m_manager->GetActionID(id); | ||
} | ||
|
||
}; | ||
|
||
} // End of namespace cse491 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* This file is part of the Fall 2023, CSE 491 course project. | ||
* @brief A networking interface that allows information to be sent across a network | ||
* @note Status: PROTOTYPE | ||
**/ | ||
|
||
#pragma once | ||
#include <map> | ||
#include <sstream> | ||
#include <vector> | ||
#include "SFML/Network/Packet.hpp" | ||
|
||
namespace netWorth{ | ||
/** | ||
* The server that will be running and that allows clients to connect to | ||
*/ | ||
class ServerManager { | ||
private: | ||
std::unordered_map<size_t, size_t> m_action_map; ///Map of agent IDs to most recent action selected | ||
|
||
protected: | ||
|
||
public: | ||
const static constexpr unsigned short m_initConnectionPort = 55000; ///Port for initial client connection | ||
|
||
unsigned short m_maxClientPort = 55000; ///Port that is incremented for client thread handoff | ||
|
||
/** | ||
* Default constructor (AgentBase) | ||
* @param id agent ID | ||
* @param name agent name | ||
*/ | ||
ServerManager() = default; | ||
|
||
/** | ||
* Report action from agent to manager | ||
* @param entity_id ID of reorting entity | ||
* @param action_id action ID | ||
*/ | ||
void TellAction(size_t entity_id, size_t action_id) { | ||
m_action_map[entity_id] = action_id; | ||
} | ||
|
||
/** | ||
* Convert action map to packet to send to client | ||
* @return packet containing action map as series of integers | ||
*/ | ||
sf::Packet ActionMapToPacket() { | ||
sf::Packet pkt; | ||
pkt << m_action_map.size(); | ||
for (auto pair : m_action_map) { | ||
pkt << pair.first << pair.second; | ||
} | ||
//std::cout << m_action_map.size(); | ||
return pkt; | ||
} | ||
|
||
}; // End of class ServerManager | ||
} // End of namespace netWorth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.