2019-08-16 07:26:22 +08:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from libp2p.crypto.keys import PrivateKey, PublicKey
|
2019-08-03 07:50:44 +08:00
|
|
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
|
|
|
from libp2p.peer.id import ID
|
|
|
|
from libp2p.security.base_transport import BaseSecureTransport
|
|
|
|
from libp2p.security.secure_conn_interface import ISecureConn
|
|
|
|
|
|
|
|
|
2019-08-17 00:19:37 +08:00
|
|
|
class BaseSession(ISecureConn):
|
2019-08-03 07:50:44 +08:00
|
|
|
"""
|
|
|
|
``BaseSession`` is not fully instantiated from its abstract classes as it
|
|
|
|
is only meant to be used in clases that derive from it.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, transport: BaseSecureTransport, conn: IRawConnection, peer_id: ID
|
|
|
|
) -> None:
|
2019-08-03 08:41:29 +08:00
|
|
|
self.local_peer = transport.local_peer
|
|
|
|
self.local_private_key = transport.local_private_key
|
2019-08-14 06:19:22 +08:00
|
|
|
self.conn = conn
|
2019-08-03 07:50:44 +08:00
|
|
|
self.remote_peer_id = peer_id
|
2019-08-16 07:26:22 +08:00
|
|
|
self.remote_permanent_pubkey = None
|
2019-08-03 07:50:44 +08:00
|
|
|
|
2019-08-16 07:26:22 +08:00
|
|
|
self.initiator = self.conn.initiator
|
|
|
|
self.writer = self.conn.writer
|
|
|
|
self.reader = self.conn.reader
|
2019-08-03 08:41:29 +08:00
|
|
|
|
|
|
|
# TODO clean up how this is passed around?
|
|
|
|
def next_stream_id(self) -> int:
|
2019-08-14 06:19:22 +08:00
|
|
|
return self.conn.next_stream_id()
|
|
|
|
|
|
|
|
async def write(self, data: bytes) -> None:
|
|
|
|
await self.conn.write(data)
|
|
|
|
|
|
|
|
async def read(self) -> bytes:
|
|
|
|
return await self.conn.read()
|
|
|
|
|
|
|
|
def close(self) -> None:
|
|
|
|
self.conn.close()
|
2019-08-03 08:41:29 +08:00
|
|
|
|
2019-08-03 07:50:44 +08:00
|
|
|
def get_local_peer(self) -> ID:
|
|
|
|
return self.local_peer
|
|
|
|
|
2019-08-16 07:26:22 +08:00
|
|
|
def get_local_private_key(self) -> PrivateKey:
|
2019-08-03 07:50:44 +08:00
|
|
|
return self.local_private_key
|
|
|
|
|
|
|
|
def get_remote_peer(self) -> ID:
|
|
|
|
return self.remote_peer_id
|
|
|
|
|
2019-08-16 07:26:22 +08:00
|
|
|
def get_remote_public_key(self) -> Optional[PublicKey]:
|
2019-08-03 07:50:44 +08:00
|
|
|
return self.remote_permanent_pubkey
|