-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BIG Serialization Update #102
Changes from all commits
ca8b330
746e171
0ee4b38
b17dfd5
86d5da9
29ef862
4ce1c0f
e971adc
1ba682b
04b11ed
d0b95b1
14ed8d4
e2edb6c
1fb1ebf
7ccc59e
daceac4
6a66d12
18851a1
448771e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to update this, looks like it's the same as ClientInterface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this and client interface both match NetworkingInterface. It's worth taking a pass to clean these up. I know keeping up with them is a pain, but it really does help orient folks who are new to the code. |
||
* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm being nitpicky here, but it would be nice if ID was consistent between this and GetActionID. |
||
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 |
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like these aren't used elsewhere. I'm assuming they come into play in multiplayer? If so that's totally fine. |
||
|
||
/** | ||
* 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is super minor, but as we move to the final deadline, feel free to cut lines like this instead of commenting. Old versions are always available on GitHub anyway!
(I don't think we'd take points off for something like this, unless it's particularly egregious.)