johnpentyrch

server multithread

Jun 18th, 2020
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import socketserver
  2. from collections import namedtuple
  3. from fl_networking_tools import get_binary, send_binary
  4. from threading import Event
  5.  
  6.  
  7. '''
  8. Commands:
  9. COMMANDS
  10. QUES - question command
  11. SANS - answer to question
  12. JOIN - request to join
  13. '''
  14. class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
  15.     pass
  16.  
  17. NUMBER_OF_PLAYERS = 2
  18. players = []
  19. num_answers=0
  20. ready_to_start = Event()
  21. wait_for_answers = Event()
  22.  
  23.  
  24.  
  25.  
  26.  
  27. # Named tuples are extensions of the tuple structure, with contents you can refer to by name. In this case, the question will be held in a variable named q and the answer in answer.
  28. # This is just the set up of the question - it will be sent to the client using the send_binary function when a request is made.
  29. Question = namedtuple('Question', ['q', 'answer'])
  30.  
  31. q1 = Question("Expand the acronym ALU", "Arithmetic Logic Unit")
  32. # The socketserver module uses 'Handlers' to interact with connections. When a client connects a version of this class is made to handle it.
  33. class QuizGame(socketserver.BaseRequestHandler):
  34.     # The handle method is what actually handles the connection
  35.     def handle(self):
  36.         #Retrieve Command
  37.         for request in get_binary(self.request):
  38.             # Add this to the top of handle()
  39.             global players # Make sure this is global
  40.             global num_answers
  41.             num_answer=0
  42.             if request[0] == "JOIN":
  43.             # Code to handle join
  44.                 team_name = request[1]
  45.                 players.append(team_name)
  46.                 if len(players) == NUMBER_OF_PLAYERS:
  47.             # If correct number of players
  48.                     ready_to_start.set()
  49.             # Trigger the event
  50.  
  51.             # Send the confirmation response
  52.                 send_binary(self.request, [2, "Game on; Good Luck"])
  53.     # Wait for the ready to start event
  54.                 ready_to_start.wait()
  55.  
  56.  
  57.                
  58.             elif request[0] == "QUES":
  59.                 #Send question
  60.                 send_binary(self.request, (1, q1.q))
  61.         #Your server code goes here
  62.             elif request[0] == 'SANS':
  63.                 num_answers+=1
  64.                 print(q1.answer,request[1])
  65.                 if num_answers != len(players):
  66.                     # Trigger the event
  67.                     wait_for_answers.wait()
  68.                 wait_for_answers.set()
  69.                 if request[1].replace(' ','').replace('\t','').lower() ==  q1.answer.replace(' ','').replace('\t','').lower():
  70.                     send_binary(self.request, (2,'Correct, Answer is: '+ q1.answer))
  71.                 else:
  72.                     send_binary(self.request, (3,'Incorrect. Answer is: '+ q1.answer))  
  73.                 num_answers=0    
  74.  
  75.  
  76. # Open the quiz server and bind it to a port - creating a socket
  77. # This works similarly to the sockets you used before, but you have to give it both an address pair (IP and port) and a handler for the server.
  78. quiz_server = ThreadedTCPServer(('127.0.0.1', 2065), QuizGame)
  79. quiz_server.serve_forever()
Add Comment
Please, Sign In to add comment