aboutsummaryrefslogtreecommitdiffstats
path: root/netmodel/interfaces/socket
diff options
context:
space:
mode:
Diffstat (limited to 'netmodel/interfaces/socket')
-rw-r--r--netmodel/interfaces/socket/__init__.py171
-rw-r--r--netmodel/interfaces/socket/tcp.py86
-rw-r--r--netmodel/interfaces/socket/udp.py88
-rw-r--r--netmodel/interfaces/socket/unix.py87
4 files changed, 432 insertions, 0 deletions
diff --git a/netmodel/interfaces/socket/__init__.py b/netmodel/interfaces/socket/__init__.py
new file mode 100644
index 00000000..00581eb4
--- /dev/null
+++ b/netmodel/interfaces/socket/__init__.py
@@ -0,0 +1,171 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+import json
+
+from netmodel.network.packet import Packet
+from netmodel.network.interface import Interface, InterfaceState
+
+class Protocol(asyncio.Protocol):
+ def __init__(self):
+ self._transport = None
+
+ def terminate(self):
+ if self._transport:
+ self._transport.close()
+
+ def send(self, packet):
+ if isinstance(packet, Packet):
+ data = json.dumps(packet.to_query().to_dict())
+ else:
+ data = packet
+ self.send_impl(data)
+
+ def receive(self, data, ingress_interface):
+ try:
+ packet = Packet.from_query(Query.from_dict(json.loads(data)))
+ except:
+ packet = data
+ self.receive(packet, ingress_interface)
+
+
+class ServerProtocol(Protocol):
+ # asyncio.Protocol
+
+ def __init__(self):
+ super().__init__()
+
+ def connection_made(self, transport):
+ """
+ Called when a connection is made.
+ The argument is the _transport representing the pipe connection.
+ To receive data, wait for data_received() calls.
+ When the connection is closed, connection_lost() is called.
+ """
+ self._transport = transport
+ self.set_state(InterfaceState.Up)
+
+ def connection_lost(self, exc):
+ """
+ Called when the connection is lost or closed.
+ The argument is an exception object or None (the latter
+ meaning a regular EOF is received or the connection was
+ aborted or closed).
+ """
+ self.set_state(InterfaceState.Down)
+
+class ClientProtocol(Protocol):
+ def __init__(self, interface, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._interface = interface
+
+ # asyncio.Protocol
+
+ def connection_made(self, transport):
+ """
+ Called when a connection is made.
+ The argument is the _transport representing the pipe connection.
+ To receive data, wait for data_received() calls.
+ When the connection is closed, connection_lost() is called.
+ """
+ self._transport = transport
+ self._interface.set_state(InterfaceState.Up)
+
+ def connection_lost(self, exc):
+ """
+ Called when the connection is lost or closed.
+ The argument is an exception object or None (the latter
+ meaning a regular EOF is received or the connection was
+ aborted or closed).
+ """
+ self._interface.set_state(InterfaceState.Down)
+
+
+
+#------------------------------------------------------------------------------
+
+class SocketServer:
+ def __init__(self, *args, **kwargs):
+ # For a server, an instance of asyncio.base_events.Server
+ self._transport = None
+ self._clients = list()
+
+ def terminate(self):
+ """Close the server and terminate all clients.
+ """
+ self._socket.close()
+ for client in self._clients:
+ self._clients.terminate()
+
+ def send(self, packet):
+ """Broadcast packet to all connected clients.
+ """
+ for client in self._clients:
+ self._clients.send(packet)
+
+ def receive(self, packet):
+ raise RuntimeError('Unexpected packet received by server interface')
+
+ def __repr__(self):
+ if self._transport:
+ peername = self._transport.get_extra_info('peername')
+ else:
+ peername = 'not connected'
+ return '<Interface {} {}>'.format(self.__interface__, peername)
+
+ async def pending_up_impl(self):
+ try:
+ self._server = await self._create_socket()
+ except Exception as e:
+ await self._set_state(InterfaceState.Error)
+ self._error = str(e)
+
+ # Only the server interface is up once the socket has been created and
+ # is listening...
+ await self._set_state(InterfaceState.Up)
+
+class SocketClient:
+ def __init__(self, *args, **kwargs):
+ # For a client connection, this is a tuple
+ # (_SelectorSocketTransport, protocol)
+ self._transport = None
+ self._protocol = None
+
+ def send_impl(self, packet):
+ self._protocol.send(packet)
+
+ async def pending_up_impl(self):
+ try:
+ self._transport, self._protocol = await self._create_socket()
+ except Exception as e:
+ await self._set_state(InterfaceState.Error)
+ self._error = str(e)
+
+ def pending_down_impl(self):
+ if self._socket:
+ self._transport.close()
+
+ def __repr__(self):
+ if self._socket:
+ peername = self._transport.get_extra_info('peername')
+ else:
+ peername = 'not connected'
+ return '<Interface {} {}>'.format(self.__interface__, peername)
+
+
diff --git a/netmodel/interfaces/socket/tcp.py b/netmodel/interfaces/socket/tcp.py
new file mode 100644
index 00000000..5b886d9a
--- /dev/null
+++ b/netmodel/interfaces/socket/tcp.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+
+from netmodel.interfaces.socket import ServerProtocol, ClientProtocol
+from netmodel.interfaces.socket import SocketClient, SocketServer
+from netmodel.network.interface import Interface
+
+DEFAULT_ADDRESS = '127.0.0.1'
+DEFAULT_PORT = 7000
+
+class TCPProtocol:
+ def send_impl(self, data):
+ msg = data.encode()
+ self._transport.write(msg)
+
+ # asyncio.Protocol
+
+ def data_received(self, data):
+ """
+ Called when some data is received.
+ The argument is a bytes object.
+ """
+ msg = data.decode()
+ self.receive(msg, ingress_interface=self)
+
+class TCPServerProtocol(TCPProtocol, ServerProtocol, Interface):
+ __interface__ = 'tcp'
+
+ def __init__(self, *args, **kwargs):
+ # Note: super() does not call all parents' constructors
+ Interface.__init__(self, *args, **kwargs)
+ ServerProtocol.__init__(self)
+
+class TCPClientProtocol(TCPProtocol, ClientProtocol):
+ pass
+
+#------------------------------------------------------------------------------
+
+class TCPServerInterface(SocketServer, Interface):
+ __interface__ = 'tcpserver'
+
+ def __init__(self, *args, **kwargs):
+ SocketServer.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def new_protocol(self):
+ p = TcpServerProtocol(callback = self._callback, hook=self._hook)
+ self.spawn_interface(p)
+ return p
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ return loop.create_server(self.new_protocol, self._address, self._port)
+
+class TCPClientInterface(SocketClient, Interface):
+ __interface__ = 'tcpclient'
+
+ def __init__(self, *args, **kwargs):
+ SocketClient.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ protocol = lambda : TCPClientProtocol(self)
+ return loop.create_connection(protocol, self._address, self._port)
diff --git a/netmodel/interfaces/socket/udp.py b/netmodel/interfaces/socket/udp.py
new file mode 100644
index 00000000..d3fdb696
--- /dev/null
+++ b/netmodel/interfaces/socket/udp.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+
+from netmodel.interfaces.socket import ServerProtocol, ClientProtocol
+from netmodel.interfaces.socket import SocketClient, SocketServer
+from netmodel.network.interface import Interface
+
+DEFAULT_ADDRESS = '127.0.0.1'
+DEFAULT_PORT = 7000
+
+class UDPProtocol:
+
+ def send_impl(self, data):
+ msg = data.encode()
+ self._transport.sendto(msg)
+
+ # asyncio.Protocol
+
+ def datagram_received(self, data, addr):
+ msg = data.decode()
+ self.receive(msg, ingress_interface=self)
+
+ def error_received(self, exc):
+ print('Error received:', exc)
+
+class UDPServerProtocol(UDPProtocol, ServerProtocol, Interface):
+ __interface__ = 'udp'
+
+ def __init__(self, *args, **kwargs):
+ # Note: super() does not call all parents' constructors
+ Interface.__init__(self, *args, **kwargs)
+ ServerProtocol.__init__(self)
+
+class UDPClientProtocol(UDPProtocol, ClientProtocol):
+ pass
+
+#------------------------------------------------------------------------------
+
+class UDPServerInterface(SocketServer, Interface):
+ __interface__ = 'udpserver'
+
+ def __init__(self, *args, **kwargs):
+ SocketServer.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def new_protocol(self):
+ p = UdpServerProtocol(callback = self._callback, hook=self._hook)
+ self.spawn_interface(p)
+ return p
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ return loop.create_datagram_endpoint(self.new_protocol,
+ local_addr=(self._address, self._port))
+
+class UDPClientInterface(SocketClient, Interface):
+ __interface__ = 'udpclient'
+
+ def __init__(self, *args, **kwargs):
+ SocketClient.__init__(self)
+ self._address = kwargs.pop('address', DEFAULT_ADDRESS)
+ self._port = kwargs.pop('port', DEFAULT_PORT)
+ Interface.__init__(self, *args, **kwargs)
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ protocol = lambda : UDPClientProtocol(self)
+ return loop.create_datagram_endpoint(protocol,
+ remote_addr=(self._address, self._port))
diff --git a/netmodel/interfaces/socket/unix.py b/netmodel/interfaces/socket/unix.py
new file mode 100644
index 00000000..eec3d680
--- /dev/null
+++ b/netmodel/interfaces/socket/unix.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+#
+# Copyright (c) 2017 Cisco and/or its affiliates.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import asyncio
+
+from netmodel.interfaces.socket import ServerProtocol, ClientProtocol
+from netmodel.interfaces.socket import SocketClient, SocketServer
+from netmodel.network.interface import Interface
+from netmodel.util.misc import silentremove
+
+DEFAULT_PATH = '/tmp/unix_interface'
+
+class UnixProtocol:
+
+ def send_impl(self, data):
+ msg = data.encode()
+ self._transport.write(msg)
+
+ def data_received(self, data):
+ """
+ Called when some data is received.
+ The argument is a bytes object.
+ """
+ msg = data.decode()
+ self.receive(msg, ingress_interface=self)
+
+class UnixServerProtocol(UnixProtocol, ServerProtocol, Interface):
+ __interface__ = 'unix'
+
+ def __init__(self, *args, **kwargs):
+ # Note: super() does not call all parents' constructors
+ Interface.__init__(self, *args, **kwargs)
+ ServerProtocol.__init__(self)
+
+class UnixClientProtocol(UnixProtocol, ClientProtocol):
+ pass
+
+#------------------------------------------------------------------------------
+
+class UnixServerInterface(SocketServer, Interface):
+ __interface__ = 'unixserver'
+
+ def __init__(self, *args, **kwargs):
+ SocketServer.__init__(self)
+ self._path = kwargs.pop('path', DEFAULT_PATH)
+ Interface.__init__(self, *args, **kwargs)
+
+ def terminate(self):
+ silentremove(self._path)
+
+ def new_protocol(self):
+ p = UnixServerProtocol(callback=self._callback, hook=self._hook)
+ self.spawn_interface(p)
+ return p
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ silentremove(self._path)
+ return loop.create_unix_server(self.new_protocol, self._path)
+
+class UnixClientInterface(SocketClient, Interface):
+ __interface__ = 'unixclient'
+
+ def __init__(self, *args, **kwargs):
+ SocketClient.__init__(self)
+ self._path = kwargs.pop('path', DEFAULT_PATH)
+ Interface.__init__(self, *args, **kwargs)
+
+ def _create_socket(self):
+ loop = asyncio.get_event_loop()
+ protocol = lambda : UnixClientProtocol(self)
+ return loop.create_unix_connection(protocol, self._path)