<commit_before>﻿#include "TraCIServer.h"
#include "programmer.h"
#include <thread>
#include "shawn/storage.h"
#include <shellapi.h>

bool starts_with(std::string const& in_string,
				 std::string const& prefix)
{
	return prefix.length() <= in_string.length() &&
		std::equal(prefix.begin(), prefix.end(), in_string.begin());
}


TraCIServer::TraCIServer()
{
	running = false;

	//try to get port from command line arguments
	int argc;
	LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
	std::string prefix(CMDARG_PORT);

	port = DEFAULT_PORT; // if it fails, use the default port
	for (int i = 0; i < argc; i++)
	{
		// convert from widestring to normal string
		std::wstring temp(argv[i]);
		std::string str(temp.begin(), temp.end());

		// check if argument prefix matches
		if (starts_with(str, prefix))
		{
			std::string s_port = str.substr(prefix.length(), str.npos);
			port = std::stoi(s_port);
		}
	}

	ssocket = new tcpip::Socket(port);
	outgoing = new tcpip::Storage();
}


TraCIServer::~TraCIServer()
{
	delete(ssocket);
	delete(outgoing);

	if (DEBUG)
		this->p_printf("Server succesfully shut down");
}

/**
 * \brief Starts this instance, binding it to a port and awaiting connections. This method is blocking, and as such should be run in a separate thread.
 */
void TraCIServer::run()
{
	running = true;
	std::string version_str = "Paramics TraCI plugin v" + std::string(PLUGIN_VERSION) + " on Paramics v" + std::to_string(qpg_UTL_parentProductVersion());
	this->p_printf(version_str);
	this->p_printf("Simulation start time: " + std::to_string(qpg_CFG_simulationTime()));
	this->p_printf("Awaiting connections on port " + std::to_string(port));
	ssocket->accept();
	this->p_printf("Accepted connection");
	this->waitForCommands();<commit_msg>Added error managing for invalid port strings in cmd line arguments.<commit_after>﻿#include "TraCIServer.h"
#include "programmer.h"
#include <thread>
#include "shawn/storage.h"
#include <shellapi.h>

bool starts_with(std::string const& in_string,
				 std::string const& prefix)
{
	return prefix.length() <= in_string.length() &&
		std::equal(prefix.begin(), prefix.end(), in_string.begin());
}


TraCIServer::TraCIServer()
{
	running = false;

	//try to get port from command line arguments
	int argc;
	LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
	std::string prefix(CMDARG_PORT);

	port = DEFAULT_PORT; // if it fails, use the default port
	for (int i = 0; i < argc; i++)
	{
		// convert from widestring to normal string
		std::wstring temp(argv[i]);
		std::string str(temp.begin(), temp.end());

		// check if argument prefix matches
		if (starts_with(str, prefix))
		{
			std::string s_port = str.substr(prefix.length(), str.npos);
			try
			{
				port = std::stoi(s_port);
			} catch(...)
			{
				this->p_printf("Invalid port identifier - Falling back to default port");
				port = DEFAULT_PORT;
			}
			
		}
	}

	ssocket = new tcpip::Socket(port);
	outgoing = new tcpip::Storage();
}


TraCIServer::~TraCIServer()
{
	delete(ssocket);
	delete(outgoing);

	if (DEBUG)
		this->p_printf("Server succesfully shut down");
}

/**
 * \brief Starts this instance, binding it to a port and awaiting connections. This method is blocking, and as such should be run in a separate thread.
 */
void TraCIServer::run()
{
	running = true;
	std::string version_str = "Paramics TraCI plugin v" + std::string(PLUGIN_VERSION) + " on Paramics v" + std::to_string(qpg_UTL_parentProductVersion());
	this->p_printf(version_str);
	this->p_printf("Simulation start time: " + std::to_string(qpg_CFG_simulationTime()));
	this->p_printf("Awaiting connections on port " + std::to_string(port));
	ssocket->accept();
	this->p_printf("Accepted connection");
	this->waitForCommands();
{'fraction_non_alphanumeric': 0.1500667556742323, 'fraction_numerical': 0.000534045393858478, 'mean_word_length': 4.825816485225506, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3699679', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1270, 'n_words': 327}