universe@0: /* universe@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@0: * universe@0: * Copyright 2014 Mike Becker. All rights reserved. universe@0: * universe@0: * Redistribution and use in source and binary forms, with or without universe@0: * modification, are permitted provided that the following conditions are met: universe@0: * universe@0: * 1. Redistributions of source code must retain the above copyright universe@0: * notice, this list of conditions and the following disclaimer. universe@0: * universe@0: * 2. Redistributions in binary form must reproduce the above copyright universe@0: * notice, this list of conditions and the following disclaimer in the universe@0: * documentation and/or other materials provided with the distribution. universe@0: * universe@0: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@0: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@0: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@0: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@0: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@0: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@0: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@0: * POSSIBILITY OF SUCH DAMAGE. universe@0: * universe@0: */ universe@0: universe@1: #include "terminal-chess.h" universe@1: #include universe@1: universe@1: int get_settings(int argc, char **argv, Settings *settings) { universe@1: char *valid; universe@1: universe@1: for (char opt ; (opt = getopt(argc, argv, "hp:")) != -1 ;) { universe@1: switch (opt) { universe@1: case 'p': universe@1: if (strtol(optarg, &valid, 10) < 1025 || *valid != '\0') { universe@1: fprintf(stderr, universe@1: "Invalid port number (%s) - choose a number > 1024\n", universe@1: optarg); universe@1: return 1; universe@1: } else { universe@1: settings->port = optarg; universe@1: } universe@1: break; universe@1: case 'h': universe@1: case '?': universe@1: settings->printhelp = 1; universe@1: break; universe@1: } universe@1: } universe@1: universe@1: if (optind == argc - 1) { universe@1: settings->serverhost = argv[optind]; universe@1: } else if (optind < argc - 1) { universe@1: fprintf(stderr, "Too many arguments\n"); universe@1: return 1; universe@1: } universe@1: universe@1: return 0; universe@1: } universe@1: universe@1: Settings default_settings() { universe@1: Settings settings; universe@1: memset(&settings, 0, sizeof(Settings)); universe@1: settings.port = "27015"; universe@1: return settings; universe@1: } universe@1: universe@1: int cleanup(Settings *settings, int exitcode) { universe@1: if (settings->server) { universe@1: if (net_destroy(settings->server)) { universe@1: perror("Server shutdown failed"); universe@1: } universe@1: } universe@1: universe@1: return exitcode; universe@1: } universe@0: universe@0: int main(int argc, char **argv) { universe@1: universe@1: Settings settings = default_settings(); universe@1: get_settings(argc, argv, &settings); universe@1: universe@1: if (settings.printhelp) { universe@1: printf( universe@1: "Usage: %s [OPTION]... [HOST]\n" universe@1: "Starts/joins a network chess game\n\n" universe@1: " -h This help page\n" universe@1: " -p TCP port to use (default: 27015)\n", universe@1: argv[0]); universe@1: return EXIT_SUCCESS; universe@1: } universe@1: universe@1: Server server; universe@1: settings.server = &server; universe@1: universe@1: if (is_server(&settings)) { universe@1: if (net_find(&server, settings.serverhost, settings.port)) { universe@1: perror("Can't find server"); universe@1: return cleanup(&settings, EXIT_FAILURE); universe@1: } universe@1: universe@1: if (net_connect(&server)) { universe@1: perror("Can't connect to server"); universe@1: return cleanup(&settings, EXIT_FAILURE); universe@1: } universe@1: } else { universe@1: if (net_create(&server, settings.port)) { universe@1: perror("Server creation failed"); universe@1: return cleanup(&settings, EXIT_FAILURE); universe@1: } universe@1: universe@1: if (net_listen(&server)) { universe@1: perror("Listening for client failed"); universe@1: return cleanup(&settings, EXIT_FAILURE); universe@1: } universe@1: } universe@1: universe@1: return cleanup(&settings, EXIT_SUCCESS); universe@0: } universe@0: