Commit bdb7c26e authored by Agrigor's avatar Agrigor

Merge branch 'master' of gitlab:hacksaar/Jeopardy

parents f7eb3565 d4a65d8f
*.kde*
.kde*/
.idea
\ No newline at end of file
###############################################################################
########################## IMPORT LIBRARIES ###################################
###############################################################################
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ IMPORT LIBRARIES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import time
import signal
import sys
import socket
import threading
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ GLOBAL CONSTANTS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###############################################################################
########################## GLOBAL CONSTANTS ###################################
###############################################################################
# Network Constants
SOURCE_IP = sys.argv[1]
PORT = 50000
UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
###############################################################################
########################## DEFINITION FUNCTIONS ###############################
###############################################################################
def init():
# Initialize source ip adress
try:
socket.inet_aton(SOURCE_IP)
except socket.error:
print("First argument was no valid IP-address")
sys.exit(0)
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
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~ CLASS DEFINITION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class Connection:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.socket = ""
def check_ip(self):
try:
daten, addr = UDP.recvfrom(1024)
if addr[0] == SOURCE_IP:
print(addr[0], daten.decode())
else:
print("Any hacker (IP: " + addr[0] + ") tries to kidding me, now I'll kill him!")
except OSError:
print("...no connection...")
time.sleep(1)
###############################################################################
################################# PROGRAM #####################################
###############################################################################
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()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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
main()
client = Client(sys.argv[1], PORT)
client.main()
###############################################################################
#################################### END ######################################
###############################################################################
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -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:
......@@ -78,12 +77,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