Beispiel #1
0
/*
** Initialize the controller
**
*/
int init()
{
	/* Open the message queue */	
	if (!open_msg_queue()) return 0;
	//close the message queue to flush all messages;
	while (!close_msg_queue()) {};
	/* Open the message queue again*/	
	if (!open_msg_queue()) return 0;
	if (!create_FIFO()) return 0;
	printf("%d: - Controller has been created.\n",getpid());
	return 1;
}
Beispiel #2
0
static int create_server_mqs()
{
	int i;
	char mq_name[12];
	for (i = 0; i < NUM_SERVERS; i++) {
		sprintf(mq_name, "/mq_server_%d", i+1);
		server_mqs[i] = open_msg_queue(mq_name);
		if (server_mqs[i] == (mqd_t)-1)
			exit(EXIT_FAILURE);
	}
	return 0;
}
Beispiel #3
0
int main(int argc, char** argv)
{
    // open clients <-> gateway message queue
    mqd_t mq = open_msg_queue(MQ_NAME);
    if (mq == (mqd_t)-1)
        exit(EXIT_FAILURE);
    
    // open gateway <-> servers message queues
    create_server_mqs();
    
    Command cmd;
    int ret = receive_command(mq, &cmd);
    if (ret == -1)
    	exit(EXIT_FAILURE);
    Command *command = &cmd;
    
    int server_id = command->crypt[0];
    
    while(strcmp(command->name, "exit") != 0) {
    	server_id = command->crypt[0] - 1;
    	// if there are no more actions to take
    	if (server_id == -2)
    		unlock_client(command->name);
    	else {
    		// forward command to server
    		send_command(server_mqs[server_id], command);
    	}
    	
    	ret = receive_command(mq, command);
    	if (ret == -1)
    		exit(EXIT_FAILURE);
    }
    // send the exit command to the servers
    int i;
    for (i = 0; i < NUM_SERVERS; i++)
    	send_command(server_mqs[i], command);
    
    close_server_mqs();
    mq_close(mq);
    return 0;
}
Beispiel #4
0
int main(int argc, char** argv)
{
	if (argc < 2) {
		fprintf(stderr, "Too few arguments given to server.\n./server N\n");
		exit(EXIT_FAILURE);
	}
	
	int server_id = atoi(argv[1]);
	pdecryptf_t decrypt = choose_decryption(server_id);
	
	char mq_name[13];
	sprintf(mq_name, "/mq_server_%d", server_id);
	mqd_t mq = open_msg_queue(mq_name);
	
	Command cmd;
    int ret = receive_command(mq, &cmd);
    if (ret == -1)
    	exit(EXIT_FAILURE);
    Command *command = &cmd;
	
	mqd_t ret_mq;
	while(strcmp(command->name, "exit") != 0) {
		
		//open shared memory
		char* shm_name = command->name;
		int shm_size = command->dim;
		int shm_fd;
		void* mem = open_shared_memory(shm_name, shm_size, &shm_fd);
		if (mem == NULL) {
			exit(EXIT_FAILURE);
		}
		
		// get size of the message from the first 
		// integer in the shared memory
		int dim_message;
		memcpy(&dim_message, mem, sizeof(int));
		
		// decrypt message
		void* out = malloc(1000);
		int decrypt_size = decrypt(mem+sizeof(int), dim_message, out);
		
		// copy result back in shared memory
		memcpy(mem, &decrypt_size, sizeof(int));
		memcpy(mem + sizeof(int), out, decrypt_size);
		
		// Close shared memory
		close_shared_memory(mem, shm_size, shm_fd, shm_name);
		
		// Send message back to gateway with self removed
		int i;
		for (i = 1; i < 17; i++)
			command->crypt[i-1] = command->crypt[i]; 
		ret_mq = open_msg_queue(MQ_GATEWAY);
		ret = send_command(ret_mq, command);
		if (ret == -1)
    		exit(EXIT_FAILURE);
		mq_close(ret_mq);
		
		// Get another message from the mq
		ret = receive_command(mq, command);
		if (ret == -1)
    		exit(EXIT_FAILURE);
	}
	mq_close(mq);
	mq_close(ret_mq);
    return 0;
}