Example #1
0
int main()
{
    int server_fifo_fd;
    int client_fifo_fd;

    int res;
    char client_fifo_name[NAME_SIZE];

    //message msg;
    CourseInfo CI;

    char *p;
    //BUG:: Using the following ,when server_fifo exist, it will keep waiting when use CheckFile ,may be it is in using ,and was blocked .
    //FIXME:: Don't CheckFile just DeleteFile
//    if(UTIL_CheckFile(SERVER_FIFO_NAME))
    {
        UTIL_DeleteFile(SERVER_FIFO_NAME);
        cout <<"Delete "<< SERVER_FIFO_NAME << endl;

    }

    if (mkfifo(SERVER_FIFO_NAME, 0777) == -1)
    {
        fprintf(stderr, "Sorry, create server fifo failure!\n");
        exit(EXIT_FAILURE);
    }

    server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);
    if (server_fifo_fd == -1)
    {
        fprintf(stderr, "Sorry, server fifo open failure!\n");
        exit(EXIT_FAILURE);
    }

//    sleep(5);

//   while ((res = read(server_fifo_fd, &CI, sizeof(CI))) > 0)
    int count =0;

    while (1)
    {
        if((res = read(server_fifo_fd, &CI, sizeof(CI))) > 0)
        {
            p = CI.Path;
            while (*p)
            {
                *p = toupper(*p);
                ++p;
            }

            //CI.str="YYY";

            sprintf(client_fifo_name, CLIENT_FIFO_NAME, CI.client_pid);
            client_fifo_fd = open(client_fifo_name, O_WRONLY);
            if (client_fifo_fd == -1)
            {
                fprintf(stderr, "Sorry, client fifo open failure!\n");
                exit(EXIT_FAILURE);
            }

            //        write(client_fifo_fd, &CI, sizeof(CI));
            ResultInfo RI;
            RI.client_pid = CI.client_pid;
            //RI.result = SUCCEED;
            RI.result = FAIL;

            int grade = CI.Grade;
            string strHash ;
            strHash.assign(CI.Hash, strlen(CI.Hash));
            switch(grade)
            {
            case 1:
            {
                Vhash1.push_back(strHash);
                cout << "Vhash1 size " << Vhash1.size() << endl;
            }
            break;

            case 2:
            {
                Vhash2.push_back(strHash);
                cout << "Vhash2 size " << Vhash2.size() << endl;
            }
            break;

            case 3:
            {
                Vhash3.push_back(strHash);
                cout << "Vhash3 size " << Vhash3.size() << endl;
            }
            break;
            default:
                break;
            }
            cout << count++ << endl;

            write(client_fifo_fd, &RI, sizeof(RI));
            close(client_fifo_fd);
        }
        else//keep try open
        {
            //sleep(1);
        }
    }

    close(server_fifo_fd);
    unlink(SERVER_FIFO_NAME);
    exit(EXIT_SUCCESS);
}
Example #2
0
int main()
{
    int server_fifo_fd;
    int client_fifo_fd;

    int res;
    char client_fifo_name[NAME_SIZE];

	MessageClient MC1;
	memset(&MC1, 0 , sizeof(MC1));

//    if(UTIL_CheckFile(SERVER_FIFO_NAME))
	{
		UTIL_DeleteFile(SERVER_FIFO_NAME);
		cout <<"Delete "<< SERVER_FIFO_NAME << endl;
	
	}

    if (mkfifo(SERVER_FIFO_NAME, 0777) == -1)
    {
        fprintf(stderr, "Sorry, create server fifo failure!\n");
        exit(EXIT_FAILURE);
    }

    server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY);
    if (server_fifo_fd == -1)
    {
        fprintf(stderr, "Sorry, server fifo open failure!\n");
        exit(EXIT_FAILURE);
    }

//    sleep(5);

