CPSC 329 Software Development Fall 2026

CPSC 329 Lab 11: Protocol and Client Threads

Lab 10 provided practice with threads on the server side: supporting multiple clients and handling the thread safety issues that arise with shared state. This lab focuses on protocol, conversation and model state, client threads, and code organization — things that come up as the interactions between client and server become more complex.

Objectives

Successful completion of this lab means that you:

Collaboration and AI

Labs are for learning and practice — you may help and be helped by others when it comes to figuring out what to do, but in the end you need to understand and be able to do the things for yourself.

This lab can be done individually or in pairs. If you work together, use pair programming rather than splitting things up and working in parallel.

You may not use AI to figure out what to do or to do the task for you.

Due Date, Deliverables, and Handin

due Fri 11/20 at the start of class

To hand in your work:


Preliminaries

The Riddle Room

Players join the room and can submit riddles at any time. The server keeps the submitted riddles in a queue. There is one current riddle, posed to everyone in the room; when someone answers it correctly, everyone is told, and the next riddle in the queue becomes the current riddle. If the queue is empty, there's no current riddle until someone submits one. You can't answer your own riddle.

In this version, a riddle only ends when someone answers it correctly — if no one knows the answer, all the players can do is leave and hope that eventually someone who knows the answer will show up. Optional extensions address this problem by allowing a riddle to end unsolved: its submitter can reveal the answer or leave, or everyone can give up.

The Protocol

The riddle room's protocol follows the same style as the joke server's: a message starts with a command word, possibly followed by more words on the same line (its arguments). When the messages need to contain content that could include spaces (such as a riddle or its answer), the line with the command is followed by additional lines containing the content.

sendermessagemeaning
serverhello riddleroom/1sent when a client connects — the server speaks first
clientjoin namejoin the room (name is one word)
serverwelcome namejoined; followed by the current riddle (a riddle message), if there is one
serverjoined name
left name
another player joined or left the room (sent to everyone else in the room)
clientsubmit riddle + 2 more lines: the riddle, the answeradd a riddle to the queue
serversubmittedthe riddle was added
serverriddle submitter + 1 more line: the riddlea new current riddle (sent to everyone)
clientanswer + 1 more line: the answeranswer the current riddle
serverincorrectthe answer was wrong (sent only to the player who answered)
serversolved name + 1 more line: the answerthe current riddle was solved by name (sent to everyone); the next riddle (if any) follows
clientquitleave the room; the server closes the connection
servererror reasonthe client's message was rejected; nothing changed

A client which closes the connection or loses it (without sending quit) is treating as having left the room (the server sends a left message to everyone).

The error replies, and the reason each is rejected:

replywhenrejected because of
error usage: join name (one word)join without a name, or with more than one wordthe message's format
error can't submit kindsubmit followed by anything other than riddlethe message's format
error unknown command: commanda command the server doesn't knowthe message's format
error join firstsubmit riddle or answer before joiningconversation state
error already joinedjoin after joiningconversation state
error the name name is already takenjoin with the name of a player already in the roommodel state
error the riddle and answer can't be emptysubmit riddle with an empty riddle or answerthe message's content
error no riddle right nowanswer when there's no current riddlemodel state
error you can't answer your own riddleanswer when you submitted the current riddlemodel state

A message that's rejected still has its extra lines read: a rejected submit riddle or answer is followed by its riddle and answer, or its answer, just as an accepted one is. Otherwise those lines would be read as the client's next commands.

The sequence diagram below shows the protocol in action with alice and bob as two players. (Lines in ‹› stand for the actual content.) Note that when there's no current riddle, the submitted riddle is posed right away — so the riddle message reaches alice before the submitted reply to her request.

sequence diagram of the riddle
room protocol

Provided Code and Files

The code is in two packages, organized by whether it is used by the client or the server. Nothing is shared between them: the client and the server know each other only through the protocol.

Setup

Create an Eclipse project and share it to a local Git repository:

Running Several Programs at Once

As in lab 10, run the server in Eclipse and the clients in terminal windows:


The Riddle Room

Work through the parts below in order. Places where you need to add code are marked with TODO comments in the provided code, labeled with the specific steps (e.g. 2c, 4a).

1. A Lockstep Client

The provided client is lockstep like the joke server's clients: after sending a message, it reads from the server until the reply it is waiting for arrives (readUntilReply). It doesn't read from the server at any other time.

The server sends messages that the client didn't ask for — the current riddle after the welcome, other players joining and leaving, new riddles — and the lockstep client only finds out about them when it next happens to read from the server. The joke server never sent anything the client hadn't asked for, so being lockstep was fine there.

The riddle room is different from the joke server in other ways too:

