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