//   while ((res = read(server_fifo_fd, &CI, sizeof(CI))) > 0)
   int count =0;
   
   while (1)
   {
	//First read Operation Type
    	if((res = read(server_fifo_fd, &MC1, sizeof(MC1))) > 0)
	{
		
		//CI.str="YYY";

		ResultInfo RI;
		RI.result = SUCCEED;

		RI.MC.client_pid = MC1.client_pid;
		CourseInfo CI;
		LimitInfo LI;
		int OT = MC1.OT;

		switch(OT)
		{
			case Book:
				{
					if((res = read( server_fifo_fd, &CI, sizeof(CI))) > 0)
					{
						//sleep(4);//stand for do some post and get and annylyse

						//do the book thing
						string strHash ;
						strHash.assign(CI.Hash, strlen(CI.Hash));
						Vhash1.push_back(strHash);

						//sleep(4);//stand for do some post and get and analyse

						cout << "Got a book  :" << CI.Hash <<" " << MC1.client_pid << endl;
					}
				}
				break;
			
			case LimitSpeed:
				{

					if((res = read(server_fifo_fd, &LI, sizeof(LI))) > 0)
					{
						//do the Limit thing
						//sleep(4);//stand for do some post and get and annylyse

						cout << "LI.DownloadSpeedLimitTo :" << LI.DownloadSpeedLimitTo << endl;
					}
				}
				break;

			case Status:
				{
				//Vhash3.push_back(strHash);
				//cout << "Vhash3 size " << Vhash3.size() << endl;
				}
				break;
			default:
				break;
		}
		cout << count++ << endl;
		
		sprintf(client_fifo_name, CLIENT_FIFO_NAME, MC1.client_pid);
		client_fifo_fd = open(client_fifo_name, O_WRONLY);
		if (client_fifo_fd == -1)
		{
			fprintf(stderr, "Sorry, client fifo open failure!\n");
			exit(EXIT_FAILURE);
		}
		write(client_fifo_fd, &RI, sizeof(RI));
		close(client_fifo_fd);
	}
	else//keep try open
	{
		sleep(1);//need to sleep for a few second for it will make 100%cpu
	}
    }

    close(server_fifo_fd);
    unlink(SERVER_FIFO_NAME);
    exit(EXIT_SUCCESS);
}
Example #3
0
int main()
{
	int server_fifo_fd = -1;
	int client_fifo_fd = -1;

	int res;
	char client_fifo_name[NAME_SIZE];

	MessageClient MC1;
	memset(&MC1, 0 , sizeof(MC1));
	printf("\n starting 1:\n");
	//    if(UTIL_CheckFile(SERVER_FIFO_NAME))
	{
		UTIL_DeleteFile(SERVER_FIFO_NAME);
		cout <<"Delete "<< SERVER_FIFO_NAME ;	
	}
	printf("\n starting 2: mkfifo SERVER_FIFO_NAME\n");
	if (mkfifo(SERVER_FIFO_NAME, 0666) == -1)
	{
		fprintf(stderr, "Sorry, create server fifo failure!\n");
		exit(EXIT_FAILURE);
	}
	printf("\n starting 3: open SERVER_FIFO_NAME\n");
	
	
	server_fifo_fd = open(SERVER_FIFO_NAME, O_RDONLY/*|O_NONBLOCK*/);
	if (server_fifo_fd == -1)
	{
		fprintf(stderr, "Sorry, server fifo open failure!\n");
		exit(EXIT_FAILURE);
	}
	printf("\n starting while:\n");
	while (1)
	{		
		ResultInfo RI;
		//First read Operation Type
		
		printf("\n starting 4: read server_fifo_fd MessageClient \n");
		if((res = read(server_fifo_fd, &MC1, sizeof(MessageClient))) > 0)
		{						
			CourseInfo CI;		
			int OT = MC1.OT;
			printf("OT = %d\t",OT);
			switch(OT)
			{
				case OT_Book:
					{
						printf("\n starting 5: read  server_fifo_fd CourseInfo \n");
						if((res = read( server_fifo_fd, &CI, sizeof(CourseInfo))) > 0)
						{
// 							sleep(1);//stand for do some post and get and annylyse														

							cout << "Got a book  Hash:" << CI.Hash <<" client " << MC1.client_pid << " Grade "<<CI.Grade  << endl;
						}
					}
					break;
				
				default:
					break;
			}
			
			fflush(stdout);
	
			sprintf(client_fifo_name, "%s", CLIENT_FIFO_NAME);
			if(client_fifo_fd == -1)
			{
				printf("\n open client_fifo_name \n");
				client_fifo_fd = open(client_fifo_name, O_WRONLY);
			}
			if (client_fifo_fd == -1)
			{
				printf("\n open again client_fifo_name \n");
				if(!UTIL_CheckFile(client_fifo_name))
				{
					printf("\n%s not exist \n" , client_fifo_name);				
					fprintf(stderr, "Sorry, client fifo open failure!\n");
					exit(EXIT_FAILURE);
				}
			}
			
			
			RI.result = 0;
			RI.MC.client_pid = MC1.client_pid;
			printf("write ResultInfo %d\t", OT);
			write(client_fifo_fd, &RI, sizeof(ResultInfo) );
			printf("write ResultInfo complete\n");
			fflush(stdout);
			
// 			close(server_fifo_fd);
		}
		/*
		 * IMPORTANT :
		 * 如果不想用sleep等的方法, 则需要close (server_fifo_fd), 再打开, 以达到 block read 的效果
		 * */
		else//keep try open
		{
			printf("in sleep res =%d\n", res);
			fflush(stdout);
			sleep(1);//need to sleep for a few second for it will make 100%cpu
		}
	}

	close(server_fifo_fd);
	//     unlink(SERVER_FIFO_NAME);
	exit(EXIT_SUCCESS);
}
Example #4
0
int bnbtmain( )
{
	gmtxOutput.Initialize( );
	gmtxMySQL.Initialize( );

	UTIL_LogPrint( "Tracker Start\n" );

	srand( (unsigned int)time( 0 ) );

	gtStartTime = time( 0 );

	CFG_Open( CFG_FILE );
	CFG_SetDefaults( );
	CFG_Close( CFG_FILE );

	// XBNBT Language Configuration
	LANG_CFG_Init( LANG_CFG_FILE );

	UTIL_LogPrint( "Setting debug level, access and error log files\n" );

	guiFlushInterval = CFG_GetInt( "bnbt_flush_interval", 100 );
	
	// Log
	gstrLogDir = CFG_GetString( "bnbt_log_dir", string( ) );
	gstrLogFilePattern = CFG_GetString( "bnbt_log_file_pattern", "%Y-%m-%d.log" );
	
	if( !gstrLogDir.empty( ) && gstrLogDir[gstrLogDir.size( ) - 1] != PATH_SEP )
		gstrLogDir += PATH_SEP;

	gpLog = 0;
	gulLogCount = 0;
	
	// Error log
	gstrErrorLogDir = CFG_GetString( "bnbt_error_log_dir", string( ) );
	gstrErrorLogFilePattern = CFG_GetString( "bnbt_error_log_file_pattern", "%Y-%m-%de.log" );
	
	if( !gstrErrorLogDir.empty( ) && gstrErrorLogDir[gstrErrorLogDir.size( ) - 1] != PATH_SEP )
		gstrErrorLogDir += PATH_SEP;

	gpErrorLog = 0;
	gulErrorLogCount = 0;

	// Access log
	gstrAccessLogDir = CFG_GetString( "bnbt_access_log_dir", string( ) );
	gstrAccessLogFilePattern = CFG_GetString( "bnbt_access_log_file_pattern", "%Y-%m-%da.log" );

	if( !gstrAccessLogDir.empty( ) && gstrAccessLogDir[gstrAccessLogDir.size( ) - 1] != PATH_SEP )
		gstrAccessLogDir += PATH_SEP;

	gpAccessLog = 0;
	gulAccessLogCount = 0;

	// Set the debug level
	gbDebug = CFG_GetInt( "bnbt_debug", 1 ) == 0 ? false : true;
	gucDebugLevel = (unsigned char)CFG_GetInt( "bnbt_debug_level", 0 );

	UTIL_LogPrint( "***********************************************\n" );

	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
	{
		UTIL_LogPrint( "Debug is on at level (%u)\n", gucDebugLevel );
		
		if( gstrLogDir.empty( ) )
			UTIL_LogPrint( "Log directory is not set\n" );
		else
			UTIL_LogPrint( "Log dir (%s)\n", gstrLogDir.c_str( ) );

		if( gstrErrorLogDir.empty( ) )
			UTIL_LogPrint( "Error log directory is not set\n" );
		else
			UTIL_LogPrint( "Error log dir (%s)\n", gstrErrorLogDir.c_str( ) );

		if( gstrErrorLogDir.empty( ) )
			UTIL_LogPrint( "Access log directory is not set\n" );
		else
			UTIL_LogPrint( "Access log dir (%s)\n", gstrAccessLogDir.c_str( ) );
	}

	// Other globals
	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
		UTIL_LogPrint( "Setting global variables\n" );

	// PID
	gstrPID = CFG_GetString( "bnbt_pid_file", string( ) );

	if( gstrPID.empty( ) )
	{
		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "PID file is not set\n" );
	}
	else
	{
		// Write the process ID to file
		const long clPID( (long)getpid( ) );

		UTIL_LogPrint( "Recording PID (%ld) to file (%s)\n", clPID, gstrPID.c_str( ) );

		FILE *pFile = FILE_ERROR;

		pFile = fopen( gstrPID.c_str( ), "wt" );

		if( pFile == FILE_ERROR )
			UTIL_LogPrint( "Unable to write PID file (%s)\n", gstrPID.c_str( ) );
		else
		{
			fprintf( pFile, "%ld", clPID );
			fclose( pFile );
		}

		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "Recording PID completed\n" );
	}
	
	guiRestartServerInterval = CFG_GetInt( "bnbt_restart_interval", 24 );
	gulRestartServerNext = GetTime( ) + guiRestartServerInterval * 3600;

	guiMaxConns = CFG_GetInt( "bnbt_max_conns", 64 );
	guiMaxRecvSize = CFG_GetInt( "bnbt_max_recv_size", 524288 );
	gstrStyle = CFG_GetString( "bnbt_style_sheet", string( ) );
	gstrCharSet = CFG_GetString( "bnbt_charset", "utf-8" );
	gstrRealm = CFG_GetString( "bnbt_realm", "BNBT" );
	gstrPasswordKey = CFG_GetString( "bnbt_password_key", "ZiJingBT" );

	// The Trinity Edition - Addition Begins
	// Sets the value for the Custom NT Service Name variable
	gstrNTServiceName = CFG_GetString( "cbtt_service_name", "BNBT Service" );

	// TCP window size
	guiSO_RECBUF = CFG_GetInt( "xbnbt_so_recbuf", 128 ) * 1024;
	guiSO_SNDBUF = CFG_GetInt( "xbnbt_so_sndbuf", 128 ) * 1024;

	// Naggles algorithm
	gbTCP_NODELAY = CFG_GetInt( "xbnbt_tcp_nodelay", 0 ) == 0 ? false : true;

