Exemplo n.º 1
0
void handleInput(char *comm, size_t size) {
    if (strncmp(comm, "ECHO", 4) == 0) {
        sendEcho(comm, size - 5);
    } else if (strncmp(comm, "LIST", 4) == 0) {
        sendList();
    } else if (strncmp(comm, "2ALL", 4) == 0) {
        sendToAll(comm, size - 5);
    } else if (strncmp(comm, "2ONE", 4) == 0) {
        strtok(comm, " ");
        char *str = strtok(comm + 5, " ");
        if (str == NULL) {
            fprintf(stderr, "Wrong number of arguments\n");
            return;
        }
        int id = atoi(str);
        str = strtok(NULL, " ");
        size_t size = 0;
        if (str != NULL)
            size = strlen(str);
        sendToOne(str, id, size);
    } else if (strncmp(comm, "FRIENDS", 7) == 0) {
        sendFriends(comm, size - 8);
    } else if (strncmp(comm, "ADD", 3) == 0) {
        sendAddFriends(comm, size - 4);
    } else if (strncmp(comm, "DEL", 3) == 0) {
        sendDelFriends(comm, size - 4);
    } else if (strncmp(comm, "2FRIENDS", 8) == 0) {
        sendToFriends(comm, size - 9);
    } else if (strncmp(comm, "STOP", 4) == 0) {
        exit(0);
    } else {
        printf("Wrong command -> %s\n", comm);
    }
}
Exemplo n.º 2
0
TennisServer::TennisServer(QObject *parent)
:   QObject(parent), l2capServer(0), clientSocket(0), stream(0), lagReplyTimeout(0)
{    
    elapsed.start();
    ballElapsed.start();
    lagTimer.setInterval(1000);
    connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendEcho()));
}
Exemplo n.º 3
0
	MojErr testQueue()
	{
		// send a high-latency request and build up a service-side queue
		const MojChar* echoStr = _T("{\"hello\":\"world\"}");
		MojObject expectedResponse;
		MojErr err = formatEchoResponse(echoStr, expectedResponse);
		MojTestErrCheck(err);

		err = sendEcho(echoStr, expectedResponse, _T("slowecho"));
		MojTestErrCheck(err);

		for (int i = 0; i < 10; i++) {
			err = sendEcho(echoStr, expectedResponse, _T("echo"));
			MojTestErrCheck(err);
		}

		err = receiveResponses();
		MojTestErrCheck(err);

		return MojErrNone;
	}
Exemplo n.º 4
0
/*
 * This function is specific to this example.
 * Your own code will likely do something different
 */
void EchoWindow::createGUI()
{
    lineEdit = new QLineEdit;
    label = new QLabel;
    label->setFrameStyle(QFrame::Box | QFrame::Plain);
    button = new QPushButton(tr("Send Message"));
    statusBar = new QStatusBar( this );

    connect(lineEdit, SIGNAL(editingFinished()),
            this, SLOT(sendEcho()));
    connect(button, SIGNAL(clicked()),
            this, SLOT(sendEcho()));

    layout = new QGridLayout;
    layout->addWidget(new QLabel(tr("Message:")), 0, 0);
    layout->addWidget(lineEdit, 0, 1);
    layout->addWidget(new QLabel(tr("Answer:")), 1, 0);
    layout->addWidget(label, 1, 1);
    layout->addWidget(button, 2, 1, Qt::AlignRight);
    layout->addWidget(statusBar, 3, 0, 1, 2);
    layout->setSizeConstraint(QLayout::SetFixedSize);
}
Exemplo n.º 5
0
int EchoWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QWidget::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: sendEcho(); break;
        default: ;
        }
        _id -= 1;
    }
    return _id;
}
Exemplo n.º 6
0
	MojErr testInvalidMethod()
	{
		MojObject expectedResponse;
		MojErr err = formatErrResponse(MojErrMethodNotFound, expectedResponse);
		MojTestErrCheck(err);
		// send echo with invalid method
		err = sendEcho(_T("{\"hello\":\"world\"}"), expectedResponse, _T("bogusmethod"));
		MojTestErrCheck(err);

		err = receiveResponses();
		MojTestErrCheck(err);

		return MojErrNone;
	}
Exemplo n.º 7
0
	MojErr testInvalidPayload()
	{
		// send an invaild error request and verify error code response

		MojObject expectedResponse;
		MojErr err = formatErrResponse(MojErrJsonParsePropName, expectedResponse);
		MojTestErrCheck(err);

		// send echo with invalid json
		err = sendEcho(_T("{{\"hello\":\"world\"}"), expectedResponse, _T("echo"));
		MojTestErrCheck(err);

		err = receiveResponses();
		MojTestErrCheck(err);

		return MojErrNone;
	}
