source: fact/FADctrl/FADctrl.cc@ 10099

Last change on this file since 10099 was 10099, checked in by ogrimm, 14 years ago
Corrupt events (wrong start-package flag) are removed from internal buffer
File size: 5.5 KB
Line 
1//
2// FADctrl
3//
4
5#include <stdio.h>
6#include <readline/history.h>
7
8#include "FAD.h"
9
10const char READLINE_HIST_FILE[] = "/tmp/.history.FADctrl";
11
12void OpenOtherSockets();
13
14// ================
15// Main program
16// ================
17
18int main() {
19
20 // Uses getc() for readline library (allows interruption by signal)
21 rl_getc_function = getc;
22
23 // Load history buffer
24 read_history(READLINE_HIST_FILE);
25
26 system("clear");
27 printf("\n*** FADctrl (built %s, %s, revision %s) *** \n\n",__DATE__, __TIME__, REVISION);
28
29 // Construct main instance (static ensures destructor is called with exit())
30 static FAD M;
31
32 // Do not kill process if writing to closed socket
33 signal(SIGPIPE,SIG_IGN);
34
35 // Create thread to connect to other sockets
36 int RetVal;
37 pthread_t Thread;
38 if ((RetVal = pthread_create(&Thread, NULL, (void * (*)(void *)) OpenOtherSockets, NULL)) != 0) {
39 printf("pthread_create() failed for OpenOtherSockets(%s)\n", strerror(RetVal));
40 exit(EXIT_FAILURE);
41 }
42
43 // Initialise all boards
44 M.PrintMessage("Initalizing all boards...\n");
45
46 DimClient::sendCommand(SERVER_NAME"/Command", "dwrite off");
47 DimClient::sendCommand(SERVER_NAME"/Command", "domino off");
48 sleep(1);
49
50 DimClient::sendCommand(SERVER_NAME"/Command", "dac 0 25000");
51 DimClient::sendCommand(SERVER_NAME"/Command", "dac 1-3 0");
52 DimClient::sendCommand(SERVER_NAME"/Command", "dac 4-7 28800");
53 sleep (1);
54
55 //DimClient::sendCommand(SERVER_NAME"/Command", "roi all 10");
56 //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 29");
57 //sleep (1);
58
59 //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
60 //sleep (1);
61
62 //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 30");
63 //sleep (1);
64
65 //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
66 //sleep (1);
67
68 //DimClient::sendCommand(SERVER_NAME"/Command", "address 44 0");
69 //sleep (1);
70
71 //DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
72 DimClient::sendCommand(SERVER_NAME"/Command", "domino on");
73 DimClient::sendCommand(SERVER_NAME"/Command", "dwrite on");
74 DimClient::sendCommand(SERVER_NAME"/Command", "roi all 1024");
75 sleep (1);
76
77 //EmptySockets(SocketDescriptor, 8, 750000L);
78 M.PrintMessage("Finished initalizing all boards\n");
79
80 // Command loop
81 char *Command;
82 std::string LastHist;
83
84 while (!M.ExitRequest) {
85 Command = readline("\rFADctrl>");
86
87 // Check for interruption by signal
88 if (Command == NULL) continue;
89
90 // Add command to history
91 if(strlen(Command) > 0 && LastHist != Command) {
92 add_history(Command);
93 LastHist = Command;
94 }
95
96 // Process command
97 DimClient::sendCommand(SERVER_NAME"/Command", Command);
98 free(Command);
99 }
100
101 // Save history buffer
102 int Ret = write_history(READLINE_HIST_FILE);
103 if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE, strerror(Ret));
104
105 // Terminate thread for other sockets
106 if ((Ret = pthread_cancel(Thread)) != 0) printf("Error: Could not request thread cancellation in main() (%s)\n", strerror(Ret));
107 if ((Ret = pthread_join(Thread, NULL)) != 0) printf("pthread_join() failed in main () (%s)\n", strerror(Ret));
108}
109
110
111// ====================
112// OpenOtherSockets()
113// ====================
114
115void OpenOtherSockets() {
116
117 //ETHZ
118 static char Hostname[] = "192.33.99.225";
119 //TUDO
120 //static char Hostname[] = "129.217.160.119";
121 int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
122 int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
123 fd_set DescriptorList;
124 char Buffer[1000000];
125
126 // Resolve hostname
127 struct hostent *Host = gethostbyname(Hostname);
128 if (Host == 0) {
129 printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
130 return;
131 }
132
133 // Connect to server
134 struct sockaddr_in SocketAddress;
135 SocketAddress.sin_family = PF_INET;
136 SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
137
138 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
139 // Open socket descriptor
140 if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
141 printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
142 return;
143 }
144
145 // Determine highest socket number for select()
146 if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
147
148 // Connect to server
149 SocketAddress.sin_port = htons((unsigned short) List[i]);
150 if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
151 printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
152 return;
153 }
154 }
155
156 while(true) {
157 // Wait for data from terminal (stdin) or from sockets
158 FD_ZERO(&DescriptorList);
159 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
160 if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
161 perror("OtherSockets: Error with select()");
162 break;
163 }
164
165 // Data from socket
166 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
167 Ret = read(Socket[i], Buffer, sizeof(Buffer));
168 if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
169 else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
170 else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
171 }
172 }
173
174 // Close all sockets
175 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
176 if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
177 printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));
178 }
179 }
180}
Note: See TracBrowser for help on using the repository browser.