void JobServer::executeCommand(std::string s) { std::stringstream ss; std::string line; ss << s; getline(ss, line, ' '); //get the first argument from the string if(line == "issuejob") //if it's issuejob { MyJob* mj = getJob(s); //create the new job queuedJobs.insertEntry(mj); //add it in the queued jobs while(!queuedJobs.isEmpty() && concurrency > activeJobs.length()) addNewJob(); //then try to activate it sendResponse(mj); //send the response jobCount++; //increase the number of total jobs return; //and return } if(line == "setConcurrency") //if it's set concurrency { setConcurrency(getSecondToLastNumber(s)); //change the concurrency while(!queuedJobs.isEmpty() && concurrency > activeJobs.length()) addNewJob(); //try to add more jobs return; //return } if(line == "stop") //if it's stop { //get the number you want stopped int jobid = getSecondToLastNumber(s); if(queuedJobs.jobIdExists(jobid)) //check if it's in queued jobs { queuedJobs.removeEntryByJobId(jobid); //and remove it } else //else { int pid = activeJobs.getPidByJobId(jobid); //get it's pid if(pid != -1) { kill(pid, SIGTERM);//send a signal for it to be terminated activeJobs.removeEntryByJobId(jobid); //and remove it from the list } while(!queuedJobs.isEmpty() && concurrency > activeJobs.length()) addNewJob(); //then add new jobs to replace that } return; //and return } if(line == "poll") { //if it's poll getline(ss, line, ' '); if(line == "queued") //find out if it's queued or active sendResponse(1); else //and send the appropriate response sendResponse(0); return; //then return } if(line == "exit") { //if it's exit terminate(); //close up everything exit(0); //and exit } }
void server_run() { int filedes,filed,size,numbofargs,i,jsize=0,jobID=0,digit_count = 0,num,status,N,id; pid_t cpid,commpid,stopid; char *buffer; char *temp; char *command; char *jobname; char *str; char **args; static struct sigaction act; act.sa_handler = handler; sigfillset(&(act.sa_mask)); sigaction(SIGRTMIN, &act, NULL); static struct sigaction chd; chd.sa_handler = child_handler; sigfillset(&(chd.sa_mask)); sigaction(SIGCHLD, &chd, NULL); if (( filedes = open("server", O_CREAT | O_RDWR , 0666))== -1) { perror (" creating "); exit (3) ; } cpid = getpid(); num = cpid; digit_count += no_of_digits(num); str = malloc(sizeof(char)*(digit_count+1));//to str einai ena string pou exei mesa to pid tou server str[digit_count] = '\0'; while(num > 0){ str[digit_count-1] = num%10 + '0'; num = num/10; digit_count--; } if ((write(filedes,str,sizeof(strlen(str)+1))) == -1) { perror ("Error in Writing "); exit (4) ; } free(str); if ((filed = open("myfifo",O_RDWR)) <0 ){ perror (" fifo open problem "); exit(8); } list_create(); kill(getppid(),SIGRTMIN); while (1) { flag = 0; while (flag == 0) sleep(1); jsize = 0; read(filed,&commpid,sizeof(pid_t)); read(filed,&numbofargs,sizeof(int)); args = (char**)malloc(numbofargs*sizeof(char*)); read(filed,&size,sizeof(int)); buffer = (char *)malloc(size); read(filed,buffer,size); temp = (char *)malloc(size); strcpy(temp,buffer); command = strtok(temp," ");//pairnw thn entolh(issuejob,poll klp) for (i=0;i<numbofargs;i++) { args[i] = strtok(NULL," "); jsize += strlen(args[i])+1;//kai ta argument auths } jsize += 1; jobname = (char *)malloc(jsize); jobname[0] = '\0'; for (i=0;i<numbofargs;i++) { strcat(jobname,args[i]); strcat(jobname," "); } if (strcmp(command,"issuejob") == 0) { if (args[0] == NULL) {//se periptwsh pou den dwthei argument meta to issuejob free(args); free(temp); free(buffer); free(jobname); continue; } jobID++; issuejob(args,numbofargs,jobname,jobID,&status); write(filed,&jobID,sizeof(int)); write(filed,&status,sizeof(int)); write(filed,&jsize,sizeof(int)); write(filed,jobname,jsize); kill(commpid,SIGRTMIN); } else if (strcmp(command,"setConcurrency") == 0) { if (args[0] == NULL) { free(args); free(temp); free(buffer); free(jobname); continue; } N = atoi(args[0]); setConcurrency(N); } else if (strcmp(command,"stop") == 0) { if (args[0] == NULL) { free(args); free(temp); free(buffer); free(jobname); continue; } id = atoi(args[0]); search_and_delete(id,&stopid); if (stopid != 0) kill(stopid,SIGKILL); } else if (strcmp(command,"poll") == 0) { if (args[0] == NULL) { free(args); free(temp); free(buffer); free(jobname); continue; } if (strcmp(args[0],"running") == 0) poll(1,filed); else if (strcmp(args[0],"queued") == 0) poll(0,filed); else { free(args); free(temp); free(buffer); free(jobname); continue; } kill(commpid,SIGRTMIN); } else if (strcmp(command,"exit") == 0) { queued_delete_all();//sbinw olh th lista anamonhs free(args); free(temp); free(buffer); free(jobname); while (get_running_jobs() != 0)//kai prin kleisei o server perimenw na teleiwsoun oles oi running jobs sleep(1); break; } free(args); free(temp); free(buffer); free(jobname); } list_destroy(); remove("server"); remove("myfifo"); }