aboutsummaryrefslogtreecommitdiffstats
path: root/netmodel/interfaces/socket/__init__.py
blob: 00581eb4f88a9fd04f47df927244614697d52fa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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)