Skip to content

prestige_irc.connection

Avahe Kellenberger edited this page Dec 11, 2018 · 29 revisions
Classes
Connection
MessageListener

Class: Connection

A class for basic generic connectivity.

Functions

Link Description
init Readies a connection to a server at a specific port, and keeps the connection alive.
connect Connect to a server.
connect_socket Connect to a server using the given socket.
disconnect Disconnects from the server.
is_connection_alive Checks if connection is still live.
send_data Sends bytes across the connection.
send Helper function; sends a string across the connection as bytes.
add_listener Adds a listener to the connection.
remove_listener Removes a listener from the connection.
_process_data Processed the bytes received by the server.
__dispatch_listeners Dispatches the listeners waiting for the object
__listen Listens to incoming data from the socket.

def __init__

Readies a connection to a server at a specific port, and keeps the connection alive.
__init__(self)

def connect

Connect to a server.
connect(self, ip_address, port, timeout=None)
Parameter Type Description
ip_address str The IP address of the server.
port int The port number to bind to.
timeout int|None The number of seconds to wait to stop attempting to connect if a connection has not yet been made. Default value is None.

Returns:

Type Description
bool If the connection was successfully established.

def connect_socket

Connect to a server using the given socket.
connect_socket(self, sock, ip_address, port)
Parameter Type Description
sock socket The unconnected socket with which to establish the connection.
ip_address str The IP address of the server.
port int The port number to bind to.

Returns:

Type Description
bool If the connection was successfully established.

Throws:

Type Reason
socket.error If a connection could not be made.

def disconnect

Disconnects from the server.
disconnect(self)

Returns:

Type Description
bool If the connection was successfully terminated.

def is_connection_alive

Checks if connection is still live.
is_connection_alive(self)

Returns:

Type Description
bool If the connection is currently connected.

def send_data

Sends bytes across the connection.
send_data(self, data)
Parameter Type Description
data bytes The bytes to send.

def send

Helper function; sends a string across the connection as bytes.
send(self, message, crlf_ending=True)
Parameter Type Description
message str The message to send across the connection.
crlf_ending bool If the method should ensure the message ends with CR-LF. Default value is True.

def add_listener

Adds a listener to the connection.
add_listener(self, listener)
Parameter Type Description
listener A function which accepts an object which is created from Connection._process_data(bytes), which is called each time data is received from the server.

def remove_listener

Removes a listener from the connection.
remove_listener(self, listener)
Parameter Type Description
listener A function which accepts an object which is created from Connection._process_data(bytes), which is called each time data is received from the server.

def _process_data

Processes the bytes received by the server.
_process_data(self, data)
Parameter Type Description
data bytes Processes the bytes received by the server.

Returns:

Type Description
object An object used by the listeners.

def __dispatch_listeners

Dispatches the listeners waiting for the object.
__dispatch_listeners(self, obj)
Parameter Type Description
obj object The object to send to the listeners.

def __listen

Listens to incoming data from the socket.
__listen(self, buffer_size=4096)
Parameter Type Description
buffer_size int The number of bytes for the socket to receive. Default value is 4096.

Listens for messages, and uses a filter to determine if the message should be accepted by the listener.

If the message should be accepted, the implementation should then call MessageListener.receive.

Functions

Link Description
init Creates the listener.
accept Calls the message_filter parameter passed into the constructor.
receive Calls the receive parameter passed into the constructor.

def __init__

Creates the listener.
__init__(self, receive, message_filter=None)
Parameter Type Description
receive (Connection, object) -> None A method which takes a message as a parameter. This should only be called after message_filter returns True.
message_filter (Connection, object|None) -> bool A method which takes a connection and a message as parameters, and returns if the message should be accepted or not. If the message_filter is None, all messages will be accepted and passed to receive. Default value of message_filter is None.

def accept

Calls the `message_filter` parameter passed into the constructor.
accept(self, connection, message)
Parameter Type Description
connection Connection The connection the message was sent over.
message object The message received.

Returns:

Type Description
bool If the message should be accepted.

def receive

Calls the `receive` parameter passed into the constructor.
accept(self, connection, message)
Parameter Type Description
connection Connection The connection the message was sent over.
message object The message received.