Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tbaseserver.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys socket.h=""></sys>
Toshihiro Shimizu 890ddd
#include <netinet in.h=""></netinet>
Toshihiro Shimizu 890ddd
#include <netdb.h></netdb.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <unistd.h></unistd.h>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <string></string>
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TBaseServer::TBaseServer(int port)
Toshihiro Shimizu 890ddd
	: m_port(port), m_socketId(-1), m_stopped(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TBaseServer::~TBaseServer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_socketId != -1)
Toshihiro Shimizu 890ddd
		stop();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TBaseServer::start()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
	// Windows Socket startup
Toshihiro Shimizu 890ddd
	WSADATA wsaData;
Toshihiro Shimizu 890ddd
	WORD wVersionRequested = MAKEWORD(1, 1);
Toshihiro Shimizu 890ddd
	int irc = WSAStartup(wVersionRequested, &wsaData);
Toshihiro Shimizu 890ddd
	if (irc != 0)
Toshihiro Shimizu 890ddd
		throw("Windows Socket Startup failed");
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	int c;
Toshihiro Shimizu 890ddd
	FILE *fp;
Toshihiro Shimizu 890ddd
	register int ns;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((m_socketId = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
		char szError[1024];
Toshihiro Shimizu 890ddd
		wsprintf(szError, TEXT("Allocating socket failed. Error: %d"), WSAGetLastError());
Toshihiro Shimizu 890ddd
		throw(szError);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		throw("Allocating socket failed.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	struct sockaddr_in myname;
Toshihiro Shimizu 890ddd
	myname.sin_family = AF_INET;
Toshihiro Shimizu 890ddd
	myname.sin_port = htons(m_port);
Toshihiro Shimizu 890ddd
	myname.sin_addr.s_addr = INADDR_ANY;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (bind(m_socketId, (sockaddr *)&myname, sizeof myname) < 0) {
Toshihiro Shimizu 890ddd
		throw("Unable to bind to connection");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef TRACE
Toshihiro Shimizu 890ddd
	cout << "Listening ..." << endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// length of the queue of pending connections
Toshihiro Shimizu 890ddd
	int maxPendingConnections = 5;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (listen(m_socketId, maxPendingConnections) < 0) {
Toshihiro Shimizu 890ddd
		throw("Unable to start listening");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (true) {
Toshihiro Shimizu 890ddd
		register int clientSocket;
Toshihiro Shimizu 890ddd
		if ((clientSocket = accept(m_socketId, 0, 0)) < 0) {
Toshihiro Shimizu 890ddd
			if (m_stopped)
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				throw("Error accepting from client");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		printf("ho accettato\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		char msgFromClient[1024];
Toshihiro Shimizu 890ddd
		memset(msgFromClient, 0, sizeof(msgFromClient));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Receive data from the client.
Toshihiro Shimizu 890ddd
		int iReturn = recv(clientSocket, msgFromClient, sizeof(msgFromClient), 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Verify that data was received. If yes, use it.
Toshihiro Shimizu 890ddd
		if (iReturn == SOCKET_ERROR) {
Toshihiro Shimizu 890ddd
			char szError[1024];
Toshihiro Shimizu 890ddd
			wsprintf(szError, TEXT("No data is received, receive failed.")
Toshihiro Shimizu 890ddd
								  TEXT(" Error: %d"),
Toshihiro Shimizu 890ddd
					 WSAGetLastError());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			throw(szError);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		fp = fdopen(clientSocket, "r");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		printf("Partito!\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int i = 0;
Toshihiro Shimizu 890ddd
		while ((int)(c = fgetc(fp)) != EOF) {
Toshihiro Shimizu 890ddd
			putchar(c);
Toshihiro Shimizu 890ddd
			msgFromClient[i] = c;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		printf("msg finito\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		char *argv[128];
Toshihiro Shimizu 890ddd
		int argc = extractArgs(msgFromClient, argv);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		string reply = exec(argc, argv);
Toshihiro Shimizu 890ddd
		reply += "\n";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (int i = 0; i < argc; ++i)
Toshihiro Shimizu 890ddd
			delete[] argv[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Send the reply string from the server to the client.
Toshihiro Shimizu 890ddd
		if (send(clientSocket, reply.c_str(), reply.length() + 1, 0) < 0) {
Toshihiro Shimizu 890ddd
			char szError[1024];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
			wsprintf(szError,
Toshihiro Shimizu 890ddd
					 TEXT("Sending data to the client failed. Error: %d"),
Toshihiro Shimizu 890ddd
					 WSAGetLastError());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			throw(szError);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef WIN32
Toshihiro Shimizu 890ddd
		close(clientSocket);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TBaseServer::stop()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//  TThread::ScopedLock sl(m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_stopped) {
Toshihiro Shimizu 890ddd
		m_stopped = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
		if (closesocket(m_socketId) != 0) {
Toshihiro Shimizu 890ddd
			char szError[1024];
Toshihiro Shimizu 890ddd
			wsprintf(szError, TEXT(" Error: %d"), WSAGetLastError());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			throw(szError);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		close(m_socketId);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_socketId = -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TBaseServer::extractArgs(char *s, char *argv[])
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	string ss(s);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string::size_type pos1 = 0;
Toshihiro Shimizu 890ddd
	string::size_type pos2 = ss.find(",");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<string> args;</string>
Toshihiro Shimizu 890ddd
	while (pos2 != string::npos) {
Toshihiro Shimizu 890ddd
		string arg = ss.substr(pos1, pos2 - pos1);
Toshihiro Shimizu 890ddd
		args.push_back(arg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pos1 = pos2 + 1;
Toshihiro Shimizu 890ddd
		pos2 = ss.find(",", pos1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string arg = ss.substr(pos1, ss.length() - pos1);
Toshihiro Shimizu 890ddd
	args.push_back(arg);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<string>::iterator it = args.begin();</string>
Toshihiro Shimizu 890ddd
	for (int i = 0; it != args.end(); ++it, ++i) {
Toshihiro Shimizu 890ddd
		string arg = *it;
Toshihiro Shimizu 890ddd
		argv[i] = new char[arg.size() + 1];
Toshihiro Shimizu 890ddd
		strcpy(argv[i], arg.c_str());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return args.size();
Toshihiro Shimizu 890ddd
}