| 1 | //
|
|---|
| 2 | // FADctrl
|
|---|
| 3 | //
|
|---|
| 4 |
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <readline/history.h>
|
|---|
| 7 |
|
|---|
| 8 | #include "FAD.h"
|
|---|
| 9 |
|
|---|
| 10 | const char READLINE_HIST_FILE[] = "/tmp/.history.FADctrl";
|
|---|
| 11 |
|
|---|
| 12 | void OpenOtherSockets();
|
|---|
| 13 |
|
|---|
| 14 | // ================
|
|---|
| 15 | // Main program
|
|---|
| 16 | // ================
|
|---|
| 17 |
|
|---|
| 18 | int 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 |
|
|---|
| 116 | void OpenOtherSockets() {
|
|---|
| 117 |
|
|---|
| 118 | static char Hostname[] = "192.33.99.225";
|
|---|
| 119 | int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
|
|---|
| 120 | int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret;
|
|---|
| 121 | fd_set DescriptorList;
|
|---|
| 122 | char Buffer[1000000];
|
|---|
| 123 |
|
|---|
| 124 | // Resolve hostname
|
|---|
| 125 | struct hostent *Host = gethostbyname(Hostname);
|
|---|
| 126 | if (Host == 0) {
|
|---|
| 127 | printf("OtherSockets: Could not resolve host name for %s\n", Hostname);
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | // Connect to server
|
|---|
| 132 | struct sockaddr_in SocketAddress;
|
|---|
| 133 | SocketAddress.sin_family = PF_INET;
|
|---|
| 134 | SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
|
|---|
| 135 |
|
|---|
| 136 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
|
|---|
| 137 | // Open socket descriptor
|
|---|
| 138 | if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
|
|---|
| 139 | printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
|
|---|
| 140 | return;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | // Determine highest socket number for select()
|
|---|
| 144 | if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i];
|
|---|
| 145 |
|
|---|
| 146 | // Connect to server
|
|---|
| 147 | SocketAddress.sin_port = htons((unsigned short) List[i]);
|
|---|
| 148 | if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
|
|---|
| 149 | printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
|
|---|
| 150 | return;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | while(true) {
|
|---|
| 155 | // Wait for data from terminal (stdin) or from sockets
|
|---|
| 156 | FD_ZERO(&DescriptorList);
|
|---|
| 157 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
|
|---|
| 158 | if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
|
|---|
| 159 | perror("OtherSockets: Error with select()");
|
|---|
| 160 | break;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | // Data from socket
|
|---|
| 164 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
|
|---|
| 165 | Ret = read(Socket[i], Buffer, sizeof(Buffer));
|
|---|
| 166 | if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
|
|---|
| 167 | else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
|
|---|
| 168 | else printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]);
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | // Close all sockets
|
|---|
| 173 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
|
|---|
| 174 | if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
|
|---|
| 175 | printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | }
|
|---|