refactor(Clientjorobany): move string_control to client.

This commit is contained in:
Francisco 2023-04-29 16:08:34 -05:00
parent 7470e9afaf
commit ea3d8cc9c5
2 changed files with 10 additions and 14 deletions

View File

@ -1,18 +1,12 @@
class Calculator:
def __init__(self, client, string_control='\n'):
def __init__(self, client):
self.client = client
self.string_control = string_control
def calc(self, operation):
fixed_operation = operation + self.string_control
self.client.connect()
self.client.send(
fixed_operation
)
response = self.client.getText(
self.string_control
)
self.client.send(operation)
response = self.client.getText()
self.client.close()
result = Result(response)
return result.get_content()

View File

@ -3,23 +3,24 @@ import socket
class ClientJorobany:
coding = 'utf-8'
def __init__(self, host, port):
def __init__(self, host, port, string_control):
self.host = host
self.port = port
self.string_control = string_control
def connect(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
def getText(self, string_control):
def getText(self):
string_control_idx = 0
complete_text = ''
tmp_string_control = ''
while True:
char = self.socket.recv(1).decode(self.coding)
if char == string_control[string_control_idx]:
if char == self.string_control[string_control_idx]:
tmp_string_control += char
if tmp_string_control == string_control:
if tmp_string_control == self.string_control:
break
string_control_idx += 1
continue
@ -30,7 +31,8 @@ class ClientJorobany:
return complete_text
def send(self, data):
self.socket.sendall(data.encode())
fixed_data = data + self.string_control
self.socket.sendall(fixed_data.encode())
def close(self):
self.socket.close()