src/main.c

Wed, 05 Feb 2014 14:07:43 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 05 Feb 2014 14:07:43 +0100
changeset 1
e5fbb8f9edbe
parent 0
98034084033f
child 2
0a08f79c320d
permissions
-rw-r--r--

added (single) client / server architecture

     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 <string.h>
    33 int get_settings(int argc, char **argv, Settings *settings) {
    34     char *valid;
    36     for (char opt ; (opt = getopt(argc, argv, "hp:")) != -1 ;) {
    37         switch (opt) {
    38             case 'p':
    39                 if (strtol(optarg, &valid, 10) < 1025 || *valid != '\0') {
    40                     fprintf(stderr,
    41                         "Invalid port number (%s) - choose a number > 1024\n",
    42                         optarg);
    43                     return 1;
    44                 } else {
    45                     settings->port = optarg;
    46                 }
    47                 break;
    48             case 'h':
    49             case '?':
    50                 settings->printhelp = 1;
    51                 break;
    52         }
    53     }
    55     if (optind == argc - 1) {
    56         settings->serverhost = argv[optind];
    57     } else if (optind < argc - 1) {
    58         fprintf(stderr, "Too many arguments\n");
    59         return 1;
    60     }
    62     return 0;
    63 }
    65 Settings default_settings() {
    66     Settings settings;
    67     memset(&settings, 0, sizeof(Settings));
    68     settings.port = "27015";
    69     return settings;
    70 }
    72 int cleanup(Settings *settings, int exitcode) {
    73     if (settings->server) {
    74         if (net_destroy(settings->server)) {
    75             perror("Server shutdown failed");
    76         }
    77     }
    79     return exitcode;
    80 }
    82 int main(int argc, char **argv) {
    84     Settings settings = default_settings();
    85     get_settings(argc, argv, &settings);
    87     if (settings.printhelp) {
    88         printf(
    89             "Usage: %s [OPTION]... [HOST]\n"
    90             "Starts/joins a network chess game\n\n"
    91             "  -h            This help page\n"
    92             "  -p            TCP port to use (default: 27015)\n",
    93             argv[0]);
    94         return EXIT_SUCCESS;
    95     }
    97     Server server;
    98     settings.server = &server;
   100     if (is_server(&settings)) {
   101         if (net_find(&server, settings.serverhost, settings.port)) {
   102             perror("Can't find server");
   103             return cleanup(&settings, EXIT_FAILURE);
   104         }
   106         if (net_connect(&server)) {
   107             perror("Can't connect to server");
   108             return cleanup(&settings, EXIT_FAILURE);
   109         }
   110     } else {
   111         if (net_create(&server, settings.port)) {
   112             perror("Server creation failed");
   113             return cleanup(&settings, EXIT_FAILURE);
   114         }
   116         if (net_listen(&server)) {
   117             perror("Listening for client failed");
   118             return cleanup(&settings, EXIT_FAILURE);
   119         }
   120     }
   122     return cleanup(&settings, EXIT_SUCCESS);
   123 }

mercurial