1
0
mirror of https://github.com/TheGreyDiamond/Enlight.git synced 2025-12-17 23:40:45 +01:00

Code works

This commit is contained in:
TheGreyDiamond
2020-09-04 23:23:14 +02:00
parent 93b66fb6a9
commit af06e92632

View File

@@ -76,27 +76,32 @@ class enlightSession():
self.__client__.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self.__client__.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.__client__.bind(("", 37020)) self.__client__.bind(("", 37020))
logging.info("Starting lighthouse thread") logging.info("Starting lighthouse thread")
self.__activ__ = True
self.__client_thread__ = threading.Thread(target=self.lighthouseMain, args=(), name="Lighthouse server") self.__client_thread__ = threading.Thread(target=self.lighthouseMain, args=(), name="Lighthouse server")
self.__client_thread__.start() self.__client_thread__.start()
self.__activ__ = True
def lighthouseMain(self): def lighthouseMain(self):
''' The main thread for searching/finding sessions ''' ''' The main thread for searching/finding sessions '''
while True: while self.__activ__:
data, addr = self.__client__.recvfrom(1024) try:
data = data.decode("utf-8") data, addr = self.__client__.recvfrom(1024)
data = data.replace("|", "") except OSError:
proc = data.split(";") logging.warning("Client socket, failure. Session maybe not avaiable anymore?")
logging.info("Got broadcast from " + str(addr) + " with data " + str(proc))
if(proc[3] != VERSION): #if(proc[3] == VERSION):
logging.warning("Software versions are not compatible. ABORT")
else: else:
## More data handling data = data.decode("utf-8")
if(proc[2] not in self.allOnlineSessions): data = data.replace("|", "")
self.allOnlineSessions[proc[2]] = proc[1] proc = data.split(";")
logging.info("Found new session named " + proc[1])
logging.info("Got broadcast from " + str(addr) + " with data " + str(proc))
if(proc[3] != VERSION): #if(proc[3] == VERSION):
logging.warning("Software versions are not compatible. ABORT")
else:
## More data handling
if(proc[2] not in self.allOnlineSessions):
self.allOnlineSessions[proc[2]] = proc[1]
logging.info("Found new session named " + proc[1])
def researchAllSessions(self): def researchAllSessions(self):
if(self.__role__ != HOST): if(self.__role__ != HOST):
@@ -119,6 +124,18 @@ class enlightSession():
def getSessionMembers(self): def getSessionMembers(self):
return(self.members) return(self.members)
def leave(self):
''' Leaves the session, can takes at least two seconds '''
if(self.__role__ == USER or self.__role__ == ADMIN):
## TODO: Send leave command to host
self.__activ__ = False
time.sleep(2)
self.__client__.shutdown(socket.SHUT_RDWR)
self.__client__.close()
logging.info("Closed Lighthouse port")
else:
logging.warning("leave was called, without the role set to USER or ADMIN. Did you mean .stopSession?")
def stopSession(self): def stopSession(self):
if(self.__role__ == HOST): if(self.__role__ == HOST):
self.allowJoin = False self.allowJoin = False
@@ -128,18 +145,10 @@ class enlightSession():
self.__server_thread__ = None self.__server_thread__ = None
self.__server__.shutdown(socket.SHUT_RDWR) self.__server__.shutdown(socket.SHUT_RDWR)
self.__server__.close() self.__server__.close()
logging.info("Closed Discovery server port")
else: else:
logging.warning("stopSession was called, without the role set to HOST. Did you mean .leave?") logging.warning("stopSession was called, without the role set to HOST. Did you mean .leave?")
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
# Thanks to @stevenreddie
# client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
def testModule(): def testModule():
testSession = enlightSession("TestSession", role = HOST) testSession = enlightSession("TestSession", role = HOST)
userSession = enlightSession("myLocalSession", role = USER) userSession = enlightSession("myLocalSession", role = USER)
@@ -149,5 +158,6 @@ def testModule():
userSession.researchAllSessions() userSession.researchAllSessions()
time.sleep(5) time.sleep(5)
testSession.stopSession() testSession.stopSession()
userSession.leave()
testModule() testModule()