1425bb815Sopenharmony_ci#!/usr/bin/env python
2425bb815Sopenharmony_ci
3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation
4425bb815Sopenharmony_ci#
5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License.
7425bb815Sopenharmony_ci# You may obtain a copy of the License at
8425bb815Sopenharmony_ci#
9425bb815Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
10425bb815Sopenharmony_ci#
11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS
13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and
15425bb815Sopenharmony_ci# limitations under the License.
16425bb815Sopenharmony_ci
17425bb815Sopenharmony_ciimport struct
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ciMAX_BUFFER_SIZE = 256
20425bb815Sopenharmony_ci
21425bb815Sopenharmony_ciclass RawPacket(object):
22425bb815Sopenharmony_ci    """ Simplified transmission layer. """
23425bb815Sopenharmony_ci    def __init__(self, protocol):
24425bb815Sopenharmony_ci        self.protocol = protocol
25425bb815Sopenharmony_ci        self.data_buffer = b""
26425bb815Sopenharmony_ci
27425bb815Sopenharmony_ci    def connect(self, config_size):
28425bb815Sopenharmony_ci        """  Create connection. """
29425bb815Sopenharmony_ci        self.protocol.connect()
30425bb815Sopenharmony_ci        self.data_buffer = b""
31425bb815Sopenharmony_ci
32425bb815Sopenharmony_ci        # It will return with the Network configurations, which has the following struct:
33425bb815Sopenharmony_ci        # header [1] - size[1]
34425bb815Sopenharmony_ci        # configuration [config_size]
35425bb815Sopenharmony_ci        len_expected = config_size + 1
36425bb815Sopenharmony_ci
37425bb815Sopenharmony_ci        while len(self.data_buffer) < len_expected:
38425bb815Sopenharmony_ci            self.data_buffer += self.protocol.receive_data()
39425bb815Sopenharmony_ci
40425bb815Sopenharmony_ci        expected = struct.pack("B", config_size)
41425bb815Sopenharmony_ci
42425bb815Sopenharmony_ci        if self.data_buffer[0:1] != expected:
43425bb815Sopenharmony_ci            raise Exception("Unexpected configuration")
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_ci        result = self.data_buffer[1:len_expected]
46425bb815Sopenharmony_ci        self.data_buffer = self.data_buffer[len_expected:]
47425bb815Sopenharmony_ci
48425bb815Sopenharmony_ci        return result
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci    def close(self):
51425bb815Sopenharmony_ci        """ Close connection. """
52425bb815Sopenharmony_ci        self.protocol.close()
53425bb815Sopenharmony_ci
54425bb815Sopenharmony_ci    def send_message(self, _, data):
55425bb815Sopenharmony_ci        """ Send message. """
56425bb815Sopenharmony_ci        msg_size = len(data)
57425bb815Sopenharmony_ci
58425bb815Sopenharmony_ci        while msg_size > 0:
59425bb815Sopenharmony_ci            bytes_send = self.protocol.send_data(data)
60425bb815Sopenharmony_ci            if bytes_send < msg_size:
61425bb815Sopenharmony_ci                data = data[bytes_send:]
62425bb815Sopenharmony_ci            msg_size -= bytes_send
63425bb815Sopenharmony_ci
64425bb815Sopenharmony_ci    def get_message(self, blocking):
65425bb815Sopenharmony_ci        """ Receive message. """
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci        # Connection was closed
68425bb815Sopenharmony_ci        if self.data_buffer is None:
69425bb815Sopenharmony_ci            return None
70425bb815Sopenharmony_ci
71425bb815Sopenharmony_ci        while True:
72425bb815Sopenharmony_ci            if len(self.data_buffer) >= 1:
73425bb815Sopenharmony_ci                size = ord(self.data_buffer[0])
74425bb815Sopenharmony_ci                if size == 0:
75425bb815Sopenharmony_ci                    raise Exception("Unexpected data frame")
76425bb815Sopenharmony_ci
77425bb815Sopenharmony_ci                if len(self.data_buffer) >= size + 1:
78425bb815Sopenharmony_ci                    result = self.data_buffer[1:size + 1]
79425bb815Sopenharmony_ci                    self.data_buffer = self.data_buffer[size + 1:]
80425bb815Sopenharmony_ci                    return result
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ci            if not blocking and not self.protocol.ready():
83425bb815Sopenharmony_ci                return b''
84425bb815Sopenharmony_ci
85425bb815Sopenharmony_ci            received_data = self.protocol.receive_data(MAX_BUFFER_SIZE)
86425bb815Sopenharmony_ci
87425bb815Sopenharmony_ci            if not received_data:
88425bb815Sopenharmony_ci                return None
89425bb815Sopenharmony_ci
90425bb815Sopenharmony_ci            self.data_buffer += received_data
91