jsfw/main.c

67 lines
1.4 KiB
C
Raw Normal View History

2022-08-30 11:45:53 -05:00
#include "client.h"
2022-08-27 19:29:43 -05:00
#include "hid.h"
2022-08-29 17:27:03 -05:00
#include "server.h"
2022-08-30 08:37:34 -05:00
#include "util.h"
2022-08-27 19:29:43 -05:00
2022-08-30 17:54:56 -05:00
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
2022-08-30 11:45:53 -05:00
const char *USAGE[] = {
"jsfw client [address] [port]\n",
"jsfw server [port]\n",
2022-08-27 19:29:43 -05:00
};
2022-09-11 04:23:31 -05:00
// Start the server
2022-08-27 19:29:43 -05:00
void server(uint16_t port) {
printf("[Server (0.0.0.0:%u)]\n\n", port);
2022-08-27 19:29:43 -05:00
2022-08-30 08:37:34 -05:00
pthread_t thread;
pthread_create(&thread, NULL, hid_thread, NULL);
2022-08-29 17:27:03 -05:00
server_run(port);
}
2022-08-27 19:29:43 -05:00
2022-09-11 04:23:31 -05:00
// Start the client
2022-08-30 11:45:53 -05:00
void client(char *address, uint16_t port) {
2022-08-30 12:08:36 -05:00
printf("[Client (%s:%d)]\n\n", address, port);
2022-08-30 11:45:53 -05:00
client_run(address, port);
2022-08-27 19:29:43 -05:00
}
2022-08-30 11:45:53 -05:00
int main(int argc, char *argv[]) {
if (argc < 2) {
2022-08-27 19:29:43 -05:00
printf("Usage: %s", USAGE[0]);
printf(" %s", USAGE[1]);
return 1;
}
2022-08-30 11:45:53 -05:00
char *mode = argv[1];
2022-08-27 19:29:43 -05:00
2022-08-30 11:45:53 -05:00
if (strcmp(mode, "server") == 0) {
2022-09-11 04:23:31 -05:00
if (argc < 3) {
2022-08-27 19:29:43 -05:00
panicf("Usage: %s", USAGE[1]);
2022-09-11 04:23:31 -05:00
}
2022-08-27 19:29:43 -05:00
uint16_t port = parse_port(argv[2]);
server(port);
2022-08-30 11:45:53 -05:00
} else if (strcmp(mode, "client") == 0) {
2022-09-11 04:23:31 -05:00
if (argc < 4) {
2022-08-27 19:29:43 -05:00
panicf("Usage: %s", USAGE[0]);
2022-09-11 04:23:31 -05:00
}
2022-08-27 19:29:43 -05:00
2022-08-30 11:45:53 -05:00
char *address = argv[2];
uint16_t port = parse_port(argv[3]);
2022-08-29 17:27:03 -05:00
client(address, port);
2022-08-27 19:29:43 -05:00
} else {
printf("Unknown mode: '%s'\n", mode);
printf("Usage: %s", USAGE[0]);
printf(" %s", USAGE[1]);
return 1;
}
return 0;
}