Commit 76639977 authored by Gallacchi Mattia's avatar Gallacchi Mattia
Browse files

Correct server empty data behavior

parent 4edcf5c2
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -3,12 +3,12 @@ from pyrsvp.rsvpclient import RsvpClientTcp, RsvpClientNotAck
from pyrsvp.rsvptypes import *


def start_cb(status : str):
    print(status)
def start_cb(args : list):
    print(args)

if __name__ == "__main__":

    client = RsvpClientTcp()
    client = RsvpClientTcp("localhost", 55000)

    args = [
        RsvpInteger("ARG1", 1),
+9 −24
Original line number Diff line number Diff line
@@ -2,25 +2,17 @@ from pyrsvp.rsvpserver import RsvpServer, RsvpServerTcp
from pyrsvp.rsvptypes import *
import argparse

# class handles:
    
#     @RsvpServer.Command("START")
#     def start(self, values : dict):

#         print(values)
#         return RsvpServer.reply(True, [RsvpString("STATUS", "PASS")])

# @RsvpServer.Command("EXCEPTION")
# def handle_exception(ex : Exception) -> str:
def handle_exception(ex : Exception) -> str:
    
#     error =f"{type(ex).__name__} {ex}"
    error =f"{type(ex).__name__} {ex}"

#     try:
#         raise ex
#     except Exception as ex:
#         print(f"{type(ex).__name__}: {ex}")
    try:
        raise ex
    except Exception as ex:
        print(f"{type(ex).__name__}: {ex}")

#     return RsvpServer.reply(False, [RsvpString("ERROR", error)])
    return RsvpServer.reply(False, [RsvpString("ERROR", error)])

#Define commands and their callbacks
@RsvpServer.Command("START")
@@ -38,21 +30,14 @@ if __name__ == "__main__":
        description="This is a TCP server for RSVP"
    )

    parser.add_argument("-i", "--ip_address", help="IP address of the server")
    parser.add_argument("-p", "--port", help="Port used by the server")

    args = parser.parse_args()

    print(args)

    ip = "localhost"
    port = 8080

    if args.ip_address:
        ip = args.ip_address
    port = 55000

    if args.port:
        port  = int(args.port)

    srv = RsvpServerTcp(ip, port)
    srv = RsvpServerTcp("", port)
    srv.run()
+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ class RsvpClientTcp(RsvpClient):

        if callback:
            try:
                callback(*data)
                callback(data)
            except Exception as ex:
                raise RsvpClientInvalidCallback(ex)
        else:
+16 −2
Original line number Diff line number Diff line
@@ -146,6 +146,8 @@ class RsvpServerTcp(RsvpServer):
            """Handle new data received from the client
            """
            
            counter = 0

            while True:
                
                try:
@@ -155,16 +157,28 @@ class RsvpServerTcp(RsvpServer):
                    try:
                        buf = self.server.socket.recv(1, socket.MSG_PEEK | socket.MSG_DONTWAIT)
                        if buf == b'':
                            print("Hello")
                            continue
                    except BlockingIOError:
                    except Exception as ex:
                        print(ex)
                        break
                
                # If too many empty data is received consider that the client is dead
                if counter > 1000:
                    break

                if len(data) == 0: 
                    counter = counter + 1
                    continue
                

                reply = super().parse_data(data.decode())
                self.wfile.write(reply.encode())

        def finish(self) -> None:
            print(f"Client {self.client_address[0]} disconnected")
            return super().finish()

    def run(self):
        """Run the TCP server
        """