Main.c 1.38 KB
#include <stdlib.h>
#include <stdio.h> /* defines FILENAME_MAX */
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "CSlim/Slim.h"
#include "Com/SocketServer.h"
#include "CSlim/SlimConnectionHandler.h"
#include "Com/TcpComLink.h"

Slim * slim;

int connection_handler(int socket)
{
	int result = 0;
	TcpComLink * comLink = TcpComLink_Create(socket);

  result = Slim_HandleConnection(slim, (void*)comLink, &TcpComLink_send, &TcpComLink_recv);

	TcpComLink_Destroy(comLink);

	return result;
}

int main(int ac, char** av)
{
	// Put env
	char* lExecutablePath = "build/Debug/bin/";
	if ( !access("Release.flag", R_OK)) {
		lExecutablePath = "build/Release/bin/";
		setenv("BUILD_TYPE","Release",1);
	} else {
		setenv( "BUILD_TYPE","Debug",1);
	}
	const char* path = getenv("PATH");
	char _currentPath[FILENAME_MAX];
	getcwd(_currentPath,sizeof(_currentPath));
	printf("%s:%s\n%s",lExecutablePath,path,_currentPath);
	char* lNewPath = (char*) malloc(strlen(_currentPath)+strlen(path)+strlen(lExecutablePath)+3);
	sprintf(lNewPath,"%s/%s:%s",_currentPath,lExecutablePath,path);
	setenv( "PATH",lNewPath,1);

	// Server
	slim = Slim_Create();
	SocketServer* server = SocketServer_Create();
	SocketServer_register_handler(server, &connection_handler);

	int result = SocketServer_Run(server, av[1]);

	SocketServer_Destroy(server);
  Slim_Destroy(slim);
	return result;
}