Beispiel #1
0
  inline static dt::string call(vm_heap &heap, const dt::string &file_name) {
    std::string file_name_string(file_name.to_string());
    
    ::np1::io::gzfile f;
    if (!f.open_ro(file_name_string.c_str())) {
      return dt::string();
    }

    // Get the file size so we can provide an order-of-magnitude estimate for the buffer size.
    uint64_t file_size = 0;
    if (!::np1::io::file::get_size(file_name_string.c_str(), file_size)) {
      return dt::string();
    }

    // This cast down from uint64_t to size_t is ok because it's just an allocation estimate.
    ::np1::io::ext_heap_buffer_output_stream<vm_heap> buffer_output_stream(heap, file_size);
    unsigned char buffer[256 * 1024];
    size_t bytes_read = 0;
    bool result;
    while ((result = f.read_some(buffer, sizeof(buffer), &bytes_read)) && (bytes_read > 0)) {
      buffer_output_stream.write(buffer, bytes_read);
    }
    
    if (!result) {
      return dt::string();
    }
    
    char *final_buffer_ptr = (char *)buffer_output_stream.ptr();
    size_t total_size = buffer_output_stream.size();
    str::replace_invalid_utf8_sequences(final_buffer_ptr, total_size, '?');
    return dt::string(final_buffer_ptr, total_size);
  }
Beispiel #2
0
    sf::Image load_image(const char* file_name, std::size_t file_name_length)
    {
      std::string file_name_string(file_name, file_name_length);
      auto stream = make_ifstream(file_name_string);
      if (!stream) throw detail::image_error(file_name_string);

      return load_image(stream);
    }
