int setSchedTime(struct timeval tMin) { int ret = 0; struct __client *pClient; struct timeval tRet; pClient = g_Resource1.next; while(pClient) { struct timeval tCPaddMA; struct timeval tMaxAge; setTimeValue(&tMaxAge, g_Resource1.uiMaxAge/1000, (g_Resource1.uiMaxAge%1000)*1000); addTimeValue(&tCPaddMA, g_Resource1.tBaseTime, tMaxAge); if(isBiggerThan(pClient->tSched, tCPaddMA)) { struct timeval tTemp; struct timeval tAllow; subTimeValue(&tTemp, pClient->tSched, tCPaddMA); setTimeValue(&tAllow, (pClient->uiReqInterval*2/10)/1000, ((pClient->uiReqInterval*2/10)%1000)*1000); //setTimeValue(&tAllow, (pClient->uiReqInterval*3/10)/1000, ((pClient->uiReqInterval*3/10)%1000)*1000); //setTimeValue(&tAllow, (pClient->uiReqInterval/10)/1000, ((pClient->uiReqInterval/10)%1000)*1000); //setTimeValue(&tAllow, (pClient->uiReqInterval/5)/1000, ((pClient->uiReqInterval/5)%1000)*1000); //setTimeValue(&tAllow, (pClient->uiReqInterval/4)/1000, ((pClient->uiReqInterval/4)%1000)*1000); //setTimeValue(&tAllow, (pClient->uiReqInterval/2)/1000, ((pClient->uiReqInterval/2)%1000)*1000); //setTimeValue(&tAllow, 0, 10*1000); if(isBiggerThan(tAllow, tTemp)) { //TODO: set new schedule time //setTimeValue(&(pClient->tSched), tCPaddMA.tv_sec , tCPaddMA.tv_usec); setTimeValue(&(pClient->tSched), g_Resource1.tBaseTime.tv_sec , g_Resource1.tBaseTime.tv_usec); } else { //TODO: leave it for next processing ; } } else { setTimeValue(&(pClient->tSched), g_Resource1.tBaseTime.tv_sec , g_Resource1.tBaseTime.tv_usec); } pClient = pClient->next; } return ret; }
int isCachedDataValid(struct timeval curTime) { struct timeval timeB; struct timeval timeRet; int ret = 0; //printf("%s:g_Resource1.uiCachedAge=%d, g_Resource1.uiMaxAge=%d\n", __func__, g_Resource1.uiCachedAge, g_Resource1.uiMaxAge); if( (g_Resource1.uiCachedAge<g_Resource1.uiMaxAge) && (g_Resource1.tCachedTime.tv_sec>0) ) { struct timeval tMaxAge; struct timeval tTemp; setTimeValue(&tMaxAge, g_Resource1.uiMaxAge/1000, (g_Resource1.uiMaxAge%1000)*1000); addTimeValue(&tTemp, g_Resource1.tCachedTime, tMaxAge); //printf("%s:tMaxAge=%6ld.%06ld\n", __func__, tMaxAge.tv_sec, tMaxAge.tv_usec); printf("%s:tTemp=%6ld.%06ld,curTime=%6ld.%06ld\n", __func__, tTemp.tv_sec, tTemp.tv_usec, curTime.tv_sec, curTime.tv_usec); if(isBiggerThan(tTemp, curTime)) ret = 1; else ret = 0; } else { ret = 0; } return ret; }
int getSchedTime(struct timeval *tRet, struct timeval tNow, unsigned int uiReqInterval) { struct __client *pClient = g_Resource1.next; struct timeval tReqInterval; tReqInterval.tv_sec = uiReqInterval/1000; tReqInterval.tv_usec = uiReqInterval%1000*1000; if(g_Resource1.iClientNumber == 0) { tRet->tv_sec = tNow.tv_sec+1; tRet->tv_usec = 0; addTimeValue(&(g_Resource1.tBaseTime), *tRet, tReqInterval); } else { //struct timeval tSched = pClient->tSched; struct timeval tSched; setTimeValue(&tSched, g_Resource1.tBaseTime.tv_sec, g_Resource1.tBaseTime.tv_usec); while(1) { struct timeval tTemp = tSched; subTimeValue(&tTemp, tTemp, tReqInterval); if(isBiggerThan(tTemp, tNow)) { tSched = tTemp; } else { break; } } setTimeValue(tRet, tSched.tv_sec, tSched.tv_usec); } printf("Req=%d;S(%ld.%06ld);B(%ld.%06ld);N(%ld.%06ld)\n", uiReqInterval, tRet->tv_sec, tRet->tv_usec, g_Resource1.tBaseTime.tv_sec, g_Resource1.tBaseTime.tv_usec, tNow.tv_sec, tNow.tv_usec); }
int updateCache(struct timeval curTime) { struct timeval temp; int ret = 0; if(isBiggerThan(curTime, g_Resource1.tCachedTime)) { subTimeValue(&temp, curTime, g_Resource1.tCachedTime); g_Resource1.uiCachedAge = temp.tv_sec*1000 + temp.tv_usec/1000; } else { g_Resource1.uiCachedAge = 0; } //printf("%s:g_Resource1.uiCachedAge=%d\n", __func__, g_Resource1.uiCachedAge); return ret; }
int getEarlistSchedTime(struct timeval *ptMin, struct timeval curTime) { int ret = 0; struct __client *pClient; pClient = g_Resource1.next; while(pClient) { //if(pClient->tSched) { if(isBiggerThan(*ptMin, pClient->tSched)) { setTimeValue(ptMin, pClient->tSched.tv_sec, pClient->tSched.tv_usec); } } pClient = pClient->next; } return ret; }
int setSchedTime(struct timeval tMin) { int ret = 0; struct __client *pClient; struct timeval tRet; pClient = g_Resource1.next; while(pClient) { if(isEqualTo(pClient->tSched, tMin)) { ; //printf("Equal!\n"); } else { subTimeValue(&tRet, pClient->tSched, tMin); addTimeValue(&tRet, tRet, tRet); struct timeval tB; tB.tv_sec = pClient->uiReqInterval/1000; tB.tv_usec = (pClient->uiReqInterval%1000)*1000; //printf("NOT Equal!\n"); if(isBiggerThan(tB, tRet)) { setTimeValue(&(pClient->tSched), tMin.tv_sec , tMin.tv_usec); } } pClient = pClient->next; } return ret; }
/** * Adds a movie in the correct order of a list using recursion. * Creates a new head if necessary. * * precondition: none * * param: movie - the movie to insert * param: head - the head of the list to insert into * param: method - the method of sorting (BY_TITLE, BY_LENGTH, BY_RATING) * param: size - the size of the list. */ void model::InterlacedMovieList::addToList(MovieNode* movie, MovieNode* head, const string& method, int size) { if (size == 1) { int result = compareMovies(head, movie, method); if (result > 0 && this->size == 1) { replaceHead(head, movie, method); return; } else { insertAfter(head, movie, method); return; } } else { MovieNode* next; if (method.compare(BY_TITLE) == 0) { next = head->getNextTitle(); } else if (method.compare(BY_LENGTH) == 0) { next = head->getNextLength(); } else { next = head->getNextRating(); } int comparedToHead = compareMovies(movie, head, method); int comparedToNext = compareMovies(movie, next, method); if (size == this->size && comparedToHead == -1) { insertAfter(head, movie, method); } if (isEqualTo(comparedToHead) || (isSmallerThan(comparedToHead) && isBiggerThan(comparedToNext))) { insertAfter(head, movie, method); return; } else { addToList(movie, next, method, (size - 1)); return; } } }
int main(int argc, char *argv[]) { char line[MAXLINE], message[MAXLINE+1]; int n, pid; struct sockaddr_in proxy_addr; int maxfdp1; int s; fd_set read_fds; struct __message msg = {0x0, }; struct __message resp = {0x0, }; if(argc < 3) { printf("usage1 : %s [proxy_ip#] [proxy_port#]\n", argv[0]); printf("usage2 : %s [proxy_ip#] [proxy_port#] [monitoring_mode#] [monitoring_interval#]\n", argv[0]); exit(0); } //if(argc else if(argc >= 5) { g_monMode = atoi(argv[3]); g_monInterval = atoi(argv[4]); if(argc >= 6) { strncpy(g_strLogName, argv[5], strlen(argv[5])); if(argc == 7) { g_iLogCount = atoi(argv[6]); } } else { strncpy(g_strLogName, CLIENT_LOG_NAME, strlen(CLIENT_LOG_NAME)); } } /* fLog = fopen(g_strLogName, "w"); printf("fLog=%d\n", fLog); fprintf(fLog, "Hello Minjin\n"); fprintf(fLog, "Hello Minjin!!!\n"); fclose(fLog); */ if( (s=socket(PF_INET, SOCK_STREAM, 0)) < 0 ) { printf("client : socket failed!\n"); exit(0); } //if( (s=socket bzero((char *)&proxy_addr, sizeof(struct sockaddr_in)); proxy_addr.sin_family = AF_INET; proxy_addr.sin_addr.s_addr = inet_addr(argv[1]); proxy_addr.sin_port = htons(atoi(argv[2])); if(connect(s, (struct sockaddr *)&proxy_addr, sizeof(struct sockaddr_in)) < 0) { printf("client : connect failed!\n"); exit(0); } else { printf("client : connected server!\n"); } //if(connect( maxfdp1 = s + 1; FD_ZERO(&read_fds); memset(&msg, 0x0, sizeof(struct __message)); srand(time(NULL)); msg.owner = random()%10000; printf("CLIEND:owner=%d\n", msg.owner); //#if !ENABLE_REPEATE if(g_monMode == RESOURCE_CMD_REGISTER) { initMessage(&msg); msg.cmd = RESOURCE_CMD_REGISTER; msg.req_dur = g_monInterval; if(send(s, &msg, sizeof(struct __message), 0) < 0) { printf("client : send failed!\n"); } //if(send( } //#endif //#if 0 while(1) { FD_SET(0, &read_fds); FD_SET(s, &read_fds); //#if ENABLE_REPEATE if(g_monMode == RESOURCE_CMD_GET) { initMessage(&msg); msg.cmd = RESOURCE_CMD_GET; if(send(s, &msg, sizeof(struct __message), 0) < 0) { printf("client : send failed!\n"); } //if(send( } //#endif if(select(maxfdp1, &read_fds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0) { printf("client : select failed\n"); exit(0); }//if(select if(FD_ISSET(s, &read_fds)) { int size; if((size = recv(s, &resp, sizeof(struct __message), 0)) > 0) { handleMessage(resp); } //if((size } //if(FD_ISSET( if(g_monMode == RESOURCE_CMD_GET) { //usleep(g_monInterval*1000); struct timeval timeSched; struct timeval timeB; struct timeval timeNow; timeB.tv_sec = g_monInterval/1000; timeB.tv_usec = (g_monInterval%1000)*1000; addTimeValue(&timeSched, msg.client_started, timeB); //printf("timeSched:%ld.%06ld\n", timeSched.tv_sec, timeSched.tv_usec); while(1) { usleep(1000); gettimeofday(&timeNow, NULL); if(isBiggerThan(timeNow, timeSched)) break; } } } //while(1) //fclose(fLog); return 0; } //int main(
void *pthreadWatchResource(void *arg) { struct timeval timeSched; struct timeval tStart; while(!g_iExit) { if(g_Resource1.iClientNumber) { //TODO: get current time //gettimeofday(&tStart, NULL); //TODO: search all clients registered struct __client *client = g_Resource1.next; while(client) { gettimeofday(&tStart, NULL); //TODO: find scheduled client if(isBiggerThan(tStart, client->tSched)) { struct __message msg; struct timeval tEnd; //TODO: if cached resource is valid, then use it if(g_uiCacheMode && isCachedDataValid(tStart) ) { //TODO: update cache timing information updateCache(tStart); //TODO: set server process time with zero setTimeValue(&(msg.server_recved), 0, 0); setTimeValue(&(msg.server_started), 0, 0); setTimeValue(&(msg.server_finished), 0, 0); //TODO: set message with time information msg.resource = g_Resource1.iCachedResource; msg.uiMaxAge = g_Resource1.uiMaxAge - g_Resource1.uiCachedAge; //printf("%s:uiMaxAge=%d(%d-%d)\n", __func__, msg.uiMaxAge, g_Resource1.uiMaxAge, g_Resource1.uiCachedAge); //TODO: get current time gettimeofday(&tEnd, NULL); } else { //TODO: init cache initCache(); //TODO: get a fresh resource from a server getResourceFromServer(&msg); //TODO: get current time gettimeofday(&tEnd, NULL); //TODO: set cached with information setCache(msg, tEnd); } //TODO: set client process time setTimeValue(&(msg.client_recved), 0, 0); setTimeValue(&(msg.client_started), 0, 0); //TODO: set proxy process time setTimeValue(&(msg.proxy_recved), tStart.tv_sec, tStart.tv_usec); setTimeValue(&(msg.proxy_started), tStart.tv_sec, tStart.tv_usec); setTimeValue(&(msg.proxy_finished), tEnd.tv_sec, tEnd.tv_usec); //TODO: dump message dumpMessage(msg); //TODO: send information as response from proxy to client int temp = send(client->iFd, &msg, sizeof(struct __message), 0); //TODO: set a new scheduled time of client struct timeval tB; tB.tv_sec = client->uiReqInterval/1000; tB.tv_usec = (client->uiReqInterval%1000)*1000; addTimeValue(&(client->tSched), client->tSched, tB); //printf("tStart=%ld.%06ld, tSched=%ld.%06ld\n", // tStart.tv_sec, tStart.tv_usec, // client->tSched.tv_sec, client->tSched.tv_usec); } client = client->next; //printf("%s:client=0x%x\n", __func__, (unsigned int)client); } //TODO: inser client node as time sequence //updateClient(); //TODO: if(g_uiCacheAlgorithm == 1) { if(isBiggerThan(tStart, g_Resource1.tBaseTime)) { //TODO: update check point updateBaseTime(); } } else if(g_uiCacheAlgorithm == 2) { struct timeval tCPaddMA; struct timeval tMaxAge; setTimeValue(&tMaxAge, g_Resource1.uiMaxAge/1000, (g_Resource1.uiMaxAge%1000)*1000); addTimeValue(&tCPaddMA, g_Resource1.tBaseTime, tMaxAge); if(isBiggerThan(tStart, tCPaddMA)) { setSchedule(tStart); } } usleep(5*1000); } //if(g_Resource1.iClientNumber) usleep(50*1000); } return NULL; }