예제 #1
0
void* stopKeeperThread(void * args){
    Robot *rb = (Robot*) args;
    int socket_fd = rb->socket_robo;
     if(socket_fd){
        int recvbytes = 0;
        int BUFFER_SIZE = 2048;
        char read_buff[BUFFER_SIZE];
          while(1){
            bzero(read_buff, sizeof(read_buff));
            if(-1 == (recvbytes = recv(socket_fd,read_buff,BUFFER_SIZE, 0))){
                printf("read data fail \n");
            }
            else
            {
                string recvCmd(read_buff);

                if(recvCmd == "stopKeeper"){
                    printf("receive scan\n");
                    //do something
                    rb->abort = true;
                    char tmp[BUFFER_SIZE];
                    strcpy(tmp, "stopKeeper_ack");
                    if (send(socket_fd, tmp, (int)strlen(tmp), 0) < 0){
                        printf("send stopKeeper_ack fail\n");
                    }
                    else{
                        printf("send stopKeeper_ack success\n");
                    }
                    return NULL;
                }//if
            }//else
        }//while
    }
    return NULL;
}
예제 #2
0
//FIXME Apparently XPutFile exists so use that instead.
//FIXME hardcoded ad-hid format for dag.
void putFile(int sock, char *ad, char*hid, const char *fin, const char *fout)
{
	char cmd[512];
	sprintf(cmd, "put %s %s %s %s ",  ad,hid,fin,fout);
	sendCmd(sock, cmd);
	recvCmd((void *)&sock);
	say("done with put file\n");
}
예제 #3
0
void  Robot::listenAndAct(){
    int socket_fd = socket_robo;
    if(socket_fd){
        int recvbytes = 0;
        int BUFFER_SIZE = 2048;
        char read_buff[BUFFER_SIZE];

        while(1){
            bzero(read_buff, sizeof(read_buff));
            if(-1 == (recvbytes = recv(socket_fd,read_buff,BUFFER_SIZE, 0))){
                printf("read data fail \n");
            }
            else
            {
                string recvCmd(read_buff);

                if(recvCmd == "scan"){
                    printf("receive scan\n");
                    //do something
                    drawMap();
                    char tmp[BUFFER_SIZE];
                    strcpy(tmp, "scan_ack");
                    if (send(socket_fd, tmp, (int)strlen(tmp), 0) < 0){
                        printf("send scan_ack fail\n");
                    }
                    else{
                        printf("send scan_ack success\n");
                    }
                }
                else if(recvCmd == "keeper"){
                    printf("receive keeper\n");
                    //do something
                    pthread_t t1;
                    pthread_create(&t1, NULL, stopKeeperThread, this);
                    keepGoal();
                    char tmp[BUFFER_SIZE];
                    strcpy(tmp, "keeper_ack");
                    if (send(socket_fd, tmp, (int)strlen(tmp), 0) < 0){
                        printf("send keeper_ack fail\n");
                    }
                    else{
                        printf("send keeper_ack success\n");
                    }
                }
                else if(recvCmd == "shoot"){
                    printf("receive shoot\n");
                    //do something
                    shoot();
                    char tmp[BUFFER_SIZE];
                    strcpy(tmp, "shoot_ack");
                    if (send(socket_fd, tmp, (int)strlen(tmp), 0) < 0){
                        printf("send shoot_ack fail\n");
                    }
                    else{
                        printf("send shoot_ack success\n");
                    }
                }
                else if(recvCmd == "spin"){
                    printf("receive spin\n");
                    //do something
                    spin();
                    char tmp[BUFFER_SIZE];
                    strcpy(tmp, "spin_ack");
                    if (send(socket_fd, tmp, (int)strlen(tmp), 0) < 0){
                        printf("send spin_ack fail\n");
                    }
                    else{
                        printf("send spin_ack success\n");
                    }
                }
                else if(recvCmd == "exit"){
                    printf("receive exit\n");
                    close(socket_fd);
                    break;
                }
                else if(recvCmd == "pic"){
                    char cmd[200];
                    bzero(cmd, sizeof(cmd));
                    strcpy(cmd, "sendpicstart");
                    if(send(socket_fd, cmd, (int)strlen(cmd), 0) < 0){
                        printf("send start cmd fail\n");
                        continue;
                    }
                    if(-1 == (recvbytes = recv(socket_fd,read_buff,BUFFER_SIZE, 0))){
                        printf("receive start cmd ack fail\n");
                        continue;
                    }
                    if(strcmp(read_buff, "fileack") != 0){
                        printf("receive not fileack\n");
                        continue;
                    }

                    int sendContent = 1;
                    int width = 128, height = 128;
                    IplImage* src = cvLoadImage("Radar.png");
                    if(src == NULL){
                        printf("no Radar.png\n");
                        continue;
                    }
                    int dst_width = 128, dst_height = 128;
                    IplImage* img = cvCreateImage(cvSize(dst_width, dst_height), src->depth, 3);
                    cvResize(src, img, CV_INTER_LINEAR);

                    uchar* img_data = (uchar *)img->imageData;

                    int sendRgbSize = 128*3;
                    int dataCharSize = width*height*3*3;
                    int sendCharSize = 128*3*3;
                    int sendTime = dataCharSize/sendCharSize;
                    printf("sendTime: %d\n", sendTime);

                    uchar sendChardata[sendCharSize];
                    for(int i = 0; i < sendTime; i++){
                        bzero(sendChardata, sendCharSize);
                        for(int j = 0; j < sendRgbSize; j++){
                            int x = (int)img_data[i*sendRgbSize+j];
                            sendChardata[j*3+0] = x/100+'0';
                            sendChardata[j*3+1] = (x%100)/10+'0';
                            sendChardata[j*3+2] = (x%10)+'0';
                        }
                        //cout << i << endl;

                        if(send(socket_fd, sendChardata, sendCharSize, 0) < 0){
                            printf("%d send rgb_data fail\n", i);
                            sendContent = 0;
                            break;
                        }

                        //cout << i << " send rgb_data success" << endl;
                        if(-1 == (recvbytes = recv(socket_fd,read_buff,BUFFER_SIZE, 0))){
                            printf("%d receive data ack fail\n", i);
                            sendContent = 0;
                            break;
                        }
                        if(strcmp(read_buff, "fileack") != 0){
                            printf("%d receive not ackfile\n", i);
                            sendContent = 0;
                            break;
                        }
                    }
                    if(sendContent == 0){
                        printf("sendContent = false\n");
                        continue;
                    }
                    bzero(cmd, sizeof(cmd));
                    strcpy(cmd, "sendpicfinish");
                    if(send(socket_fd, cmd, (int)strlen(cmd), 0) < 0){
                        printf("send file finish fail\n");
                        continue;
                    }
                    printf("data all send\n");
                    cvReleaseImage(&src);
                    cvReleaseImage(&img);
                }//else if

            }//else
        }
    }
}