mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-09 13:28:16 +02:00
make AssignmentClient use event loop, closes #1291
This commit is contained in:
parent
1e3ab1a201
commit
d21583d9c5
2 changed files with 18 additions and 80 deletions
|
@ -6,6 +6,8 @@
|
||||||
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
|
||||||
#include <Assignment.h>
|
#include <Assignment.h>
|
||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <NodeList.h>
|
#include <NodeList.h>
|
||||||
|
@ -17,21 +19,15 @@
|
||||||
#include "AssignmentClient.h"
|
#include "AssignmentClient.h"
|
||||||
|
|
||||||
const char ASSIGNMENT_CLIENT_TARGET_NAME[] = "assignment-client";
|
const char ASSIGNMENT_CLIENT_TARGET_NAME[] = "assignment-client";
|
||||||
const long long ASSIGNMENT_REQUEST_INTERVAL_USECS = 1 * 1000 * 1000;
|
const long long ASSIGNMENT_REQUEST_INTERVAL_MSECS = 1 * 1000;
|
||||||
|
|
||||||
AssignmentClient::AssignmentClient(int &argc, char **argv,
|
AssignmentClient::AssignmentClient(int &argc, char **argv,
|
||||||
Assignment::Type requestAssignmentType,
|
Assignment::Type requestAssignmentType,
|
||||||
const sockaddr_in& customAssignmentServerSocket,
|
const sockaddr_in& customAssignmentServerSocket,
|
||||||
const char* requestAssignmentPool) :
|
const char* requestAssignmentPool) :
|
||||||
QCoreApplication(argc, argv),
|
QCoreApplication(argc, argv),
|
||||||
_requestAssignmentType(requestAssignmentType),
|
_requestAssignment(Assignment::RequestCommand, requestAssignmentType, requestAssignmentPool)
|
||||||
_customAssignmentServerSocket(customAssignmentServerSocket),
|
|
||||||
_requestAssignmentPool(requestAssignmentPool)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int AssignmentClient::exec() {
|
|
||||||
// set the logging target to the the CHILD_TARGET_NAME
|
// set the logging target to the the CHILD_TARGET_NAME
|
||||||
Logging::setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME);
|
Logging::setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME);
|
||||||
|
|
||||||
|
@ -39,74 +35,18 @@ int AssignmentClient::exec() {
|
||||||
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_UNASSIGNED);
|
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_UNASSIGNED);
|
||||||
|
|
||||||
// set the custom assignment socket if we have it
|
// set the custom assignment socket if we have it
|
||||||
if (_customAssignmentServerSocket.sin_addr.s_addr != 0) {
|
if (customAssignmentServerSocket.sin_addr.s_addr != 0) {
|
||||||
nodeList->setAssignmentServerSocket((sockaddr*) &_customAssignmentServerSocket);
|
nodeList->setAssignmentServerSocket((sockaddr*) &customAssignmentServerSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
// change the timeout on the nodelist socket to be as often as we want to re-request
|
// call a timer function every ASSIGNMENT_REQUEST_INTERVAL_MSECS to ask for assignment, if required
|
||||||
nodeList->getNodeSocket()->setBlockingReceiveTimeoutInUsecs(ASSIGNMENT_REQUEST_INTERVAL_USECS);
|
qDebug() << "Waiting for assignment -" << _requestAssignment << "\n";
|
||||||
|
|
||||||
timeval lastRequest = {};
|
QTimer* timer = new QTimer(this);
|
||||||
|
connect(timer, SIGNAL(timeout()), SLOT(sendAssignmentRequest()));
|
||||||
unsigned char packetData[MAX_PACKET_SIZE];
|
timer->start(ASSIGNMENT_REQUEST_INTERVAL_MSECS);
|
||||||
ssize_t receivedBytes = 0;
|
}
|
||||||
|
|
||||||
sockaddr_in senderSocket = {};
|
void AssignmentClient::sendAssignmentRequest() {
|
||||||
|
NodeList::getInstance()->sendAssignment(_requestAssignment);
|
||||||
// create a request assignment, accept assignments defined by the overidden type
|
|
||||||
Assignment requestAssignment(Assignment::RequestCommand, _requestAssignmentType, _requestAssignmentPool);
|
|
||||||
|
|
||||||
qDebug() << "Waiting for assignment -" << requestAssignment << "\n";
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
if (usecTimestampNow() - usecTimestamp(&lastRequest) >= ASSIGNMENT_REQUEST_INTERVAL_USECS) {
|
|
||||||
gettimeofday(&lastRequest, NULL);
|
|
||||||
|
|
||||||
// if we're here we have no assignment, so send a request
|
|
||||||
nodeList->sendAssignment(requestAssignment);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nodeList->getNodeSocket()->receive((sockaddr*) &senderSocket, packetData, &receivedBytes) &&
|
|
||||||
(packetData[0] == PACKET_TYPE_DEPLOY_ASSIGNMENT || packetData[0] == PACKET_TYPE_CREATE_ASSIGNMENT)
|
|
||||||
&& packetVersionMatch(packetData)) {
|
|
||||||
|
|
||||||
// construct the deployed assignment from the packet data
|
|
||||||
Assignment* deployedAssignment = AssignmentFactory::unpackAssignment(packetData, receivedBytes);
|
|
||||||
|
|
||||||
qDebug() << "Received an assignment -" << *deployedAssignment << "\n";
|
|
||||||
|
|
||||||
// switch our nodelist domain IP and port to whoever sent us the assignment
|
|
||||||
if (packetData[0] == PACKET_TYPE_CREATE_ASSIGNMENT) {
|
|
||||||
|
|
||||||
nodeList->setDomainIP(QHostAddress((sockaddr*) &senderSocket));
|
|
||||||
nodeList->setDomainPort(ntohs(senderSocket.sin_port));
|
|
||||||
|
|
||||||
nodeList->setOwnerUUID(deployedAssignment->getUUID());
|
|
||||||
|
|
||||||
qDebug("Destination IP for assignment is %s\n", nodeList->getDomainIP().toString().toStdString().c_str());
|
|
||||||
|
|
||||||
// run the deployed assignment
|
|
||||||
deployedAssignment->run();
|
|
||||||
} else {
|
|
||||||
qDebug("Received a bad destination socket for assignment.\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
qDebug("Assignment finished or never started - waiting for new assignment\n");
|
|
||||||
|
|
||||||
// delete the deployedAssignment
|
|
||||||
delete deployedAssignment;
|
|
||||||
|
|
||||||
// reset our NodeList by switching back to unassigned and clearing the list
|
|
||||||
nodeList->setOwnerType(NODE_TYPE_UNASSIGNED);
|
|
||||||
nodeList->reset();
|
|
||||||
|
|
||||||
// set the NodeList socket back to blocking
|
|
||||||
nodeList->getNodeSocket()->setBlocking(true);
|
|
||||||
|
|
||||||
// reset the logging target to the the CHILD_TARGET_NAME
|
|
||||||
Logging::setTargetName(ASSIGNMENT_CLIENT_TARGET_NAME);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,10 @@ public:
|
||||||
Assignment::Type requestAssignmentType = Assignment::AllTypes,
|
Assignment::Type requestAssignmentType = Assignment::AllTypes,
|
||||||
const sockaddr_in& customAssignmentServerSocket = sockaddr_in(),
|
const sockaddr_in& customAssignmentServerSocket = sockaddr_in(),
|
||||||
const char* requestAssignmentPool = NULL);
|
const char* requestAssignmentPool = NULL);
|
||||||
|
private slots:
|
||||||
int exec();
|
void sendAssignmentRequest();
|
||||||
private:
|
private:
|
||||||
Assignment::Type _requestAssignmentType;
|
Assignment _requestAssignment;
|
||||||
sockaddr_in _customAssignmentServerSocket;
|
|
||||||
const char* _requestAssignmentPool;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* defined(__hifi__AssignmentClient__) */
|
#endif /* defined(__hifi__AssignmentClient__) */
|
||||||
|
|
Loading…
Reference in a new issue