1 | //
|
---|
2 | // FADctrl
|
---|
3 | //
|
---|
4 |
|
---|
5 | #include <stdio.h>
|
---|
6 | #include <readline/history.h>
|
---|
7 | #include <string>
|
---|
8 |
|
---|
9 | #include "FAD.h"
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | const string READLINE_HIST_FILE = string(getenv("HOME"))+"/.history_FADctrl";
|
---|
14 | void OpenOtherSockets();
|
---|
15 |
|
---|
16 | // ================
|
---|
17 | // Main program
|
---|
18 | // ================
|
---|
19 |
|
---|
20 | int 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 | 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.c_str());
|
---|
103 | if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE.c_str(), 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 |
|
---|
115 | void 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 | }
|
---|