Commit bdb7c26e authored by Agrigor's avatar Agrigor

Merge branch 'master' of gitlab:hacksaar/Jeopardy

parents f7eb3565 d4a65d8f
*.kde* *.kde*
.kde*/ .kde*/
.idea
\ No newline at end of file
############################################################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
########################## IMPORT LIBRARIES ################################### # ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
############################################################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import time import time
import signal
import sys import sys
import socket import socket
import threading
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ GLOBAL CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###############################################################################
########################## GLOBAL CONSTANTS ###################################
###############################################################################
# Network Constants
SOURCE_IP = sys.argv[1]
PORT = 50000 PORT = 50000
UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
############################################################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
########################## DEFINITION FUNCTIONS ############################### # ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
############################################################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def init(): class Connection:
# Initialize source ip adress def __init__(self, ip, port):
try: self.ip = ip
socket.inet_aton(SOURCE_IP) self.port = port
except socket.error: self.socket = ""
print("First argument was no valid IP-address")
sys.exit(0) def check_ip(self):
else:
# Initialize signal handling for interrupt by CTRL+C or kill command
signal.signal(signal.SIGINT, quit)
# Initialize UDP socket binding
UDP.bind(("", PORT))
# Things to be done before end of programm
def quit(signal, frame):
UDP.close()
print("\n Bye Bye")
sys.exit(0)
###############################################################################
################################## MAIN #######################################
###############################################################################
# Mainfunction which includes the whole program
def main():
# Initialize
init()
while True:
# Wait on button event and print some lazy messages
try: try:
daten, addr = UDP.recvfrom(1024) socket.inet_aton(self.ip)
if addr[0] == SOURCE_IP: return True
print(addr[0], daten.decode()) except socket.error:
else: print("First argument was no valid IP-address")
print("Any hacker (IP: " + addr[0] + ") tries to kidding me, now I'll kill him!") sys.exit(1)
except OSError:
print("...no connection...") def create_client_socket(self):
time.sleep(1) self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind(("", self.port))
###############################################################################
################################# PROGRAM ##################################### 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()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MAIN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Client:
def __init__(self, ip, port):
self.foo = Connection(ip, port)
if self.foo.check_ip():
self.foo.create_client_socket()
def main(self):
try:
self.foo.receive(1024)
finally:
self.cleanup()
def cleanup(self):
print("\n Bye Bye")
sys.exit(0)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Start Client # Start Client
main() client = Client(sys.argv[1], PORT)
client.main()
############################################################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#################################### END ###################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
############################################################################### # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...@@ -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:
...@@ -78,12 +77,12 @@ class Server: ...@@ -78,12 +77,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