#ifdef WIN32
	// start winsock
	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
		UTIL_LogPrint( "Starting the windows sockets\n" );

	WSADATA wsaData;

	const int ciResult( WSAStartup( MAKEWORD(2,2), &wsaData ) );

	if ( ciResult != NO_ERROR )
	{
		UTIL_LogPrint( ( gmapLANG_CFG["unable_to_start_winsock"] + "\n" ).c_str( ), GetLastErrorString( ) );

		// Delete the PID file
		if( !gstrPID.empty( ) )
		{
			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Deleting the PID file\n" );

			UTIL_DeleteFile( gstrPID.c_str( ) );
		}
		
		// Close the log
		if( gpLog )
		{
			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Closing the log\n" );

			fclose( gpLog );
		}

		// Close the access log
		if( gpAccessLog )
		{
			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Closing the access log\n" );

			fclose( gpAccessLog );
		}

		// Close the error log
		if( gpErrorLog )
		{
			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Closing the error log\n" );

			fclose( gpErrorLog );
		}

		UTIL_LogPrint( "Tracker Stop\n" );

		gmtxMySQL.Destroy( );
		gmtxOutput.Destroy( );

		return 1;
	}
#endif

	// start mysql
	if( gbDebug )
		if( gucDebugLevel & DEBUG_BNBT )
			UTIL_LogPrint( "Setting MySQL dstate global variables\n" );

	if( !( gpMySQL = mysql_init( 0 ) ) )
	{
		UTIL_LogPrint( ( gmapLANG_CFG["bnbt_mysql_error"] + "\n" ).c_str( ), mysql_error( gpMySQL ) );

		// Delete the PID file
		if( !gstrPID.empty( ) )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Deleting the PID file\n" );

			UTIL_DeleteFile( gstrPID.c_str( ) );
		}
		
		// Close the log
		if( gpLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the log\n" );

			fclose( gpLog );
		}

		// Close the access log
		if( gpAccessLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the access log\n" );

			fclose( gpAccessLog );
		}

		// Close the error log
		if( gpErrorLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the error log\n" );

			fclose( gpErrorLog );
		}

		UTIL_LogPrint( "Tracker Stop\n" );

		gmtxMySQL.Destroy( );
		gmtxOutput.Destroy( );

		return 1;
	}

	gmapMySQL.clear( );
	gmapMySQL[pthread_self( )] = gpMySQL;
	gstrMySQLHost = CFG_GetString( "mysql_host", string( ) );
	gstrMySQLDatabase = CFG_GetString( "mysql_database", "bnbt" );
	gstrMySQLUser = CFG_GetString( "mysql_user", string( ) );
	gstrMySQLPassword = CFG_GetString( "mysql_password", string( ) );
	guiMySQLPort = CFG_GetInt( "mysql_port", 0 );
	
	if( !( mysql_real_connect( gpMySQL, gstrMySQLHost.c_str( ), gstrMySQLUser.c_str( ), gstrMySQLPassword.c_str( ), 0, guiMySQLPort, 0, 0 ) ) )
	{
		UTIL_LogPrint( ( gmapLANG_CFG["bnbt_mysql_error"] + "\n" ).c_str( ), mysql_error( gpMySQL ) );

		// Delete the PID file
		if( !gstrPID.empty( ) )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Deleting the PID file\n" );

			UTIL_DeleteFile( gstrPID.c_str( ) );
		}
		
		// Close the log
		if( gpLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the log\n" );

			fclose( gpLog );
		}

		// Close the access log
		if( gpAccessLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the access log\n" );

			fclose( gpAccessLog );
		}

		// Close the error log
		if( gpErrorLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the error log\n" );

			fclose( gpErrorLog );
		}

		UTIL_LogPrint( "Tracker Stop\n" );

		gmtxMySQL.Destroy( );
		gmtxOutput.Destroy( );

		return 1;
	}
	
	UTIL_LogPrint( ( gmapLANG_CFG["bnbt_mysql_connected"] + "\n" ).c_str( ) );

	UTIL_MySQLCreateDatabase( );

	if( mysql_select_db( gpMySQL, gstrMySQLDatabase.c_str( ) ) )
	{
		UTIL_LogPrint( ( gmapLANG_CFG["bnbt_mysql_error"] + "\n" ).c_str( ), mysql_error( gpMySQL ) );

		// Delete the PID file
		if( !gstrPID.empty( ) )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Deleting the PID file\n" );

			UTIL_DeleteFile( gstrPID.c_str( ) );
		}
		
		// Close the log
		if( gpLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the log\n" );

			fclose( gpLog );
		}

		// Close the access log
		if( gpAccessLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the access log\n" );

			fclose( gpAccessLog );
		}

		// Close the error log
		if( gpErrorLog )
		{
			if( gbDebug )
				if( gucDebugLevel & DEBUG_BNBT )
					UTIL_LogPrint( "Closing the error log\n" );

			fclose( gpErrorLog );
		}

		UTIL_LogPrint( "Tracker Stop\n" );

		gmtxMySQL.Destroy( );
		gmtxOutput.Destroy( );

		return 1;
	}

	UTIL_MySQLCreateTables( );

	// Create the server
	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
		UTIL_LogPrint( "Creating server\n" );

	gpServer = new CServer( );

	//
	// Link
	//

