Skip to content

TCPConnection equivalent for listening for AIS client connections? #188

@nsands

Description

@nsands

The TCPConnection class works when you want to connect to an AIS TCP server. However, our AIS service acts as a client and wants to initiate a connection to me.

Is there any equivalent class in pyais to cater for this?

I made the following class (a slightly modified copy of the TCPConnection class) which seems to work well for this purpose. At least it works well with our AIS service. However, I'd rather use an existing pyais class if it exists.

`class TCPListener(SocketStream):
"""
Read AIS data from a remote TCP client
https://en.wikipedia.org/wiki/NMEA_0183
"""

def recv(self) -> bytes:
    return self._fobj.recv(self.BUF_SIZE)

def __init__(
    self,
    host: str,
    port: int = 80,
    preprocessor: typing.Optional[PreprocessorProtocol] = None,
    tbq: typing.Optional[TagBlockQueue] = None
) -> None:
    sock: socket = socket(AF_INET, SOCK_STREAM)
    try:
        sock.bind((host, port))
        sock.listen()
        print(f"AIS TCP Listener listening on:  {host}:{port}")

        client_sock, client_address = sock.accept()
        print(f"Client has connected:  {client_address}")
    except ConnectionRefusedError as e:
        sock.close()
        raise ConnectionRefusedError(f"Failed to connect to {host}:{port}") from e
    super().__init__(client_sock, preprocessor=preprocessor, tbq=tbq)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions