Ejemplo n.º 1
0
void drawBoard(int page) {
	switch(page) {
		case 0:
			registerBody("NULL", newHeader(60, 1, "INTRODUCTION", -1));

			registerBody("NULL", newText(65, 9, "STOP!", 0));
			registerBody("NULL", newText(40, 12, "    Welcome to *hammertime*, a text-based \"visual\" framework made and functional with the language C.", max(60, screen.width-40)));
			registerBody("NULL", newText(40, 15, "    The original idea was to create a simple yet useful     framework to use in projects using C, that didn't require   much knowledge in programming.", max(60, screen.width-40)));

			registerBody("NULL", newText(40, 19, "    This little guide will help you with the basics of      *hammertime*. The window on the left indicates the page     you're on. To proceed to the next page, type 'next' (or     'n'). To return to the previous page, type 'prev' (or 'p'). Or if you want you can just type the page's number and pressenter.", max(60, screen.width-40)));
			registerBody("NULL", newText(40, 26, "    Type 'exit' at any time to leave this guide.", 0));

			registerBody("NULL", newText(45, 32, "https://github.com/RenatoGeh/hammertime/wiki", 0));
		break;
		case 1:
			registerBody("NULL", newHeader(60, 1, "USING *HAMMERTIME*", -1));

			registerBody("NULL", newText(40, 10, "    First of all, lets see how to include *hammertime* in   any C source file.", 60));
			registerBody("NULL", newText(40, 13, "    Make sure all files from *hammertime* are in the same   folder as your project.", 60));
			registerBody("NULL", newText(40, 16, "    Just as any other library, you'll need to include       \"hammertime.h\" in the beginning of your C project(#include  \"hammertime.h\").", 60));
			registerBody("NULL", newText(40, 20, "    Instead of a \"int main()\" function, you'll write the    things *hammertime* will print in a \"int run()\" function.", 60));
			registerBody("NULL", newText(40, 23, "    But just as a normal C source file, you can write other functions as well.", 60));
			registerBody("NULL", newText(40, 25, "    Example of a simple *hammertime* project:", 60));
			registerBody("NULL", newTextBox(40, 29, "#include \"hammertime.h\"", 40, '#'));
		break;
		case 2:
			registerBody("NULL", newHeader(60, 1, "BODIES AND BASIC DRAWING", -1));

			registerBody("NULL", newText(40, 10, "    In *hammertime*, the basic thing you'll need to learn is how to create (or register) \"bodies\" and make them visible.", 60));
			registerBody("NULL", newText(40, 13, "    A body is some sort of object, and it may vary from a simple text to a circle made of \"$\".", 60));
			registerBody("NULL", newText(40, 16, "    To create a simple body you follow this model:", 60));
			registerBody("NULL", newText(40, 19, "       registerBody(name, newThing(thing parameters));", 60));
		break;
	}
}
Ejemplo n.º 2
0
void *accepted(void *csd){
	PEER *p = (PEER *) csd;
	DATA *data = malloc(sizeof(DATA));
	DATA *reply = malloc(sizeof(DATA));
	int client_sd = p->client_sd;
	int len, status;
	int id = p->id;
	
	while(1){

		printf("B4 recv data\n");
		data = recv_data(client_sd,(unsigned int *)&len,&status);
		printf("AFTER recv data\n");
		switch(status){
			case -1:
			case -2:
				printf("Status = error(%d)\n", status);
				close(client_sd);
				offline(id);
				printf("[Disconnected] Client %s:%d\n", 
					getClientAddr(&p->client_addr),
					ntohs(p->client_addr.sin_port));
				return 0;
			default:
				break;
		}

		printf("DATA command: %d\n", data->command);

	    switch(data->command){
	    	case LOGIN:
	    		printf("Client %s:%d: [LOGIN] Username="******"\n");
	    		data->arg->ip = ntohl(p->client_addr.sin_addr.s_addr);
	    		status = online(data->arg, id);
	    		if(status >= 0 && status < 10){
	    			reply = newHeader();
	    			reply->command = LOGIN_OK;
	    			reply->length = 1;
					if(!send_data(client_sd,reply,(unsigned int*)&len)) return 0;
	    		}else{
	    			printf("[ERROR] Client %s:%d ", 
						getClientAddr(&p->client_addr),
						ntohs(p->client_addr.sin_port));
	    			reply = newHeader();
	    			reply->command = ERROR;
	    			switch(status){
	    				case -1:
	    					printf("maximum connection exceed!");
	    					reply->error = TOO_MUCH_CONN;
	    					break;
	    				case -2:
	    					printf("duplication of name!");
	    					reply->error = SAME_NAME;
	    					break;
	    				case -3:
	    					printf("duplication of port and IP address!");
	    					reply->error = SAME_CONN;
	    			}
	    			printf("\n");
	    			reply->length = 7;
	    			if(!send_data(client_sd,reply,(unsigned int*)&len)) return 0;
	    			close(client_sd);
					printf("[Disconnected] Client %s:%d\n", 
						getClientAddr(&p->client_addr),
						ntohs(p->client_addr.sin_port));
					return 0;
	    		}
	    		break;
	    	case GET_LIST:
	    		printf("Client %s:%d: GET_LIST\n", 
	    			getClientAddr(&p->client_addr),
	    			ntohs(p->client_addr.sin_port));
	    		reply = newHeader();
	    		reply->command = GET_LIST_OK;
	    		getOnlineList(reply);
	    		reply->length += 7;
				if(!send_data(client_sd,reply,(unsigned int*)&len)) return 0;
	    		break;
	    	default:
	    		printf("Client %s:%d: Unknown command\n", 
	    			getClientAddr(&p->client_addr),
	    			ntohs(p->client_addr.sin_port));
	    		break;
	    }
	}

	if(client_sd) close(client_sd);
	offline(id);
	printOnlineList();
	printf("[Disconnected] Client %s:%d\n", 
		getClientAddr(&p->client_addr),
		p->client_addr.sin_port);

}
Ejemplo n.º 3
0
int main(int argc, char **argv){
    int sd = socket(AF_INET, SOCK_STREAM, 0);
    int client_sd;
    int addr_len;
    int len;
    int i;

    pthread_t pth[10];

	struct sockaddr_in server_addr;
	struct sockaddr_in client_addr;
	memset(&server_addr,0,sizeof(server_addr));
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	server_addr.sin_port = htons(PORT);

	for(i=0;i<10;i++){
		peers[i] = NULL;
	}

	if(bind(sd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0){
		printf("Socket cannot bind to a port. %s \nError number: %d\n", strerror(errno), errno);
	}

	if(listen(sd,3) < 0){
		printf("Cannot listen to a port. %s \nError number: %d\n", strerror(errno), errno);
	}

	addr_len = sizeof(client_addr);
	while(1){
		if((client_sd = (accept(sd, (struct sockaddr *) &client_addr, (unsigned int*)&addr_len))) < 0){
			printf("Connection error. Accepting client failed. %s \nError number: %d\n", strerror(errno), errno);
			exit(0);
		}

		printf("[Connected] Client: %s:%d\n", 
			getClientAddr(&client_addr), 
			htons(client_addr.sin_port));
		for(i=0;i<10;i++){
			if(peers[i]==NULL){
				passing[i].id = i;
				passing[i].client_sd = client_sd;
				passing[i].sd = sd;
				passing[i].client_addr = client_addr;
				pthread_create(&pth[i], NULL, accepted, (void *) &passing[i]);
				break;
			}
		}
		if(i==10){
			DATA *reply = newHeader();
			printf("[ERROR] Client %s:%d ", 
				getClientAddr(&client_addr),
				ntohs(client_addr.sin_port));
			reply = newHeader();
			reply->command = ERROR;
			printf("maximum connection exceed!");
			reply->error = TOO_MUCH_CONN;
			printf("\n");
			reply->length = 7;
			send_data(client_sd,reply,(unsigned int*)&len);
			close(client_sd);
			printf("[Disconnected] Client %s:%d\n", 
				getClientAddr(&client_addr),
				ntohs(client_addr.sin_port));
		}
		
	}
	for(i=0;i<10;i++)
		pthread_join(pth[i],NULL);

	close(sd);
    return 0;
}