joke server (lab 10)riddle room
messages the client didn't ask fornone: every message from the server is a replyjoins, departures, new riddles, solved riddles
conversation stateimplicit: where the handler is in its code (in the middle of tellRiddle, whatever line arrives next is the answer)explicit: ClientHandler's Phase, checked for each message
model statenone: any riddle can be asked for at any timethe current riddle and the queue, in Room
shared data and rulesa plain list, locked by the handlersRoom, which locks its own data and enforces the rules
how a change reaches other clients(it doesn't)the room notifies its listeners (the ClientHandlers), which send messages

2. A Listener Thread for the Client

To show messages as soon as they arrive, the client needs a thread which does nothing but wait for and handle messages from the server, while the main thread waits for the user to type commands. The connection to the server is a job of its own, so first it gets a class of its own — like ServerConnection in the auction client from class. The connection will then be the task run by the listener thread.

The lockstep client's state was implicit — while it was inside readUntilReply("welcome","error"), it was "waiting to find out whether I joined" — just like the joke server's handler. Now that messages can arrive at any time, the client keeps its state explicitly and changes it when handleMessage handles a message: whether it has joined (name_), whether the connection is still open (connected_), and its copy of the room's state (whether there's a current riddle, and who submitted it). That copy is only for showing the user what's going on (and for convenience checks) — the server's model is the one that counts. (The blue transitions come in part 4.)

state diagram of the client's
state

Note that the auction client defined specific phases (WAITING_FOR_HELLO, NOT_JOINED, etc) — that's useful when there are many conversation states. In this case there are only a few so the client's state is simply what its instance variables say: name_ is null until the client has joined, currentSubmitter_ is null when there's no current riddle, and connected_ says whether the connection is still open.

Also note what the client doesn't keep track of: whether it's waiting for a reply. It doesn't need to since it just shows each reply when it arrives. A GUI client would likely want such a state — for example, to disable an Answer button between sending an answer and getting incorrect or solved back, like the "bid pending" state of the auction client from class.

3. The Answer Protocol

Part 4 adds answering a riddle, which is already in the protocol table above: the client sends answer followed by a line with the answer, the server replies incorrect to the player who answered, or tells everyone solved — followed by the next riddle, if there is one. This part shows that piece of the protocol in the diagrams; there's nothing to write.

The sequence diagram shows four scenarios (highlighted): an incorrect answer, a correct answer with another riddle waiting in the queue, alice trying to answer her own riddle, and two correct answers sent at nearly the same time.

sequence diagram for answering a
riddle

There are two kinds of state that decide what happens when a message arrives at the server. Each connection has its own conversation state — has this client joined yet? — which ClientHandler keeps (its Phase). The room itself has model state — is there a current riddle? — which is the same for everyone; Room keeps it. A request can be rejected because of either one: submit riddle before joining is rejected because of the conversation state, while an answer when there's no current riddle is rejected because of the room's state.

Both kinds of state are shown in state diagrams. (event / action on each arrow is the message which arrives, or what happens, and what the server does in response.) The additions for answering are in blue, which you'll be implementing in the next part.

For the connection's conversation state:

conversation state diagram
with answer added

For the room's model state (NO_RIDDLE and so on are the outcomes the room reports back to the handler):

model state diagram with
answer added

The design decisions behind this part of the protocol:

One gap in what's implemented: a riddle nobody can answer would stay the current riddle forever. That's what reveal and revealed are for, but they aren't part of this lab (except as an extension).

4. Answering a Riddle

Adding answer involves every part of the server and the client. Here's how a message flows through the server, using submit riddle (which is already implemented) as an example:

  1. ClientHandler.run reads a line from the client and, based on the command word, calls a handleX method (handleSubmit).

  2. The handleX method reads any extra lines of the message (the riddle and the answer), checks the conversation state (has the client joined?), and calls the model (room_.submit).

  3. The model (Room) checks the rules and changes its state, and if something changed that other players need to know about, calls a RoomListener method on each of its listeners (riddlePosed, if there was no current riddle).

  4. Each ClientHandler, as a listener, turns the notification into a message to its own client (riddle submitter and the riddle).

  5. Back in the handleX method, a reply is sent to the client which made the request (submitted).

In MVC terms, Room is the model and ClientHandler is the view for one remote player: run is its input and sending to the client is its output. The handleX methods act as the controller. Because the handler is also the room's listener, all of the protocol — reading and writing messages — is in one class. (With a big protocol, the handleX methods could be moved into a separate controller class to keep ClientHandler from getting too big.)

Note also that a listener method such as riddlePosed is called by whichever thread changed the room — often another client's handler thread. That's why ClientHandler's send methods are synchronized: several threads may send to the same client.

5. Wrapup

You don't need to hand in anything for this part, but you should think about and answer the questions for yourself to make sure you understand what is going on.

If You Have Time

This part is optional. If you have time, add one or more of the following.