void 
CoordinateOnItemObject::createCoordinatePackets() {
   // The mapIDs to process
   for ( uint32 i = 0; i < m_inputs.size() ; i++) {
      if ( m_mapIDs.linearSearch( m_inputs[i].m_mapID ) == MAX_UINT32 ) {
         // Add unique mapID
         m_mapIDs.addLast( m_inputs[i].m_mapID );
         m_packetIDs.addLast( getNextPacketID() );
      }
   }
   
   for ( uint32 i = 0 ; i < m_mapIDs.getSize() ; i++ ) {
      uint32 mapID = m_mapIDs[ i ];
      CoordinateOnItemRequestPacket* p = new CoordinateOnItemRequestPacket(
         m_packetIDs[ i ], getRequestID(), m_bboxWanted );
      p->setMapID( mapID );

      uint32 nbrAdded = 0;
      for ( uint32 i = 0; i < m_inputs.size() ; i++) {
         if ( m_inputs[i].m_mapID == mapID ) {
            p->add( m_inputs[i].m_itemID, m_inputs[i].m_offset );
            m_inputs[i].m_index = nbrAdded;
            nbrAdded++;
         }
      }
      m_packetsReadyToSend.add( new PacketContainer( p, 0, 0, 
                                                     MODULE_TYPE_MAP ) ); 
   }

   m_nbrCoordinatePackets = m_mapIDs.getSize();
   mc2dbg2 << "[COIO]: m_nbrCoordinatePackets = " << m_nbrCoordinatePackets
          << endl;
   m_nbrReceivedCoordinatePackets = 0;
}
// Useful to when you want to save the EziFBIncomingRequest Object in CCArray or CCDictionary.
CCObject* EziFBIncomingRequest::copyWithZone(CCZone *pZone)
{
    CCZone* pNewZone = NULL;
    EziFBIncomingRequest* pCopy = NULL;
    if(pZone && pZone->m_pCopyObject)
    {
        //in case of being called at sub class
        pCopy = (EziFBIncomingRequest*)(pZone->m_pCopyObject);
    }
    else
    {
        pCopy = new EziFBIncomingRequest();
        pNewZone = new CCZone(pCopy);
    }
    
    pCopy->setReceiverID(getReceiverID());
    pCopy->setSender(getSender());
    pCopy->setRequestID(getRequestID());
    pCopy->setMessage(getMessage());
    pCopy->setConsumed(isConsumed());
    pCopy->setDataDictionary(getDataDictionary());
    
    CC_SAFE_DELETE(pNewZone);
    return pCopy;
}
Example #3
0
//Main Method
int main(int argc, char *argv[])
{
	if(argc != 5) {
		quit("Usage: %s <server_port> <robot_IP/robot_hostname> <robot_ID> <image_id>", argv[0]);
	}
	//read args
	unsigned short localUDPPort = atoi(argv[1]);
	char* robotAddress = argv[2];
	char* robotID = argv[3];
	char* imageID = argv[4];

	plog("Read arguments");
	plog("Robot address: %s", robotAddress);
	plog("Robot ID: %s", robotID);
	plog("Image ID: %s", imageID);

	//listen for ctrl-c and call flushBuffersAndExit()
	signal(SIGINT, flushBuffersAndExit);

	//Create socket for talking to clients
	int clientSock;
	if((clientSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
		quit("could not create client socket - socket() failed");
	}

	plog("Created client socket: %d", clientSock);

	//Construct local address structure for talking to clients
	struct sockaddr_in localAddress;
	memset(&localAddress, 0, sizeof(localAddress));		//Zero out structure
	localAddress.sin_family = AF_INET;					//Any Internet address family
	localAddress.sin_addr.s_addr = htonl(INADDR_ANY);	//Any incoming interface
	localAddress.sin_port = htons(localUDPPort);		//The port clients will sendto

	if(bind(clientSock, (struct sockaddr *) &localAddress, sizeof(localAddress)) < 0) {
		quit("could not bind to client socket - bind() failed");
	}

	plog("binded to client socket");

	//Loop for each client request
	for(;;) {
		plog("Start loop to handle client request");

		//Receive request from client
		struct sockaddr_in clientAddress;
		unsigned int clientAddressLen = sizeof(clientAddress);	//in-out parameter
		char clientBuffer[MAXLINE+1];	//Buffer for incoming client requests
		memset(clientBuffer, 0, MAXLINE+1);

		int recvMsgSize;
		//Block until receive a guess from a client
		if((recvMsgSize = recvfrom(clientSock, clientBuffer, MAXLINE, 0,
						(struct sockaddr *) &clientAddress, &clientAddressLen)) < 0) {
			quit("could not receive client request - recvfrom() failed");
		}

		plog("Received request of %d bytes", recvMsgSize);

		//Interpret client request
		char* requestRobotID = getRobotID(clientBuffer);
		if(strcmp(robotID, requestRobotID) != 0) {
			fprintf(stderr, "invalid request - robot ID's don't match\n");
			continue;
		}

		plog("Requested robot ID: %s", requestRobotID);

		char* requestStr = getRequestStr(clientBuffer);
		char* robotPort = getRobotPortForRequestStr(requestStr);

		plog("Request string: %s", requestStr);
		plog("Calculated port: %s", robotPort);

		//Send HTTP request to robot
		int robotSock;
		if((robotSock = setupClientSocket(robotAddress, robotPort, SOCKET_TYPE_TCP)) < 0) {
			quit("could not connect to robot");
		}

		plog("Set up robot socket: %d", robotSock);

		char* httpRequest = generateHTTPRequest(robotAddress, robotID, requestStr, imageID);


		plog("Created http request: %s", httpRequest);

		if(write(robotSock, httpRequest, strlen(httpRequest)) != strlen(httpRequest)) {
			quit("could not send http request to robot - write() failed");
		}

		plog("Sent http request to robot");

		free(httpRequest);

		plog("freed http request");

		//Read response from Robot
		int pos = 0;
		char* httpResponse = malloc(MAXLINE);
		int n;
		char recvLine[MAXLINE+1]; //holds one chunk of read data at a time
		while((n = read(robotSock, recvLine, MAXLINE)) > 0) {
			memcpy(httpResponse+pos, recvLine, n);
			pos += n;
			httpResponse = realloc(httpResponse, pos+MAXLINE);
		}
		uLong complen, ucomplen;

		plog("Received http response of %d bytes", pos);
		plog("http response: ");
#ifdef DEBUG
		int j;
		for(j = 0; j < pos; j++)
			fprintf(stderr, "%c", httpResponse[j]);
#endif

		//Parse Response from Robot
		char* httpBody = strstr(httpResponse, "\r\n\r\n")+4;
		ucomplen = (uLong)atoi((strstr(httpResponse, "Content-Length: ")+16));
		complen = (((int)ceil(ucomplen * 1.001)) +12);
		char complen_str[10];
		char ucomplen_str[10];
		char* compressed=malloc(complen);
		printf("ucomplen: %lu\n", ucomplen);

		compress((Bytef *)compressed, &complen, (Bytef *)httpBody, ucomplen);

		int httpBodyLength = (int)complen +(sprintf(complen_str, "%lus\t", complen) + sprintf(ucomplen_str, "%lus\t", ucomplen));
		char* send = malloc(httpBodyLength);
		int temp = sprintf(send, "%lus\t%lus\t", complen, ucomplen);
		memcpy((send+(temp*sizeof(char))), compressed, complen);

		plog("http body of %d bytes", httpBodyLength);
		plog("http body: ");
#ifdef DEBUG
		for(j = 0; j < httpBodyLength; j++)
			fprintf(stderr, "%c", httpBody[j]);
#endif

		//Send response back to the UDP client
		uint32_t requestID = getRequestID(clientBuffer);
		sendResponse(clientSock, &clientAddress, clientAddressLen, requestID, send, httpBodyLength);

		plog("sent http body response to client");

		free(httpResponse);
		free(send);
		free(compressed);

		plog("freed http response");
		plog("End loop to handle client request");
	}
}