Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##
- # Project Agile Development - Blender - Copyright (c) BLE-Team 5 - 2018
- # Permission is hereby granted to any person to use, copy, modify and merge.
- #
- # The copyright notice and this permission must be included in the file.
- #
- # @author Alex Antonides <{@link http://www.alex-antonides.com/}>
- # @author Micah Bredenhorst
- # @ignore
- ##
- import time, uuid
- from threading import Thread
- from connector import Connector
- ##
- # This method will be called at first once the file is loaded.
- # @def start()
- ##
- def start():
- _server = Server()
- ##
- # This is the Server class which the server uses.
- # The Server is able to send and retrieve packets.
- ##
- class Server:
- LISTEN_EVENT_ON_JOIN_REQUEST = "JRQ"
- LISTEN_EVENT_ON_LOCATION_UDPATE = "LUP"
- LISTEN_EVENT_ON_PING = "PING"
- SEND_EVENT_ON_JOIN_HANDLED = "JHR" #This is the same as Join_Request SO Maybe I should change it.
- SEND_EVENT_ON_LOCATION_UPDATE = "LUP"
- SEND_EVENT_ON_ID_SEND = "IDS"
- SEND_EVENT_PING = "PING"
- SEND_EVENT_UPDATE_SERVERSLOT = "SLOT"
- SEND_EVENT_ASSIGN_PLAYER_NUMBER = "NUM"
- CONSOLE_COMMAND_EXIT = "exit"
- CONSOLE_CONNECTIONS_AMOUNT = "amountplayers"
- CONSOLE_COMMAND_PING = "ping"
- CONSOLE_COMMAND_TEST = "Test"
- MAX_CLIENT_CONNECTIONS = 4
- _udp_ip = "" # The IP the server is listening to. Default: "" - all.
- _udp_port = 5006 # The port of the server. Default: 5006
- _connector = None # The connection socket of the server.
- _command = None # The last given command given to the server.
- active = False # If the Server is active or not.
- PlayerList = () # Empty player list
- ##
- # @def __init__( self )
- # @internal
- # @param self
- ##
- def __init__( self ):
- # Instantiate the receiver and the sender.
- self._connector = Connector( self._udp_ip, self._udp_port )
- # Listen to specific messages.
- self._connector.ListenTo( Server.LISTEN_EVENT_ON_LOCATION_UDPATE, self._onLocationUpdate, -1 )
- self._connector.ListenTo( Server.LISTEN_EVENT_ON_PING, self._onPing, -1 )
- # Initializement is ready! Show the message in the console!
- self.__printInitializeFinished()
- # Start the listening
- self._start()
- ##
- # @def __del__( self )
- # @internal
- # @param self
- ##
- def __del__(self):
- self._end()
- ##
- # @def _update( self )
- # @private
- # @param self
- ##
- def _start( self ):
- if self.active == True:
- return
- else:
- def handleListening(self):
- while self._command != Server.CONSOLE_COMMAND_EXIT:
- connections = self._connector.GetConnections()
- self._connector.ListenOnce( connections )
- def handleConsoleInput(self):
- while self._command != Server.CONSOLE_COMMAND_EXIT:
- self._command = input(" -> ")
- if self._command == "":
- print("Please put in a command.")
- elif self._command == Server.CONSOLE_COMMAND_EXIT:
- self._end()
- elif self._command == Server.CONSOLE_COMMAND_TEST:
- self._RQCS
- elif self._command == Server.CONSOLE_CONNECTIONS_AMOUNT:
- 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() ) ), "." )
- elif self._command == Server.CONSOLE_COMMAND_PING:
- connections = self._connector.GetConnections()
- for connection in connections:
- self._connector.Send( self.SEND_EVENT_PING, self.SEND_EVENT_PING, connection['conn'] )
- else:
- print("Command", self._command, "is not known to the console.")
- self._connector.Host( self._onAccept, self.MAX_CLIENT_CONNECTIONS )
- lis = Thread( name="lis", target= handleListening, kwargs={ 'self': self } )
- lis.start()
- cip = Thread( name="cip", target= handleConsoleInput, kwargs={ 'self': self } )
- cip.start()
- self.active = True
- ##
- # @def _end( self )
- # @private
- # @param self
- ##
- def _end( self ):
- if self.active == False:
- return
- else:
- self.active = False
- connections = self._connector.GetConnections()
- for connection in connections:
- connection['conn'].close()
- self._connector.Close()
- self._command = Server.CONSOLE_COMMAND_EXIT
- print("Server is shutting down..")
- ##
- # @def _onAccept( self, address, connection )
- # @private
- # @param self
- # @param address
- # @param connection
- ##
- def _onAccept( self, address, acceptor ):
- connections = self._connector.GetConnections()
- activeConnections = len(self._connector.GetConnections())
- print( address, "'s request has been accepted. ID:", acceptor['ID'], ", Player Number:", activeConnections,"has been given. Places left:", ( Server.MAX_CLIENT_CONNECTIONS - len( connections ) ) )
- # Send the acceptation to each connection.
- for connection in connections:
- if acceptor['ID'] != connection['ID']:
- self._connector.Send( Server.SEND_EVENT_ON_JOIN_HANDLED, { 'ID': connection['ID'], 'PlayerNumber': activeConnections }, connection['conn'] )
- else:
- self._connector.Send( Server.SEND_EVENT_ON_ID_SEND, acceptor['ID'], acceptor['conn'] )
- self._connector.Send( Server.SEND_EVENT_ASSIGN_PLAYER_NUMBER, activeConnections, connection['conn'] )
- ##
- # @def _onLocationUpdate( self, address, data, tracker )
- # @private
- # @param self
- # @param address
- # @param data
- # @param data.ID
- # @param data.position
- # @param tracker
- ##
- def _onLocationUpdate( self, address, data, tracker):
- #print (data)
- if self._connector.IsIDConnected( data['ID'] ) == False:
- return False
- else:
- connections = self._connector.GetConnections()
- # Send the location to each connection.
- for connection in connections:
- # We don't want to send the location back to the player who sent it.
- if data['ID'] != connection['ID']:
- self._connector.Send( Server.SEND_EVENT_ON_LOCATION_UPDATE, data['position'], connection['conn'] )
- ##
- # @def _onPing( self, address, data, tracker )
- # @private
- # @param self
- # @param address
- # @param data
- # @param tracker
- ##
- def _onPing( self, address, data, tracker ):
- print(address, "has pinged you!")
- #Micah's bullshittery
- def _clientIndex(self, address, data, tracker):
- #This function must send the amount of active connections to all active connections.
- #Requests the current amount of players.
- activeConnections = len( self._connector.GetConnections() )
- for connection in connections:
- self._connector.Send( Server.SEND_EVENT_UPDATE_SERVERSLOT, activeConnections, connection['conn'] )
- def _objectOwner(self, address, data, tracker):
- pass
- def _playerNumber(self, address, data, tracker):
- pass
- ##
- # @def __printInitializeFinished( self )
- # @internal
- # @param self
- ##
- def __printInitializeFinished( self ):
- print("""\
- _______ _ _
- (_______) | | |
- _ ___ | | | _____ ____ ___ _____
- | | / _ \| | |(____ | _ \ /___) ___ |
- | |____| |_| | | |/ ___ | |_| |___ | ____|
- \______)___/ \_)_)_____| __/(___/|_____)
- |_|
- Project Agile Development - Blender - Team 5
- """)
- print("Server has succesfully been initialized. Type \'exit\' to stop the server.")
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement