Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a connection factory for the async client #158

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions irc/client_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import threading
import logging

from . import connection
from .client import ServerConnection, ServerNotConnectedError, Reactor,\
SimpleIRCClient, Event, _ping_ponger

Expand Down Expand Up @@ -99,8 +100,8 @@ class AioConnection(ServerConnection):
protocol_class = IrcProtocol

async def connect(
self, server, port, nickname,
password=None, username=None, ircname=None
self, server, port, nickname, password=None, username=None,
ircname=None, connect_factory=connection.AioFactory()
):
"""Connect/reconnect to a server.

Expand All @@ -113,6 +114,9 @@ async def connect(
* username - The username
* ircname - The IRC name ("realname")

* connect_factory - An async callable that takes the event loop and the
server address, and returns a connection (with a socket interface)

This function can be called to reconnect a closed connection.

Returns the AioProtocol instance (used for handling incoming
Expand All @@ -133,12 +137,11 @@ async def connect(
self.username = username or nickname
self.ircname = ircname or nickname
self.password = password
self.connect_factory = connect_factory

connection = self.reactor.loop.create_connection(
lambda: self.protocol_class(self, self.reactor.loop),
self.server,
self.port,
)
protocol_instance = self.protocol_class(self, self.reactor.loop)
connection = self.connect_factory(
protocol_instance, self.server_address)
transport, protocol = await connection

self.transport = transport
Expand Down
36 changes: 36 additions & 0 deletions irc/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,39 @@ def connect(self, server_address):
sock.connect(server_address)
return sock
__call__ = connect


class AioFactory:
"""
A class for creating async custom socket connections.

To create a simple connection:

server_address = ('localhost', 80)
Factory()(protocol_instance, server_address)

To create an SSL connection:

Factory(ssl=True)(protocol_instance, server_address)

To create an IPv6 connection:

Factory(ipv6=True)(protocol_instance, server_address)

Note that Factory doesn't save the state of the socket itself. The
caller must do that, as necessary. As a result, the Factory may be
re-used to create new connections with the same settings.

"""

family = socket.AF_INET

def __init__(self, **kwargs):
self.connection_args = kwargs

def connect(self, protocol_instance, server_address):
return protocol_instance.loop.create_connection(
lambda: protocol_instance, *server_address,
**self.connection_args)

__call__ = connect