Changeset 86 in tmcsimulator for branches/FEPSimulator/FEPSim.cpp
- Timestamp:
- 10/09/2017 11:58:19 PM (9 years ago)
- File:
-
- 1 edited
-
branches/FEPSimulator/FEPSim.cpp (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/FEPSimulator/FEPSim.cpp
r84 r86 1 1 #include "FEPSim.h" 2 2 3 /** 4 * Constructor 5 * 6 * @param host The host rpc server ip address 7 * @param networkFile the xml network file 8 */ 9 FEPSim::FEPSim(char * ATMShost) { 3 FEPSim::FEPSim(char * ATMShost, int FEP_PROG, int FEP_REV, int SOCK_PORT) { 10 4 this->ATMSHost = ATMShost; 5 this->FEP_PROG = FEP_PROG; 6 this->FEP_REV = FEP_REV; 7 this->SOCK_PORT = SOCK_PORT; 11 8 } 12 9 13 /**14 * Destructor15 */16 10 FEPSim::~FEPSim() { 17 cout << "Destroying client..." << endl; 18 clnt_destroy(clnt); 11 19 12 } 20 13 21 /**22 * Handler for the ATMS RPC Response (to the client RPC Call)23 * @param response pointer to fep_reply struct24 */25 14 void FEPSim::handleCallResponse(void *response) { 26 15 // Failed RPC Call … … 34 23 } 35 24 36 void FEPSim::sendReplys(char * xml) 37 { 25 void FEPSim::sendReplys(char * xml) { 38 26 NetworkReader networkReader = NetworkReader(xml); 39 27 vector<FEP_LINE*> lines = networkReader.getLines(); 40 28 vector<STATION*> ldsMap = networkReader.getStations(); 41 29 42 30 // Send one reply for every FEPLine 43 31 for (int i = 0; i < lines.size(); i++) { … … 104 92 } 105 93 106 /**107 * Sends an fep_reply for every line in the FEP.108 */109 94 void FEPSim::updateATMS(char * xml) { 110 if(createClient(this->ATMSHost)) 111 { 95 if (createClient()) { 112 96 sendReplys(xml); 97 cout << "Destroying client..." << endl; 98 clnt_destroy(clnt); 113 99 } 114 100 } 115 101 116 /** 117 * Creates the RPC Client. If not successful, exit with status 1. 118 * @param host rpc server ip 119 */ 120 bool FEPSim::createClient(char * host) { 102 bool FEPSim::createClient() { 121 103 /* Create RPC Client to communicate with ATMS */ 122 104 bool success = true; 123 cout << "Creating RPC Client" << endl; 124 clnt = clnt_create( host, /*100090,*/ 103121, 32, "tcp");105 106 clnt = clnt_create(this->ATMSHost, FEP_PROG, FEP_REV, "tcp"); 125 107 /* Check if client creation failed */ 126 108 if (clnt == (CLIENT *) NULL) { 127 cerr << "Can't create client to " << host << endl;109 cerr << "Can't create client to " << this->ATMSHost << endl; 128 110 success = false; 129 } 130 else { 111 } else { 131 112 cout << "Client created" << endl; 132 113 } … … 134 115 } 135 116 136 /** 137 * Main driver for ATMSCommunicator. Creates an RPC Client from command line args. 138 * @param argc 139 * @param argv 140 * @return 141 */ 142 int main(int argc, char *argv[]) { 143 if(argc != 3) 144 { 145 cerr << "Usage: FEPSim <ATMS_Host> <FEP_ATMSDriver_PortNo" << endl; 146 exit(1); 147 } 148 char *FEPSimHost = argv[1]; 149 FEPSim fep = FEPSim(FEPSimHost); 150 117 void FEPSim::runSockServer() { 151 118 int sockfd, newsockfd, portno, clilen; 152 119 char buffer[BUFF_SIZE]; 153 120 struct sockaddr_in serv_addr, cli_addr; 154 121 int n; 155 portno = atoi(argv[2]);122 portno = this->SOCK_PORT; 156 123 157 124 /* First call to socket() function */ … … 184 151 185 152 /* Accept actual connections from the client */ 186 while (1) {187 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) &clilen);153 while (1) { 154 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t *) & clilen); 188 155 189 156 if (newsockfd < 0) { … … 195 162 // zero out buffer 196 163 bzero(buffer, BUFF_SIZE); 197 164 198 165 // read XML from socket 199 166 int totBytes = 0; 200 while ((n = recv(newsockfd, &buffer[totBytes], sizeof (buffer), 0)) > 0) {167 while ((n = recv(newsockfd, &buffer[totBytes], sizeof (buffer), 0)) > 0) { 201 168 totBytes += n; 202 169 } 203 170 204 171 if (n < 0) { 205 172 perror("ERROR reading from socket"); 206 173 exit(1); 207 174 } 208 175 209 176 // send data to atms 210 fep.updateATMS(buffer);177 updateATMS(buffer); 211 178 } 212 213 return 0;214 179 }
Note: See TracChangeset
for help on using the changeset viewer.
