Changeset 10120
- Timestamp:
- 01/27/11 08:13:14 (14 years ago)
- Location:
- fact/FADctrl
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/FADctrl/FAD.cc
r10117 r10120 68 68 69 69 // Construct boards 70 BoardList = Tokenize(GetConfig("BoardList","129.217.160.119")); 71 BoardList = Tokenize("192.33.99.225"); 70 BoardList = Tokenize(GetConfig("BoardList")); 72 71 73 72 for (unsigned int i=0; i<BoardList.size(); i++) { … … 85 84 int Ret; 86 85 if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchEventThread,(void *) this)) != 0) { 87 Message(ERROR, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret)); 88 Thread = pthread_self(); 86 Message(FATAL, "pthread_create() failed in FAD::FAD() (%s)", strerror(Ret)); 89 87 } 90 88 … … 105 103 106 104 // Wait for DIM service thread to quit 107 if (pthread_equal(Thread, pthread_self()) == 0) { 108 if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret)); 109 } 105 if ((Ret = pthread_join(Thread, NULL)) != 0) Message(ERROR, "pthread_join() failed in ~FAD() (%s)", strerror(Ret)); 110 106 111 107 // Delete all boards (cancels threads automatically) … … 114 110 delete Command; 115 111 delete ConsoleOut; 116 free(ConsoleText); 112 free(ConsoleText); 117 113 } 118 114 … … 208 204 } 209 205 210 if (Match(Parameter[1],"com")) PrintMessage(" all active boards switched to command mode - socket 0\n");211 else if (Match(Parameter[1],"daq")) PrintMessage(" all active boards switched to DAQ mode - socket 1..7\n");206 if (Match(Parameter[1],"com")) PrintMessage("All active boards switched to command mode - socket 0\n"); 207 else if (Match(Parameter[1],"daq")) PrintMessage("All active boards switched to DAQ mode - socket 1..7\n"); 212 208 else PrintUsage(); 213 209 } … … 283 279 } 284 280 285 if (Match(Parameter[1],"enable")) PrintMessage(" all active boards accept nowincoming triggers\n");286 else if (Match(Parameter[1],"disable")) PrintMessage(" no active board accepts any incoming trigger anymore.\n");281 if (Match(Parameter[1],"enable")) PrintMessage("All active boards accept incoming triggers\n"); 282 else if (Match(Parameter[1],"disable")) PrintMessage("No active board accepts incoming triggers\n"); 287 283 } 288 284 -
fact/FADctrl/FAD.h
r10117 r10120 48 48 int NumEvents; // Number of event taken 49 49 50 std::vector<std::string> BoardList;51 50 std::vector<class FADBoard *> Boards; 52 51 … … 88 87 int Pipe[2]; 89 88 int NumEventsRequested; // Number of events requested 89 std::vector<std::string> BoardList; 90 90 }; 91 91 -
fact/FADctrl/FADBoard.cc
r10117 r10120 23 23 ACalibTime = -1; 24 24 Status.Update.tv_sec = -1; 25 Thread = pthread_self(); // For checking in destructor26 25 27 26 Name = new char [Server.size()+1]; // Name in permanent memory for DIM service 28 27 strcpy(Name, Server.c_str()); 28 29 // Initialise mutex for synchronization 30 pthread_mutexattr_t Attr; 31 32 if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) { 33 m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret)); 34 } 35 if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) { 36 m->Message(m->FATAL, "pthread_mutex_init() failed (%s)", strerror(Ret)); 37 } 38 39 // Initialise condition variable for synchronization 40 if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) { 41 m->Message(m->FATAL, "pthread_cond_init() failed (%s)", strerror(Ret)); 42 } 29 43 30 44 // Resolve hostname … … 37 51 // Open socket descriptor 38 52 if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 39 m-> PrintMessage("Could not open socket for %s (%s)\n", Server.c_str(), strerror(errno));53 m->Message(m->ERROR, "Could not open socket for %s (%s)\n", Server.c_str(), strerror(errno)); 40 54 return; 41 55 } … … 62 76 DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0); 63 77 64 // Initialise mutex for synchronization65 pthread_mutexattr_t Attr;66 67 if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {68 m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));69 }70 if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {71 m->Message(m->ERROR, "pthread_mutex_init() failed (%s)", strerror(Ret));72 return;73 }74 75 // Initialise condition variable for synchronization76 if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {77 m->Message(m->ERROR, "pthread_cond_init() failed (%s)", strerror(Ret));78 return;79 }80 81 78 // Create thread that receives data 82 79 if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) { 83 m->Message(m->ERROR, "pthread_create() failed in FADBoard() (%s)\n", strerror(Ret)); 84 Thread = pthread_self(); 85 return; 80 m->Message(m->FATAL, "pthread_create() failed in FADBoard() (%s)", strerror(Ret)); 81 } 82 83 // Create thread to connect to other sockets 84 if ((Ret = pthread_create(&OtherThread, NULL, (void * (*)(void *)) OpenOtherSockets, (void *) Name)) != 0) { 85 m->Message(m->FATAL, "pthread_create() failed for OpenOtherSockets (%s)", strerror(Ret)); 86 86 } 87 87 … … 96 96 int Ret; 97 97 98 // Cancel thread (if it did not quit already) and wait for it to quit 99 if (pthread_equal(Thread, pthread_self()) == 0) { 100 if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret)); 101 if ((Ret = pthread_join(Thread, NULL)) != 0) m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret)); 98 // Avoid segmentation faults by chekcing for InitOK 99 if (InitOK) { 100 // Terminate thread for other sockets 101 if ((Ret = pthread_cancel(OtherThread)) != 0) { 102 m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() for OtherThread (%s)", strerror(Ret)); 103 } 104 if ((Ret = pthread_join(OtherThread, NULL)) != 0) { 105 m->Message(m->ERROR, "pthread_join() failed in ~FADBoard for OtherThread (%s)", strerror(Ret)); 106 } 107 108 // Cancel thread (if it did not quit already) and wait for it to quit 109 if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) { 110 m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret)); 111 } 112 if ((Ret = pthread_join(Thread, NULL)) != 0) { 113 m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret)); 114 } 115 116 delete DIM_Name; 117 delete DIM_ID; 118 delete DIM_Temp; 119 delete DIM_DAC; 120 delete DIM_ROI; 121 } 122 123 delete[] Name; 124 125 // Close socket descriptor 126 if ((Socket != -1) && (close(Socket) == -1)) { 127 m->PrintMessage("Could not close socket descriptor (%s)", strerror(errno)); 102 128 } 103 129 … … 108 134 109 135 // Delete mutex 110 if ( InitOK && ((Ret = pthread_mutex_destroy(&Mutex)) != 0)) {136 if ((Ret = pthread_mutex_destroy(&Mutex)) != 0) { 111 137 m->Message(m->ERROR, "pthread_mutex_destroy() failed in ~FADBoard (%s)", strerror(Ret)); 112 }113 114 delete DIM_Name;115 delete DIM_ID;116 delete DIM_Temp;117 delete DIM_DAC;118 delete DIM_ROI;119 delete[] Name;120 121 // Close socket descriptor122 if ((Socket != -1) && (close(Socket) == -1)) {123 m->PrintMessage("Could not close socket descriptor (%s)", strerror(errno));124 138 } 125 139 } … … 524 538 } 525 539 } 540 541 542 // 543 // OpenOtherSockets() 544 // 545 void FADBoard::OpenOtherSockets(char *Hostname) { 546 547 int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007}; 548 int Socket[sizeof(List)/sizeof(int)], MaxSocketNum=0, Ret; 549 fd_set DescriptorList; 550 char Buffer[1000000]; 551 552 // Resolve hostname 553 struct hostent *Host = gethostbyname(Hostname); 554 if (Host == 0) { 555 printf("OtherSockets: Could not resolve host name for %s\n", Hostname); 556 return; 557 } 558 559 // Connect to server 560 struct sockaddr_in SocketAddress; 561 SocketAddress.sin_family = PF_INET; 562 SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr; 563 564 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) { 565 // Open socket descriptor 566 if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 567 printf("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno)); 568 return; 569 } 570 571 // Determine highest socket number for select() 572 if (Socket[i] > MaxSocketNum) MaxSocketNum = Socket[i]; 573 574 // Connect to server 575 SocketAddress.sin_port = htons((unsigned short) List[i]); 576 if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) { 577 printf("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno)); 578 return; 579 } 580 } 581 582 while(true) { 583 // Wait for data from terminal (stdin) or from sockets 584 FD_ZERO(&DescriptorList); 585 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList); 586 if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) { 587 perror("OtherSockets: Error with select()"); 588 break; 589 } 590 591 // Data from socket 592 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) { 593 Ret = read(Socket[i], Buffer, sizeof(Buffer)); 594 if(Ret == 0) printf("OtherSockets: Connection to port %d not existing anymore\n", List[i]); 595 else if (Ret == -1) printf("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno)); 596 else ;//printf("OtherSockets: Read %d bytes from port %d\n", Ret, List[i]); 597 } 598 } 599 600 // Close all sockets 601 for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) { 602 if ((Socket[i] != -1) && (close(Socket[i]) == -1)) { 603 printf("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno)); 604 } 605 } 606 } -
fact/FADctrl/FADBoard.h
r10117 r10120 25 25 class FAD *m; 26 26 int Socket; 27 pthread_t Thread ;27 pthread_t Thread, OtherThread; 28 28 pthread_mutex_t Mutex; 29 DimService *DIM_Name; 30 DimService *DIM_ID; 31 DimService *DIM_Temp; 32 DimService *DIM_ROI; 33 DimService *DIM_DAC; 29 DimService *DIM_Name, *DIM_ID, *DIM_Temp, *DIM_ROI, *DIM_DAC; 34 30 35 31 void ReadLoop(); 36 32 static void LaunchThread(class FADBoard *); 33 static void OpenOtherSockets(char *); 37 34 38 35 public: -
fact/FADctrl/FADctrl.cc
r10103 r10120 12 12 13 13 const string READLINE_HIST_FILE = string(getenv("HOME"))+"/.history_FADctrl"; 14 void OpenOtherSockets();15 14 16 15 // ================ … … 20 19 int main() { 21 20 22 // Uses getc() for readline library (allows interruption by signal) 21 // Uses getc() for readline library (allows interruption by signal) and load history buffer 23 22 rl_getc_function = getc; 24 25 // Load history buffer26 23 read_history(READLINE_HIST_FILE.c_str()); 27 24 … … 35 32 signal(SIGPIPE,SIG_IGN); 36 33 37 // Create thread to connect to other sockets38 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 34 // Initialise all boards 46 M.PrintMessage("Initalizing all boards...\n");47 48 35 DimClient::sendCommand(SERVER_NAME"/Command", "dwrite off"); 49 36 DimClient::sendCommand(SERVER_NAME"/Command", "domino off"); 50 sleep(1);51 52 37 DimClient::sendCommand(SERVER_NAME"/Command", "dac 0 25000"); 53 38 DimClient::sendCommand(SERVER_NAME"/Command", "dac 1-3 0"); 54 39 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 40 DimClient::sendCommand(SERVER_NAME"/Command", "domino on"); 75 41 DimClient::sendCommand(SERVER_NAME"/Command", "dwrite on"); 76 42 DimClient::sendCommand(SERVER_NAME"/Command", "roi all 1024"); 77 78 43 DimClient::sendCommand(SERVER_NAME"/Command", "trigger enable"); 79 80 44 M.PrintMessage("Finished initalizing all boards\n"); 81 45 … … 104 68 int Ret = write_history(READLINE_HIST_FILE.c_str()); 105 69 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 sockets108 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 70 } 111 112 113 // ====================114 // OpenOtherSockets()115 // ====================116 117 void OpenOtherSockets() {118 119 //ETHZ120 static char Hostname[] = "192.33.99.225";121 //TUDO122 //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 hostname129 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 server136 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 descriptor142 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 server151 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 sockets160 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 socket168 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 sockets177 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 } -
fact/FADctrl/History.txt
r10117 r10120 8 8 23/1/2011 Data can be written to disk in M0 format 9 9 24/1/2011 Integrated amplitude calibration into FADBoard class 10 27/1/2011 Moved OpenOtherSockets() to FADBoard class
Note:
See TracChangeset
for help on using the changeset viewer.