Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. ##
  2. # Project Agile Development - Blender - Copyright (c) BLE-Team 5 - 2018
  3. # Permission is hereby granted to any person to use, copy, modify and merge.
  4. #
  5. # The copyright notice and this permission must be included in the file.
  6. #
  7. # @author Alex Antonides <{@link http://www.alex-antonides.com/}>
  8. # @author Micah Bredenhorst
  9. # @ignore
  10. ##
  11. import time, uuid
  12.  
  13. from threading import Thread
  14. from connector import Connector
  15.  
  16. ##
  17. # This method will be called at first once the file is loaded.
  18. # @def start()
  19. ##
  20. def start():
  21. _server = Server()
  22.  
  23. ##
  24. # This is the Server class which the server uses.
  25. # The Server is able to send and retrieve packets.
  26. ##
  27. class Server:
  28.  
  29. LISTEN_EVENT_ON_JOIN_REQUEST = "JRQ"
  30. LISTEN_EVENT_ON_LOCATION_UDPATE = "LUP"
  31. LISTEN_EVENT_ON_PING = "PING"
  32.  
  33. SEND_EVENT_ON_JOIN_HANDLED = "JHR" #This is the same as Join_Request SO Maybe I should change it.
  34. SEND_EVENT_ON_LOCATION_UPDATE = "LUP"
  35. SEND_EVENT_ON_ID_SEND = "IDS"
  36. SEND_EVENT_PING = "PING"
  37. SEND_EVENT_UPDATE_SERVERSLOT = "SLOT"
  38. SEND_EVENT_ASSIGN_PLAYER_NUMBER = "NUM"
  39.  
  40. CONSOLE_COMMAND_EXIT = "exit"
  41. CONSOLE_CONNECTIONS_AMOUNT = "amountplayers"
  42. CONSOLE_COMMAND_PING = "ping"
  43. CONSOLE_COMMAND_TEST = "Test"
  44.  
  45. MAX_CLIENT_CONNECTIONS = 4
  46.  
  47. _udp_ip = "" # The IP the server is listening to. Default: "" - all.
  48. _udp_port = 5006 # The port of the server. Default: 5006
  49.  
  50. _connector = None # The connection socket of the server.
  51.  
  52. _command = None # The last given command given to the server.
  53.  
  54. active = False # If the Server is active or not.
  55.  
  56. PlayerList = () # Empty player list
  57.  
  58.  
  59. ##
  60. # @def __init__( self )
  61. # @internal
  62. # @param self
  63. ##
  64. def __init__( self ):
  65. # Instantiate the receiver and the sender.
  66. self._connector = Connector( self._udp_ip, self._udp_port )
  67.  
  68. # Listen to specific messages.
  69. self._connector.ListenTo( Server.LISTEN_EVENT_ON_LOCATION_UDPATE, self._onLocationUpdate, -1 )
  70. self._connector.ListenTo( Server.LISTEN_EVENT_ON_PING, self._onPing, -1 )
  71.  
  72.  
  73. # Initializement is ready! Show the message in the console!
  74. self.__printInitializeFinished()
  75.  
  76. # Start the listening
  77. self._start()
  78.  
  79. ##
  80. # @def __del__( self )
  81. # @internal
  82. # @param self
  83. ##
  84. def __del__(self):
  85. self._end()
  86.  
  87. ##
  88. # @def _update( self )
  89. # @private
  90. # @param self
  91. ##
  92. def _start( self ):
  93. if self.active == True:
  94. return
  95. else:
  96. def handleListening(self):
  97. while self._command != Server.CONSOLE_COMMAND_EXIT:
  98. connections = self._connector.GetConnections()
  99. self._connector.ListenOnce( connections )
  100.  
  101. def handleConsoleInput(self):
  102. while self._command != Server.CONSOLE_COMMAND_EXIT:
  103. self._command = input(" -> ")
  104.  
  105. if self._command == "":
  106. print("Please put in a command.")
  107. elif self._command == Server.CONSOLE_COMMAND_EXIT:
  108. self._end()
  109. elif self._command == Server.CONSOLE_COMMAND_TEST:
  110. self._RQCS
  111. elif self._command == Server.CONSOLE_CONNECTIONS_AMOUNT:
  112. print("Total room for connections:", Server.MAX_CLIENT_CONNECTIONS, ". Amount of connections:", len( self._connector.GetConnections() ), ". Room left:", ( Server.MAX_CLIENT_CONNECTIONS - len( self._connector.GetConnections() ) ), "." )
  113. elif self._command == Server.CONSOLE_COMMAND_PING:
  114. connections = self._connector.GetConnections()
  115. for connection in connections:
  116. self._connector.Send( self.SEND_EVENT_PING, self.SEND_EVENT_PING, connection['conn'] )
  117. else:
  118. print("Command", self._command, "is not known to the console.")
  119.  
  120. self._connector.Host( self._onAccept, self.MAX_CLIENT_CONNECTIONS )
  121.  
  122. lis = Thread( name="lis", target= handleListening, kwargs={ 'self': self } )
  123. lis.start()
  124.  
  125. cip = Thread( name="cip", target= handleConsoleInput, kwargs={ 'self': self } )
  126. cip.start()
  127.  
  128. self.active = True
  129.  
  130. ##
  131. # @def _end( self )
  132. # @private
  133. # @param self
  134. ##
  135. def _end( self ):
  136. if self.active == False:
  137. return
  138. else:
  139. self.active = False
  140.  
  141. connections = self._connector.GetConnections()
  142. for connection in connections:
  143. connection['conn'].close()
  144.  
  145. self._connector.Close()
  146. self._command = Server.CONSOLE_COMMAND_EXIT
  147. print("Server is shutting down..")
  148.  
  149. ##
  150. # @def _onAccept( self, address, connection )
  151. # @private
  152. # @param self
  153. # @param address
  154. # @param connection
  155. ##
  156. def _onAccept( self, address, acceptor ):
  157. connections = self._connector.GetConnections()
  158. activeConnections = len(self._connector.GetConnections())
  159.  
  160. print( address, "'s request has been accepted. ID:", acceptor['ID'], ", Player Number:", activeConnections,"has been given. Places left:", ( Server.MAX_CLIENT_CONNECTIONS - len( connections ) ) )
  161. # Send the acceptation to each connection.
  162. for connection in connections:
  163. if acceptor['ID'] != connection['ID']:
  164. self._connector.Send( Server.SEND_EVENT_ON_JOIN_HANDLED, { 'ID': connection['ID'], 'PlayerNumber': activeConnections }, connection['conn'] )
  165. else:
  166. self._connector.Send( Server.SEND_EVENT_ON_ID_SEND, acceptor['ID'], acceptor['conn'] )
  167. self._connector.Send( Server.SEND_EVENT_ASSIGN_PLAYER_NUMBER, activeConnections, connection['conn'] )
  168. ##
  169. # @def _onLocationUpdate( self, address, data, tracker )
  170. # @private
  171. # @param self
  172. # @param address
  173. # @param data
  174. # @param data.ID
  175. # @param data.position
  176. # @param tracker
  177. ##
  178. def _onLocationUpdate( self, address, data, tracker):
  179. #print (data)
  180. if self._connector.IsIDConnected( data['ID'] ) == False:
  181. return False
  182. else:
  183. connections = self._connector.GetConnections()
  184. # Send the location to each connection.
  185. for connection in connections:
  186. # We don't want to send the location back to the player who sent it.
  187. if data['ID'] != connection['ID']:
  188. self._connector.Send( Server.SEND_EVENT_ON_LOCATION_UPDATE, data['position'], connection['conn'] )
  189.  
  190. ##
  191. # @def _onPing( self, address, data, tracker )
  192. # @private
  193. # @param self
  194. # @param address
  195. # @param data
  196. # @param tracker
  197. ##
  198. def _onPing( self, address, data, tracker ):
  199. print(address, "has pinged you!")
  200.  
  201. #Micah's bullshittery
  202.  
  203. def _clientIndex(self, address, data, tracker):
  204. #This function must send the amount of active connections to all active connections.
  205.  
  206. #Requests the current amount of players.
  207. activeConnections = len( self._connector.GetConnections() )
  208. for connection in connections:
  209. self._connector.Send( Server.SEND_EVENT_UPDATE_SERVERSLOT, activeConnections, connection['conn'] )
  210.  
  211. def _objectOwner(self, address, data, tracker):
  212. pass
  213.  
  214. def _playerNumber(self, address, data, tracker):
  215. pass
  216. ##
  217. # @def __printInitializeFinished( self )
  218. # @internal
  219. # @param self
  220. ##
  221. def __printInitializeFinished( self ):
  222. print("""\
  223. _______ _ _
  224. (_______) | | |
  225. _ ___ | | | _____ ____ ___ _____
  226. | | / _ \| | |(____ | _ \ /___) ___ |
  227. | |____| |_| | | |/ ___ | |_| |___ | ____|
  228. \______)___/ \_)_)_____| __/(___/|_____)
  229. |_|
  230.  
  231. Project Agile Development - Blender - Team 5
  232. """)
  233. print("Server has succesfully been initialized. Type \'exit\' to stop the server.")
  234.  
  235. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement