| 35 | |
| 36 | |
| 37 | class Client: |
| 38 | |
| 39 | def __init__(self): |
| 40 | # Creates a client instance |
| 41 | self.socket = Socket() |
| 42 | self.file_path = None |
| 43 | self.run_path = None |
| 44 | self.choice = None |
| 45 | self.unregistered = 0 |
| 46 | |
| 47 | def __del__(self): |
| 48 | try: |
| 49 | if self.unregistered == 0: |
| 50 | self.unregister() |
| 51 | except: |
| 52 | print("Error - Client could not be destroyed") |
| 53 | |
| 54 | def getFilepath(self, file_path): |
| 55 | # Gets arguments from Command-Line and assigns to instance of client |
| 56 | self.file_path = file_path |
| 57 | |
| 58 | def setRunpath(self, file_prefix): |
| 59 | self.run_path = os.path.join(get_dpdk_runtime_dir(file_prefix), |
| 60 | RUNTIME_SOCKET_NAME) |
| 61 | |
| 62 | def register(self): |
| 63 | # Connects a client to DPDK-instance |
| 64 | if os.path.exists(self.file_path): |
| 65 | os.unlink(self.file_path) |
| 66 | try: |
| 67 | self.socket.recv_fd.bind(self.file_path) |
| 68 | except socket.error as msg: |
| 69 | print("Error - Socket binding error: " + str(msg) + "\n") |
| 70 | self.socket.recv_fd.settimeout(2) |
| 71 | self.socket.send_fd.connect(self.run_path) |
| 72 | JSON = (API_REG + self.file_path + "\"}}") |
| 73 | self.socket.send_fd.sendall(JSON.encode()) |
| 74 | |
| 75 | self.socket.recv_fd.listen(1) |
| 76 | self.socket.client_fd = self.socket.recv_fd.accept()[0] |
| 77 | |
| 78 | def unregister(self): |
| 79 | # Unregister a given client |
| 80 | self.socket.client_fd.send((API_UNREG + self.file_path + "\"}}").encode()) |
| 81 | self.socket.client_fd.close() |
| 82 | |
| 83 | def requestMetrics(self): |
| 84 | # Requests metrics for given client |
| 85 | self.socket.client_fd.send(METRICS_REQ.encode()) |
| 86 | data = self.socket.client_fd.recv(BUFFER_SIZE).decode() |
| 87 | print("\nResponse: \n", data) |
| 88 | |
| 89 | def repeatedlyRequestMetrics(self, sleep_time): |
| 90 | # Recursively requests metrics for given client |
| 91 | print("\nPlease enter the number of times you'd like to continuously request Metrics:") |
| 92 | n_requests = int(input("\n:")) |
| 93 | # Removes the user input from screen, cleans it up |
| 94 | print("\033[F") |
no outgoing calls
no test coverage detected