Exemplo n.º 8
0
	MojErr testEchoRequest()
	{
		// send a valid echo request and verify response

		const MojChar* echoStr = _T("{\"hello\":\"world\"}");
		MojObject expectedResponse;
		MojErr err = formatEchoResponse(echoStr, expectedResponse);
		MojTestErrCheck(err);

		err = sendEcho(echoStr, expectedResponse, _T("echo"));
		MojTestErrCheck(err);

		err = receiveResponses();
		MojTestErrCheck(err);

		return MojErrNone;
	}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->statusBar->showMessage("Qt to Arduino HDLC command router example", 3000);
    serial = new QSerialPort(this);
    this->fillPortsInfo();
    HDLC_qt* hdlc = HDLC_qt::instance();

    QObject::connect(hdlc, SIGNAL(hdlcTransmitByte(QByteArray)),
                     this, SLOT(putChar(QByteArray)));

    QObject::connect(hdlc, SIGNAL(hdlcTransmitByte(char)),
                     this, SLOT(putChar(char)));

    QObject::connect(hdlc, SIGNAL(hdlcValidFrameReceived(QByteArray, quint16)),
                     this, SLOT(HDLC_CommandRouter(QByteArray, quint16)));

    // All data coming from serial port (should go straight to HDLC validation)
    QObject::connect(this, SIGNAL(dataReceived(QByteArray)),
                     hdlc, SLOT(charReceiver(QByteArray)));

    // Allso echo chars HEX value
    QObject::connect(this, SIGNAL(dataReceived(QByteArray)),
                     this, SLOT(print_hex_value(QByteArray)));

    QObject::connect(this, SIGNAL(sendDataFrame(QByteArray, quint16)),
                     hdlc, SLOT(frameDecode(QByteArray, quint16)));

    QObject::connect(ui->pushButton_connect, SIGNAL(clicked()),
                     this, SLOT(openSerialPort()));

    QObject::connect(serial, SIGNAL(readyRead()),
                     this, SLOT(readData()));

    QObject::connect(ui->dial, SIGNAL(valueChanged(int)),
                     this, SLOT(setServoPosition()) );

    QObject::connect(ui->pushButton_echo, SIGNAL(clicked()),
                     this, SLOT(sendEcho()) );

    QObject::connect(ui->pushButton_LED, SIGNAL(clicked()),
                     this, SLOT(toggleLED()) );
}
Exemplo n.º 10
0
/**
 * return value is sent directly to return value of main()
 */
static int
pingMainloop(int fd)
{
	unsigned sent = 0;
	unsigned recvd = 0;
	double lastpingTime = 0; /* last time we sent out a ping */
	double curPingTime;   /* if we ping now, this is the timestamp of it */
        double lastRecvTime = 0; /* last time we got a reply */
        int recvErrors = 0;

	if (options.verbose > 2) {
		fprintf(stderr, "%s: mainloop(%d)\n", argv0, fd);
	}

	startTime = clock_get_dbl();

	printf("GTPING %s (%s) packet version %d\n",
	       options.target,
	       options.targetip,
	       options.version);

        lastRecvTime = startTime;
	while (!sigintReceived) {
                /* max time to wait for replies before checking if it's time
                 * to send another ping */
		double timewait;
		int n;
		struct pollfd fds;

                /* sent all we are going to send, and got all replies
                 * (either errors or good replies)
                 */
                if (options.count
                    && (sent == options.count)
                    && (sent == (recvd + recvErrors))) {
                        break;
                }

                /* time to send yet? */
		curPingTime = clock_get_dbl();

                /* if clock is not monotonic and time set backwards
                 * since last ping, start a new ping cycle */
                if (curPingTime < lastpingTime) {
                        lastpingTime = curPingTime - options.interval - 0.001;
                }

		if (curPingTime > lastpingTime + options.interval) {
			if (options.count && (curSeq == options.count)) {
				if (lastRecvTime+options.wait < curPingTime) {
                                        break;
                                }
			} else if (0 <= sendEcho(fd, curSeq++)) {
                                sent++;
                                lastpingTime = curPingTime;
                                if (options.flood) {
                                        printf(".");
                                        fflush(stdout);
                                }
			}
		}

		fds.fd = fd;
		fds.events = POLLIN;
		fds.revents = 0;
		
                /* max waittime: until it's time to send the next one */
                timewait = lastpingTime+options.interval - clock_get_dbl();

                /* never wait more than an interval. this can happen if
                 * clock is not monotonic */
                if (timewait > options.interval) {
                        timewait = options.interval;
                }

                /* this should never happen, should have been taken care of
                 * above. */
		if (timewait < 0) {
			timewait = 0;
		}

                /* leave room for overhead */
                timewait *= 0.5;

		switch ((n = poll(&fds, 1, (int)(timewait * 1000)))) {
		case 1: /* read ready */
			if (fds.revents & POLLERR) {
                                if (handleRecvErr(fd, NULL, 0)) {
                                        recvErrors++;
                                }
			}
			if (fds.revents & POLLIN) {
				n = recvEchoReply(fd);
                                if (!n) {
                                        recvd++;
                                        lastRecvTime = clock_get_dbl();
                                } else if (n > 0) {
                                        /* still ok, but no reply */
                                } else { /* n < 0 */
                                        return 1;
                                }
			}
			break;
		case 0: /* timeout */
			break;
		case -1: /* error */
			switch (errno) {
			case EINTR:
			case EAGAIN:
				break;
			default:
				fprintf(stderr, "%s: poll([%d], 1, %d): %s\n",
					argv0,
					fd,
					(int)(timewait*1000),
					strerror(errno));
				exit(2);
			}
			break;
		default: /* can't happen */
			fprintf(stderr, "%s: poll() returned %d!\n", argv0, n);
			exit(2);
			break;
		}
			
	}
	printf("\n--- %s GTP ping statistics ---\n"
               "%u packets transmitted, %u received, "
               "%d%% packet loss, "
               "time %dms\n"
               "%u out of order, %u dups, "
               "%u connection refused",
	       options.target,
               sent, recvd,
	       (int)((100.0*(sent-recvd))/sent),
               (int)(1000*(clock_get_dbl()-startTime)),
               reorder, dups,
               connectionRefused);
        errInspectionPrintSummary();
        printf("\n");
	if (totalTimeCount) {
		printf("rtt min/avg/max/mdev = %.3f/%.3f/%.3f/%.3f ms",
		       1000*totalMin,
		       1000*(totalTime / totalTimeCount),
		       1000*totalMax,
		       1000*sqrt((totalTimeSquared -
				  (totalTime * totalTime)
				  /totalTimeCount)/totalTimeCount));
	}
	printf("\n");
	return recvd == 0;
}
Exemplo n.º 11
0
/**
 * FIXME: this function needs a cleanup, and probably some merging
 * with pingMainloop()
 */
