src/main.c

Sun, 23 Feb 2014 21:03:35 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 23 Feb 2014 21:03:35 +0100
changeset 2
0a08f79c320d
parent 1
e5fbb8f9edbe
child 3
3693fd2eb0e9
permissions
-rw-r--r--

fixed network code + added game info and transmission of game info

universe@0 1 /*
universe@0 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@0 3 *
universe@0 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@0 5 *
universe@0 6 * Redistribution and use in source and binary forms, with or without
universe@0 7 * modification, are permitted provided that the following conditions are met:
universe@0 8 *
universe@0 9 * 1. Redistributions of source code must retain the above copyright
universe@0 10 * notice, this list of conditions and the following disclaimer.
universe@0 11 *
universe@0 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@0 13 * notice, this list of conditions and the following disclaimer in the
universe@0 14 * documentation and/or other materials provided with the distribution.
universe@0 15 *
universe@0 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@0 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@0 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@0 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@0 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@0 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@0 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@0 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@0 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@0 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@0 26 * POSSIBILITY OF SUCH DAMAGE.
universe@0 27 *
universe@0 28 */
universe@0 29
universe@1 30 #include "terminal-chess.h"
universe@1 31 #include <string.h>
universe@2 32 #include <time.h>
universe@1 33
universe@1 34 int get_settings(int argc, char **argv, Settings *settings) {
universe@1 35 char *valid;
universe@2 36 unsigned long int time, port;
universe@2 37 uint8_t timeunit = 60;
universe@2 38 size_t len;
universe@1 39
universe@2 40 for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) {
universe@1 41 switch (opt) {
universe@2 42 case 'b':
universe@2 43 settings->gameinfo.servercolor = BLACK;
universe@2 44 break;
universe@2 45 case 'r':
universe@2 46 settings->gameinfo.servercolor = (rand()>>5) & 1 ? WHITE : BLACK;
universe@2 47 break;
universe@2 48 case 't':
universe@2 49 case 'a':
universe@2 50 len = strlen(optarg);
universe@2 51 if (optarg[len-1] == 's') {
universe@2 52 optarg[len-1] = '\0';
universe@2 53 timeunit = 1;
universe@2 54 }
universe@2 55
universe@2 56 if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
universe@2 57 || *valid != '\0') {
universe@2 58 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
universe@2 59 return 1;
universe@2 60 } else {
universe@2 61 if (opt=='t') {
universe@2 62 settings->gameinfo.time = timeunit * time;
universe@1 63 } else {
universe@2 64 settings->gameinfo.addtime = time;
universe@1 65 }
universe@2 66 }
universe@2 67 break;
universe@2 68 case 'p':
universe@2 69 port = strtol(optarg, &valid, 10);
universe@2 70 if (port < 1025 || port > 65535 || *valid != '\0') {
universe@2 71 fprintf(stderr,
universe@2 72 "Invalid port number (%s) - choose a number between "
universe@2 73 "1025 and 65535\n",
universe@2 74 optarg);
universe@2 75 return 1;
universe@2 76 } else {
universe@2 77 settings->port = optarg;
universe@2 78 }
universe@2 79 break;
universe@2 80 case 'h':
universe@2 81 case '?':
universe@2 82 settings->printhelp = 1;
universe@2 83 break;
universe@1 84 }
universe@1 85 }
universe@1 86
universe@1 87 if (optind == argc - 1) {
universe@1 88 settings->serverhost = argv[optind];
universe@1 89 } else if (optind < argc - 1) {
universe@1 90 fprintf(stderr, "Too many arguments\n");
universe@1 91 return 1;
universe@1 92 }
universe@1 93
universe@1 94 return 0;
universe@1 95 }
universe@1 96
universe@1 97 Settings default_settings() {
universe@1 98 Settings settings;
universe@1 99 memset(&settings, 0, sizeof(Settings));
universe@2 100 settings.gameinfo.servercolor = WHITE;
universe@1 101 settings.port = "27015";
universe@1 102 return settings;
universe@1 103 }
universe@1 104
universe@2 105 void dump_gameinfo(Gameinfo *gameinfo) {
universe@2 106 int serverwhite = gameinfo->servercolor == WHITE;
universe@2 107 printf(
universe@2 108 "Game details:\n"
universe@2 109 " Server plays %s - Client plays %s\n",
universe@2 110 serverwhite?"white":"black", serverwhite?"black":"White"
universe@2 111 );
universe@2 112 if (gameinfo->time > 0) {
universe@2 113 if (gameinfo->time % 60) {
universe@2 114 printf(" Time limit: %ds + %ds\n",
universe@2 115 gameinfo->time, gameinfo->addtime);
universe@2 116 } else {
universe@2 117 printf(" Time limit: %dm + %ds\n",
universe@2 118 gameinfo->time/60, gameinfo->addtime);
universe@2 119 }
universe@2 120 } else {
universe@2 121 printf(" No time limit\n");
universe@2 122 }
universe@2 123 }
universe@2 124
universe@1 125 int cleanup(Settings *settings, int exitcode) {
universe@1 126 if (settings->server) {
universe@1 127 if (net_destroy(settings->server)) {
universe@1 128 perror("Server shutdown failed");
universe@1 129 }
universe@1 130 }
universe@1 131
universe@1 132 return exitcode;
universe@1 133 }
universe@0 134
universe@0 135 int main(int argc, char **argv) {
universe@2 136 srand(time(NULL));
universe@1 137
universe@1 138 Settings settings = default_settings();
universe@2 139 if (get_settings(argc, argv, &settings)) {
universe@2 140 return 1;
universe@2 141 }
universe@1 142
universe@1 143 if (settings.printhelp) {
universe@1 144 printf(
universe@2 145 "Usage: terminal-chess [OPTION]... [HOST]\n"
universe@2 146 "Starts/joins a network chess game\n"
universe@2 147 "\nGeneral options\n"
universe@1 148 " -h This help page\n"
universe@2 149 " -p TCP port to use (default: 27015)\n"
universe@2 150 "\nServer options\n"
universe@2 151 " -a <time> Specifies the time to add after each move\n"
universe@2 152 " -b Server plays black pieces (default: white)\n"
universe@2 153 " -r Distribute color randomly\n"
universe@2 154 " -t <time> Specifies time limit (default: no limit)\n"
universe@2 155 "\nNotes\n"
universe@2 156 "White pieces are displayed as uppercase and black pieces as "
universe@2 157 "lowercase letters.\n"
universe@2 158 "The time unit for -a is seconds and for -t minutes by default. To "
universe@2 159 "specify\nseconds for the -t option, use the s suffix.\n"
universe@2 160 "Example: -t 150s\n"
universe@2 161 );
universe@1 162 return EXIT_SUCCESS;
universe@1 163 }
universe@1 164
universe@1 165 Server server;
universe@1 166 settings.server = &server;
universe@1 167
universe@1 168 if (is_server(&settings)) {
universe@2 169 dump_gameinfo(&(settings.gameinfo));
universe@2 170 printf("\nListening for client...\n");
universe@2 171 if (net_create(&server, settings.port)) {
universe@2 172 perror("Server creation failed");
universe@2 173 return cleanup(&settings, EXIT_FAILURE);
universe@2 174 }
universe@2 175
universe@2 176 if (net_listen(&server)) {
universe@2 177 perror("Listening for client failed");
universe@2 178 return cleanup(&settings, EXIT_FAILURE);
universe@2 179 }
universe@2 180
universe@2 181 printf("Client connected - transmitting gameinfo...\n");
universe@2 182 net_send(server.client->fd, NETCODE_GAMEINFO,
universe@2 183 &(settings.gameinfo), sizeof(settings.gameinfo));
universe@2 184 } else {
universe@1 185 if (net_find(&server, settings.serverhost, settings.port)) {
universe@1 186 perror("Can't find server");
universe@1 187 return cleanup(&settings, EXIT_FAILURE);
universe@1 188 }
universe@1 189
universe@1 190 if (net_connect(&server)) {
universe@1 191 perror("Can't connect to server");
universe@1 192 return cleanup(&settings, EXIT_FAILURE);
universe@1 193 }
universe@2 194
universe@2 195 printf("Connection established!\n\n");
universe@2 196 if (net_recieve_code(server.fd) == NETCODE_GAMEINFO) {
universe@2 197 net_recieve_data(server.fd, &(settings.gameinfo),
universe@2 198 sizeof(settings.gameinfo));
universe@2 199 dump_gameinfo(&(settings.gameinfo));
universe@2 200 } else {
universe@2 201 fprintf(stderr, "Server sent invalid gameinfo.\n");
universe@1 202 }
universe@1 203 }
universe@1 204
universe@1 205 return cleanup(&settings, EXIT_SUCCESS);
universe@0 206 }
universe@0 207

mercurial