<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
Authors: 
<NAME> <<EMAIL>>

Maintainers: 
Currently maintained by the authors.

This file is part of the MONICA model. 
Copyright (C) Leibniz Centre for Agricultural Landscape Research (ZALF)
*/

#include <cstdlib>

#include "zhelpers.hpp"
#include "tools/debug.h"
#include "monica-zmq-defaults.h"

using namespace Tools;
using namespace std;
using namespace Monica;

string appName = "monica-zmq-proxy";
string version = "0.0.1";

int main (int argc, 
					char** argv)
{
	int frontendPort = defaultProxyFrontendPort;
	int backendPort = defaultProxyBackendPort;
	bool startControlNode = false;
	int controlPort = defaultControlPort;
	
	auto printHelp = [=]()
	{
		cout
			<< appName << " " << endl
			<< " [[-f | --frontend-port] FRONTEND-PORT (default: " << frontendPort << ")] ... run " << appName << " with given frontend port" << endl
			<< " [[-b | --backend-port] BACKEND-PORT (default: " << backendPort << ")] ... run " << appName << " with given backend port" << endl
			<< " [-c | --start-control-node] ... start control node, connected to proxy" << endl
			<< " [[-cp | --control-port] CONTROL-NODE-PORT (default: " << controlPort << ")] ... run control node at given port" << endl
			<< " [-h | --debug] ... enable debug outputs" << endl
			<< " [-h | --help] ... this help output" << endl
			<< " [-v | --version] ... outputs " << appName << " version" << endl;
	};

	for(auto i = 1; i < argc; i++)
	{
		string arg = argv[i];
		if((arg == "-f" || arg == "--frontend-port")
			 && i + 1 < argc)
			frontendPort = stoi(argv[++i]);
		else if((arg == "-b" || arg == "--backend-port")
						&& i + 1 < argc)
			backendPort = stoi(argv[++i]);
		else if(arg == "-c" || arg == "--start-control-node")
			startControlNode = true;
		else if((arg == "-cp" || arg == "--control-port")
						&& i + 1 < argc)
			controlPort = stoi(argv[++i]);
		else if(arg == "-d" || arg == "--debug")
			activateDebug = true;
		else if(arg == "-h" || arg == "--help")
			printHelp(), exit(0);
		else if(arg == "-v" || arg == "--version")
			cout << appName << " version " << version << endl, exit(0);
	}
	
	zmq::context_t context(1);

	// setup the proxy 
	// socket facing clients
	zmq::socket_t frontend (context, ZMQ_ROUTER);
	string feAddress = string("tcp://*:") + to_string(frontendPort);
	try
	{
		frontend.bind(feAddress);
	}
	catch(zmq::error_t e)
	{
		cerr << "Couldn't bind frontend socket to address: " << feAddress << "! Error: [" << e.what() << "]" << endl;
		exit(1);
	}
	debug() << "Bound " << appName << " zeromq router socket to frontend address: " << feAddress << "!" << endl;

	// socket facing services
	zmq::socket_t backend (context, ZMQ_DEALER);
	string beAddress = string("tcp://*:") + to_string(backendPort);
	try
	{
		backend.bind(beAddress);
	}
	catch(zmq::error_t e)
	{
		cerr << "Couldn't bind backend socket to address: " << beAddress << "! Error: [" << e.what() << "]" << endl;
		exit(1);
	}
	debug() << "Bound " << appName << " zeromq dealer socket to backend address: " << beAddress << "!" << endl;

	if(startControlNode)
	{
		ostringstream oss;
#ifdef WIN32
		oss << "start /b monica-zmq-control -f " << frontendPort << " -b " << backendPort << " -c " << controlPort;
#else
		oss << "monica-zmq-control -f " << frontendPort << " -b " << backendPort << " -c " << controlNodePort << " &";
#endif

		int res = system(oss.str().c_str());
		debug() << "result of running '" << oss.str() << "': " << res << endl;
	}
	
	// start the proxy
	try
	{
		zmq::proxy((void*)frontend, (void*)backend, nullptr);
	}
	catch(zmq::error_t e)
	{
		cerr << "Couldn't start proxy! Error: [" << e.what() << "]" << endl;
		exit(1);
	}

	return 0;
}<commit_msg>forgot to update var name in ifdef'ed code<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
Authors: 
<NAME> <<EMAIL>>

Maintainers: 
Currently maintained by the authors.