//	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
//		UTIL_LogPrint( "Creating link client/server\n" );

//	gpLink = 0;
//	gpLinkServer = 0;

//	if( CFG_GetInt( "bnbt_tlink_server", 0 ) != 0 )
//		gpLinkServer = new CLinkServer( );
//	else
//	{
//		if( !CFG_GetString( "bnbt_tlink_connect", string( ) ).empty( ) )
//		{
//#ifdef WIN32
//            int iLinkThreadResult = (int)_beginthread( ( void (*)(void *) )StartLink, 0, 0 );
//            
//			if( iLinkThreadResult == -1 )
//				UTIL_LogPrint( ( gmapLANG_CFG["unable_to_spawn_link_thread_win32"] + "\n" ).c_str( ) );
//#else
//			pthread_t thread;

//			// set detached state since we don't need to join with any threads

//			pthread_attr_t attr;
//			pthread_attr_init( &attr );
//			pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );

//			int iLinkThreadResult = pthread_create( &thread, &attr, ( void * (*)(void *) )StartLink, 0 );

//			if( iLinkThreadResult != 0 )
//				UTIL_LogPrint( ( gmapLANG_CFG["unable_to_spawn_link_thread"] + "\n" ).c_str( ), strerror( iLinkThreadResult ) );
//#endif
//		}
//	}

	//
	// HUBLink
	//