Beispiel #3
0
/******************************************************************************
Description: function for transmitting the frames
Input Value.:
Return Value:
******************************************************************************/
void server_transmit (int sock, string userID)
{
    // printf("transmitting part\n");
    
    int n;
    char response[] = "ok";
    char file_name_temp[60];
    char *file_name;
    int write_length = 0;
    int length = 0;
    queue<string> *imgQueue = new queue<string>();    // queue storing the file names 

    // grap the lock
    pthread_mutex_lock(&queue_map_lock);
    queue_map[userID] = imgQueue; // put the address of queue into map
    pthread_mutex_unlock(&queue_map_lock);

    pthread_mutex_t queueLock; // mutex lock for queue operation
    sem_t *sem_match = 0;

    // init the mutex lock
    if (pthread_mutex_init(&queueLock, NULL) != 0)
    {
        errorSocket("ERROR mutex init failed", sock);
    }

    if (!orbit)
    {
        char buffer[BUFFER_SIZE];
        char *file_size_char;
        int file_size;
        int received_size = 0;

        // reponse to the client
        n = write(sock, response, sizeof(response));
        if (n < 0)
        {
            pthread_mutex_destroy(&queueLock);
            errorSocket("ERROR writting to socket", sock);
        }

        while (!global_stop)
        {
            received_size = 0;
            // receive the file info
            bzero(buffer, sizeof(buffer));
            n = read(sock,buffer, sizeof(buffer));
            if (n <= 0)
            {
                pthread_mutex_destroy(&queueLock);
                // signal the result thread to terminate
                sem_post(sem_match);
                errorSocket("ERROR reading from socket", sock);
            } 

            // store the file name and the block count
            file_name = strtok(buffer, ",");
            strcpy(file_name_temp, file_name);
            if (debug) printf("\n[server] file name: [%s]\n", file_name);
            file_size_char = strtok(NULL, ",");
            file_size = strtol(file_size_char, NULL, 10);
            if (debug) printf("file size: %d\n", file_size);

            // calculate the time consumption here
            struct timeval tpstart,tpend;
            double timeuse;
            gettimeofday(&tpstart,NULL);

            // reponse to the client
            n = write(sock, response, sizeof(response));
            if (n <= 0)
            {
                pthread_mutex_destroy(&queueLock);
                // signal the result thread to terminate
                sem_post(sem_match);
                errorSocket("ERROR writting to socket", sock);
            } 


            if (!storm)
            {
                FILE *fp = fopen(file_name, "w");  
                if (fp == NULL)  
                {  
                    printf("File:\t%s Can Not Open To Write!\n", file_name);  
                    break;
                }  

                int done = 0;
                // receive the data from client and store them into buffer
                bzero(buffer, sizeof(buffer));
                while((length = recv(sock, buffer, sizeof(buffer), 0)))  
                {
                    if (length < 0)  
                    {  
                        printf("Recieve Data From Client Failed!\n");  
                        break;  
                    }

                    int remain = file_size - received_size;
                    if (remain < BUFFER_SIZE) {
                        length = remain;
                        done = 1;
                    }
              
                    write_length = fwrite(buffer, sizeof(char), length, fp);  
                    if (write_length < length)
                    {  
                        printf("File:\t Write Failed!\n");  
                        break;  
                    }  
                    bzero(buffer, sizeof(buffer));

                    if (done)
                    {
                        if (debug) printf("file size full\n");
                        break;
                    }
                    received_size += length;
                }

                // print out time comsumption
                gettimeofday(&tpend,NULL);
                timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us
                // printf("used time:%fus\n",timeuse);
                printf("receive used time:%fms\n",timeuse / 1000);

                if (debug) printf("[server] Recieve Finished!\n\n");  

                // finished 
                fclose(fp);
            }
            // below is storm mode
            else
            {
                // in storm mode, don't save img into disk
                int offset = 0;
                char* img = new char[file_size];
                int done = 0;
                // receive the data from server and store them into buffer
                bzero(buffer, sizeof(buffer));
                while((length = recv(sock, buffer, sizeof(buffer), 0)))  
                {
                    if (length < 0)  
                    {  
                        printf("Recieve Data From Client Failed!\n");  
                        break;  
                    }

                    int remain = file_size - offset;
                    if (remain < BUFFER_SIZE) {
                        length = remain;
                        done = 1;
                    }
              
                    // copy the content into img
                    for (int i = 0; i < length; ++i)
                    {
                        img[i + offset] = buffer[i];
                    }

                    bzero(buffer, sizeof(buffer));
                    if (done)
                    {
                        if (debug) printf("offset: %d\n", offset + remain);
                        if (debug) printf("file size full\n");
                        break;
                    }
                    offset += length;
                    // if (debug) printf("offset: %d\n", offset);
                }

                // print out time comsumption
                gettimeofday(&tpend,NULL);
                timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us
                // printf("used time:%fus\n",timeuse);
                printf("receive used time:%fms\n",timeuse / 1000);

                if (debug) printf("[server] Recieve Finished!\n\n");  

                // send request to spout
                if (debug) printf("Now try to connect the spout\n");
                int sockfd, ret;
                int spoutPort = 9878;
                struct sockaddr_in spout_addr;
                struct hostent *spout;
                struct in_addr ipv4addr;
                char buf_spout[100];
                char* spout_IP;
                const int len = spoutIP.length();
                spout_IP = new char[len+1];
                strcpy(spout_IP, spoutIP.c_str());

                sockfd = socket(AF_INET, SOCK_STREAM, 0);
                if (sockfd < 0)
                {
                    printf("ERROR opening socket\n");
                    return;
                }
                inet_pton(AF_INET, spout_IP, &ipv4addr);
                spout = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET);
                if (debug) printf("\n[server] Spout address: %s\n", spout_IP);
                if (spout == NULL) {
                    fprintf(stderr,"ERROR, no such host\n");
                    exit(0);
                }
                bzero((char *) &spout_addr, sizeof(spout_addr));
                spout_addr.sin_family = AF_INET;
                bcopy((char *)spout->h_addr, (char *)&spout_addr.sin_addr.s_addr, spout->h_length); 
                spout_addr.sin_port = htons(spoutPort);

                while (connect(sockfd,(struct sockaddr *) &spout_addr, sizeof(spout_addr)) < 0)
                {
                    printf("The spout is not available now, wait a while and reconnect\n\n");
                    usleep(100000); // sleep 100ms
                }

                printf("[server] Get connection to spout\n");

                bzero(buf_spout, sizeof(buf_spout));
                sprintf(buf_spout, "%d", file_size);
                if (debug) printf("[server] send the file size\n");
                ret = write(sockfd, buf_spout, sizeof(buf_spout));
                if (ret < 0)
                {
                    printf("error sending\n");
                    return;
                }

                // get the response
                bzero(buf_spout, sizeof(buf_spout));
                if (debug) printf("[server] now wait for response\n");
                ret = read(sockfd, buf_spout, sizeof(buf_spout));
                if (ret < 0)
                {
                    printf("error reading\n");
                    return;
                }

                if (debug) printf("got response: %s\n", buf_spout);

                if (debug) printf("[server] send the img\n");
                ret = write(sockfd, img, file_size);
                if (ret < 0)
                {
                    printf("error sending\n");
                    return;
                }
                if (debug) printf("ret: %d\n", ret);
                printf("[server] Finished transmitting image to spout\n\n");

                close(sockfd);
                delete[] img;

            }

            // lock the queue, ensure there is only one thread modifying the queue
            pthread_mutex_lock(&queueLock);

            // store the file name to the waiting queue
            string file_name_string(file_name_temp);
            imgQueue->push(file_name_string);

            pthread_mutex_unlock(&queueLock);
            // get the address of sem_match
            if (sem_match == 0)
            {
                while (sem_map.find(userID) == sem_map.end());
                sem_match = sem_map[userID];
            }
            // signal the result thread to do image processing
            sem_post(sem_match);
            // pause();
        }

        close(sock);
    }
    // below is orbit mode
    else
    {
        // get the id length
        int id_length = 1;
        int divisor = 10;
        while (sock / divisor > 0)
        {
            ++id_length;
            divisor *= 10;
        }
        int recv_length = BUFFER_SIZE * 4; // 4096 bytes per time
        char buffer[recv_length];
        char *file_size_char;
        int file_size;
        int received_size = 0;

        if (debug) printf("\nstart receiving file\n");


        // reponse to the client
        MsgD.send(sock, response, sizeof(response));

        while (!global_stop)
        {
            received_size = 0;
            bzero(buffer, sizeof(buffer));
            // get the file info from client
            n = MsgD.recv(sock, buffer, 100);
            if (n < 0)
            {
                pthread_mutex_destroy(&queueLock);
                // signal the result thread to terminate
                sem_post(sem_match);
                errorSocket("ERROR reading from socket", sock);
                return;
            } 
            file_name = strtok(buffer, ",");
            strcpy(file_name_temp, file_name);
            printf("\n[server] file name: [%s]\n", file_name);
            file_size_char = strtok(NULL, ",");
            file_size = strtol(file_size_char, NULL, 10);
            printf("[server] file size: %d\n", file_size);

            // calculate the time consumption here
            struct timeval tpstart,tpend;
            double timeuse;
            gettimeofday(&tpstart,NULL);

            // reponse to the client
            MsgD.send(sock, response, sizeof(response));

            // local mode
            if (!storm) {

                FILE *fp = fopen(file_name, "w");  
                if (fp == NULL)  
                {  
                    printf("File:\t[%s] Can Not Open To Write!\n", file_name);  
                }  


                // receive the data from server and store them into buffer
                while(1)  
                {
                    bzero(buffer, sizeof(buffer));
                    n = MsgD.recv(sock, buffer, sizeof(buffer));
                    if (n <= 0)
                    {
                        pthread_mutex_destroy(&queueLock);
                        // signal the result thread to terminate
                        sem_post(sem_match);
                        errorSocket("ERROR reading from socket", sock);
                    } 
                    
                    if (file_size - received_size <= recv_length)
                    {
                        int remain = file_size - received_size;
                        write_length = fwrite(buffer, sizeof(char), remain, fp);  
                        if (write_length < remain)  
                        {  
                            printf("File:\t Write Failed!\n");  
                            break;  
                        }
                        break;
                    }

                    write_length = fwrite(buffer, sizeof(char), recv_length, fp);  
                    if (write_length < recv_length)  
                    {  
                        printf("File:\t Write Failed!\n");  
                        break;  
                    }  
                    received_size += recv_length;
                }

                // print out time comsumption
                gettimeofday(&tpend,NULL);
                timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us
                // printf("used time:%fus\n",timeuse);
                printf("receive used time:%fms\n",timeuse / 1000);

                printf("[server] Recieve Finished!\n\n");  
                // finished 
                fclose(fp);
            }
            // below is storm mode
            else {
                // in storm mode, don't save img into disk
                char* img = new char[file_size];
                int offset = 0;
                // receive the data from client and store them into buffer
                while(1)  
                {
                    bzero(buffer, sizeof(buffer));
                    n = MsgD.recv(sock, buffer, sizeof(buffer));
                    if (n <= 0)
                    {
                        pthread_mutex_destroy(&queueLock);
                        // signal the result thread to terminate
                        sem_post(sem_match);
                        errorSocket("ERROR reading from socket", sock);
                    } 
                    
                    if (file_size - received_size <= recv_length)
                    {
                        int remain = file_size - received_size;
                        // copy the content into img
                        for (int i = 0; i < remain; ++i)
                        {
                            img[i + offset] = buffer[i];
                        }
                        break;
                    }

                    // copy the content into img
                    for (int i = 0; i < recv_length; ++i)
                    {
                        img[i + offset] = buffer[i];
                    }
                    offset += recv_length;

                    received_size += recv_length;
                }

                // print out time comsumption
                gettimeofday(&tpend,NULL);
                timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;// notice, should include both s and us
                // printf("used time:%fus\n",timeuse);
                printf("receive used time:%fms\n",timeuse / 1000);

                if (debug) printf("[server] Recieve Finished!\n\n");  

                // send request to spout
                if (debug) printf("Now try to connect the spout\n");
                int sockfd, ret;
                int spoutPort = 9878;
                struct sockaddr_in spout_addr;
                struct hostent *spout;
                struct in_addr ipv4addr;
                char buf_spout[100];
                char* spout_IP;
                const int len = spoutIP.length();
                spout_IP = new char[len+1];
                strcpy(spout_IP, spoutIP.c_str());

                sockfd = socket(AF_INET, SOCK_STREAM, 0);
                if (sockfd < 0)
                {
                    printf("ERROR opening socket\n");
                    return;
                }
                inet_pton(AF_INET, spout_IP, &ipv4addr);
                spout = gethostbyaddr(&ipv4addr, sizeof(ipv4addr), AF_INET);
                if (debug) printf("\n[server] Spout address: %s\n", spout_IP);
                if (spout == NULL) {
                    fprintf(stderr,"ERROR, no such host\n");
                    exit(0);
                }
                bzero((char *) &spout_addr, sizeof(spout_addr));
                spout_addr.sin_family = AF_INET;
                bcopy((char *)spout->h_addr, (char *)&spout_addr.sin_addr.s_addr, spout->h_length); 
                spout_addr.sin_port = htons(spoutPort);

                while (connect(sockfd,(struct sockaddr *) &spout_addr, sizeof(spout_addr)) < 0)
                {
                    printf("The spout is not available now, wait a while and reconnect\n\n");
                    usleep(100000); // sleep 100ms
                }

                printf("[server] Get connection to spout\n");

                bzero(buf_spout, sizeof(buf_spout));
                sprintf(buf_spout, "%d", file_size);
                if (debug) printf("[server] send the file size\n");
                ret = write(sockfd, buf_spout, sizeof(buf_spout));
                if (ret < 0)
                {
                    printf("error sending\n");
                    return;
                }

                // get the response
                bzero(buf_spout, sizeof(buf_spout));
                if (debug) printf("[server] now wait for response\n");
                ret = read(sockfd, buf_spout, sizeof(buf_spout));
                if (ret < 0)
                {
                    printf("error reading\n");
                    return;
                }

                if (debug) printf("got response: %s\n", buf_spout);

                if (debug) printf("[server] send the img\n");
                ret = write(sockfd, img, file_size);
                if (ret < 0)
                {
                    printf("error sending\n");
                    return;
                }
                if (debug) printf("ret: %d\n", ret);
                printf("[server] Finished transmitting image to spout\n\n");

                close(sockfd);
                delete[] img;
            }
            
            // lock the queue, ensure there is only one thread modifying the queue
            pthread_mutex_lock(&queueLock);

            // store the file name to the waiting queue
            string file_name_string(file_name_temp);
            imgQueue->push(file_name_string);

            pthread_mutex_unlock(&queueLock);
            // get the address of sem_match
            if (sem_match == 0)
            {
                while (sem_map.find(userID) == sem_map.end());
                sem_match = sem_map[userID];
            }
            // signal the result thread to do image processing
            sem_post(sem_match);
        }
    }

    delete(imgQueue);
    printf("[server] Connection closed. --- transmit\n\n");
    // pthread_exit(NULL); //terminate calling thread!
    return;

}
/******************************************************************************
Description: function for transmitting the frames
Input Value.:
Return Value:
******************************************************************************/
void server_transmit (int sock, string userID)
{
    // printf("transmitting part\n");
    
    int n;
    char buffer[BUFFER_SIZE];
    char response[] = "ok";
    char file_name_temp[60];
    char *file_name;
    int write_length = 0;
    int length = 0;
    char *block_count_char;
    int block_count;
    int count = 0;
    queue<string> *imgQueue = new queue<string>();    // queue storing the file names 

    // grap the lock
    pthread_mutex_lock(&queue_map_lock);
    queue_map[userID] = imgQueue; // put the address of queue into map
    pthread_mutex_unlock(&queue_map_lock);

    pthread_mutex_t queueLock; // mutex lock for queue operation
    sem_t *sem_match = 0;

    // init the mutex lock
    if (pthread_mutex_init(&queueLock, NULL) != 0)
    {
        errorSocket("ERROR mutex init failed", sock);
    }

    if (!orbit)
    {
        // reponse to the client
        n = write(sock, response, sizeof(response));
        if (n < 0)
        {
            pthread_mutex_destroy(&queueLock);
            errorSocket("ERROR writting to socket", sock);
        }

        while (!global_stop)
        {
            // receive the file info
            bzero(buffer,BUFFER_SIZE);
            n = read(sock,buffer, sizeof(buffer));
            if (n <= 0)
            {
                pthread_mutex_destroy(&queueLock);
                // signal the result thread to terminate
                sem_post(sem_match);
                errorSocket("ERROR reading from socket", sock);
            } 

            // store the file name and the block count
            file_name = strtok(buffer, ",");
            strcpy(file_name_temp, file_name);
            printf("\n[server] file name: %s\n", file_name);
            block_count_char = strtok(NULL, ",");
            block_count = strtol(block_count_char, NULL, 10);
            // printf("block count: %d\n", block_count);

            // reponse to the client
            n = write(sock, response, sizeof(response));
            if (n <= 0)
            {
                pthread_mutex_destroy(&queueLock);
                // signal the result thread to terminate
                sem_post(sem_match);
                errorSocket("ERROR writting to socket", sock);
            } 

            FILE *fp = fopen(file_name, "w");  
            if (fp == NULL)  
            {  
                printf("File:\t%s Can Not Open To Write!\n", file_name);  
                break;
            }  

            // receive the data from server and store them into buffer
            bzero(buffer, sizeof(buffer));
            count = 0;
            while((length = recv(sock, buffer, BUFFER_SIZE, 0)))  
            {
                if (length < 0)  
                {  
                    printf("Recieve Data From Client Failed!\n");  
                    break;  
                }
          
                write_length = fwrite(buffer, sizeof(char), length, fp);  
                if (write_length < length)  
                {  
                    printf("File:\t Write Failed!\n");  
                    break;  
                }  
                bzero(buffer, BUFFER_SIZE);
                ++count;
                if (count >= block_count)
                {
                    // printf("block count full\n");
                    break;
                }
            }
            printf("[server] Recieve Finished!\n\n");  
            // finished 
            fclose(fp);

            // lock the queue, ensure there is only one thread modifying the queue
            pthread_mutex_lock(&queueLock);

            // store the file name to the waiting queue
            string file_name_string(file_name_temp);
            imgQueue->push(file_name_string);

            pthread_mutex_unlock(&queueLock);
            // get the address of sem_match
            if (sem_match == 0)
            {
                while (sem_map.find(userID) == sem_map.end());
                sem_match = sem_map[userID];
            }
            // signal the result thread to do image processing
            sem_post(sem_match);
        }

        close(sock);
    }
    // below is orbit mode
    else
    {
        // get the id length
        int id_length = 1;
        int divisor = 10;
        while (sock / divisor > 0)
        {
            ++id_length;
            divisor *= 10;
        }
        int recv_length = BUFFER_SIZE - 6 - id_length;
        char *file_size_char;
        int file_size;
        int received_size = 0;

        if (debug) printf("\nstart receiving file\n");


        // reponse to the client
        MsgD.send(sock, response, BUFFER_SIZE);

        while (!global_stop)
        {
            received_size = 0;
            bzero(buffer, BUFFER_SIZE);
            // get the file info from client
            n = MsgD.recv(sock, buffer, BUFFER_SIZE);
            if (n < 0)
            {
                errorSocket("ERROR reading from socket", sock);
            }
            file_name = strtok(buffer, ",");
            strcpy(file_name_temp, file_name);
            printf("\n[server] file name: %s\n", file_name);
            file_size_char = strtok(NULL, ",");
            file_size = strtol(file_size_char, NULL, 10);
            printf("file size: %d\n", file_size);

            // reponse to the client
            MsgD.send(sock, response, BUFFER_SIZE);
            
            FILE *fp = fopen(file_name, "w");  
            if (fp == NULL)  
            {  
                printf("File:\t%s Can Not Open To Write!\n", file_name);  
            }  


            // receive the data from server and store them into buffer
            while(1)  
            {
                bzero(buffer, BUFFER_SIZE);
                n = MsgD.recv(sock, buffer, BUFFER_SIZE);
                if (n < 0)
                {
                    errorSocket("ERROR reading from socket", sock);
                }
                
                if (file_size - received_size <= recv_length)
                {
                    int remain = file_size - received_size;
                    write_length = fwrite(buffer, sizeof(char), remain, fp);  
                    if (write_length < remain)  
                    {  
                        printf("File:\t Write Failed!\n");  
                        break;  
                    }
                    break;
                }

                write_length = fwrite(buffer, sizeof(char), recv_length, fp);  
                if (write_length < recv_length)  
                {  
                    printf("File:\t Write Failed!\n");  
                    break;  
                }  
                received_size += BUFFER_SIZE - 6 - id_length;
            }
            printf("[server] Recieve Finished!\n\n");  
            // finished 
            fclose(fp);

            // lock the queue, ensure there is only one thread modifying the queue
            pthread_mutex_lock(&queueLock);

            // store the file name to the waiting queue
            string file_name_string(file_name_temp);
            imgQueue->push(file_name_string);

            pthread_mutex_unlock(&queueLock);
            // get the address of sem_match
            if (sem_match == 0)
            {
                while (sem_map.find(userID) == sem_map.end());
                sem_match = sem_map[userID];
            }
            // signal the result thread to do image processing
            sem_post(sem_match);
        }
    }

    delete(imgQueue);
    printf("[server] Connection closed. --- transmit\n\n");
    pthread_exit(NULL); //terminate calling thread!

}