Skip to content

Commit

Permalink
#10 Implemented signal handling
Browse files Browse the repository at this point in the history
Implemented signal handling on the server and client scripts. The applications detect three signals: SIGINT, SIGQUIT and SIGTSTP.  On any signal, the sockets are closed and object deleted.
  • Loading branch information
mkristofic committed Jan 4, 2020
1 parent 8464dba commit c5efdf4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 17 additions & 2 deletions client/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
from modules.Streamer import Streamer
from modules.Recorder import Recorder
import sys, signal

recorder = Recorder(0)
stream = Streamer('127.0.0.1', 5005, recorder) #35.204.145.0

# SIGNAL HANDLING
def receive_signal(signal_number, frame):
global stream, recorder
del stream
print("\nStreamer released.")
del recorder
print("Recorder released.")
print("Application exited.")
sys.exit()

def main():
recorder = Recorder(0)
stream = Streamer('127.0.0.1', 5005, recorder) #35.204.145.0
stream.send_to_server()

if __name__ == "__main__":
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGQUIT, receive_signal)
signal.signal(signal.SIGTSTP, receive_signal)
main()
16 changes: 15 additions & 1 deletion server/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask, render_template, Response
from modules.Detector import Detector
import socket
import socket, sys, signal

# FLASK CONFIG
app = Flask(__name__)
Expand All @@ -26,6 +26,17 @@ def rec():
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + data + b'\r\n')

# SIGNAL HANDLING
def receive_signal(signal_number, frame):
global detector, sock, app
del detector
print("\nDetector released.")
sock.close()
print("Socket closed.")
del app
print("Application exited.")
sys.exit()

@app.route('/')
def index():
return render_template('index.html')
Expand All @@ -35,4 +46,7 @@ def video_feed():
return Response(rec(), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
signal.signal(signal.SIGINT, receive_signal)
signal.signal(signal.SIGQUIT, receive_signal)
signal.signal(signal.SIGTSTP, receive_signal)
app.run(debug=True) #ssl_context='adhoc'

0 comments on commit c5efdf4

Please sign in to comment.