void transferThread::halt() { this->m_stop = true; emit sendStatus("Aborted"); emit sendLog("Transfer Aborted"); }
NelderMead::NelderMead(std::vector<double *> newParamPtrs, std::vector<double> expectedRanges, void *object, double (*score)(void *object)) { alpha = 1; gamma = 2; rho = -0.5; sigma = 0.5; unlimited = false; std::vector<double *>streamlinedPtrs; for (int i = 0; i < newParamPtrs.size(); i++) { if (newParamPtrs[i] != NULL) streamlinedPtrs.push_back(newParamPtrs[i]); else { expectedRanges.erase(expectedRanges.begin() + i); newParamPtrs.erase(newParamPtrs.begin() + i); i--; } } paramPtrs = streamlinedPtrs; evaluationFunction = score; evaluatingObject = object; int testPointCount = (int)paramCount() + 1; testPoints.resize(testPointCount); if (paramCount() == 0) return; assert(paramCount() > 1); for (int i = 0; i < testPoints.size(); i++) { testPoints[i].second = 0; testPoints[i].first.resize(paramCount()); logged << "Test point parameter " << i << ": "; for (int j = 0; j < paramCount(); j++) { if (i == 0) { testPoints[i].first[j] = *paramPtrs[j]; } if (i > 0) { int minJ = i - 1; double scale = 2; testPoints[i].first[j] = testPoints[0].first[j] + (j == minJ) * scale * expectedRanges[j]; } logged << testPoints[i].first[j] << ", " << std::endl; } } logged << "Test point evaluations: "; for (int i = 0; i < testPoints.size(); i++) { evaluateTestPoint(i); logged << testPoints[i].second << ", "; } logged << std::endl; std::vector<double> centroid = calculateCentroid(); logged << "Starting centroid: " << std::endl; for (int i = 0; i < centroid.size(); i++) { logged << centroid[i] << ", "; } logged << std::endl; sendLog(LogLevelDebug); }
int main(int argc, char** argv) { signal(SIGHUP, HUPhandler); signal(SIGTERM, killhandler); signal(SIGINT, killhandler); signal(SIGQUIT, killhandler); signal(SIGALRM, deathklockhandler); //signal(SIGINT, killhandler); printf("My process ID : %d\n", getpid()); pid_t g_mainpid = getpid(); //Load the config for the first time g_configstruct = loadConf(); //Fork off the log processes pid_t logpid; //logfork logpid = fork(); if(logpid < 0) { printf("Error"); exit(1); } else if (logpid==0) { srvlog(); } //makelogfifio mkfifo(g_logfifo, 0666); g_logfifofd = open(g_logfifo, O_WRONLY); if(!g_killflag) //prevent log from starting { sendLog("status_ready"); } //Main program loop while(1) { //printf("Waiting for log\n"); fflush(stdout); //write(g_logfifofd, "status_ready", sizeof("status_ready")); if(g_killflag==1) { fflush(stdout); break; } web(); fflush(stdout); } //The final countdown! g_deathklock = logpid; alarm(5); sendLog("status_shutdown"); waitpid(logpid,NULL,0); printf("logger shutdown!\n"); alarm(0); close(g_logfifofd); unlink(g_logfifo); printf("Application Terminated Successfully\n\n"); fflush(stdout); return 0; }
void *udpConnection(void *args) { // sleep(5); //TCP communication w/ Mr. Manager struct threadArgs *thrdArg = (struct threadArgs *)args; int sockfd = thrdArg->fd; char addr[15]; strcpy(addr, thrdArg->adr); char host[15]; strcpy(host, thrdArg->hst); strcpy(listenOn , thrdArg->listen); //UDP listen sockfd_udp int sockfd_udp; struct addrinfo hints_udp, *servinfo_udp, *p_udp; struct sockaddr_storage their_addr; int MAXBUFLEN = 100; char listenBuf[MAXBUFLEN],sendBuf[MAXBUFLEN]; socklen_t addr_len; char s_udp[INET6_ADDRSTRLEN]; int rv, numbytes; memset(&hints_udp, 0, sizeof hints_udp); hints_udp.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4 hints_udp.ai_socktype = SOCK_DGRAM; hints_udp.ai_flags = AI_PASSIVE; // use my IP if ((rv = getaddrinfo(NULL, listenOn , &hints_udp, &servinfo_udp)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); } // loop through all the results and bind to the first we can for(p_udp = servinfo_udp; p_udp != NULL; p_udp = p_udp->ai_next) { if ((sockfd_udp = socket(p_udp->ai_family, p_udp->ai_socktype, p_udp->ai_protocol)) == -1) { perror("listener: socket"); continue; } if (bind(sockfd_udp, p_udp->ai_addr, p_udp->ai_addrlen) == -1) { close(sockfd_udp); perror("listener: bind"); continue; } break; } if (p_udp == NULL) { fprintf(stderr, "listener: failed to bind socket\n"); } freeaddrinfo(servinfo_udp); ////////////////////UDP THREAD///////////////////////////////////////////////////////// while(1) { //recieve message/packet from mr. manager to fwd //udp recieve printf("listener: waiting to recvfrom...\n"); //setup select to wait for connection ack struct timeval tv; fd_set readfds; tv.tv_sec = 2; tv.tv_usec = 500000; FD_ZERO(&readfds); FD_SET(sockfd_udp, &readfds); select(sockfd_udp+1, &readfds, NULL, NULL, &tv); if (FD_ISSET(sockfd_udp, &readfds)) { addr_len = sizeof their_addr; if ((numbytes = recvfrom(sockfd_udp, listenBuf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) { perror("recvfrom"); exit(1); } } if (numbytes == 0) continue; char sendBuf[100]; int i = 0; //debug messages printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s_udp, sizeof s_udp)); printf("listener: packet is %d bytes long\n", numbytes); listenBuf[numbytes] = '\0'; //Copy listenBuf to sendBuf memcpy ( (void *) sendBuf, (void *) listenBuf, numbytes); sendBuf[numbytes] = '\0'; // strcpy(sendBuf, listenBuf); printf("listener: packet contains \"%s\"\n", listenBuf); //debug message printf("sendBuf contains : %s \n", sendBuf); //debug message char type = listenBuf[0]; if((int)type == 1) { uint send_id = (int) ( listenBuf[2] | (listenBuf[1] << 8) ); printf("send id is : %d\n", send_id); //debug message char data[50]; int k,l; for(k =3,l=0 ; k< numbytes; k++, l++) { data[l] = listenBuf[k] ; printf("%c\n" , data[l]); //debug message } data[l] = '\0'; printf("atoi of addr: %s\n", addr); //debug message if((int)send_id == atoi(addr)) //Message arrived at destination { printf("LOG DICKS\n"); sendLog(send_id, data, 1, sockfd); } else { struct nodeInfo * sendPort = findHash(send_id); //Send only if ID of next hop == ID of send_id //Else lookup info for next hop while(sendPort->ID != sendPort->nextHop) sendPort = findHash(sendPort->nextHop); //if(sendPort->ID != sendPort->nextHop) char dstPort[15]; for(k = 0; k < 15; k++ ) dstPort[k] = sendPort->udpPort[k]; //UDP forward sendLog(sendPort->ID, data, 0, sockfd); //Get external ip address struct ifaddrs * ifAddrStruct=NULL; struct ifaddrs * ifa=NULL; void * tmpAddrPtr=NULL; char addressBuffer[INET_ADDRSTRLEN]; getifaddrs(&ifAddrStruct); for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4 // is a valid IP4 Address tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN); //printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); } else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6 // is a valid IP6 Address tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; char addressBuffer[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN); //printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer); } } if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct); //UDP send sockfd_send struct addrinfo hints, *p; int sockfd_send; struct addrinfo *servinfo_send; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ((rv = getaddrinfo(addressBuffer, dstPort, &hints, &servinfo_send)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); } // loop through all the results and make a socket for(p = servinfo_send; p != NULL; p = p->ai_next) { if ((sockfd_send = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("talker: socket"); continue; } break; } if (p == NULL) { fprintf(stderr, "talker: failed to bind socket\n"); } for(k = 0 ; k<numbytes; k++) printf("%c\n" , listenBuf[k]); printf("listenBuf length is: %d\n", strlen(listenBuf)); //debug message if ((numbytes = sendto(sockfd_send, listenBuf, numbytes, 0, p->ai_addr, p->ai_addrlen)) == -1) { perror("talker: sendto"); exit(1); } freeaddrinfo(servinfo_send); printf("talker: sent %d bytes to %s\n", numbytes, dstPort); close(sockfd_send); } } else //if((int)type == 0) { char *space = " "; char message[10]; char temp[100]; char propogate[100]; int identify, lookup; char tempID[10]; char cst[10]; char templookup[10]; char savehsh[100]; strcpy(temp, listenBuf); //strcpy(propogate, listenBuf); strcpy(savehsh, listenBuf); strcpy(message, strtok(listenBuf, space)); if(strcmp(message, "ACK") != 0) { strtok(temp, space); strcpy(tempID, strtok('\0', space)); strtok('\0', space); strtok('\0', space); strcpy(cst, strtok('\0', space)); strcpy(templookup, strtok('\0', space)); if(strcmp(message, "NEW") == 0) saveHash(savehsh, 0); else if(strcmp(message, "UPDATE") == 0) { identify = atoi(tempID); updateHash(identify, cst); } //Send the ack lookup = atoi(templookup); struct nodeInfo *look = findHash(lookup); sendMessageHelper("ACK ", look->udpPort); } else ackflag = 1; //propagateControl(propogate); } } close(sockfd_udp); return NULL; }
int web() { sendLog("Web Server Started"); //Load the config for our server srvRun = 0; //set the server run vallue to zero //inital setup int websockfs, newsockfd, portno, sockdead; uint clilen; char buffer[CHAR_MAX]; struct sockaddr_in serv_addr, cli_addr; int n; //Make a new socket called websockfs websockfs = socket(AF_INET, SOCK_STREAM, 0); if (websockfs < 0) { perror("Error creating socket \n "); kill(g_mainpid,SIGTERM); } //Create socket strcuture memset(&serv_addr, 0, sizeof(serv_addr)); portno = atoi(g_configstruct.port); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(g_configstruct.interface); serv_addr.sin_port = htons(portno); //This option will let us reuse the port even if it is already in use //This allows us to restart the program with out running into issues int optval = 1; setsockopt(websockfs, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); //Create a new binding address if (bind(websockfs, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { perror("Error binding socket shutting down!"); kill(g_mainpid,SIGTERM); } //STOP THE ZOMBIES, this causes the children to die and stay dead. struct sigaction sigchld_action = { .sa_handler = SIG_DFL, .sa_flags = SA_NOCLDWAIT }; sigaction(SIGCHLD, &sigchld_action, NULL); // The listener will now fork off into a child procces pid_t listenpid; if(srvRun==0) { srvRun=1; //Begin fork fflush(stdout); listenpid = fork(); if(listenpid < 0) { sendLog("Error forking listen child"); exit(1); } else if (listenpid==0) { //Wait and listen for incoming socket connections sendLog("Listen"); listen(websockfs,5); clilen = sizeof(cli_addr); while(1) { newsockfd = accept(websockfs, (struct sockaddr *)&cli_addr, &clilen); //After we close the socket and we are sending this child a sigterm shutdown if(g_killflag==1) { wait(NULL); close(newsockfd); close(websockfs); sendLog("Web server shutdown"); exit(0); } if (newsockfd < 0) { sendLog("Error reading from sock"); exit(0); } //Another fork, after the server forms a socket connection with the //client it will fork off into another child proccess pid_t clientpid; clientpid = fork(); if(clientpid < 0) { printf("Error"); } else if (clientpid==0) { prctl(PR_SET_PDEATHSIG, SIGKILL); memset(buffer,0,sizeof(buffer)); n = read( newsockfd,buffer,sizeof(buffer)); if (n < 0) { sendLog("Error reading from socket"); } FILE *requestfile = NULL; char header[CHAR_MAX]; //Parse the users request and return a FILE to send //and a HTTP response header parser(&header,&requestfile,&buffer); //We will write to the socket our header, and file writeSock(header,requestfile,&newsockfd); //shutdown(websockfs,0); //Clean up time shutdown(newsockfd,2); close(newsockfd); fclose(requestfile); //printf("socket closed\n"); fflush(stdout); sendLog("Close Socket"); exit(0); } else { //Close the parents copy of the newsockfd close(newsockfd); } } //shutdown(websockfs,2); } printf("Going to main loop\n"); fflush(stdout); //Parent Loop continues untill the sighup or sigkill while(1) { if(g_hupflag==1) { sendLog("Reloading Config"); sendLog("Server stoping for reload"); g_deathklock = listenpid; alarm(5); kill(listenpid,SIGTERM); shutdown(websockfs,2); waitpid(listenpid,NULL,0); alarm(0); sendLog("reload_config"); g_configstruct = loadConf(); g_hupflag = 0; return 0; } sleep(1); if(g_killflag==1) { //Going through the steps to kill child printf("Got a shutdown signal!\n"); sendLog("Shutting down server"); g_deathklock = listenpid; alarm(5); kill(listenpid,SIGTERM); shutdown(websockfs,2); waitpid(listenpid,NULL,0); alarm(0); return 0; } } } return 0; }