source: tools/SkyQualityMonitor/sqm.cpp@ 124

Last change on this file since 124 was 96, checked in by ogrimm, 15 years ago
Changed cast for usleep()
File size: 4.5 KB
Line 
1/********************************************************************\
2
3 Interface to the sky quality monitor (essentially a socket interface)
4
5 Writes slow data at given rate. Terminate with CTRL-c.
6
7 Oliver Grimm, July 2009
8
9\********************************************************************/
10
11#include <stdlib.h>
12#include <string.h>
13#include <iostream>
14#include <sys/socket.h>
15#include <netdb.h>
16#include <signal.h>
17
18#include "../../drsdaq/SlowData.h"
19
20#define PERIOD 10 // Measurement period (integer)
21#define READ_CMD "rx" // Command to read from device
22#define BUF_SIZE 1000 // Read buffer size
23
24int SocketDescriptor; // Global to be accessible by exit handler
25SlowData *SlowDataClass;
26
27// Close socket descriptor when CTRL-c pressed
28void ExitHandler(int Signal) {
29 if (!close(SocketDescriptor)) printf("Connection closed.\n");
30 delete SlowDataClass;
31 exit(EXIT_SUCCESS);
32}
33
34// Main program
35int main(int argc, char *argv[]) {
36
37 char Buffer[BUF_SIZE],ServerName[BUF_SIZE]="sqm.ethz.ch";
38 char SlowDir[BUF_SIZE]="/ct3data/SlowData";
39 int DAQPort = 10001, Period = PERIOD;
40 struct sockaddr_in SocketAddress;
41 fd_set ReadFileDescriptor;
42 time_t Time;
43
44 if (argc != 5) {
45 printf("+++ Sky Quality Monitor Interface +++\n");
46 printf("Usage: %s <server name> <port> <slow data dir> <period (sec)>\n", argv[0]);
47 printf("Taking defaults: <%s> <%d> <%s> <%d>\n", ServerName, DAQPort, SlowDir, Period);
48 }
49 else {
50 snprintf(ServerName, sizeof(ServerName), argv[1]);
51 snprintf(SlowDir, sizeof(SlowDir), argv[3]);
52 DAQPort = atoi(argv[2]);
53 Period = atoi(argv[4]);
54 }
55
56 // Open socket descriptor
57 if ((SocketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
58 printf("Error: Could not open socket.");
59 exit(EXIT_FAILURE);
60 }
61
62 // Resolve hostname and try to connect to server
63 struct hostent *hostent = gethostbyname(ServerName);
64 if (hostent==0) {
65 printf("\nCould not resolve host name.\n");
66 close(SocketDescriptor);
67 exit(EXIT_FAILURE);
68 }
69
70 SocketAddress.sin_family = PF_INET;
71 SocketAddress.sin_port = htons((unsigned short) DAQPort);
72 SocketAddress.sin_addr = *(struct in_addr*) hostent->h_addr;
73
74 if (connect(SocketDescriptor, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress))==-1) {
75 printf("Error: Could not connect to server %s (port %d)\n", ServerName, DAQPort);
76 close(SocketDescriptor);
77 exit(EXIT_FAILURE);
78 }
79 printf("\nConnected to %s (port %d)\n", ServerName, DAQPort);
80 signal(SIGPIPE,SIG_IGN); // Do not kill process if writing to closed socket
81 signal(SIGINT, &ExitHandler);
82
83 // Create instance of slow data class for monitoring data
84 SlowDataClass = new SlowData("SQM", SlowDir);
85 if(SlowDataClass->ErrorCode != 0) {
86 printf("Error: Could not open slow data file in %s (%s)\n", SlowDir, strerror(SlowDataClass->ErrorCode));
87 exit(EXIT_FAILURE);
88 }
89 SlowDataClass->NewEntry("Data-Info", "Data string as returned by sqm. Commata and the letter 'm' are replaced by whitespaces");
90
91 while(true) {
92 // Write read command to socket
93 if ((write(SocketDescriptor, READ_CMD, strlen(READ_CMD)))<1) {
94 printf("Error: Could not write read command to socket.\n");
95 }
96 usleep((int) (Period*1e6/2)); // Wait half period here to allow for response
97
98 // Wait for data from socket with time-out
99 FD_ZERO(&ReadFileDescriptor);
100 FD_SET(SocketDescriptor, &ReadFileDescriptor);
101 struct timeval WaitTime = {Period, 0};
102 if (select(((int) SocketDescriptor)+1, &ReadFileDescriptor, NULL, NULL, &WaitTime)==-1) {
103 perror("Error with select()");
104 break;
105 }
106
107 if (!FD_ISSET(SocketDescriptor, &ReadFileDescriptor)) {
108 printf("Time-out of %d seconds expired before receiving response from socket.\n", PERIOD);
109 continue;
110 }
111
112 memset(Buffer, 0, BUF_SIZE);
113 if(read(SocketDescriptor, Buffer, BUF_SIZE)==0) {
114 printf("Server not existing anymore, exiting...\n");
115 break;
116 }
117
118 // Remove letters and commata from string before writing as slow data
119 for (int i=0; i<strlen(Buffer); i++) {
120 if (Buffer[i]=='m' || Buffer[i]==',' || iscntrl(Buffer[i])) Buffer[i] = ' ';
121 }
122 SlowDataClass->NewEntry("Data", Buffer);
123 if(SlowDataClass->ErrorCode != 0) {
124 printf("Error: Could not write to slow data file (%s)\n", strerror(SlowDataClass->ErrorCode));
125 break;
126 }
127 Time = time(NULL);
128 printf("\r%s %s CTRL-c to exit.",Buffer, asctime(localtime(&Time)));
129 fflush(stdout);
130 usleep((int) (Period*1e6/2)); // Wait second half period here
131
132 } // while()
133
134 ExitHandler(0);
135}
Note: See TracBrowser for help on using the repository browser.