source: fact/FADctrl/FADctrl.cc@ 10103

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