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 socket
18425bb815Sopenharmony_ciimport select
19425bb815Sopenharmony_ci
20425bb815Sopenharmony_ci# pylint: disable=too-many-arguments,superfluous-parens
21425bb815Sopenharmony_ciclass Socket(object):
22425bb815Sopenharmony_ci    """ Create a new socket using the given address family, socket type and protocol number. """
23425bb815Sopenharmony_ci    def __init__(self, address, socket_family=socket.AF_INET, socket_type=socket.SOCK_STREAM, proto=0, fileno=None):
24425bb815Sopenharmony_ci        self.address = address
25425bb815Sopenharmony_ci        self.socket = socket.socket(socket_family, socket_type, proto, fileno)
26425bb815Sopenharmony_ci
27425bb815Sopenharmony_ci    def connect(self):
28425bb815Sopenharmony_ci        """
29425bb815Sopenharmony_ci        Connect to a remote socket at address (host, port).
30425bb815Sopenharmony_ci        The format of address depends on the address family.
31425bb815Sopenharmony_ci        """
32425bb815Sopenharmony_ci        print("Connecting to: %s:%s" % (self.address[0], self.address[1]))
33425bb815Sopenharmony_ci        self.socket.connect(self.address)
34425bb815Sopenharmony_ci
35425bb815Sopenharmony_ci    def close(self):
36425bb815Sopenharmony_ci        """" Mark the socket closed. """
37425bb815Sopenharmony_ci        self.socket.close()
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_ci    def receive_data(self, max_size=1024):
40425bb815Sopenharmony_ci        """ The maximum amount of data to be received at once is specified by max_size. """
41425bb815Sopenharmony_ci        return self.socket.recv(max_size)
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_ci    def send_data(self, data):
44425bb815Sopenharmony_ci        """ Send data to the socket. The socket must be connected to a remote socket. """
45425bb815Sopenharmony_ci        return self.socket.send(data)
46425bb815Sopenharmony_ci
47425bb815Sopenharmony_ci    def ready(self):
48425bb815Sopenharmony_ci        """ Monitor the file descriptor. """
49425bb815Sopenharmony_ci        result = select.select([self.socket], [], [], 0)[0]
50425bb815Sopenharmony_ci
51425bb815Sopenharmony_ci        return self.socket in result
52