This microservice has three commands: save, load, and close.
To do any of these commands, the main program must first connect to the microservice using python sockets. To do that, the main program must:
- Open a python socket with the paremeters socket.AF_INET and socket.SOCKET_STREAM
- Bind the socket to the host and port that the microservice will connect to
- Listen for a connection request from the microservice
- Accept the connection request from the microservice, which returns a connection object
The save command is used to save the game state. To save a game state, the main program must connect to the microservice using python sockets and then:
- Create a dictionary with keys command, game_state, and file, which have values of save, a dictionary with the game state, and the file to save to, respectively. The dictionary with the game state should have keys player_1_score, player_2_score, and board. The first two keys map to integers that represent the number of games won by each player. The board value is a list of lists, where each inner list is row in the tic-tac-toe board.
- Use pickle.dumps() to serialize this dictionary as a byte stream.
- Use the sendall() method of the connection object to send the byte stream to the microservice.
The load command is used to load the game state. To load a game state, the main program must connect to the microservice using python sockets and then:
- Create a dictionary with keys command and file, which have values of load and the file to save to, respectively.
- Use pickle.dumps() to serialize this dictionary as a byte stream.
- Use the sendall() method of the connection object to send the byte stream to the microservice.
- Use the recv(1024) method of the connection object to receive the game state as a byte stream.
- Use pickle.loads() to convert the byte stream to a dictionary with the game state.
Here is an example of how the main program will request the microservice to load the game state, where conn is the connection object:
# load game state back in
load_params = {"command": "load", "file": "game_1.txt"}
load_params_b = pickle.dumps(load_params)
conn.sendall(load_params_b)
The close command is used to close the microservice. To close the microservice, the main program must connect to the microservice using python sockets and then:
- Create a dictionary with a key-value pair of "command": "close".
- Use pickle.dumps() to serialize this dictionary as a byte stream.
- Use the sendall() method of the connection object to send the byte stream to the microservice.
