Exemple #1
0
int main(int argc, char *argv[])
{
	if (argc < 2)
	{
		print_help();
		return 0;
	}

	int rw_type = -1;
	if (strcmp(argv[1], "-r") == 0)
	{
		rw_type = 0;
	}
	else
	if (strcmp(argv[1], "-w") == 0)
        {
                rw_type = 1;
        }
	else
	{
		rw_type = -1;
	}

	switch (rw_type)
	{
		case 0:
			read_hello("./helloworld.txt");
			break;
		case 1:
			if (argc < 3)
			{
				printf("please input write content.\n");
				return -1;
			}
			write_hello(argv[2], strlen(argv[2]) + 1);
			break;
		default:
			printf("hello i am comming!\n");
			break;
			
	}

	return 0;

}
void* BANG_read_peer_thread(void *self_info) {
    BANG_peer *self = (BANG_peer*)self_info;

    BANG_header *header;

    char reading = 1;

    while (reading) {
        if ((header = (BANG_header*) read_message(self,LENGTH_OF_HEADER)) != NULL) {
            switch (*header) {
            case BANG_HELLO:
                reading = read_hello(self);
                break;

            case BANG_DEBUG_MESSAGE:
                reading = read_debug_message(self);
                break;

            case BANG_SEND_MODULE:
                /* I guess we'll take it... */
                reading = read_module_message(self);
                break;

            case BANG_EXISTS_MODULE:
                /* Someone is asking us if we want a module... send out a signal! */
                reading = read_module_exists(self);
                break;

            case BANG_REQUEST_MODULE:
                /* Send signal to request the new module. */
                reading = read_module_request(self);
                break;

            case BANG_SEND_JOB:
                reading = read_job_message(self);
                break;

            case BANG_REQUEST_JOB:
                reading = read_request_job(self);
                break;

            case BANG_FINISHED_JOB:
                reading = read_finished_job(self);
                break;

            case BANG_AVAILABLE_JOB:
                reading = read_available_job(self);
                break;

            case BANG_MISMATCH_VERSION:
            case BANG_BYE:
                reading = 0;
                break;
            default:
                /**
                 * Protocol mismatch, hang up.
                 */
#ifdef BDEBUG_1
                fprintf(stderr,"Protocol mismatch on peer read thread %d, hanging up.\n",self->peer_id);
#endif
                reading = 0;
                break;
            }

            free(header);
        } else {
            reading = 0;
        }
    }

#ifdef BDEBUG_1
    fprintf(stderr,"Peer read thread %d closing on self.\n",self->peer_id);
#endif

    read_peer_thread_self_close(self);
    return NULL;
}