Documentation ¶
Index ¶
Constants ¶
const ( DefaultEpochLimit = 5 DefaultEpochMillis = 2000 DefaultWindowSize = 1 )
Default values for LSP parameters.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client interface { // ConnID returns the connection ID associated with this client. ConnID() int // Read reads a data message from the server and returns its payload. // This method should block until data has been received from the server and // is ready to be returned. It should return a non-nil error if either // (1) the connection has been explicitly closed, or (2) the connection has // been lost due to an epoch timeout and no other messages are waiting to be // returned. Read() ([]byte, error) // Write sends a data message with the specified payload to the server. // This method should NOT block, and should return a non-nil error // if the connection with the server has been lost. Write(payload []byte) error // Close terminates the client's connection with the server. It should block // until all pending messages to the server have been sent and acknowledged. // Once it returns, all goroutines running in the background should exit. // // You may assume that Read, Write, and Close will not be called after // Close has been called. Close() error }
Client defines the interface for a LSP client.
func NewClient ¶
NewClient creates, initiates, and returns a new client. This function should return after a connection with the server has been established (i.e., the client has received an Ack message from the server in response to its connection request), and should return a non-nil error if a connection could not be made (i.e., if after K epochs, the client still hasn't received an Ack message from the server in response to its K connection requests).
hostport is a colon-separated string identifying the server's host address and port number (i.e., "localhost:9999").
type Message ¶
type Message struct { Type MsgType // One of the message types listed above. ConnID int // Unique client-server connection ID. SeqNum int // Message sequence number. Size int // Size of the payload. Payload []byte // Data message payload. }
Message represents a message used by the LSP protocol.
func NewAck ¶
NewAck returns a new acknowledgement message with the specified connection ID and sequence number.
func NewData ¶
NewData returns a new data message with the specified connection ID, sequence number, and payload.
func ReadMessage ¶
ReadMessage receives message from given connection and de-serializes it.
type Params ¶
type Params struct { // EpochLimit is the number of epochs that can transpire before declaring a // connection to be lost. EpochLimit int // EpochMillis is the number of milliseconds between epochs. EpochMillis int // WindowSize is the size of the sliding window (i.e. the max number of // non-acknowledged messages that can be sent at a given time). WindowSize int }
Params defines configuration parameters for an LSP client or server.
type Server ¶
type Server interface { // Read reads a data message from a client and returns its payload, // and the connection ID associated with the client that sent the message. // This method should block until data has been received from some client. // It should return a non-nil error if either (1) the connection to some // client has been explicitly closed, (2) the connection to some client // has been lost due to an epoch timeout and no other messages from that // client are waiting to be returned, or (3) the server has been closed. // In the first two cases, the client's connection ID and a non-nil // error should be returned. In the third case, an ID with value 0 and // a non-nil error should be returned. Read() (int, []byte, error) // Write sends a data message to the client with the specified connection ID. // This method should NOT block, and should return a non-nil error if the // connection with the client has been lost. Write(connID int, payload []byte) error // CloseConn terminates the client with the specified connection ID, returning // a non-nil error if the specified connection ID does not exist. All pending // messages to the client should be sent and acknowledged. However, unlike Close, // this method should NOT block. CloseConn(connID int) error // Close terminates all currently connected clients and shuts down the LSP server. // This method should block until all pending messages for each client are sent // and acknowledged. If one or more clients are lost during this time, a non-nil // error should be returned. Once it returns, all goroutines running in the // background should exit. // // You may assume that Read, Write, CloseConn, or Close will not be called after // calling this method. Close() error }
Server defines the interface for a LSP server.
func NewServer ¶
NewServer creates, initiates, and returns a new server. This function should NOT block. Instead, it should spawn one or more goroutines (to handle things like accepting incoming client connections, triggering epoch events at fixed intervals, synchronizing events using a for-select loop like you saw in project 0, etc.) and immediately return. It should return a non-nil error if there was an error resolving or listening on the specified port number.