We were all SYNners.
A client and a server must establish a connection before they can communicate. In real networks, this is done through carefully designed handshakes that tolerate message reordering.
Implement a minimal client–server connection protocol over a simulated network.
You are given a simulated message-passing network. Messages are never dropped or duplicated, but may arrive out of order.
Your task is to implement a deterministic connection handshake between a client and a server.
Message Format
Message {
src, dst, payload
}
srcis the source of theMessage.dstis the destination of theMessage.payloadis a string, which is either"SYN","ACK", or"SYN-ACK".
Network API
net_send(message)
net_recv()
- This API is already provided (you do not need to implement it).
net_sendenqueues a message into the network.net_recvreturns the next delivered message, ornulloptif none are available.- Messages may be delivered out of order.
Classes to implement
Client {
Client(id, server_id)
connect()
tick()
connected()
}
Server {
Server(id)
tick()
has_connection(client_id)
}
Client
The constructor
Client(id, server_id)takes in two integersidandserver_id, which indicate the ids of theClientandServer, respectively.connect()initiates a connection attempt. Returns nothing.tick()is called repeatedly by the judge. Returns nothing.connected()returnstrueiff the client considers the connection established, elsefalse.
Server
The constructor
Server(id)takes in one integerid, the server’s id.tick()is called repeatedly by the judge. Returns nothing.has_connection(client_id)returnstrueiff the server considersclient_idconnected, elsefalse.
Protocol
The handshake protocol is:
Client → Server:
"SYN"Server → Client:
"SYN-ACK"Client → Server:
"ACK"
The connection is considered established when:
the client has received
"SYN-ACK"and sent"ACK", andthe server has received
"ACK"from that client.
tick semantics
The judge runs the simulation in discrete steps called ticks.
In each tick:
Zero or more messages may already be available via
net_recv()(possibly out of order).The judge calls
client.tick()andserver.tick()(order is unspecified).Any messages the client/server choose to send must be sent using
net_send()during theirtick()call.
Your tick() must:
repeatedly call
net_recv()until it returnsnullopt,process any messages that are addressed to that participant (
msg.dst == my_id),update internal protocol state accordingly,
send any required protocol messages using
net_send().
Your tick() must not:
block or wait for future messages,
use real sockets or sleeping,
assume messages arrive in order.
Progress happens across multiple calls to tick().
Behavioral Requirements
Client rules
connect()must send exactly one"SYN"message to the server (and do nothing else).After
connect()is called, the client must eventually send"ACK"only after it receives a valid"SYN-ACK"from the server.connected()must returntrueonly after the client has sent its"ACK"(in response to"SYN-ACK").
Server rules
Upon receiving
"SYN"from a clientc, the server must send"SYN-ACK"back toc.The server must mark client
cas connected only after receiving"ACK"fromc.has_connection(c)must returntrueiff clientchas been marked connected.
Out-of-Order and Unexpected Messages
Messages can arrive out of order. Your implementation must be safe under any delivery order.
If the server receives
"ACK"from clientcbefore it has ever received"SYN"fromc, it must ignore that"ACK".If the client receives
"SYN-ACK"before it has calledconnect(), it must ignore that message.Any message with an unknown payload, wrong destination, or invalid context must be ignored.
Determinism
For the same sequence of delivered messages, your implementation must behave deterministically.
There is no randomness in this problem, and no timeouts/retransmissions.
Input / Output Format
The judge will compile and run your Client and Server implementation in a hidden harness.
Therefore, you do not need to write any custom input/output parsing.
Notes
You may not use OS networking APIs (no real sockets).
You must implement the protocol logic and state transitions yourself.
The goal is to correctly handle message ordering and connection state.
Constraints
- There is exactly one
clientand oneserver. connect()is called exactly once by the judge.- Messages are never dropped or duplicated.
- Messages may be delivered out of order.
- No concurrency requirements.
Running on custom harness..