lsp

package
v0.0.0-...-3449da6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2022 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultEpochLimit  = 5
	DefaultEpochMillis = 2000
	DefaultWindowSize  = 1
)

Default values for LSP parameters.

Variables

This section is empty.

Functions

func WriteMessage

func WriteMessage(connection *net.UDPConn, addr *net.UDPAddr, message *Message) error

WriteMessage serializes given message and send it into by given connection.

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

func NewClient(hostport string, params *Params) (Client, error)

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

func NewAck(connID, seqNum int) *Message

NewAck returns a new acknowledgement message with the specified connection ID and sequence number.

func NewConnect

func NewConnect() *Message

NewConnect returns a new connect message.

func NewData

func NewData(connID, seqNum, size int, payload []byte) *Message

NewData returns a new data message with the specified connection ID, sequence number, and payload.

func ReadMessage

func ReadMessage(connection *net.UDPConn) (*Message, *net.UDPAddr, error)

ReadMessage receives message from given connection and de-serializes it.

func (*Message) String

func (m *Message) String() string

String returns a string representation of this message. To pretty-print a message, you can pass it to a format string like so:

msg := NewConnect()
fmt.Printf("Connect message: %s\n", msg)

type MsgType

type MsgType int

MsgType is an integer code describing an LSP message type.

const (
	MsgConnect MsgType = iota // Sent by clients to make a connection w/ the server.
	MsgData                   // Sent by clients/servers to send data.
	MsgAck                    // Sent by clients/servers to ack connect/data msgs.
)

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.

func NewParams

func NewParams() *Params

NewParams returns a Params with default field values.

func (*Params) String

func (p *Params) String() string

String returns a string representation of this params. To pretty-print a params, you can pass it to a format string like so:

params := NewParams()
fmt.Printf("New params: %s\n", params)

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

func NewServer(port int, params *Params) (Server, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL