diff options
Diffstat (limited to 'utils/hwstub/tools/hwstub_server.cpp')
-rw-r--r-- | utils/hwstub/tools/hwstub_server.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/utils/hwstub/tools/hwstub_server.cpp b/utils/hwstub/tools/hwstub_server.cpp new file mode 100644 index 0000000000..6cb8010897 --- /dev/null +++ b/utils/hwstub/tools/hwstub_server.cpp | |||
@@ -0,0 +1,127 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2016 by Amaury Pouly | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <cstdio> | ||
22 | #include <thread> | ||
23 | #include <chrono> | ||
24 | #include <cstring> | ||
25 | #include <iostream> | ||
26 | #include "hwstub.hpp" | ||
27 | #include "hwstub_usb.hpp" | ||
28 | #include "hwstub_uri.hpp" | ||
29 | #include "hwstub_net.hpp" | ||
30 | #include <signal.h> | ||
31 | #include <getopt.h> | ||
32 | |||
33 | /* capture CTRL+C */ | ||
34 | volatile sig_atomic_t g_exit_loop = 0; | ||
35 | |||
36 | void do_signal(int sig) | ||
37 | { | ||
38 | g_exit_loop = 1; | ||
39 | } | ||
40 | |||
41 | std::shared_ptr<hwstub::context> g_ctx; | ||
42 | std::shared_ptr<hwstub::net::server> g_srv; | ||
43 | |||
44 | int usage() | ||
45 | { | ||
46 | printf("usage: hwstub_server [options]\n"); | ||
47 | printf(" --help/-h Display this help\n"); | ||
48 | printf(" --verbose/-v Verbose output\n"); | ||
49 | printf(" --context/-c <uri> Context URI (see below)\n"); | ||
50 | printf(" --server/-s <uri> Server URI (see below)\n"); | ||
51 | printf("\n"); | ||
52 | hwstub::uri::print_usage(stdout, true, true); | ||
53 | return 1; | ||
54 | } | ||
55 | |||
56 | int main(int argc, char **argv) | ||
57 | { | ||
58 | hwstub::uri::uri ctx_uri = hwstub::uri::default_uri(); | ||
59 | hwstub::uri::uri srv_uri = hwstub::uri::default_server_uri(); | ||
60 | bool verbose = false; | ||
61 | |||
62 | while(1) | ||
63 | { | ||
64 | static struct option long_options[] = | ||
65 | { | ||
66 | {"help", no_argument, 0, 'h'}, | ||
67 | {"verbose", no_argument, 0, 'v'}, | ||
68 | {"context", required_argument, 0, 'c'}, | ||
69 | {"server", required_argument, 0, 's'}, | ||
70 | {0, 0, 0, 0} | ||
71 | }; | ||
72 | |||
73 | int c = getopt_long(argc, argv, "hvc:s:", long_options, NULL); | ||
74 | if(c == -1) | ||
75 | break; | ||
76 | switch(c) | ||
77 | { | ||
78 | case -1: | ||
79 | break; | ||
80 | case 'v': | ||
81 | verbose = true; | ||
82 | break; | ||
83 | case 'h': | ||
84 | return usage(); | ||
85 | case 'c': | ||
86 | ctx_uri = hwstub::uri::uri(optarg); | ||
87 | break; | ||
88 | case 's': | ||
89 | srv_uri = hwstub::uri::uri(optarg); | ||
90 | break; | ||
91 | default: | ||
92 | abort(); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | if(optind != argc) | ||
97 | return usage(); | ||
98 | |||
99 | /* intercept CTRL+C */ | ||
100 | signal(SIGINT, do_signal); | ||
101 | |||
102 | std::string error; | ||
103 | g_ctx = hwstub::uri::create_context(ctx_uri, &error); | ||
104 | if(!g_ctx) | ||
105 | { | ||
106 | printf("Cannot create context: %s\n", error.c_str()); | ||
107 | return 1; | ||
108 | } | ||
109 | g_ctx->start_polling(); | ||
110 | |||
111 | g_srv = hwstub::uri::create_server(g_ctx, srv_uri, &error); | ||
112 | if(!g_srv) | ||
113 | { | ||
114 | printf("Cannot create server: %s\n", error.c_str()); | ||
115 | return 1; | ||
116 | } | ||
117 | if(verbose) | ||
118 | g_srv->set_debug(std::cout); | ||
119 | |||
120 | while(!g_exit_loop) | ||
121 | std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
122 | printf("Shutting down...\n"); | ||
123 | g_srv.reset(); /* will cleanup */ | ||
124 | g_ctx.reset(); /* will cleanup */ | ||
125 | |||
126 | return 0; | ||
127 | } | ||