//	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
//		UTIL_LogPrint( "Creating hublink client/server\n" );

//	gpHUBLink = 0;
//	gpHUBLinkServer = 0;

//	if( CFG_GetInt( "xbnbt_thlink_server", 0 ) != 0 )
//		gpHUBLinkServer = new CHUBLinkServer( );
//	else
//	{
//		if( !CFG_GetString( "xbnbt_thlink_connect", string( ) ).empty( ) )
//		{
//#ifdef WIN32
//            int iHubLinkThreadResult = (int)_beginthread( ( void (*)(void *) )StartHUBLink, 0, 0 );
//            
//			if( iHubLinkThreadResult == -1 )
//				UTIL_LogPrint( ( gmapLANG_CFG["unable_to_spawn_hublink_thread_win32"] + "\n" ).c_str( ) );
//#else
//			pthread_t thread;

//			// set detached state since we don't need to join with any threads

//			pthread_attr_t attr;
//			pthread_attr_init( &attr );
//			pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );

//			int iHubLinkThreadResult = pthread_create( &thread, &attr, ( void * (*)(void *) )StartHUBLink, 0 );

//			if( iHubLinkThreadResult != 0 )
//				UTIL_LogPrint( ( gmapLANG_CFG["unable_to_spawn_hublink_thread"] + "\n" ).c_str( ), strerror( iHubLinkThreadResult ) );
//#endif
//		}
//	}

	//
	// update loop
	//

	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
		UTIL_LogPrint( "Entering update loop\n" );

	bool bDoUpdate = true;

	while( bDoUpdate )
	{
		if( gbDebug && ( gucDebugLevel & DEBUG_LOOPS ) && gpServer )
			UTIL_LogPrint( "Updating server\n" );

		if( gpServer && gpServer->Update( true ) )
		{
			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Deleting server\n" );

			delete gpServer;

			gpServer = 0;	 

//			if( gpLinkServer )
//			{
//				if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
//					UTIL_LogPrint( "Deleting link server\n" );

//				delete gpLinkServer;

//				gpLinkServer = 0;
//			}

//			if( gpHUBLinkServer )
//			{
//				if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
//					UTIL_LogPrint( "Deleting hublink server\n" );

//				delete gpHUBLinkServer;

//				gpHUBLinkServer = 0;
//			}

			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Exit the update loop\n" );

			bDoUpdate = false;
		}
		else
		if( gpServer && guiRestartServerInterval > 0 && GetTime( ) > gulRestartServerNext )
		{
			if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
				UTIL_LogPrint( "Restarting server\n" );

			delete gpServer;

			gpServer = new CServer( );
			
			gulRestartServerNext = GetTime( ) + guiRestartServerInterval * 3600;
		}

//		if( gpLinkServer && gucDebugLevel != 0 )
//		{
//			if( gbDebug && ( gucDebugLevel & DEBUG_LOOPS ) )
//				UTIL_LogPrint( "Updating link server\n" );

//			gpLinkServer->Update( );
//		}

//		if( gpHUBLinkServer && gucDebugLevel != 0 )
//		{
//			if( gbDebug && ( gucDebugLevel & DEBUG_LOOPS ) )
//				UTIL_LogPrint( "Updating hublink server\n" );

//			gpHUBLinkServer->Update( );
//		}
	}

	//
	// wait for the link or it might make a big mess
	//

