|
@@ -4,6 +4,7 @@
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
|
|
*/
|
|
|
|
|
|
+#include <signal.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <stdint.h>
|
|
|
#include <unistd.h>
|
|
@@ -28,6 +29,30 @@ typedef struct {
|
|
|
sync_information sync;
|
|
|
} sync_ack;
|
|
|
|
|
|
+/* global variables */
|
|
|
+char * filepath;
|
|
|
+uint8_t file_closed = -1;
|
|
|
+wavfile * file;
|
|
|
+int sock;
|
|
|
+
|
|
|
+/*
|
|
|
+ * Function: signal_handler
|
|
|
+ * ------------------------
|
|
|
+ * handles TERM and INT to ensure an orderly shutdown
|
|
|
+ *
|
|
|
+ * signal: the number of the signal sent
|
|
|
+ */
|
|
|
+static void signal_handler(int signal) {
|
|
|
+ printf("signal %d received, shutting down\n", signal);
|
|
|
+ if (!file_closed) {
|
|
|
+ wavfile_close(file);
|
|
|
+ }
|
|
|
+ free(filepath);
|
|
|
+ shutdown(sock, SHUT_RDWR);
|
|
|
+ close(sock);
|
|
|
+ exit(EXIT_SUCCESS);
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
* Function: construct_file_path
|
|
|
* ----------------------------------------
|
|
@@ -65,13 +90,11 @@ int main(int argc, char **argv) {
|
|
|
char * ip_address = NULL;
|
|
|
char * directory = NULL;
|
|
|
uint16_t port = 0;
|
|
|
- int opt, sock, conn, bytes_received;
|
|
|
+ int opt, bytes_received, conn;
|
|
|
struct sockaddr_in client;
|
|
|
unsigned int namelen;
|
|
|
sync_information sync;
|
|
|
uint8_t buffer[BUF_SIZE];
|
|
|
- wavfile * file;
|
|
|
- char * file_path;
|
|
|
sync_ack ack_packet;
|
|
|
while ((opt = getopt(argc, argv, "i:p:d:")) != -1) {
|
|
|
switch(opt) {
|
|
@@ -90,12 +113,16 @@ int main(int argc, char **argv) {
|
|
|
printf("Usage: %s -i IPV4_ADDRESS -p PORT -d DIRECTORY\n", argv[0]);
|
|
|
return EXIT_SUCCESS;
|
|
|
}
|
|
|
+ struct sigaction action;
|
|
|
+ action.sa_handler = signal_handler;
|
|
|
+ sigaction(SIGTERM, &action,NULL);
|
|
|
+ sigaction(SIGINT, &action,NULL);
|
|
|
if ((sock = tcpserver_start(ip_address, port)) < 0 ) {
|
|
|
printf("Failed to start TCP server\n");
|
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
|
- file_path = malloc(strlen(directory) + 1 + 29);
|
|
|
- if (file_path == NULL) {
|
|
|
+ filepath = malloc(strlen(directory) + 1 + 29);
|
|
|
+ if (filepath == NULL) {
|
|
|
printf("Error allocating memory for file path\n");
|
|
|
return EXIT_FAILURE;
|
|
|
}
|
|
@@ -110,9 +137,10 @@ int main(int argc, char **argv) {
|
|
|
printf("Invalid sample rate received\n");
|
|
|
continue;
|
|
|
}
|
|
|
- construct_file_path(directory, file_path);
|
|
|
- printf("Current file path: %s\n", file_path);
|
|
|
- file = wavfile_create(file_path, 1, sync.sample_rate, sync.bits);
|
|
|
+ construct_file_path(directory, filepath);
|
|
|
+ printf("Current file path: %s\n", filepath);
|
|
|
+ file = wavfile_create(filepath, 1, sync.sample_rate, sync.bits);
|
|
|
+ file_closed = 0;
|
|
|
strncpy(ack_packet.ack,"ACK", 4);
|
|
|
ack_packet.sync = sync;
|
|
|
send(conn, &ack_packet, sizeof(ack_packet), 0);
|
|
@@ -125,6 +153,7 @@ int main(int argc, char **argv) {
|
|
|
wavfile_append_data(file, buffer, bytes_received);
|
|
|
}
|
|
|
wavfile_close(file);
|
|
|
+ file_closed = 1;
|
|
|
}
|
|
|
return EXIT_SUCCESS;
|
|
|
}
|