int main()
{
	int ch;
	
	initscr();			/* start curses	*/
	raw();				/* disable line buffering */
	keypad(stdscr, TRUE);
	noecho();

	start_color();
	use_default_colors();
	init_pair(10, COLOR_WHITE, COLOR_RED);

	statuswin = subwin(stdscr, 1, 0, 0, 0);
	hdrwin = subwin(stdscr, 1, 0, 1, 0);

	blockpad = newpad(1000, 60);

	errorw = statuswin;

	wclear(stdscr);

	db_connect();
	if (db_is_connected())
	{
		reporterror("connected");
		rels = db_fetch_relations(&nrels);
		if (rels)
			display_relations(hdrwin, blockpad, rels, nrels);
	}

	move_selection(0);
	refresh();

	for (;;)
	{
		refresh_screen();

		ch = getch();

		werase(statuswin);
		wrefresh(statuswin);

		switch (ch)
		{
			case KEY_UP:
				if (displayed_block != InvalidBlockNumber)
					scroll_blockpad(-1);
				else
					move_selection(-1);
				break;
			case KEY_DOWN:
				if (displayed_block != InvalidBlockNumber)
					scroll_blockpad(1);
				else
					move_selection(1);
				break;
			case KEY_NPAGE:
				if (displayed_block != InvalidBlockNumber)
					scroll_blockpad(15);
				else
					move_selection(15);
				break;
			case KEY_PPAGE:
				if (displayed_block != InvalidBlockNumber)
					scroll_blockpad(-15);
				else
					move_selection(-15);
				break;

			case KEY_ENTER:
			case KEY_RIGHT:
				if (displayed_block == InvalidBlockNumber)
				{
					block = db_fetch_block(rels[selected_rel].relname, "main", 0);
					if (block)
					{
						blockpad_pos_save = blockpad_pos;
						blockpad_pos = 0;
						displayed_block = 0;
						display_block(hdrwin, blockpad, block, displayed_block);
					}
				}
				else
				{
					displayed_block++;
					block = db_fetch_block(rels[selected_rel].relname, "main", displayed_block);
					if (block)
					{
						blockpad_pos_save = blockpad_pos;
						blockpad_pos = 0;
						display_block(hdrwin, blockpad, block, displayed_block);
					}
				}
				break;

			case KEY_LEFT:
				if (displayed_block != InvalidBlockNumber)
				{
					displayed_block--;
					if (displayed_block == InvalidBlockNumber)
					{
						blockpad_pos = blockpad_pos_save;
						if (displayed_block == InvalidBlockNumber)
						{
							display_relations(hdrwin, blockpad, rels, nrels);
							mvwchgat(blockpad, selected_rel, 0, 40, A_REVERSE, 0, NULL);
						}
					}
					else
					{
						block = db_fetch_block(rels[selected_rel].relname, "main", displayed_block);
						if (block)
						{
							blockpad_pos_save = blockpad_pos;
							blockpad_pos = 0;
							display_block(hdrwin, blockpad, block, displayed_block);
						}
					}

				}
				break;

			case 'g':				/* goto block */
				if (displayed_block != InvalidBlockNumber)
				{
					char str[11];
					BlockNumber blkno;
					char *endptr;

					werase(hdrwin);
					mvwprintw(hdrwin, 0, 0, "Goto block: ");
					refresh_screen();
					echo();
					getnstr(str, sizeof(str) - 1);
					noecho();

					blkno = strtoul(str, &endptr, 10);
					if (*endptr != '\0')
					{
						werase(hdrwin);
						mvwprintw(hdrwin, 0, 0, "Invalid block number");
					}
					else
					{
						block = db_fetch_block(rels[selected_rel].relname, "main", blkno);
						if (block)
						{
							displayed_block = blkno;
							blockpad_pos_save = blockpad_pos;
							blockpad_pos = 0;
							display_block(hdrwin, blockpad, block, displayed_block);
						}
					}
				}
				break;

			case 'q':
				endwin();			/* End curses mode		  */
				exit(0);
				break;
			default:
				reporterror("unknown key: %c", ch);
		}
	}
	endwin();			/* End curses mode		  */

	return 0;
}
Exemple #2
0
int main(int argc, char** argv)
{
    if(argc > 1)
    {
        if(strcmp(argv[1], "-d") == 0)
        {
            if(daemon(1,1) != 0)
            {
                fprintf(stderr, "Could not start daemon\n");
            }
        }
    }
    
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);

    /* Do a test connection to see if the server
     * is even up */
    PGconn* conn = db_connect();
    if(!db_is_connected(conn))
    {
        fprintf(stderr, "The PostgreSQL server is not running.\n");
        return 1;
    }
    db_disconnect(conn);

    int serversock = 0;
    int clientsock = 0;

    struct sockaddr_in server;
    struct sockaddr_in client;

    if((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    {
        fprintf(stderr, "Failed to create socket\n");
        return 1;
    }

    memset(&server, 0, sizeof(server));         /* Clear struct */
    server.sin_family = AF_INET;                /* Internet IP */
    server.sin_addr.s_addr = htonl(INADDR_ANY); /* Incoming addr */
    server.sin_port = htons(4000);              /* Server port */

    if(bind(serversock, (struct sockaddr*) &server, sizeof(server)) < 0)
    {
        fprintf(stderr, "Failed to bind the server socket\n");
        return 1;
    }

    if(listen(serversock, MAXPENDING) < 0)
    {
        fprintf(stderr, "Failed to listen on server socket\n");
        return 1;
    }
    printf("JBlux server 1.0\n");

    /* Start the game world */
    pthread_t world_thread;
    pthread_create(&world_thread, NULL, init_world, NULL);

    while(1)
    {
        unsigned int clientlen = sizeof(client);
        /* Wait for client connection */
        if((clientsock = accept(serversock, (struct sockaddr*) &client,
                    &clientlen)) < 0)
        {
            printf("Failed to accept client connection\n");
        }

        printf("Client connect: %s\n", inet_ntoa(client.sin_addr));
        handle_client(&clientsock);
    }

    return 0;
}