Example #1
0
static void processCommand(ClientSocket &sock)
{
	SyncDocument *doc = trackView->getDocument();
	int strLen, serverIndex, newRow;
	std::string trackName;
	const sync_track *t;
	unsigned char cmd = 0;
	if (sock.recv((char*)&cmd, 1)) {
		switch (cmd) {
		case GET_TRACK:
			// read data
			sock.recv((char *)&strLen, sizeof(int));
			strLen = ntohl(strLen);
			if (!sock.connected())
				return;

			if (!strLen) {
				sock.disconnect();
				InvalidateRect(trackViewWin, NULL, FALSE);
				return;
			}

			trackName.resize(strLen);
			if (!sock.recv(&trackName[0], strLen))
				return;

			if (int(strlen(trackName.c_str())) != strLen) {
				sock.disconnect();
				InvalidateRect(trackViewWin, NULL, FALSE);
				return;
			}

			// find track
			serverIndex = sync_find_track(doc,
			    trackName.c_str());
			if (0 > serverIndex)
				serverIndex =
				    int(doc->createTrack(trackName));

			// setup remap
			doc->clientSocket.clientTracks[trackName] = clientIndex++;

			// send key-frames
			t = doc->tracks[serverIndex];
			for (int i = 0; i < (int)t->num_keys; ++i)
				doc->clientSocket.sendSetKeyCommand(trackName,
				    t->keys[i]);

			InvalidateRect(trackViewWin, NULL, FALSE);
			break;

		case SET_ROW:
			sock.recv((char*)&newRow, sizeof(int));
			trackView->setEditRow(ntohl(newRow));
			break;
		}
	}
}
Example #2
0
static void* run(void* arg)
{
	IntruderThread* obj = (IntruderThread*)arg;
	char recv_buf[5];
	while(true)
	{
		try
		{
			//connect to the socket to get intruder data; this obj is client
			ClientSocket client = ClientSocket(src_str, src_port);
			//if yes, then notify the ServerObj
			while(true)
			{
				int recv_size = client.recv(&(recv_buf[0]), 5);
				if(recv_size<=0)
				{
					throw SocketException ( "Could not read from socket." );
				}
				//				printf("Receiving %d bytes from server:", recv_size);
				//				for(int i=0;i<5;i++)
				//				{
				//					printf("%2.2x",(char)recv_buf[i]);
				//				}
				if(recv_buf[4]==0x01)
				{
//					cout << "\t ====> Intruder!" << endl;
					(obj->intruderDetected)(obj->handler, SRC_INTRUDER);
				}
				//				printf("\n");
			}
		}
		catch(SocketException &ex)
		{
			cout << "Error " << ex.description() << endl;
			sleep(1);
		}
	}
	return NULL;
}
Example #3
0
int main(int argc, char **argv) {
    // Validate command line arguments
    if (argc < 3) {
        cerr << "Usage: server [camera address] [camera port]\n";
        cerr << "Options: [-b OR --benchmark] [-v OR --verbose] [-m OR --max INT] [-s OR --skip INT]\n";
        return 1;
    }

    // Handle options
    long max = 0;
    bool verbose = false;

    for (int i = 3; i < argc; i++) {
        string str(argv[i]);

        if (str == "-v" || str == "--verbose") verbose = true;
    }
    
    // Loop forever pulling sendables
    for (; ;) {
        // Initiate a connection
        if (verbose) cerr << "Initiating connection...\n";
        ClientSocket *s = new ClientSocket(argv[1], argv[2]);
        
        // If the connection can't be set up, retry once every second
        while (!s->isOpen()) {
            delete s;
            s = new ClientSocket(argv[1], argv[2]);
	        this_thread::sleep_for(chrono::milliseconds(100));
        }
        
        // Get the camera's id number
        char id;
        s->recv(&id, 1);
        if (verbose) cerr << "id: " << (int)id << "\n";
        
        // Create a directory if it doesn't already exist
        stringstream sss;
        sss  << id;
        mkdir(sss.str().c_str(), 0755);
        
        // Count sent sendables
        int sent = 0;

        // Receive paths
        while(s->isOpen()) {
            // Declare variables
            char c = 0;
            string str = "";
            
            // Receive            
            try {
                s->recv(&c, 1);
                
                while (c != 0) {
                    str += c;
                    s->recv(&c, 1);
                }
            } catch (...) { break; }
            
            cout << str << "\n";
            
            sent++;
            
            // Exit if the maximum number of sendables has been hit
            if (max != 0 && sent == max) {
                s->close();
                exit(0);
            }
        }
    }
}