Skip to content

Commit

Permalink
Added "broadcast" and "listClients" functionality.
Browse files Browse the repository at this point in the history
Broadcasting commands to all clients and listing them via listClients is now possible.
  • Loading branch information
werkamsus committed Sep 15, 2017
1 parent c350ec5 commit 74bbd72
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
12 changes: 11 additions & 1 deletion Server/SendGetMethods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ bool Server::GetPacketType(int ID, PacketType & _packettype)
void Server::SendString(int ID, std::string & _string, PacketType _packettype)
{
PS::Message message(_string);
connections[ID]->pm.Append(message.toPacket(_packettype));
if (ID == -2)
{
for (int i = 0; i < connections.size(); i++)
{
connections[i]->pm.Append(message.toPacket(_packettype));
}
}
else
{
connections[ID]->pm.Append(message.toPacket(_packettype));
}
}

bool Server::GetString(int ID, std::string & _string)
Expand Down
2 changes: 1 addition & 1 deletion Server/general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ bool General::processParameter(std::string &command, std::string compCommand)
}
else
return false;
}
}
2 changes: 1 addition & 1 deletion Server/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class General
{
public: //functions
static void outputMsg(std::string message, int msgType);
static void outputMsg(std::string message, int msgType);
static bool General::processParameter(std::string &command, std::string compCommand);
public: //variables
static bool cmdMode;
Expand Down
29 changes: 23 additions & 6 deletions Server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void Server::HandleInput()
while (true)
{
std::getline(std::cin, userinput);
if (currentSessionID == -1)

if (currentSessionID == -1) //handle command while not having selected a client
{
if (General::processParameter(userinput, "connect"))
{
Expand All @@ -61,23 +62,39 @@ void Server::HandleInput()
else
{
currentSessionID = inputInt;
General::outputMsg("Connected to Session " + currentSessionID, 1);
General::outputMsg("Connected to Session " + std::to_string(currentSessionID), 1);
}
inputInt = 0;
userinput.empty();
}
else if (General::processParameter(userinput, "broadcast")) //broadcasts commands to all clients
{
General::outputMsg("Entering broadcast mode. To disable, type 'exitSession'", 1);
currentSessionID = -2;
}
else if (General::processParameter(userinput, "listClients")) //counts clients (TODO: list clients)
{
if (connections.size() <= 0)
{
General::outputMsg("No Clients connected", 2);
}
else
{
General::outputMsg("Listing all Clients, Connected: " + std::to_string(connections.size()), 1);
}
}
else
General::outputMsg("Please connect to a session with 'connect'", 2);
}
else
else //handle command when client is selected
{
if (userinput == "exitSession")
{
General::outputMsg("Exited Session " + currentSessionID, 1);
General::outputMsg("Exited Session " + std::to_string(currentSessionID), 1);
currentSessionID = -1;
}

if (General::processParameter(userinput, "switchSession"))
else if (General::processParameter(userinput, "switchSession"))
{
inputInt = atoi(userinput.c_str());
int tempInt = connections.size() - 1;
Expand All @@ -86,7 +103,7 @@ void Server::HandleInput()
else
{
currentSessionID = inputInt;
General::outputMsg("Switched to Session " + currentSessionID, 1);
General::outputMsg("Switched to Session " + std::to_string(currentSessionID), 1);
}
inputInt = 0;
userinput.empty();
Expand Down

0 comments on commit 74bbd72

Please sign in to comment.