diff --git a/include/openspace/network/parallelconnection.h b/include/openspace/network/parallelconnection.h index f13037a842..5297654c31 100644 --- a/include/openspace/network/parallelconnection.h +++ b/include/openspace/network/parallelconnection.h @@ -80,7 +80,7 @@ namespace openspace{ bool isHost(); - void requestHostship(); + void requestHostship(const std::string &password); void setPassword(const std::string &password); diff --git a/src/network/parallelconnection.cpp b/src/network/parallelconnection.cpp index 87857de2d5..26329206e7 100644 --- a/src/network/parallelconnection.cpp +++ b/src/network/parallelconnection.cpp @@ -855,13 +855,18 @@ namespace openspace { return _isHost.load(); } - void ParallelConnection::requestHostship(){ + void ParallelConnection::requestHostship(const std::string &password){ std::vector buffer; buffer.reserve(headerSize()); + uint32_t passcode = hash(password); + //write header writeHeader(buffer, MessageTypes::HostshipRequest); + //write passcode + buffer.insert(buffer.end(), reinterpret_cast(&passcode), reinterpret_cast(&passcode) + sizeof(uint32_t)); + //send message queueMessage(buffer); } @@ -1136,7 +1141,7 @@ namespace openspace { { "requestHostship", &luascriptfunctions::requestHostship, - "", + "string", "Request to be the host for this session" }, } diff --git a/src/network/parallelconnection_lua.inl b/src/network/parallelconnection_lua.inl index caff3a2f56..059129e20a 100644 --- a/src/network/parallelconnection_lua.inl +++ b/src/network/parallelconnection_lua.inl @@ -160,12 +160,27 @@ int disconnect(lua_State* L) { int requestHostship(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - if(OsEng.isMaster()){ - OsEng.parallelConnection()->requestHostship(); + const bool isFunction = (lua_isfunction(L, -1) != 0); + if (isFunction) { + // If the top of the stack is a function, it is ourself + const char* msg = lua_pushfstring(L, "method called without argument"); + return luaL_error(L, "bad argument (%s)", msg); } + + const int type = lua_type(L, -1); + if (type == LUA_TSTRING) { + std::string pwd = luaL_checkstring(L, -1); + if(OsEng.isMaster()){ + OsEng.parallelConnection()->requestHostship(pwd); + } + return 0; + } + else { + const char* msg = lua_pushfstring(L, "%s expected, got %s", + lua_typename(L, LUA_TSTRING), luaL_typename(L, -1)); + return luaL_error(L, "bad argument #%d (%s)", 1, msg); + } + return 0; }