static int get_wbuf_from_sigs(struct iobuf *wbuf, struct slist *slist, uint8_t *end_flags) { static char req[32]=""; struct sbuf *sb=slist->blks_to_request; while(sb && !(sb->flags & SBUF_NEED_DATA)) sb=sb->next; if(!sb) { slist->blks_to_request=NULL; if((*end_flags)&END_SIGS && !((*end_flags)&END_BLK_REQUESTS)) { iobuf_from_str(wbuf, CMD_GEN, (char *)"blk_requests_end"); (*end_flags)|=END_BLK_REQUESTS; } return 0; } if(!sb->protocol2->bsighead) { // Trying to move onto the next file. // ??? Does this really work? if(sb->protocol2->bend) { slist->blks_to_request=sb->next; printf("move to next\n"); } if((*end_flags)&END_SIGS && !((*end_flags)&END_BLK_REQUESTS)) { iobuf_from_str(wbuf, CMD_GEN, (char *)"blk_requests_end"); (*end_flags)|=END_BLK_REQUESTS; } return 0; } if(sb->protocol2->bsighead->got==BLK_INCOMING) return 0; if(sb->protocol2->bsighead->got==BLK_NOT_GOT) { encode_req(sb->protocol2->bsighead, req); iobuf_from_str(wbuf, CMD_DATA_REQ, req); sb->protocol2->bsighead->requested=1; } // Move on. if(sb->protocol2->bsighead==sb->protocol2->bend) { slist->blks_to_request=sb->next; sb->protocol2->bsighead=sb->protocol2->bstart; } else { sb->protocol2->bsighead=sb->protocol2->bsighead->next; } return 0; }
static int get_wbuf_from_sigs(struct iobuf *wbuf, struct slist *slist, struct blist *blist, int sigs_end, int *blk_requests_end, struct dpth *dpth, struct conf *conf) { static char req[32]=""; struct sbuf *sb=slist->blks_to_request; while(sb && !(sb->flags & SBUF_NEED_DATA)) sb=sb->next; if(!sb) { slist->blks_to_request=NULL; if(sigs_end && !*blk_requests_end) { iobuf_from_str(wbuf, CMD_GEN, (char *)"blk_requests_end"); *blk_requests_end=1; } return 0; } if(!sb->burp2->bsighead) { // Trying to move onto the next file. // ??? Does this really work? if(sb->burp2->bend) { slist->blks_to_request=sb->next; printf("move to next\n"); } if(sigs_end && !*blk_requests_end) { iobuf_from_str(wbuf, CMD_GEN, (char *)"blk_requests_end"); *blk_requests_end=1; } return 0; } if(sb->burp2->bsighead->got==BLK_INCOMING) { // if(sigs_end // && deduplicate(sb->burp2->bsighead, dpth, conf, wrap_up)) // return -1; return 0; } if(sb->burp2->bsighead->got==BLK_NOT_GOT) { encode_req(sb->burp2->bsighead, req); iobuf_from_str(wbuf, CMD_DATA_REQ, req); sb->burp2->bsighead->requested=1; } // Move on. if(sb->burp2->bsighead==sb->burp2->bend) { slist->blks_to_request=sb->next; sb->burp2->bsighead=sb->burp2->bstart; } else { sb->burp2->bsighead=sb->burp2->bsighead->next; } return 0; }
void* _server_worker (void *args) { assert (args != NULL); // get data and free it worker_arg_t *p = (worker_arg_t *)args; server_t *server = p->server; int sd = p->sd; free (args); // get log zlog_category_t *category = zlog_get_category("server"); zlog_debug (category, "Worker: serve descripter %d.", sd); // producer shutdown signal boolean producer_shutdown = false; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // worker id unsigned int worker_id = pthread_self (); // loop while (server->shutdown == false) { // block and read request_t request; int status = _read_request (sd, &request); // update sd request.client_sd = sd; // client closed if (status == 0) { zlog_info (category, "Client at %d closed.", sd); producer_shutdown = true; pthread_mutex_lock (&mutex); pthread_mutex_unlock (&mutex); close (sd); break; } assert (status == sizeof (request_t)); // debug char tmp[256]; encode_req (&request, tmp); zlog_info (category, "REQUEST RECEIVED: %s", tmp); // deal with NOT_EXIST if (_check_movie (&request, server) == false) { response_t response; memcpy (&response.request, &request, sizeof (request_t)); response.worker_id = pthread_self (); response.data = malloc ((strlen (MSG_NOT_EXIST) + 1) * sizeof (char)); strcpy (response.data, MSG_NOT_EXIST); response.len = strlen (MSG_NOT_EXIST) + 1; // block and write _write_response (sd, (void *)&response); // free free (response.data); break; } // malloc here, free at _producer producer_arg_t *arg = (producer_arg_t *)malloc (sizeof (producer_arg_t)); assert (arg != NULL); memset (arg, 0, sizeof (producer_arg_t)); // setup arg->server = server; arg->worker_id = worker_id; memcpy (&arg->request, &request, sizeof (request_t)); arg->shutdown = &producer_shutdown; arg->mutex = &mutex; // decode command if (strcmp (request.request, REQ_START) == 0) { // start a movie // shutdown previous producer producer_shutdown = true; pthread_mutex_lock (&mutex); pthread_mutex_unlock (&mutex); producer_shutdown = false; // start from 1 arg->request.frame_number = 1; producer_shutdown = false; // submit server->cluster.submit (&server->cluster, _server_producer, (void *)arg); } else if (strcmp (request.request, REQ_SEEK) == 0) { // shutdown previous producer producer_shutdown = true; pthread_mutex_lock (&mutex); pthread_mutex_unlock (&mutex); producer_shutdown = false; arg->request.frame_number = request.frame_number; // submit server->cluster.submit (&server->cluster, _server_producer, (void *)arg); } else if (strcmp (request.request, REQ_STOP) == 0) { producer_shutdown = true; } else { // command not supported assert (false); } } pthread_mutex_destroy (&mutex); }