Exemple #1
0
void create_client(unsigned int part_size, char *message)
{
	int write_length;

	size_t text_length;
	char text[MAX_MESSAGE_SIZE];

	if (message) {
		text_length = strlen(message);
		
		if (!simulate_sending) {
			create_connection();
			LOG(DEBUG, "Connection created");
		}

		strncpy(text, message, MAX_MESSAGE_SIZE);

		part_size = prepare_connection(text_length, part_size);
		if (part_size == 0)
			die("preparing connection");

		LOG(DEBUG, "{ Message from command line to send: '%s' (%u)", text, text_length);
		write_length = send_message(/*connection, */text, text_length, part_size);
		LOG(DEBUG, "} Message written; packet size: %i", write_length);

		if (!simulate_sending)
			close_connection();

		LOG(INFO, "Nothing more to do. Exiting.");
	} else {

		LOG(INFO, "Type message and press enter");

		for (;;) {
			printf(CLIENT_PROMPT);

			text_length = read_line(text, MAX_MESSAGE_SIZE);

			if (!simulate_sending) {
				create_connection();
				LOG(DEBUG, "Connection created");
			}

			part_size = prepare_connection(text_length, part_size);
			if (part_size == 0)
				die("preparing connection");

			LOG(DEBUG, "Message to send: '%s' (%u)", text, text_length);

			write_length = send_message(/*connection, */text, text_length, part_size);
			//sleep(1); // Prevent client from spamming

			if (!simulate_sending)
				close_connection();
		}

		LOG(INFO, "Server closed connection?");
	}
}
// ========== The Main Function ============
int main (int argc, char *argv[])
{
	configure_server(config);

	signalHandling();

#if DEBUG
	printf("Checking if transmission directory exists...\n");
#endif // if DEBUG
	dirChkCreate(BISON_TRANSFER_DIR.c_str(), "transfer");

	printf("Server Starting Up...\n");

	// Insert PID file management stuff here

	// keep on preparing the connection until it is prepared.
	while (1) {
		if (!prepare_connection())
			break;
		for (int i = 0; i > ERROR_WAIT; i++)
			usleep(1000);
	}

#if DEBUG
	printf("Server ready, waiting for connection.\n");
#endif // if DEBUG

	// Loop to keep accepting connections
	while (1) {
		const char* ret = update_filetable(BISON_TRANSFER_DIR, filetable);
		if (ret) {
			std::cerr << "Error on filetable update: " << ret << std::endl;
		}
		handle_connection();
		usleep(5000);					// wait a bit so that we won't -
		// overload the system
	}

	return 0;
}