synchronize time over parallel connection

This commit is contained in:
Emil Axelsson
2016-09-22 18:42:19 +02:00
parent fa8eee5386
commit 112fe7fc54
8 changed files with 332 additions and 274 deletions
@@ -54,6 +54,7 @@ class SyncBuffer;
class ModuleEngine;
class WindowWrapper;
class SettingsEngine;
class TimeManager;
namespace interaction { class InteractionHandler; }
namespace gui { class GUI; }
@@ -88,6 +89,7 @@ public:
WindowWrapper& windowWrapper();
ghoul::fontrendering::FontManager& fontManager();
DownloadManager& downloadManager();
TimeManager& timeManager();
#ifdef OPENSPACE_MODULE_ONSCREENGUI_ENABLED
gui::GUI& gui();
@@ -143,6 +145,7 @@ private:
std::unique_ptr<LuaConsole> _console;
std::unique_ptr<ModuleEngine> _moduleEngine;
std::unique_ptr<SettingsEngine> _settingsEngine;
std::unique_ptr<TimeManager> _timeManager;
std::unique_ptr<DownloadManager> _downloadManager;
#ifdef OPENSPACE_MODULE_ONSCREENGUI_ENABLED
std::unique_ptr<gui::GUI> _gui;
@@ -97,6 +97,7 @@ namespace openspace{
double _dt;
bool _paused;
bool _requiresTimeJump;
double _timestamp;
void serialize(std::vector<char> &buffer){
//add current time
@@ -110,6 +111,9 @@ namespace openspace{
//add wether a time jump is necessary (recompute paths etc)
buffer.insert(buffer.end(), reinterpret_cast<char*>(&_requiresTimeJump), reinterpret_cast<char*>(&_requiresTimeJump) + sizeof(_requiresTimeJump));
//add timestamp
buffer.insert(buffer.end(), reinterpret_cast<char*>(&_timestamp), reinterpret_cast<char*>(&_timestamp) + sizeof(_timestamp));
};
void deserialize(const std::vector<char> &buffer){
@@ -134,6 +138,12 @@ namespace openspace{
//is a time jump required?
size = sizeof(_requiresTimeJump);
memcpy(&_requiresTimeJump, buffer.data() + offset, size);
offset += size;
// timestamp
size = sizeof(_timestamp);
memcpy(&_timestamp, buffer.data() + offset, size);
offset += size;
};
};
+14 -60
View File
@@ -105,41 +105,21 @@ namespace openspace {
class ParallelConnection{
public:
ParallelConnection();
~ParallelConnection();
void clientConnect();
void setPort(const std::string &port);
void setAddress(const std::string &address);
void setName(const std::string& name);
bool isAuthenticated();
bool isHost();
const std::string& hostName();
void requestHostship(const std::string &password);
void resignHostship();
void setPassword(const std::string &password);
void signalDisconnect();
void preSynchronization();
void sendScript(const std::string& script);
//void scriptMessage(const std::string propIdentifier, const std::string propValue);
/**
* Returns the Lua library that contains all Lua functions available to affect the
* remote OS parallel connection. The functions contained are
@@ -148,76 +128,50 @@ namespace openspace {
* interaction
*/
static scripting::LuaLibrary luaLibrary();
Status status();
size_t nConnections();
std::shared_ptr<ghoul::Event<>> connectionEvent();
protected:
private:
//@TODO change this into the ghoul hasher for client AND server
uint32_t hash(const std::string &val);
void queueOutMessage(const Message& message);
void queueOutDataMessage(const DataMessage& dataMessage);
void queueInMessage(const Message& message);
void disconnect();
void closeSocket();
bool initNetworkAPI();
void establishConnection(addrinfo *info);
void sendAuthentication();
void listenCommunication();
int receiveData(_SOCKET & socket, std::vector<char> &buffer, int length, int flags);
void handleMessage(const Message&);
void dataMessageReceived(const std::vector<char>& messageContent);
void connectionStatusMessageReceived(const std::vector<char>& messageContent);
void nConnectionsMessageReceived(const std::vector<char>& messageContent);
void broadcast();
int receiveData(_SOCKET & socket, std::vector<char> &buffer, int length, int flags);
void sendCameraKeyframe();
void sendTimeKeyframe();
void sendFunc();
void threadManagement();
void setStatus(Status status);
void setHostName(const std::string& hostName);
void setNConnections(size_t nConnections);
//std::string scriptFromPropertyAndValue(const std::string property, const std::string value);
double calculateBufferedKeyframeTime(double originalTime);
uint32_t _passCode;
std::string _port;
std::string _address;
std::string _name;
_SOCKET _clientSocket;
std::unique_ptr<std::thread> _connectionThread;
std::unique_ptr<std::thread> _broadcastThread;
std::unique_ptr<std::thread> _sendThread;
std::unique_ptr<std::thread> _listenThread;
std::unique_ptr<std::thread> _handlerThread;
std::atomic<bool> _isConnected;
std::atomic<bool> _isRunning;
std::atomic<bool> _tryConnect;
@@ -238,16 +192,16 @@ namespace openspace {
std::deque<Message> _receiveBuffer;
std::mutex _receiveBufferMutex;
network::datamessagestructures::TimeKeyframe _latestTimeKeyframe;
std::mutex _timeKeyframeMutex;
std::atomic<bool> _latestTimeKeyframeValid;
std::map<std::string, std::string> _currentState;
std::mutex _currentStateMutex;
std::atomic<bool> _timeJumped;
std::mutex _latencyMutex;
std::deque<double> _latencyDiffs;
double _initialTimeDiff;
std::unique_ptr<std::thread> _connectionThread;
std::unique_ptr<std::thread> _broadcastThread;
std::unique_ptr<std::thread> _sendThread;
std::unique_ptr<std::thread> _listenThread;
std::unique_ptr<std::thread> _handlerThread;
std::shared_ptr<ghoul::Event<>> _connectionEvent;
};
} // namespace network
+52
View File
@@ -0,0 +1,52 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2016 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __TIMEMANAGER_H__
#define __TIMEMANAGER_H__
#include <deque>
#include <openspace/network/messagestructures.h>
namespace openspace {
class TimeManager {
public:
void preSynchronization(double dt);
void addKeyframe(const network::datamessagestructures::TimeKeyframe& kf);
void removeKeyframesBefore(double timestamp);
void removeKeyframesAfter(double timestamp);
void clearKeyframes();
private:
void consumeKeyframes(double dt);
std::deque<network::datamessagestructures::TimeKeyframe> _keyframes;
static bool compareKeyframeTimes(
const network::datamessagestructures::TimeKeyframe& a,
const network::datamessagestructures::TimeKeyframe& b);
double _latestConsumedTimestamp;
};
}
#endif // __TIMEMANAGER_H__