Commit d4a65d8f authored by Agrigor's avatar Agrigor

Minor changes and trials

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