This file is part of the MONICA model. 
Copyright (C) Leibniz Centre for Agricultural Landscape Research (ZALF)
*/

#include <cstdlib>

#include "zhelpers.hpp"
#include "tools/debug.h"
#include "monica-zmq-defaults.h"

using namespace Tools;
using namespace std;
using namespace Monica;

string appName = "monica-zmq-proxy";
string version = "0.0.1";

int main (int argc, 
					char** argv)
{
	int frontendPort = defaultProxyFrontendPort;
	int backendPort = defaultProxyBackendPort;
	bool startControlNode = false;
	int controlPort = defaultControlPort;
	
	auto printHelp = [=]()
	{
		cout
			<< appName << " " << endl
			<< " [[-f | --frontend-port] FRONTEND-PORT (default: " << frontendPort << ")] ... run " << appName << " with given frontend port" << endl
			<< " [[-b | --backend-port] BACKEND-PORT (default: " << backendPort << ")] ... run " << appName << " with given backend port" << endl
			<< " [-c | --start-control-node] ... start control node, connected to proxy" << endl
			<< " [[-cp | --control-port] CONTROL-NODE-PORT (default: " << controlPort << ")] ... run control node at given port" << endl
			<< " [-h | --debug] ... enable debug outputs" << endl
			<< " [-h | --help] ... this help output" << endl
			<< " [-v | --version] ... outputs " << appName << " version" << endl;
	};

	for(auto i = 1; i < argc; i++)
	{
		string arg = argv[i];
		if((arg == "-f" || arg == "--frontend-port")
			 && i + 1 < argc)
			frontendPort = stoi(argv[++i]);
		else if((arg == "-b" || arg == "--backend-port")
						&& i + 1 < argc)
			backendPort = stoi(argv[++i]);
		else if(arg == "-c" || arg == "--start-control-node")
			startControlNode = true;
		else if((arg == "-cp" || arg == "--control-port")
						&& i + 1 < argc)
			controlPort = stoi(argv[++i]);
		else if(arg == "-d" || arg == "--debug")
			activateDebug = true;
		else if(arg == "-h" || arg == "--help")
			printHelp(), exit(0);
		else if(arg == "-v" || arg == "--version")
			cout << appName << " version " << version << endl, exit(0);
	}
	
	zmq::context_t context(1);

	// setup the proxy 
	// socket facing clients
	zmq::socket_t frontend (context, ZMQ_ROUTER);
	string feAddress = string("tcp://*:") + to_string(frontendPort);
	try
	{
		frontend.bind(feAddress);
	}
	catch(zmq::error_t e)
	{
		cerr << "Couldn't bind frontend socket to address: " << feAddress << "! Error: [" << e.what() << "]" << endl;
		exit(1);
	}
	debug() << "Bound " << appName << " zeromq router socket to frontend address: " << feAddress << "!" << endl;

	// socket facing services
	zmq::socket_t backend (context, ZMQ_DEALER);
	string beAddress = string("tcp://*:") + to_string(backendPort);
	try
	{
		backend.bind(beAddress);
	}
	catch(zmq::error_t e)
	{
		cerr << "Couldn't bind backend socket to address: " << beAddress << "! Error: [" << e.what() << "]" << endl;
		exit(1);
	}
	debug() << "Bound " << appName << " zeromq dealer socket to backend address: " << beAddress << "!" << endl;

	if(startControlNode)
	{
		ostringstream oss;
#ifdef WIN32
		oss << "start /b monica-zmq-control -f " << frontendPort << " -b " << backendPort << " -c " << controlPort;
#else
		oss << "monica-zmq-control -f " << frontendPort << " -b " << backendPort << " -c " << controlPort << " &";
#endif

		int res = system(oss.str().c_str());
		debug() << "result of running '" << oss.str() << "': " << res << endl;
	}
	
	// start the proxy
	try
	{
		zmq::proxy((void*)frontend, (void*)backend, nullptr);
	}
	catch(zmq::error_t e)
	{
		cerr << "Couldn't start proxy! Error: [" << e.what() << "]" << endl;
		exit(1);
	}

	return 0;
}
{'fraction_non_alphanumeric': 0.20298238848181, 'fraction_numerical': 0.005142049106568968, 'mean_word_length': 3.2190889370932756, 'pattern_counts': {'":': 0, '<': 335, '<?xml version=': 0, '>': 11, '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': '7200720', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2705, 'n_words': 757}