Go to the source code of this file.
Functions | |
int | add_PMU (char pmuid[], char ip[], char port[], char protocol[]) |
void | add_PMU_Node (struct Lower_Layer_Details *) |
void * | connect_pmu_tcp (void *) |
void * | connect_pmu_udp (void *) |
int | remove_Lower_Node (char pmuid[], char protocol[]) |
void | remove_cfg_from_file (unsigned char idcode[]) |
void * | remove_llnode (void *) |
int | put_data_transmission_off (char pmuid[], char protocol[]) |
void * | data_off_llnode (void *temp) |
int | put_data_transmission_on (char pmuid[], char protocol[]) |
void * | data_on_llnode (void *temp) |
int | configuration_request (char pmuid[], char protocol[]) |
void * | config_request (void *temp) |
int | add_PDC (char ip[], char protocol[]) |
int | remove_PDC (char ip[], char port_num[], char protocol[]) |
void | display_CT () |
void | create_command_frame (int type, int pmuid, char *) |
int | checkip (char ip[]) |
int add_PDC | ( | char | ip[], | |
char | protocol[] | |||
) |
Definition at line 1316 of file new_pmu_or_pdc.c.
References Upper_Layer_Details::address_set, Upper_Layer_Details::config_change, i, Upper_Layer_Details::ip, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Upper_Layer_Details::pdc_addr, Upper_Layer_Details::port, Upper_Layer_Details::prev, Upper_Layer_Details::protocol, TCPPORT, Upper_Layer_Details::tcpup, UDPPORT, Upper_Layer_Details::UL_data_transmission_off, Upper_Layer_Details::UL_upper_pdc_cfgsent, ULfirst, and ULlast.
Referenced by new_pdc_validation().
01316 { 01317 01318 FILE *f_ip; 01319 int flag = 0; 01320 char port_num[6], line [30]; /* or other suitable maximum line size */ 01321 char *p,*i,*port; 01322 01323 f_ip = fopen("upperpdc_ip.txt","r"); 01324 01325 memset(line,'\0',30); 01326 01327 if(f_ip != NULL) { //1 if 01328 01329 while (fgets (line, 30,f_ip) != NULL) /* read a line */{ //2 if 01330 01331 i = strtok (line," "); 01332 port = strtok (NULL," "); 01333 p = strtok (NULL," "); 01334 01335 if(!strcmp(i,ip) && (!strncasecmp(p,protocol,3))){ 01336 01337 flag = 1; 01338 printf("MATCH\n"); 01339 return 1; 01340 } 01341 }// 2. if 01342 fclose (f_ip); 01343 } else { 01344 01345 printf("file upperpdc_ip.txt not present\n Created upperpdc_ip.txt\n"); 01346 f_ip = fopen("upperpdc_ip.txt","w"); // File is not present so create it 01347 fclose(f_ip); 01348 } 01349 01350 printf("flag = %d\n",flag); 01351 f_ip = fopen("upperpdc_ip.txt","a"); 01352 fprintf(f_ip,"%s",ip); 01353 fputc(' ',f_ip); 01354 if(!strncasecmp(protocol,"TCP",3)) { 01355 01356 sprintf(port_num,"%d",TCPPORT); 01357 fprintf(f_ip,"%s",port_num); 01358 fputc(' ',f_ip); 01359 01360 } else { 01361 01362 sprintf(port_num,"%d",UDPPORT); 01363 fprintf(f_ip,"%s",port_num); 01364 fputc(' ',f_ip); 01365 01366 } 01367 01368 fprintf(f_ip,"%s",protocol); 01369 fputc('\n',f_ip); 01370 fclose(f_ip); 01371 01372 /* Make a node that contains PDC IP,Port and Protocol */ 01373 struct Upper_Layer_Details *temp_pdc; 01374 temp_pdc = malloc(sizeof(struct Upper_Layer_Details)); 01375 if(!temp_pdc) { 01376 01377 printf("Not enough memory temp_pdc\n"); 01378 exit(1); 01379 01380 } 01381 01382 strcpy(temp_pdc->ip,ip); // ip 01383 if(!strncasecmp(protocol,"UDP",3)) { 01384 01385 temp_pdc->port = UDPPORT; // port 01386 01387 } else { 01388 01389 temp_pdc->port = TCPPORT; // port 01390 } 01391 01392 strncpy(temp_pdc->protocol,protocol,3); // protocol 01393 temp_pdc->protocol[3] = '\0'; 01394 01395 bzero(&temp_pdc->pdc_addr,sizeof(temp_pdc->pdc_addr)); 01396 temp_pdc->pdc_addr.sin_family = AF_INET; 01397 temp_pdc->pdc_addr.sin_addr.s_addr = inet_addr(temp_pdc->ip); 01398 temp_pdc->pdc_addr.sin_port = htons(temp_pdc->port); 01399 memset(&(temp_pdc->pdc_addr.sin_zero), '\0', 8); // zero the rest of the struct 01400 temp_pdc->config_change = 0; 01401 temp_pdc->tcpup = 1; 01402 temp_pdc->UL_upper_pdc_cfgsent = 0; 01403 temp_pdc->UL_data_transmission_off = 1; 01404 temp_pdc->address_set = 0; 01405 01406 printf("PDC IP ADRESS = %s\n",inet_ntoa(temp_pdc->pdc_addr.sin_addr)); 01407 01408 pthread_mutex_lock(&mutex_Upper_Layer_Details); 01409 if(ULfirst == NULL) { 01410 01411 ULfirst = temp_pdc; 01412 temp_pdc->prev = NULL; 01413 01414 } else { 01415 01416 01417 ULlast->next = temp_pdc; 01418 temp_pdc->prev = ULlast; 01419 01420 } 01421 01422 ULlast = temp_pdc; 01423 temp_pdc->next = NULL; 01424 01425 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 01426 return 0; 01427 01428 // }//not matched-else 01429 }
int add_PMU | ( | char | pmuid[], | |
char | ip[], | |||
char | port[], | |||
char | protocol[] | |||
) |
Definition at line 86 of file new_pmu_or_pdc.c.
References Upper_Layer_Details::config_change, connect_pmu_tcp(), connect_pmu_udp(), Lower_Layer_Details::data_transmission_off, i, Lower_Layer_Details::ip, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Lower_Layer_Details::next, Lower_Layer_Details::pmu_remove, Lower_Layer_Details::pmuid, Lower_Layer_Details::port, Lower_Layer_Details::prev, Lower_Layer_Details::protocol, Lower_Layer_Details::request_cfg_frame, ULfirst, and Lower_Layer_Details::up.
Referenced by add_pmu_validation().
00086 { 00087 00088 FILE *f_ip; 00089 int flag = 0; 00090 char *i,*p,*port_num,*id; 00091 int err; 00092 00093 // A new thread is created for each TCP connection in 'detached' mode. Thus allowing any number of threads to be created. 00094 pthread_attr_t attr; 00095 pthread_attr_init(&attr); 00096 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 00097 // immediately freed when it terminates, but 00098 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 00099 exit(1); // on the thread termination. 00100 00101 } 00102 00103 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 00104 00105 perror(strerror(err)); 00106 exit(1); 00107 } 00108 00109 f_ip = fopen("ipaddress.txt","r"); 00110 00111 char line [40]; /* or other suitable maximum line size */ 00112 memset(line,'\0',40); 00113 00114 if(f_ip != NULL) { //1 if 00115 00116 while (fgets (line, 40,f_ip) != NULL) /* read a line */{ //2 while 00117 00118 id = strtok (line," "); 00119 i = strtok (NULL," "); 00120 port_num = strtok (NULL," "); 00121 p = strtok (NULL," "); 00122 00123 if(!strcmp(id,pmuid)) { 00124 00125 flag = 1; 00126 break; 00127 00128 } else { 00129 00130 memset(line,'\0',30); 00131 continue; 00132 } 00133 }// 2. while ends 00134 fclose (f_ip); 00135 } else { 00136 00137 printf("File ipaddress.txt not present so we create it\n"); 00138 f_ip = fopen("ipaddress.txt","w"); // File is not present so create it 00139 fclose(f_ip); 00140 } 00141 00142 00143 printf("flag = %d\n",flag); 00144 if(flag) { /* 2 if there is a match */ 00145 00146 printf("%s %s is already in the ipaddress list Enter another PMU\n",pmuid,protocol); 00147 return 1; 00148 00149 } else if(!flag) { 00150 00151 f_ip = fopen("ipaddress.txt","a"); 00152 fprintf(f_ip,"%s",pmuid); 00153 fputc(' ',f_ip); 00154 fprintf(f_ip,"%s",ip); 00155 fputc(' ',f_ip); 00156 fprintf(f_ip,"%s",port); 00157 fputc(' ',f_ip); 00158 fprintf(f_ip,"%s",protocol); 00159 fputc('\n',f_ip); 00160 fclose(f_ip); 00161 00162 /* Make a node that contains PMU IP, Port and Protocol details */ 00163 struct Lower_Layer_Details *temp_pmu; 00164 temp_pmu = malloc(sizeof(struct Lower_Layer_Details)); 00165 if(!temp_pmu) { 00166 00167 printf("Not enough memory temp_pmu\n"); 00168 exit(1); 00169 } 00170 00171 temp_pmu->pmuid = atoi(pmuid); // PMUID 00172 strcpy(temp_pmu->ip,ip); // ip 00173 temp_pmu->port = atoi(port); // port 00174 strcpy(temp_pmu->protocol,protocol); // protocol 00175 temp_pmu->protocol[3] = '\0'; 00176 temp_pmu->up = 1; 00177 temp_pmu->data_transmission_off = 0; 00178 temp_pmu->pmu_remove = 0; 00179 temp_pmu->request_cfg_frame = 0; 00180 temp_pmu->next = NULL; 00181 temp_pmu->prev = NULL; 00182 00183 pthread_t t; 00184 if(!strncasecmp(protocol,"UDP",3)) { 00185 00186 if((err = pthread_create(&t,&attr,connect_pmu_udp,(void *)temp_pmu))) { 00187 00188 perror(strerror(err)); 00189 exit(1); 00190 } 00191 00192 } else if(!strncasecmp(protocol,"TCP",3)) { 00193 00194 if((err = pthread_create(&t,&attr,connect_pmu_tcp,(void *)temp_pmu))) { 00195 00196 perror(strerror(err)); 00197 exit(1); 00198 } 00199 00200 } 00201 00202 struct Upper_Layer_Details *temp_pdc = ULfirst; 00203 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00204 while(temp_pdc != NULL ) { 00205 00206 temp_pdc->config_change = 1; 00207 temp_pdc = temp_pdc->next; 00208 } 00209 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00210 return 0; 00211 } // If no match 00212 return 1; 00213 }
void add_PMU_Node | ( | struct Lower_Layer_Details * | ) |
Definition at line 512 of file new_pmu_or_pdc.c.
References LLfirst, LLlast, mutex_Lower_Layer_Details, Lower_Layer_Details::next, and Lower_Layer_Details::prev.
Referenced by connect_pmu_tcp(), and connect_pmu_udp().
00512 { 00513 00514 pthread_mutex_lock(&mutex_Lower_Layer_Details); 00515 if(LLfirst == NULL) { 00516 00517 temp_pmu->prev = NULL; 00518 LLfirst = temp_pmu; 00519 00520 } else { 00521 00522 LLlast->next = temp_pmu; 00523 temp_pmu->prev = LLlast; 00524 } 00525 00526 temp_pmu->next = NULL; 00527 LLlast = temp_pmu; 00528 00529 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 00530 00531 }
int checkip | ( | char | ip[] | ) |
Definition at line 1695 of file new_pmu_or_pdc.c.
Referenced by add_pmu_validation(), new_pdc_validation(), remove_pdc_validation(), and validation_pdc_detail().
01695 { 01696 01697 struct sockaddr_in sa; 01698 int result = inet_pton(AF_INET, ip, &(sa.sin_addr)); 01699 return result; 01700 }
void* config_request | ( | void * | temp | ) |
Definition at line 1230 of file new_pmu_or_pdc.c.
References Upper_Layer_Details::config_change, connect_pmu_tcp(), create_command_frame(), Lower_Layer_Details::data_transmission_off, Lower_Layer_Details::llpmu_addr, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Lower_Layer_Details::pmuid, Lower_Layer_Details::protocol, Lower_Layer_Details::sockfd, ULfirst, and Lower_Layer_Details::up.
Referenced by configuration_request().
01230 { 01231 01232 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp; 01233 char *cmdframe = malloc(19); 01234 int err; 01235 01236 pthread_attr_t attr; 01237 pthread_attr_init(&attr); 01238 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 01239 // immediately freed when it terminates, but 01240 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 01241 exit(1); // on the thread termination. 01242 01243 } 01244 01245 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 01246 01247 perror(strerror(err)); 01248 exit(1); 01249 } 01250 01251 if(!strncasecmp(temp_pmu->protocol,"UDP",3)) { /* If Peer is UDP */ 01252 01253 cmdframe[18] = '\0'; 01254 create_command_frame(1,temp_pmu->pmuid,cmdframe); 01255 int n; 01256 if ((n = sendto(temp_pmu->sockfd,cmdframe, 18, 01257 0,(struct sockaddr *)&temp_pmu->llpmu_addr,sizeof(temp_pmu->llpmu_addr)) == -1)) { 01258 perror("sendto"); 01259 } else { 01260 01261 printf("CmD to send CFG = %x\t%x\n", cmdframe[0], cmdframe[1]); 01262 free(cmdframe); 01263 temp_pmu->data_transmission_off = 0; 01264 01265 } 01266 01267 } else if(!strncasecmp(temp_pmu->protocol,"TCP",3)){ /* If Peer is TCP */ 01268 01269 int n; 01270 01271 if(temp_pmu->up == 0) { /* If TCP Peer is DOWN */ 01272 01273 pthread_t t; 01274 if((err = pthread_create(&t,&attr,connect_pmu_tcp,(void *)temp_pmu))) { 01275 01276 perror(strerror(err)); 01277 exit(1); 01278 } 01279 01280 struct Upper_Layer_Details *temp_pdc = ULfirst; 01281 pthread_mutex_lock(&mutex_Upper_Layer_Details); 01282 while(temp_pdc != NULL ) { 01283 01284 temp_pdc->config_change = 1; 01285 temp_pdc = temp_pdc->next; 01286 } 01287 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 01288 01289 01290 } else { /* If TCP Peer is UP */ 01291 01292 cmdframe[18] = '\0'; 01293 create_command_frame(1,temp_pmu->pmuid,cmdframe); 01294 if ((n = send(temp_pmu->sockfd,cmdframe, 18,0) == -1)) { 01295 01296 printf("temp_pmu->sockfd %d \n",temp_pmu->sockfd); 01297 perror("send"); 01298 01299 } else { 01300 01301 printf("CmD to send CFG \n"); 01302 free(cmdframe); 01303 temp_pmu->data_transmission_off = 0; 01304 } 01305 } 01306 } 01307 pthread_exit(NULL); 01308 }
int configuration_request | ( | char | pmuid[], | |
char | protocol[] | |||
) |
Definition at line 1157 of file new_pmu_or_pdc.c.
References config_request(), LLfirst, mutex_Lower_Layer_Details, Lower_Layer_Details::next, Lower_Layer_Details::pmuid, and Lower_Layer_Details::protocol.
Referenced by cmd_or_remove_pmu_validation().
01157 { 01158 01159 int flag = 0,err; 01160 01161 // A new thread is created for each TCP connection in 'detached' mode. Thus allowing any number of threads to be created. 01162 pthread_attr_t attr; 01163 pthread_attr_init(&attr); 01164 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 01165 // immediately freed when it terminates, but 01166 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 01167 exit(1); // on the thread termination. 01168 01169 } 01170 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 01171 01172 perror(strerror(err)); 01173 exit(1); 01174 } 01175 01176 pthread_mutex_lock(&mutex_Lower_Layer_Details); 01177 01178 if(LLfirst == NULL) { 01179 01180 printf("No PMU Added\n"); 01181 return 1; 01182 01183 } else { 01184 01185 flag = 1; 01186 } 01187 01188 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 01189 01190 if(flag) { 01191 01192 int match = 0; 01193 struct Lower_Layer_Details *temp_pmu = LLfirst; 01194 01195 while(temp_pmu != NULL) { 01196 01197 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) { 01198 01199 match = 1; 01200 break; 01201 01202 } else { 01203 temp_pmu = temp_pmu->next; 01204 } 01205 } 01206 if(match) { 01207 01208 // temp_pmu->request_cfg_frame = 1; 01209 pthread_t t; 01210 if((err = pthread_create(&t,&attr,config_request,(void *)temp_pmu))) { 01211 01212 perror(strerror(err)); 01213 exit(1); 01214 } 01215 return 0; 01216 01217 } else { 01218 01219 printf("No Match\n"); 01220 return 1; 01221 } 01222 } 01223 return 1; 01224 }
void* connect_pmu_tcp | ( | void * | ) |
Definition at line 220 of file new_pmu_or_pdc.c.
References add_PMU_Node(), compute_CRC(), Upper_Layer_Details::config_change, copy_cbyc(), create_command_frame(), DB_Server_addr, DB_sockfd, fp_log, Lower_Layer_Details::ip, Lower_Layer_Details::llpmu_addr, MAXBUFLEN, mutex_Upper_Layer_Details, Upper_Layer_Details::next, PMU_process_TCP(), Lower_Layer_Details::pmuid, Lower_Layer_Details::port, Lower_Layer_Details::sockfd, Lower_Layer_Details::thread_id, to_intconvertor(), ULfirst, Lower_Layer_Details::up, and yes.
Referenced by add_PMU(), config_request(), and recreate_tcp_connections().
00220 { 00221 00222 00223 int tcp_sockfd,port_num,yes = 1; 00224 struct sockaddr_in PMU_addr; 00225 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp; 00226 unsigned char *tcp_BUF,*ptr,length[2]; 00227 unsigned int flen; 00228 uint16_t cal_crc,frame_crc; 00229 00230 port_num = temp_pmu->port; 00231 00232 if ((tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 00233 perror("socket"); 00234 exit(1); 00235 } 00236 00237 if (setsockopt(tcp_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { 00238 perror("setsockopt"); 00239 exit(1); 00240 } 00241 00242 bzero(&PMU_addr,sizeof(PMU_addr)); 00243 PMU_addr.sin_family = AF_INET; 00244 PMU_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip); 00245 PMU_addr.sin_port = htons(port_num); 00246 memset(&(PMU_addr.sin_zero), '\0', 8); // zero the rest of the struct 00247 00248 /* Copy the information of Lower Layer PMU/PDC to the node */ 00249 temp_pmu->thread_id = pthread_self(); 00250 bzero(&temp_pmu->llpmu_addr,sizeof(PMU_addr)); 00251 temp_pmu->llpmu_addr.sin_family = AF_INET; 00252 temp_pmu->llpmu_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip); 00253 temp_pmu->llpmu_addr.sin_port = htons(port_num); 00254 memset(&(temp_pmu->llpmu_addr.sin_zero), '\0', 8); // zero the rest of the struct 00255 temp_pmu->sockfd = tcp_sockfd; 00256 temp_pmu->up = 1; 00257 00258 if (connect(tcp_sockfd, (struct sockaddr *)&PMU_addr, 00259 sizeof(PMU_addr)) == -1) { // Main if 00260 perror("connect"); 00261 temp_pmu->up = 0; 00262 add_PMU_Node(temp_pmu); 00263 pthread_exit(NULL); 00264 00265 } else { 00266 /* Add PMU*/ 00267 add_PMU_Node(temp_pmu); 00268 00269 tcp_BUF = malloc(MAXBUFLEN* sizeof(unsigned char)); 00270 /* Sending Command fro obtaining CFG */ 00271 int n,bytes_read; 00272 char *cmdframe = malloc(19); 00273 cmdframe[18] = '\0'; 00274 create_command_frame(1,temp_pmu->pmuid,cmdframe); 00275 if ((n = send (tcp_sockfd,cmdframe,18,0) == -1)) { 00276 00277 perror("send"); 00278 00279 } else { 00280 00281 free(cmdframe); 00282 while(1) { 00283 00284 memset(tcp_BUF, '\0', MAXBUFLEN * sizeof(unsigned char)); 00285 bytes_read = recv (tcp_sockfd, tcp_BUF,MAXBUFLEN-1,0); 00286 00287 if(bytes_read == 0) { /* When TCP Peer Terminates */ 00288 00289 printf("No data received Closing tcp socket %d\n",tcp_sockfd); 00290 temp_pmu->up = 0; 00291 00292 struct Upper_Layer_Details *temp_pdc = ULfirst; 00293 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00294 while(temp_pdc != NULL ) { 00295 00296 temp_pdc->config_change = 1; 00297 temp_pdc = temp_pdc->next; 00298 } 00299 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00300 00301 00302 pthread_exit(NULL); 00303 00304 } else if(bytes_read == -1) {/* When TCP Peer Terminates */ 00305 00306 perror("recv"); 00307 temp_pmu->up = 0; 00308 00309 struct Upper_Layer_Details *temp_pdc = ULfirst; 00310 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00311 while(temp_pdc != NULL ) { 00312 00313 temp_pdc->config_change = 1; 00314 temp_pdc = temp_pdc->next; 00315 } 00316 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00317 00318 } else { 00319 00320 ptr = tcp_BUF; 00321 ptr += 2; 00322 copy_cbyc(length,ptr,2); 00323 flen = to_intconvertor(length); 00324 cal_crc = compute_CRC(tcp_BUF,flen-2); 00325 ptr += flen -4; 00326 frame_crc = *ptr; 00327 frame_crc <<= 8; 00328 frame_crc |= *(ptr + 1); 00329 if(frame_crc != cal_crc) { 00330 00331 fprintf(fp_log,"CRC NOT MATCH\n"); 00332 continue; 00333 } 00334 if (sendto(DB_sockfd,tcp_BUF, MAXBUFLEN-1, 0, 00335 (struct sockaddr *)&DB_Server_addr,sizeof(DB_Server_addr)) == -1) { 00336 perror("sendto"); 00337 00338 } 00339 00340 tcp_BUF[bytes_read] = '\0'; 00341 PMU_process_TCP(tcp_BUF,tcp_sockfd); 00342 00343 } 00344 00345 } // while ends 00346 } 00347 }// Main if 00348 close(tcp_sockfd); 00349 pthread_exit(NULL); 00350 }
void* connect_pmu_udp | ( | void * | ) |
Definition at line 358 of file new_pmu_or_pdc.c.
References add_PMU_Node(), compute_CRC(), copy_cbyc(), create_command_frame(), DB_Server_addr, DB_sockfd, fp_log, Lower_Layer_Details::ip, LLfirst, Lower_Layer_Details::llpmu_addr, MAXBUFLEN, mutex_Lower_Layer_Details, Lower_Layer_Details::next, PMU_process_UDP(), Lower_Layer_Details::pmuid, Lower_Layer_Details::port, Lower_Layer_Details::protocol, Lower_Layer_Details::sockfd, Lower_Layer_Details::thread_id, to_intconvertor(), Lower_Layer_Details::up, and yes.
Referenced by add_PMU(), and recreate_udp_connections().
00358 { 00359 00360 int udp_sockfd,port_num,addr_len,yes = 1; 00361 struct sockaddr_in PMU_addr,their_addr; 00362 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp; 00363 struct Lower_Layer_Details *t ; 00364 unsigned char *udp_BUF,*ptr,length[2]; 00365 unsigned int flen; 00366 uint16_t cal_crc,frame_crc; 00367 00368 port_num = temp_pmu->port; 00369 00370 if ((udp_sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 00371 perror("socket"); 00372 exit(1); 00373 } 00374 00375 if (setsockopt(udp_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { 00376 perror("setsockopt"); 00377 exit(1); 00378 } 00379 00380 bzero(&PMU_addr,sizeof(PMU_addr)); 00381 PMU_addr.sin_family = AF_INET; 00382 PMU_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip); 00383 PMU_addr.sin_port = htons(port_num); 00384 memset(&(PMU_addr.sin_zero), '\0', 8); // zero the rest of the struct 00385 00386 /* Copy the information of Lower Layer PMU/PDC to the node */ 00387 temp_pmu->thread_id = pthread_self(); 00388 bzero(&temp_pmu->llpmu_addr,sizeof(PMU_addr)); 00389 temp_pmu->llpmu_addr.sin_family = AF_INET; 00390 temp_pmu->llpmu_addr.sin_addr.s_addr = inet_addr(temp_pmu->ip); 00391 temp_pmu->llpmu_addr.sin_port = htons(port_num); 00392 memset(&(temp_pmu->llpmu_addr.sin_zero), '\0', 8); // zero the rest of the struct 00393 temp_pmu->sockfd = udp_sockfd; 00394 temp_pmu->up = 1; 00395 00396 /* Add PMU*/ 00397 add_PMU_Node(temp_pmu); 00398 00399 udp_BUF = malloc(MAXBUFLEN* sizeof(unsigned char)); 00400 00401 /* Sending Command fro obtaining CFG */ 00402 addr_len = sizeof(struct sockaddr); 00403 int n,bytes_read; 00404 unsigned char *cmdframe = malloc(19); 00405 cmdframe[18] = '\0'; 00406 create_command_frame(1,temp_pmu->pmuid,(char *)cmdframe); 00407 00408 if ((n = sendto(udp_sockfd,cmdframe, 18, 0, 00409 (struct sockaddr *)&PMU_addr,sizeof(PMU_addr)) == -1)) { 00410 perror("sendto"); 00411 00412 } else { 00413 00414 free(cmdframe); 00415 fprintf(fp_log,"sent CMD for CFG\n"); 00416 /* UDP data Received */ 00417 while(1) { 00418 00419 memset(udp_BUF,'\0',MAXBUFLEN * sizeof(unsigned char)); 00420 bytes_read = recvfrom (udp_sockfd, udp_BUF,MAXBUFLEN-1,0,(struct sockaddr *)&their_addr,(socklen_t *)&addr_len); 00421 if(bytes_read == -1) { 00422 00423 perror("recvfrom"); 00424 exit(1); 00425 00426 } else { // New Datagram received 00427 00428 00429 pthread_mutex_lock(&mutex_Lower_Layer_Details); 00430 int flag = 0; 00431 if(LLfirst == NULL) { 00432 00433 flag = 0; 00434 00435 } else { 00436 00437 t = LLfirst; 00438 while(t != NULL) { 00439 00440 if((!strcmp(t->ip,inet_ntoa(their_addr.sin_addr))) 00441 && (!strncasecmp(t->protocol,"UDP",3))) { 00442 00443 flag = 1; 00444 break; 00445 00446 } else { 00447 00448 t = t->next; 00449 } 00450 } 00451 } 00452 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 00453 00454 if(flag) { 00455 00456 ptr = udp_BUF; 00457 ptr += 2; 00458 copy_cbyc(length,ptr,2); 00459 flen = to_intconvertor(length); 00460 cal_crc = compute_CRC(udp_BUF,flen-2); 00461 ptr += flen -4; 00462 frame_crc = *ptr; 00463 frame_crc <<= 8; 00464 frame_crc |= *(ptr + 1); 00465 if(frame_crc != cal_crc) { 00466 00467 fprintf(fp_log,"CRC NOT MATCH\n"); 00468 continue; 00469 } 00470 00471 //process the frame 00472 int n; 00473 if ((n = sendto(DB_sockfd,udp_BUF, MAXBUFLEN-1, 0, 00474 (struct sockaddr *)&DB_Server_addr,sizeof(DB_Server_addr)) == -1)) { 00475 perror("sendto"); 00476 00477 } else { 00478 00479 fprintf(fp_log,"DB socket %d \n",DB_sockfd); 00480 fprintf(fp_log,"DB Server %s \n",inet_ntoa(DB_Server_addr.sin_addr)); 00481 fprintf(fp_log,"UDP_BUF len %d Sent FRAME %d bytes\n",strlen((const char *)udp_BUF),n); 00482 } 00483 00484 fprintf(fp_log,"\nUDP Server got packet from %s\n",inet_ntoa(their_addr.sin_addr)); 00485 fprintf(fp_log,"packet is %d bytes long\n",bytes_read); 00486 udp_BUF[bytes_read] = '\0'; 00487 00488 //Call the udphandler 00489 PMU_process_UDP(udp_BUF,PMU_addr,udp_sockfd); 00490 00491 } else { 00492 00493 printf("Datagram PMU not authentic. We donot pass the buffer for further processing %s\n",inet_ntoa(their_addr.sin_addr)); 00494 00495 } 00496 } // Main if ends 00497 00498 } // while ends 00499 } 00500 00501 close(udp_sockfd); 00502 pthread_exit(NULL); 00503 }
void create_command_frame | ( | int | type, | |
int | pmuid, | |||
char * | ||||
) |
void* data_off_llnode | ( | void * | temp | ) |
Definition at line 972 of file new_pmu_or_pdc.c.
References create_command_frame(), Lower_Layer_Details::llpmu_addr, Lower_Layer_Details::pmuid, Lower_Layer_Details::protocol, Lower_Layer_Details::sockfd, and Lower_Layer_Details::up.
Referenced by put_data_transmission_off().
00972 { 00973 00974 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp; 00975 00976 char *cmdframe = malloc(19); 00977 create_command_frame(3,temp_pmu->pmuid,cmdframe); 00978 cmdframe[18] = '\0'; 00979 00980 if(!strncasecmp(temp_pmu->protocol,"UDP",3)) { 00981 00982 int n; 00983 if ((n = sendto(temp_pmu->sockfd,cmdframe, 18, 00984 0,(struct sockaddr *)&temp_pmu->llpmu_addr,sizeof(temp_pmu->llpmu_addr)) == -1)) { 00985 perror("sendto"); 00986 } else { 00987 00988 printf("Sent CmD to put off data transmission\n"); 00989 00990 } 00991 00992 } else if(!strncasecmp(temp_pmu->protocol,"TCP",3)){ 00993 00994 int n; 00995 if(temp_pmu->up == 1) { 00996 if ((n = send(temp_pmu->sockfd,cmdframe, 18,0) == -1)) { 00997 00998 perror("send"); 00999 01000 } else { 01001 01002 printf("Sent CmD to put off data transmission\n"); 01003 01004 } 01005 } 01006 } 01007 free(cmdframe); 01008 pthread_exit(NULL); 01009 }
void* data_on_llnode | ( | void * | temp | ) |
Definition at line 1114 of file new_pmu_or_pdc.c.
References create_command_frame(), Lower_Layer_Details::llpmu_addr, Lower_Layer_Details::pmuid, Lower_Layer_Details::protocol, Lower_Layer_Details::sockfd, and Lower_Layer_Details::up.
Referenced by put_data_transmission_on().
01114 { 01115 01116 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp; 01117 01118 char *cmdframe = malloc(19); 01119 create_command_frame(2,temp_pmu->pmuid,cmdframe); 01120 cmdframe[18] = '\0'; 01121 if(!strncasecmp(temp_pmu->protocol,"UDP",3)) { 01122 01123 int n; 01124 if ((n = sendto(temp_pmu->sockfd,cmdframe, 18, 01125 0,(struct sockaddr *)&temp_pmu->llpmu_addr,sizeof(temp_pmu->llpmu_addr)) == -1)) { 01126 perror("sendto"); 01127 } else { 01128 01129 printf("Sent CmD to put on data transmission.\n"); 01130 01131 } 01132 01133 } else if(!strncasecmp(temp_pmu->protocol,"TCP",3)){ 01134 01135 int n; 01136 if(temp_pmu->up == 1) { 01137 01138 if ((n = send(temp_pmu->sockfd,cmdframe, 18,0) == -1)) { 01139 01140 perror("send"); 01141 01142 } else { 01143 01144 printf("Sent CmD to put on data transmission.\n"); 01145 01146 } 01147 } 01148 } 01149 free(cmdframe); 01150 pthread_exit(NULL); 01151 }
void display_CT | ( | ) |
Definition at line 1553 of file new_pmu_or_pdc.c.
References Upper_Layer_Details::ip, Lower_Layer_Details::ip, LLfirst, mutex_Lower_Layer_Details, Upper_Layer_Details::next, Lower_Layer_Details::next, Lower_Layer_Details::pmuid, Upper_Layer_Details::port, Lower_Layer_Details::port, Upper_Layer_Details::protocol, Lower_Layer_Details::protocol, ULfirst, and Lower_Layer_Details::up.
01553 { 01554 01555 printf("#### CONNECTION TABLE OF SOURCE DEVICES ####\n"); 01556 printf("--------------------------------------------------------------------------------\n"); 01557 printf("| PMU ID | IP | Port | Protocol | Up |\n"); 01558 printf("--------------------------------------------------------------------------------\n"); 01559 01560 pthread_mutex_lock(&mutex_Lower_Layer_Details); 01561 01562 if(LLfirst == NULL) { 01563 01564 printf("No PMU Added\n"); 01565 01566 01567 } else { 01568 01569 struct Lower_Layer_Details *t = LLfirst; 01570 01571 while(t != NULL) { 01572 01573 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\t%d\t|\n",t->pmuid,t->ip,t->port,t->protocol,t->up); 01574 t = t->next; 01575 } 01576 01577 printf("--------------------------------------------------------------------------------\n"); 01578 01579 } 01580 01581 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 01582 01583 printf("#### CONNECTION TABLE OF DESTINATION DEVICES ####\n"); 01584 printf("-------------------------------------------------\n"); 01585 printf("| IP | Port | Protocol |\n"); 01586 printf("-------------------------------------------------\n"); 01587 01588 if(ULfirst == NULL) { 01589 01590 printf("No PDC Added\n"); 01591 01592 01593 } else { 01594 01595 struct Upper_Layer_Details *t = ULfirst; 01596 01597 while(t != NULL) { 01598 01599 printf("|%s\t|\t%d\t|\t%s\t|\n",t->ip,t->port,t->protocol); 01600 t = t->next; 01601 } 01602 01603 printf("-------------------------------------------------\n"); 01604 01605 } 01606 }
int put_data_transmission_off | ( | char | pmuid[], | |
char | protocol[] | |||
) |
Definition at line 877 of file new_pmu_or_pdc.c.
References data_off_llnode(), Lower_Layer_Details::data_transmission_off, fp_log, Lower_Layer_Details::ip, LLfirst, mutex_Lower_Layer_Details, Lower_Layer_Details::next, Lower_Layer_Details::pmuid, Lower_Layer_Details::port, and Lower_Layer_Details::protocol.
Referenced by cmd_or_remove_pmu_validation().
00877 { 00878 00879 int flag = 0,err; 00880 00881 // A new thread is created for each TCP connection in 'detached' mode. Thus allowing any number of threads to be created. 00882 pthread_attr_t attr; 00883 pthread_attr_init(&attr); 00884 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 00885 // immediately freed when it terminates, but 00886 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 00887 exit(1); // on the thread termination. 00888 00889 } 00890 00891 00892 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 00893 00894 perror(strerror(err)); 00895 exit(1); 00896 } 00897 00898 printf("#### Connection TABLE ####\n"); 00899 printf("-----------------------------------------------------------------\n"); 00900 printf("| PMU ID | IP | Port | Protocol |\n"); 00901 printf("-----------------------------------------------------------------\n"); 00902 00903 pthread_mutex_lock(&mutex_Lower_Layer_Details); 00904 00905 if(LLfirst == NULL) { 00906 00907 printf("No PMU Added\n"); 00908 return 1; 00909 00910 } else { 00911 00912 flag = 1; 00913 struct Lower_Layer_Details *t = LLfirst; 00914 00915 while(t != NULL) { 00916 00917 00918 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\n",t->pmuid,t->ip,t->port,t->protocol); 00919 t = t->next; 00920 } 00921 00922 printf("-----------------------------------------------------------------\n"); 00923 00924 } 00925 00926 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 00927 00928 if(flag) { 00929 00930 int match = 0; 00931 struct Lower_Layer_Details *temp_pmu = LLfirst; 00932 00933 while(temp_pmu != NULL) { 00934 00935 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) { 00936 00937 match = 1; 00938 break; 00939 00940 } else { 00941 00942 temp_pmu = temp_pmu->next; 00943 00944 } 00945 00946 } 00947 if(match) { 00948 00949 pthread_t t; 00950 temp_pmu->data_transmission_off = 1; 00951 if((err = pthread_create(&t,&attr,data_off_llnode,(void *)temp_pmu))) { 00952 00953 perror(strerror(err)); 00954 exit(1); 00955 } 00956 return 0; 00957 00958 } else { 00959 00960 fprintf(fp_log,"No Match\n"); 00961 return 1; 00962 } 00963 } 00964 return 1; 00965 }
int put_data_transmission_on | ( | char | pmuid[], | |
char | protocol[] | |||
) |
Definition at line 1016 of file new_pmu_or_pdc.c.
References data_on_llnode(), Lower_Layer_Details::data_transmission_off, Lower_Layer_Details::ip, LLfirst, mutex_Lower_Layer_Details, Lower_Layer_Details::next, Lower_Layer_Details::pmuid, Lower_Layer_Details::port, and Lower_Layer_Details::protocol.
Referenced by cmd_or_remove_pmu_validation().
01016 { 01017 01018 int flag = 0,err; 01019 01020 // A new thread is created for each TCP connection in 'detached' mode. Thus allowing any number of threads to be created. 01021 pthread_attr_t attr; 01022 pthread_attr_init(&attr); 01023 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 01024 // immediately freed when it terminates, but 01025 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 01026 exit(1); // on the thread termination. 01027 01028 } 01029 01030 01031 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 01032 01033 perror(strerror(err)); 01034 exit(1); 01035 } 01036 01037 printf("Lower Layer PMU/PDC whose data transmiision is off\n"); 01038 01039 printf("#### Connection TABLE ####\n"); 01040 printf("-----------------------------------------------------------------\n"); 01041 printf("| PMU ID | IP | Port | Protocol |\n"); 01042 printf("-----------------------------------------------------------------\n"); 01043 01044 pthread_mutex_lock(&mutex_Lower_Layer_Details); 01045 01046 if(LLfirst == NULL) { 01047 01048 printf("No PMU Added\n"); 01049 return 1; 01050 01051 } else { 01052 01053 flag = 1; 01054 struct Lower_Layer_Details *t = LLfirst; 01055 01056 while(t != NULL) { 01057 01058 if(t->data_transmission_off == 1) { 01059 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\n",t->pmuid,t->ip,t->port,t->protocol); 01060 } 01061 t = t->next; 01062 } 01063 01064 printf("-----------------------------------------------------------------\n"); 01065 01066 } 01067 01068 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 01069 01070 if(flag) { 01071 01072 int match = 0; 01073 struct Lower_Layer_Details *temp_pmu = LLfirst; 01074 01075 while(temp_pmu != NULL) { 01076 01077 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) { 01078 01079 match = 1; 01080 break; 01081 01082 } else { 01083 01084 temp_pmu = temp_pmu->next; 01085 01086 } 01087 01088 } 01089 if(match) { 01090 01091 temp_pmu->data_transmission_off = 0; 01092 pthread_t t; 01093 if((err = pthread_create(&t,&attr,data_on_llnode,(void *)temp_pmu))) { 01094 01095 perror(strerror(err)); 01096 exit(1); 01097 } 01098 return 0; 01099 01100 } else { 01101 01102 printf("No Match\n"); 01103 return 1; 01104 } 01105 } 01106 return 1; 01107 }
void remove_cfg_from_file | ( | unsigned char | idcode[] | ) |
Definition at line 790 of file new_pmu_or_pdc.c.
References copy_cbyc(), fp_log, cfg_frame::framesize, ncmp_cbyc(), and to_intconvertor().
Referenced by remove_llnode().
00790 { 00791 00792 FILE *file,*newfile; 00793 int result; 00794 unsigned char id[3],*line,*s,frame_len[2]; 00795 unsigned long fileLen; 00796 unsigned int framesize; 00797 00798 file = fopen("cfg.bin","rb"); 00799 newfile = fopen("ncfg.bin","wb"); 00800 00801 if (file != NULL) { 00802 00803 //Get file length 00804 fseek(file, 0, SEEK_END); 00805 fileLen = ftell(file); 00806 fseek(file, 0, SEEK_SET); 00807 00808 fprintf(fp_log,"File len %ld\n",fileLen); 00809 while (fileLen != 0) /* Till the EOF */{ 00810 00811 fseek (file,2 ,SEEK_CUR); 00812 fread(frame_len, sizeof(unsigned char),2, file); 00813 fseek (file,-4 ,SEEK_CUR); 00814 00815 framesize = to_intconvertor(frame_len); 00816 line = malloc(framesize*sizeof(unsigned char)); 00817 memset(line,'\0',sizeof(line)); 00818 fread(line, sizeof(unsigned char),framesize,file); 00819 s = line; 00820 s += 4; 00821 //match IDCODE in cfg.bin file 00822 copy_cbyc (id,s,2); 00823 id[2] = '\0'; 00824 if(!ncmp_cbyc(id,idcode,2)) { 00825 00826 fprintf(fp_log,"MATCHED the IDCODE file cfg.bin\n"); 00827 break; 00828 00829 } else { 00830 00831 //Place rest of lines in the new file 00832 fwrite(line, sizeof(unsigned char),framesize,newfile); 00833 free(line); 00834 fileLen -= framesize; 00835 } 00836 }//While ends 00837 00838 fileLen -= framesize; 00839 // If cfg.bin file still contains data copy it to ncfg.bin 00840 while (fileLen != 0) /* Till the EOF */{ 00841 00842 fseek (file,2 ,SEEK_CUR); 00843 fread(frame_len, sizeof(unsigned char),2, file); 00844 fseek (file,-4 ,SEEK_CUR); 00845 00846 framesize = to_intconvertor(frame_len); 00847 line = malloc(framesize*sizeof(unsigned char)); 00848 memset(line,'\0',sizeof(line)); 00849 fread(line, sizeof(unsigned char),framesize,file); 00850 00851 fwrite(line, sizeof(unsigned char),framesize,newfile); 00852 free(line); 00853 fileLen -= framesize; 00854 } 00855 00856 //File renaming 00857 fclose (file); 00858 fclose(newfile); 00859 00860 if( remove( "cfg.bin" ) != 0 ) 00861 perror( "Error deleting file" ); 00862 result= rename("ncfg.bin","cfg.bin"); 00863 if ( result == 0 ) 00864 fputs ("File successfully renamed",fp_log); 00865 else 00866 perror( "Error renaming file" ); 00867 00868 } else { 00869 perror ("cfg.bin"); /* why didn't the file open? */ 00870 } 00871 }
void* remove_llnode | ( | void * | ) |
Definition at line 638 of file new_pmu_or_pdc.c.
References cfgfirst, cfg_frame::cfgnext, fp_log, free_cfgframe_object(), cfg_frame::idcode, LLfirst, LLlast, mutex_cfg, mutex_file, mutex_Lower_Layer_Details, ncmp_cbyc(), Lower_Layer_Details::next, Lower_Layer_Details::pmuid, Lower_Layer_Details::prev, Lower_Layer_Details::protocol, remove_cfg_from_file(), Lower_Layer_Details::sockfd, and Lower_Layer_Details::thread_id.
Referenced by remove_Lower_Node().
00638 { 00639 00640 int rc,result; 00641 struct Lower_Layer_Details *temp_pmu = (struct Lower_Layer_Details *) temp; 00642 char pmuid[6]; 00643 pthread_t tid = temp_pmu->thread_id; 00644 00645 /* remove the entry from ipaddress.txt */ 00646 00647 FILE *fnew_ip = fopen("ipaddress1.txt","w"); 00648 FILE *f_ip = fopen("ipaddress.txt","r"); 00649 if (f_ip != NULL) { 00650 00651 char s[40]; 00652 memset(s,'\0',sizeof(s)); 00653 while (fgets (s,sizeof(s),f_ip) != NULL) { 00654 00655 memset(pmuid,'\0',6); 00656 sprintf(pmuid,"%d",temp_pmu->pmuid); 00657 if((strstr(s,pmuid))&&(strstr(s,temp_pmu->protocol))) { 00658 00659 break; 00660 00661 } else { 00662 00663 //Place rest of lines in the new file 00664 fputs(s,fnew_ip); 00665 memset(s,'\0',40); 00666 memset(pmuid,'\0',6); 00667 } 00668 }//While ends 00669 memset(s,'\0',sizeof(s)); 00670 // If ipaddress.txt file still contains data copy it to ipaddress1.txt 00671 while (fgets (s, sizeof(s),f_ip) != NULL) /* read a line */{ 00672 00673 fputs(s,fnew_ip); 00674 memset(s,'\0',40); 00675 } 00676 00677 fclose(f_ip); 00678 fclose(fnew_ip); 00679 00680 if( remove( "ipaddress.txt" ) != 0 ) 00681 perror( "Error deleting file" ); 00682 result = rename("ipaddress1.txt","ipaddress.txt"); 00683 if ( result == 0 ) 00684 fputs ("File successfully renamed",fp_log); 00685 else 00686 perror("Error renaming file" ); 00687 00688 } else { 00689 00690 perror("ipaddress.txt"); 00691 } 00692 00693 00694 /* remove the cfg object from memory */ 00695 int ind = 0,match = 0; 00696 struct cfg_frame *temp_cfg = cfgfirst,*tprev_cfg; 00697 unsigned char id_CODE[2]; 00698 tprev_cfg = temp_cfg; 00699 00700 id_CODE[0] = temp_pmu->pmuid >> 8; 00701 id_CODE[1] = temp_pmu->pmuid ; 00702 00703 while(temp_cfg != NULL){ 00704 if(!ncmp_cbyc(id_CODE,temp_cfg->idcode,2)) { 00705 00706 match = 1; 00707 break; 00708 00709 } else { 00710 00711 ind++; 00712 tprev_cfg = temp_cfg; 00713 temp_cfg = temp_cfg->cfgnext; 00714 00715 } 00716 }// While ends 00717 00718 if(match) { 00719 00720 pthread_mutex_lock(&mutex_cfg); 00721 if(!ind) { 00722 00723 // Replace the cfgfirst 00724 cfgfirst = cfgfirst->cfgnext; 00725 free_cfgframe_object(temp_cfg); 00726 fprintf(fp_log,"Out of FIRST free_cfgframe_object()\n"); 00727 // Get the new value of the CFG frame 00728 pthread_mutex_lock(&mutex_file); 00729 remove_cfg_from_file(id_CODE); 00730 pthread_mutex_unlock(&mutex_file); 00731 00732 } else { 00733 // Replace in between cfg 00734 tprev_cfg->cfgnext = temp_cfg->cfgnext; 00735 free_cfgframe_object(temp_cfg); 00736 fprintf(fp_log,"Out of free_cfgframe_object()\n"); 00737 pthread_mutex_lock(&mutex_file); 00738 remove_cfg_from_file(id_CODE); 00739 pthread_mutex_unlock(&mutex_file); 00740 } 00741 00742 pthread_mutex_unlock(&mutex_cfg); 00743 00744 } 00745 00746 pthread_mutex_lock(&mutex_Lower_Layer_Details); /* Remove the object from structure 'Lower Layer Details'*/ 00747 00748 if(temp_pmu->prev == NULL) { 00749 00750 LLfirst = temp_pmu->next; 00751 00752 if(LLfirst != NULL) 00753 LLfirst->prev = NULL; 00754 00755 00756 } else { 00757 00758 if (temp_pmu == LLlast) 00759 temp_pmu->prev->next = NULL; 00760 00761 else 00762 temp_pmu->prev->next = temp_pmu->next; 00763 } 00764 00765 if(temp_pmu->next == NULL) { 00766 00767 LLlast = temp_pmu->prev; 00768 } else { 00769 if(temp_pmu->prev != NULL) 00770 temp_pmu->prev->next = temp_pmu->next; 00771 00772 } 00773 00774 pthread_mutex_unlock(&mutex_Lower_Layer_Details); 00775 00776 rc = pthread_cancel(tid); 00777 if(rc == 0) 00778 fprintf(fp_log,"Cancelled thread %ld\n",tid); 00779 close(temp_pmu->sockfd); /* Close the socket connection */ 00780 free(temp_pmu); 00781 00782 pthread_exit(NULL); 00783 }
int remove_Lower_Node | ( | char | pmuid[], | |
char | protocol[] | |||
) |
Definition at line 539 of file new_pmu_or_pdc.c.
References Upper_Layer_Details::config_change, fp_log, Lower_Layer_Details::ip, LLfirst, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Lower_Layer_Details::next, Lower_Layer_Details::pmuid, Lower_Layer_Details::port, Lower_Layer_Details::protocol, remove_llnode(), and ULfirst.
Referenced by cmd_or_remove_pmu_validation().
00539 { 00540 00541 int flag = 0, err; 00542 00543 00544 // A new thread is created for each TCP connection in 'detached' mode. Thus allowing any number of threads to be created. 00545 pthread_attr_t attr; 00546 pthread_attr_init(&attr); 00547 if((err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))) { // In the detached state, the thread resources are 00548 // immediately freed when it terminates, but 00549 perror(strerror(err)); // pthread_join(3) cannot be used to synchronize 00550 exit(1); // on the thread termination. 00551 00552 } 00553 00554 00555 if((err = pthread_attr_setschedpolicy(&attr,SCHED_FIFO))) { // Shed policy = SCHED_FIFO (realtime, first-in first-out) 00556 00557 perror(strerror(err)); 00558 exit(1); 00559 } 00560 00561 printf("#### Connection TABLE ####\n"); 00562 printf("-----------------------------------------------------------------\n"); 00563 printf("| PMU ID | IP | Port | Protocol |\n"); 00564 printf("-----------------------------------------------------------------\n"); 00565 00566 if(LLfirst == NULL) { 00567 00568 printf("No PMU Added\n"); 00569 return 1; 00570 00571 } else { 00572 00573 flag = 1; 00574 struct Lower_Layer_Details *t = LLfirst; 00575 00576 while(t != NULL) { 00577 00578 00579 printf("|\t%d\t|%s\t|\t%d\t|\t%s\t|\n",t->pmuid,t->ip,t->port,t->protocol); 00580 t = t->next; 00581 } 00582 00583 printf("-----------------------------------------------------------------\n"); 00584 00585 } 00586 00587 if(flag) { 00588 00589 int match = 0; 00590 struct Lower_Layer_Details *temp_pmu = LLfirst; 00591 00592 while(temp_pmu != NULL) { 00593 00594 if((temp_pmu->pmuid == atoi(pmuid)) && (!strncasecmp(temp_pmu->protocol,protocol,3))) { 00595 00596 match = 1; 00597 break; 00598 00599 } else { 00600 00601 temp_pmu = temp_pmu->next; 00602 00603 } 00604 00605 } 00606 if(match) { 00607 00608 pthread_t t; 00609 if((err = pthread_create(&t,&attr,remove_llnode,(void *)temp_pmu))) { 00610 00611 perror(strerror(err)); 00612 exit(1); 00613 } 00614 00615 struct Upper_Layer_Details *temp_pdc = ULfirst; 00616 pthread_mutex_lock(&mutex_Upper_Layer_Details); 00617 while(temp_pdc != NULL ) { 00618 00619 temp_pdc->config_change = 1; 00620 temp_pdc = temp_pdc->next; 00621 } 00622 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 00623 return 0; 00624 00625 } else { 00626 00627 fprintf(fp_log,"No Match\n"); 00628 return 1; 00629 } 00630 } 00631 return 1; 00632 }
int remove_PDC | ( | char | ip[], | |
char | port_num[], | |||
char | protocol[] | |||
) |
Definition at line 1436 of file new_pmu_or_pdc.c.
References i, Upper_Layer_Details::ip, mutex_Upper_Layer_Details, Upper_Layer_Details::next, Upper_Layer_Details::port, Upper_Layer_Details::prev, Upper_Layer_Details::protocol, TCPPORT, UDPPORT, ULfirst, and ULlast.
Referenced by remove_pdc_validation().
01436 { 01437 01438 char *p,*i,*port; 01439 int flag = 0,result; 01440 01441 if(ULfirst == NULL) { 01442 01443 printf("No PDC Added\n"); 01444 return 1; 01445 01446 } else { 01447 01448 struct Upper_Layer_Details *t = ULfirst; 01449 while(t != NULL) { 01450 01451 if(!strcmp(t->ip,ip)) { 01452 if(((!strncasecmp(t->protocol,"UDP",3)) && (t->port == UDPPORT)) || ((!strncasecmp(t->protocol,"TCP",3)) && (t->port == TCPPORT))) { 01453 01454 flag = 1; 01455 break; 01456 } 01457 } 01458 01459 t = t->next; 01460 } 01461 01462 if(flag) { 01463 pthread_mutex_lock(&mutex_Upper_Layer_Details); 01464 01465 if(t->prev == NULL) { 01466 01467 ULfirst = t->next; 01468 if(ULfirst != NULL) ULfirst->prev = NULL; 01469 01470 } else { 01471 01472 t->prev->next = t->next; 01473 } 01474 01475 if(t->next == NULL) { 01476 01477 ULlast = t->prev; 01478 01479 } else { 01480 if(t->prev != NULL) 01481 t->prev->next = t->next; 01482 } 01483 01484 free(t); 01485 pthread_mutex_unlock(&mutex_Upper_Layer_Details); 01486 01487 FILE *fnew_ip = fopen("upperpdc_ip1.txt","w"); 01488 FILE *f_ip = fopen("upperpdc_ip.txt","r"); 01489 01490 if (f_ip != NULL) { 01491 01492 flag = 0; 01493 char s[40], s1[40]; 01494 memset(s,'\0',40); 01495 memset(s1,'\0',40); 01496 while (fgets (s, 40,f_ip) != NULL) { 01497 01498 strcpy(s1, s); 01499 i = strtok (s," "); 01500 port = strtok (NULL," "); 01501 p = strtok (NULL," "); 01502 01503 if(!strcmp(i,ip) && (!strncasecmp(p,protocol,3))){ 01504 01505 flag = 1; 01506 printf("MATCH\n"); 01507 break; 01508 01509 } else { 01510 01511 //Place rest of lines in the new file 01512 fputs(s1,fnew_ip); 01513 memset(s,'\0',40); 01514 memset(s1,'\0',40); 01515 } 01516 }//While ends 01517 memset(s,'\0',sizeof(s)); 01518 // If ipaddress.txt file still contains data copy it to ipaddress1.txt 01519 while (fgets (s, sizeof(s),f_ip) != NULL) /* read a line */{ 01520 01521 fputs(s,fnew_ip); 01522 memset(s,'\0',40); 01523 } 01524 01525 fclose(f_ip); 01526 fclose(fnew_ip); 01527 01528 if( remove( "upperpdc_ip.txt" ) != 0 ) 01529 perror( "Error deleting file" ); 01530 result = rename("upperpdc_ip1.txt","upperpdc_ip.txt"); 01531 if ( result != 0 ) 01532 perror("Error renaming file" ); 01533 01534 return 0; 01535 } else { 01536 01537 perror("upperpdc_ip.txt"); 01538 return 1; 01539 } 01540 01541 } else { 01542 01543 printf("\nNo Match\n"); 01544 return 1; 01545 } 01546 } 01547 }