test-JAUS.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "Util/log.H"
00034 #include <signal.h>
00035 #include <stdlib.h>
00036 #include <sys/types.h>
00037 #include <unistd.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <unistd.h>
00041 #include <netdb.h>
00042 #include <fcntl.h>
00043 #include <arpa/inet.h>
00044 #include <stdlib.h>
00045 #include <ctype.h>
00046
00047
00048 #define JAUSCC_PORT 3794
00049
00050
00051 #define BUFLEN 9500
00052
00053
00054
00055
00056 int main(const int argc, const char **argv)
00057 {
00058
00059 sigset_t sset; sigemptyset(&sset);
00060 sigaddset(&sset, SIGHUP); sigaddset(&sset, SIGPIPE);
00061 int s = sigprocmask(SIG_BLOCK, &sset, NULL);
00062 if (s != 0) PLERROR("Sigprocmask failed");
00063
00064 while(1)
00065 {
00066
00067 struct servent *sv = getservbyname("jauscc", "udp"); int po;
00068 if (sv == NULL)
00069 {
00070 LERROR("jauscc/udp not configured in /etc/services");
00071 po = JAUSCC_PORT; LERROR("Using default port %d", po);
00072 }
00073 else
00074 po = ntohs(sv->s_port);
00075 LINFO("Start. Listening to port %d", po);
00076
00077
00078 int sock = socket(AF_INET, SOCK_DGRAM, 0);
00079 if (sock == -1)
00080 { PLERROR("Cannot create server socket"); sleep(30); continue; }
00081
00082 struct sockaddr_in addr;
00083 addr.sin_family = AF_INET;
00084 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00085 addr.sin_port = htons(po);
00086 if (bind(sock, (struct sockaddr*)(&addr), sizeof(addr)) == -1)
00087 {
00088 PLERROR("Cannot bind server socket"); close(sock);
00089 sleep(30); continue;
00090 }
00091
00092 char buf[BUFLEN]; bool running = true;
00093 while(running)
00094 {
00095
00096 struct sockaddr_in fromaddr; unsigned int fromlen = sizeof(fromaddr);
00097 int ret = recvfrom(sock, buf, BUFLEN, 0,
00098 (struct sockaddr *)(&fromaddr), &fromlen);
00099 LINFO("Received message %d bytes from %s:%d", ret,
00100 inet_ntoa(fromaddr.sin_addr), ntohs(fromaddr.sin_port));
00101
00102
00103 if (strncmp(buf, "JAUS01.0", 8))
00104 { LERROR("Missing JAUS01.0 header -- SKIPPING"); continue; }
00105
00106
00107
00108
00109
00110
00111 }
00112
00113 close(sock);
00114 LERROR("Cooling off for 10 seconds..."); sleep(10);
00115 }
00116 exit(1);
00117 }