static int
tracerouteMainloop(int fd)
{
        int ttl = 0;
        int ttlTry = 0;
        double curPingTime;
        double lastRecvTime = 0;
        double lastPingTime = 0;
        int n;
        int endOfTraceroute = 0;
        int printStar = 0;
        double timewait;

	printf("GTPING traceroute to %s (%s) packet version %d.\n",
	       options.target,
	       options.targetip,
	       (int)options.version);


	while (!sigintReceived) {
		struct pollfd fds;

		fds.fd = fd;
		fds.events = POLLIN;
		fds.revents = 0;
                
                /* time to send yet? */
		curPingTime = clock_get_dbl();
		if ((lastRecvTime >= lastPingTime)
                    || (curPingTime > lastPingTime + options.interval)) {
                        if (printStar) {
                                printf("*\n");
                        }
                        ttlTry++;
                        if (ttlTry < options.traceroutehops && ttl != 0) {
                                printf("     ");
                        } else {
                                if (endOfTraceroute) {
                                        break;
                                }
                                ttl++;
                                ttlTry = 0;
                                printf("%4d ", ttl);
                                fflush(stdout);
                        }
                        if (setsockopt(fd,
                                       SOL_IP,
                                       IP_TTL,
                                       &ttl,
                                       sizeof(ttl))) {
                                fprintf(stderr,
                                        "%s: setsockopt(%d, SOL_IP, IP_TTL, "
                                        "%d): %s\n", argv0, fd, ttl,
                                        strerror(errno));
                        }

                        if (0 <= sendEcho(fd, curSeq++)) {
                                lastPingTime = curPingTime;
                                printStar = 1;
                        }
                }

                /* max waittime: until it's time to send the next one */
		timewait = lastPingTime+options.interval - clock_get_dbl();
		if (timewait < 0) {
			timewait = 0;
		}
                timewait *= 0.5; /* leave room for overhead */

		switch ((n = poll(&fds, 1, (int)(timewait * 1000)))) {
		case 1: /* read ready */
                        printStar = 0;
			if (fds.revents & POLLERR) {
                                int e;
				e = handleRecvErr(fd, NULL, lastPingTime);
                                if (e) {
                                        lastRecvTime = clock_get_dbl();
                                }
                                if (e > 1) {
                                        endOfTraceroute = 1;
                                }
			}
			if (fds.revents & POLLIN) {
				n = recvEchoReply(fd);
                                endOfTraceroute = 1;
                                if (!n) {
                                        lastRecvTime = clock_get_dbl();
                                } else if (n > 0) {
                                        /* still ok, but no reply */
                                        printStar = 1;
                                } else {
                                        return 1;
                                }
			}
			break;
		case 0: /* timeout */
			break;
		case -1: /* error */
			switch (errno) {
			case EINTR:
			case EAGAIN:
				break;
			default:
				fprintf(stderr, "%s: poll([%d], 1, %d): %s\n",
					argv0,
					fd,
					(int)(timewait*1000),
					strerror(errno));
				exit(2);
			}
			break;
		default: /* can't happen */
			fprintf(stderr, "%s: poll() returned %d!\n", argv0, n);
			exit(2);
			break;
		}
        }
        return 0;
}
Exemplo n.º 12
0
TennisClient::TennisClient(QObject *parent)
:   QObject(parent), socket(0), stream(0), elapsed(new QTime), lagTimeout(0)
{
    lagTimer.setInterval(1000);
    connect(&lagTimer, SIGNAL(timeout()), this, SLOT(sendEcho()));
}