source: fact/FADctrl/FADctrl.cc@ 10077

Last change on this file since 10077 was 10077, checked in by neise, 12 years ago
choose sock0 or sock1..7 via soc command - see help
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 21000");
51 DimClient::sendCommand(SERVER_NAME"/Command", "dac 1 0");
52 DimClient::sendCommand(SERVER_NAME"/Command", "dac 2-3 5000");
53 DimClient::sendCommand(SERVER_NAME"/Command", "dac 4-7 28800");
54 sleep (1);
55
56 DimClient::sendCommand(SERVER_NAME"/Command", "roi all 10");
57 DimClient::sendCommand(SERVER_NAME"/Command", "address 44 29");
58 sleep (1);
59
60 DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
61 sleep (1);
62
63 DimClient::sendCommand(SERVER_NAME"/Command", "address 44 30");
64 sleep (1);
65
66 DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
67 sleep (1);
68
69 DimClient::sendCommand(SERVER_NAME"/Command", "address 44 0");
70 sleep (1);
71
72 DimClient::sendCommand(SERVER_NAME"/Command", "trigger");
73 DimClient::sendCommand(SERVER_NAME"/Command", "domino on");
74 DimClient::sendCommand(SERVER_NAME"/Command", "dwrite on");
75 DimClient::sendCommand(SERVER_NAME"/Command", "roi all 1024");
76 sleep (1);
77
78 //EmptySockets(SocketDescriptor, 8, 750000L);
79 M.PrintMessage("Finished initalizing all boards\n");
80
81 // Command loop
82 char *Command;
83 std::string LastHist;
84
85 while (!M.ExitRequest) {
86 Command = readline("\rFADctrl>");
87
88 // Check for interruption by signal
89 if (Command == NULL) continue;
90
91 // Add command to history
92 if(strlen(Command) > 0 && LastHist != Command) {
93 add_history(Command);
94 LastHist = Command;
95 }
96
97 // Process command
98 DimClient::sendCommand(SERVER_NAME"/Command", Command);
99 free(Command);
100 }
101
102 // Save history buffer
103 int Ret = write_history(READLINE_HIST_FILE);
104 if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE, strerror(Ret));
105
106 // Terminate thread for other sockets
107 if ((Ret = pthread_cancel(Thread)) != 0) printf("Error: Could not request thread cancellation (%s)\n", strerror(Ret));
108 if ((Ret = pthread_join(Thread, NULL)) != 0) printf("pthread_join() failed (%s)\n", strerror(Ret));
109}
110
111
112// ====================
113// OpenOtherSockets()
114// ====================
115
116void OpenOtherSockets() {
117
118 //ETHZ
119 //static char Hostname[] = "192.33.99.225";
120 //TUDO
121 static char Hostname[] = "129.217.160.119";
122 int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
123 int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
124 fd_set DescriptorList;
125 char Buffer[1000000];
126
127 // Resolve hostname
128 struct hostent *Host = gethostbyname(Hostname);
129 if (Host == 0) {
130 printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
131 return;
132 }
133
134 // Connect to server
135 struct sockaddr_in SocketAddress;
136 SocketAddress.sin_family = PF_INET;
137 SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
138
139 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
140 // Open socket descriptor
141 if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
142 printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
143 return;
144 }
145
146 // Determine highest socket number for select()
147 if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
148
149 // Connect to server
150 SocketAddress.sin_port = htons((unsigned short) List[i]);
151 if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
152 printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
153 return;
154 }
155 }
156
157 while(true) {
158 // Wait for data from terminal (stdin) or from sockets
159 FD_ZERO(&DescriptorList);
160 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
161 if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
162 perror("OtherSockets: Error with select()");
163 break;
164 }
165
166 // Data from socket
167 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
168 Ret = read(Socket[i], Buffer, sizeof(Buffer));
169 if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
170 else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
171 else printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
172 }
173 }
174
175 // Close all sockets
176 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
177 if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
178 printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));
179 }
180 }
181}
Note: See TracBrowser for help on using the repository browser.