|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 ladyada for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +""" |
| 24 | +`adafruit_esp32spi_server` |
| 25 | +================================================================================ |
| 26 | +
|
| 27 | +TODO: better description? |
| 28 | +Server management lib to make handling and responding to incoming requests much easier |
| 29 | +
|
| 30 | +* Author(s): Matt Costi |
| 31 | +""" |
| 32 | + |
| 33 | +from micropython import const |
| 34 | +import adafruit_esp32spi_socket as socket |
| 35 | + |
| 36 | +_the_interface = None # pylint: disable=invalid-name |
| 37 | +def set_interface(iface): |
| 38 | + """Helper to set the global internet interface""" |
| 39 | + global _the_interface # pylint: disable=global-statement, invalid-name |
| 40 | + _the_interface = iface |
| 41 | + socket.set_interface(iface) |
| 42 | + |
| 43 | +NO_SOCK_AVAIL = const(255) |
| 44 | + |
| 45 | + |
| 46 | +# pylint: disable=unused-argument, redefined-builtin, invalid-name |
| 47 | +class server: |
| 48 | + """ TODO: class docs """ |
| 49 | + def __init__(self, port=80, debug=False): |
| 50 | + self.port = port |
| 51 | + self._server_sock = socket.socket(socknum=NO_SOCK_AVAIL) |
| 52 | + self._client_sock = socket.socket(socknum=NO_SOCK_AVAIL) |
| 53 | + self._debug = debug |
| 54 | + |
| 55 | + |
| 56 | + def start(self): |
| 57 | + """ start the server """ |
| 58 | + self._server_sock = socket.socket() |
| 59 | + _the_interface.start_server(self.port, self._server_sock.socknum) |
| 60 | + if self._debug: |
| 61 | + ip = _the_interface.pretty_ip(_the_interface.ip_address) |
| 62 | + print("Server available at {0}:{1}".format(ip, self.port)) |
| 63 | + print("Sever status: ", _the_interface.get_server_state(self._server_sock.socknum)) |
| 64 | + |
| 65 | + def client_available(self): |
| 66 | + """ |
| 67 | + returns a client socket connection if available.otherwise, returns a non available socket |
| 68 | + :return the client |
| 69 | + :rtype Socket |
| 70 | + """ |
| 71 | + sock = None |
| 72 | + if self._server_sock.socknum != NO_SOCK_AVAIL: |
| 73 | + if self._client_sock.socknum != NO_SOCK_AVAIL: |
| 74 | + # check previous received client socket |
| 75 | + if self._debug: |
| 76 | + print("checking if last client sock still valid") |
| 77 | + if self._client_sock.connected() and self._client_sock.available(): |
| 78 | + sock = self._client_sock |
| 79 | + if not sock: |
| 80 | + # check for new client sock |
| 81 | + if self._debug: |
| 82 | + print("checking for new client sock") |
| 83 | + client_sock_num = _the_interface.socket_available(self._server_sock.socknum) |
| 84 | + sock = socket.socket(socknum=client_sock_num) |
| 85 | + else: |
| 86 | + print("Server has not been started, cannot check for clients!") |
| 87 | + |
| 88 | + if sock and sock.socknum != NO_SOCK_AVAIL: |
| 89 | + if self._debug: |
| 90 | + print("client sock num is: ", sock.socknum) |
| 91 | + self._client_sock = sock |
| 92 | + return self._client_sock |
| 93 | + |
| 94 | + return socket.socket(socknum=NO_SOCK_AVAIL) |
0 commit comments