Commit d4a65d8f authored by Agrigor's avatar Agrigor

Minor changes and trials

parent b138e2f9
*.kde*
.kde*/
.idea
\ No newline at end of file
......@@ -14,11 +14,40 @@ import threading
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PORT = 50000
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Connection:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.socket = ""
def check_ip(self):
try:
socket.inet_aton(self.ip)
return True
except socket.error:
print("First argument was no valid IP-address")
sys.exit(1)
def create_client_socket(self):
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!")
def cleanup(self):
self.socket.close()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -28,36 +57,20 @@ import threading
class Client:
def __init__(self, ip, port):
self.source_ip = ip
self.port = port
# Initialize source ip adress
try:
socket.inet_aton(self.source_ip)
except socket.error:
print("First argument was no valid IP-address")
sys.exit(0)
else:
self.UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.UDP.bind(("", self.port))
def cleanup(self):
self.UDP.close()
print("\n Bye Bye")
sys.exit(0)
self.foo = Connection(ip, port)
if self.foo.check_ip():
self.foo.create_client_socket()
def main(self):
try:
self.daten, self.addr = self.UDP.recvfrom(1024)
if self.addr[0] == self.source_ip:
print(self.addr[0], self.daten.decode())
else:
print("Any hacker (IP: " + self.addr[0] + ") tries to kidding me, now I'll kill him!")
except OSError:
print("...no connection...")
time.sleep(1)
self.foo.receive(1024)
finally:
self.cleanup()
def cleanup(self):
print("\n Bye Bye")
sys.exit(0)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -65,7 +78,7 @@ class Client:
# Start Client
client = Client(sys.argv[1], 50000)
client = Client(sys.argv[1], PORT)
client.main()
......
......@@ -4,7 +4,6 @@
import RPi.GPIO as GPIO
import time
import sys
import socket
import threading
......@@ -35,7 +34,7 @@ class Connection:
self.socket.close()
def send(self, message):
self.socket.sendto(str.encode(self.msg), (self.target_ip, self.target_port))
self.socket.sendto(str.encode(message), (self.target_ip, self.target_port))
def observe_buzzers(self, buzzers):
for buzzer in buzzers:
......@@ -80,12 +79,12 @@ class Server:
self.connection = Connection(ip, port)
self.buzzers = [Buzzer(i, pin, BOUNCE) for i, pin in enumerate(BUZZER_PINS, start=1)]
self.connection.observe_buzzers(self.buzzers)
# Initialize target ip adress
# Initialize target ip address
try:
socket.inet_aton(ip)
except socket.error:
print("First Argument was no valid IP-Address")
sys.exit(0)
sys.exit(1)
def cleanup(self):
GPIO.cleanup()
......
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