//	if( gpLink )
//	{
//		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
//			UTIL_LogPrint( "Killing link client\n" );
//		
//		gpLink->Kill( );
//	}

//	const unsigned long culStart( GetTime( ) );

//	while( gpLink )
//	{
//		UTIL_LogPrint( ( gmapLANG_CFG["wait_link_disconnect"] + "\n" ).c_str( ) );

//		MILLISLEEP( 1000 );

//		if( GetTime( ) - culStart > 60 )
//		{
//			UTIL_LogPrint( ( gmapLANG_CFG["waited_link_disconnect"] + "\n" ).c_str( ) );

//			break;
//		}
//	}

	//
	// wait for the hub link or it might make a big mess
	//

//	if( gpHUBLink )
//	{
//		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
//			UTIL_LogPrint( "Killing hublink client\n" );

//		gpHUBLink->Kill( );
//	}

//	const unsigned long culStartHUB( GetTime( ) );

//	while( gpHUBLink )
//	{
//		UTIL_LogPrint( ( gmapLANG_CFG["wait_hublink_disconnect"] + "\n" ).c_str( ) );

//		MILLISLEEP( 1000 );

//		if( GetTime( ) - culStartHUB > 60 )
//		{
//			UTIL_LogPrint( ( gmapLANG_CFG["waited_hublink_disconnect"] + "\n" ).c_str( ) );

//			break;
//		}
//	}

	// Close the BNBT MySQL database
	if( gpMySQL )
	{
		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "Closing MySQL dstate integration build\n" );

		mysql_close( gpMySQL );
	}

#ifdef WIN32
	// Exit windows sockets
	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
		UTIL_LogPrint( "Closing the windows sockets\n" );

	WSACleanup( );
#endif

	// Delete the PID file
	if( !gstrPID.empty( ) )
	{
		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "Deleting the PID file\n" );

		UTIL_DeleteFile( gstrPID.c_str( ) );
	}
	
	// Close the log
	if( gpLog )
	{
		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "Closing the log\n" );

		fclose( gpLog );
	}

	// Close the access log
	if( gpAccessLog )
	{
		if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "Closing the access log\n" );

		fclose( gpAccessLog );
	}

	// Close the error log
	if( gpErrorLog )
	{
	 	if( gbDebug && ( gucDebugLevel & DEBUG_BNBT ) )
			UTIL_LogPrint( "Closing the error log\n" );

		fclose( gpErrorLog );
	}

	UTIL_LogPrint( "Tracker Stop\n" );

	gmtxMySQL.Destroy( );
	gmtxOutput.Destroy( );

	return 0;
}