Mon, 31 Mar 2014 11:41:08 +0200
added optional alternate config for chess lib
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "terminal-chess.h"
31 #include "game.h"
32 #include <ncurses.h>
34 static int server_open(Server *server, char *port) {
35 printw("\nListening for client...\n");
36 refresh();
37 if (net_create(server, port)) {
38 perror("Server creation failed");
39 return 1;
40 }
42 if (net_listen(server)) {
43 perror("Listening for client failed");
44 return 1;
45 }
47 return 0;
48 }
50 static int server_handshake(Client *client) {
51 net_send_code(client->fd, NETCODE_VERSION);
52 if (net_recieve_code(client->fd) != NETCODE_VERSION) {
53 fprintf(stderr, "Client uses an incompatible software version.\n");
54 return 1;
55 }
57 printw("Client connected - transmitting gameinfo...");
58 refresh();
60 return 0;
61 }
63 int server_run(Settings *settings) {
64 Server server;
66 dump_gameinfo(&(settings->gameinfo));
68 if (server_open(&server, settings->port)) {
69 net_destroy(&server);
70 return EXIT_FAILURE;
71 }
73 if (server_handshake(server.client)) {
74 net_destroy(&server);
75 return EXIT_FAILURE;
76 }
78 int fd = server.client->fd;
79 net_send_code(fd, NETCODE_GAMEINFO);
80 net_send_data(fd, &(settings->gameinfo), sizeof(settings->gameinfo));
81 printw("\rClient connected - awaiting challenge acceptance...");
82 refresh();
83 int code = net_recieve_code(fd);
84 if (code == NETCODE_ACCEPT) {
85 printw("\rClient connected - challenge accepted.");
86 clrtoeol();
88 game_start(settings, fd);
89 } else if (code == NETCODE_DECLINE) {
90 printw("\rClient connected - challenge declined.");
91 clrtoeol();
92 } else {
93 fprintf(stderr, "Invalid client response\n");
94 net_destroy(&server);
95 return EXIT_FAILURE;
96 }
98 net_destroy(&server);
99 return EXIT_SUCCESS;
100 }