<commit_before>/************************************************************************
  This file is part of Freekick.

  Freekick is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Freekick is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Freekick.  If not, see <http://www.gnu.org/licenses/>.

  Copyright <NAME>, 2008
**************************************************************************/

#include "ServerManager.h"

namespace freekick
{
    namespace match
    {
        namespace server
        {
            ServerManager::ServerManager(unsigned int port, 
                                         boost::shared_ptr<freekick::match::MatchStatus> ms,
                                         const std::string& servername,
                                         const std::string& greeting)
                : mPort(port), 
                  clients(new ClientList()),
                  mMatchStatus(ms),
                  im(new InputMonitor(ms)),
                  p(new Physics(ms, im)),
                  r(new Rules(ms, p)),
                  d(new Dispatcher(clients, this, p, r, ms)),
                  cel(new ClientEventListener(clients, im, d)),
                  console(new Console(ms)),
                  name(servername),
                  greet(greeting),
                  protocol_version("0.2")
            {
            }

            ServerManager::~ServerManager()
            {
                clients->clear();
                std::cerr << "Stopping listening.\n";
                stopListening();
            }

            bool ServerManager::run()
            {
                boost::thread console_thread(boost::bind(&freekick::match::server::Console::run, console));
                std::cerr << "Starting physics\n";
                boost::thread physics_thread(boost::bind(&freekick::match::server::Physics::run, p));
                std::cerr << "Starting listening on port " << mPort << std::endl;
                boost::thread listen_thread(boost::bind(&freekick::match::server::ServerManager::startListening, this, mPort));
                console_thread.join();
                // listen_thread.interrupt();
                return true;
            }

            void ServerManager::client_connected(client_id id)
            {
                std::cerr << "Client " << id << " connected.\n";
                messages::ServerInitMessage sim(name, protocol_version, greet);
                try
                {
                    write(sim.toString(), id);
                }
                catch(...)
                {
                    std::cerr << "Error while writing to client " << id << "; disconnecting.\n";
                    disconnect(id);
                }
            }

            void ServerManager::client_disconnected(client_id id)
            {
                ClientList::iterator it;
                it = clients->find(id);
                if (it != clients->end())<commit_msg>server: added creation of match result file after finishing match<commit_after>/************************************************************************
  This file is part of Freekick.

  Freekick is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Freekick is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with Freekick.  If not, see <http://www.gnu.org/licenses/>.

  Copyright <NAME>, 2008
**************************************************************************/

#include "ServerManager.h"

namespace freekick
{
    namespace match
    {
        namespace server
        {
            ServerManager::ServerManager(unsigned int port, 
                                         boost::shared_ptr<freekick::match::MatchStatus> ms,
                                         const std::string& servername,
                                         const std::string& greeting)
                : mPort(port), 
                  clients(new ClientList()),
                  mMatchStatus(ms),
                  im(new InputMonitor(ms)),
                  p(new Physics(ms, im)),
                  r(new Rules(ms, p)),
                  d(new Dispatcher(clients, this, p, r, ms)),
                  cel(new ClientEventListener(clients, im, d)),
                  console(new Console(ms)),
                  name(servername),
                  greet(greeting),
                  protocol_version("0.2")
            {
            }

            ServerManager::~ServerManager()
            {
                clients->clear();
                std::cerr << "Stopping listening.\n";
                stopListening();
            }

            bool ServerManager::run()
            {
                boost::thread console_thread(boost::bind(&freekick::match::server::Console::run, console));
                std::cerr << "Starting physics\n";
                boost::thread physics_thread(boost::bind(&freekick::match::server::Physics::run, p));
                std::cerr << "Starting listening on port " << mPort << std::endl;
                boost::thread listen_thread(boost::bind(&freekick::match::server::ServerManager::startListening, this, mPort));
                physics_thread.join();
                // console_thread.join();
                // listen_thread.interrupt();
                stopListening();
                mMatchStatus->createMatchResultFile();
                return true;
            }

            void ServerManager::client_connected(client_id id)
            {
                std::cerr << "Client " << id << " connected.\n";
                messages::ServerInitMessage sim(name, protocol_version, greet);
                try
                {
                    write(sim.toString(), id);
                }
                catch(...)
                {
                    std::cerr << "Error while writing to client " << id << "; disconnecting.\n";
                    disconnect(id);
                }
            }

            void ServerManager::client_disconnected(client_id id)
            {
                ClientList::iterator it;
                it = clients->find(id);
                if (it != clients->end())
{'fraction_non_alphanumeric': 0.1371794871794872, 'fraction_numerical': 0.0019943019943019944, 'mean_word_length': 1.6921012269938651, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5990755', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1547, 'n_words': 453}