Commit 5dde531f authored by Agrigor's avatar Agrigor

UDP communication is now in a class + some minor changes

parent a4b62eee
#!/usr/bin/python
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -21,33 +23,28 @@ PORT = 50000
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Connection:
class Comunication:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.socket = ""
self.__ip = ip
self.__port = port
self.__socket = ""
def check_ip(self):
try:
socket.inet_aton(self.ip)
socket.inet_aton(self.__ip)
return True
except socket.error:
print("First argument was no valid IP-address")
sys.exit(1)
except OSError:
return False
def create_client_socket(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind(("", self.port))
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.__socket.bind(("", self.__port))
def receive(self, bytes):
daten, addr = self.socket.recvfrom(bytes)
if addr[0] == self.ip:
print(addr[0] + " -> " + daten.decode())
else:
print("Any hacker (IP: " + addr[0] + ") tries to kidding me, now I'll kill him!")
return self.__socket.recvfrom(bytes)
def cleanup(self):
self.socket.close()
self.__socket.close()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -57,17 +54,27 @@ class Connection:
class Client:
def __init__(self, ip, port):
self.foo = Connection(ip, port)
if self.foo.check_ip():
self.foo.create_client_socket()
self.UDP = Comunication(ip, port)
self.IP = ip
if self.UDP.check_ip():
self.UDP.create_client_socket()
else:
print("First argument was no valid IP-address")
sys.exit(1)
def main(self):
try:
self.foo.receive(1024)
while True:
daten, addr = self.UDP.receive(1024)
if addr[0] == self.IP:
print(addr[0] + " -> " + daten.decode())
else:
print("Any hacker (IP: " + addr[0] + ") tries to kidding me, now I'll kill him!")
finally:
self.cleanup()
def cleanup(self):
self.UDP.cleanup()
print("\n Bye Bye")
sys.exit(0)
......
#!/usr/bin/python3
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -19,12 +21,13 @@ BOUNCE = 300
PORT = 50000
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Connection:
class Communication:
def __init__(self, ip, port):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.msg = ""
......@@ -44,7 +47,7 @@ class Connection:
def notify(self, buzzer):
self.msg = "Button " + str(buzzer.number) + " pressed"
self.send(self.msg)
print(self.msg)
print(self.msg + " -> " + self.target_ip)
class Buzzer:
......@@ -75,9 +78,9 @@ class Buzzer:
class Server:
def __init__(self, ip, port):
self.connection = Connection(ip, port)
self.UDP = Communication(ip, port)
self.buzzers = [Buzzer(i, pin, BOUNCE) for i, pin in enumerate(BUZZER_PINS, start=1)]
self.connection.observe_buzzers(self.buzzers)
self.UDP.observe_buzzers(self.buzzers)
# Initialize target ip address
try:
socket.inet_aton(ip)
......@@ -87,7 +90,7 @@ class Server:
def cleanup(self):
GPIO.cleanup()
self.connection.close()
self.UDP.close()
print("\n Bye Bye")
sys.exit(0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment