Skip to content

Commit

Permalink
More improvements to mkchromecast:
Browse files Browse the repository at this point in the history
    - When systray menu fails, node and multiprocessing stops correctly.
    - The systray menu is safe to use.
  • Loading branch information
muammar committed Apr 13, 2016
1 parent f4ccf81 commit c10660e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 14 deletions.
11 changes: 6 additions & 5 deletions mkchromecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
from mkchromecast.cast import *
from mkchromecast.terminate import *
import mkchromecast.systray
import os.path

import atexit

if args.tray == False:

cc = casting()
checkmktmp()
writePidFile()

if cc.ip == '127.0.0.1' or None: # We verify the local IP.
print ('Your computer is not connected to any network')
Expand Down Expand Up @@ -48,9 +51,7 @@ def terminateapp():
except KeyboardInterrupt:
atexit.register(terminateapp)
else:
import pickle
import os.path
if os.path.exists('/tmp/mkcrhomecast.tmp') == True: #This is to verify that pickle tmp file exists
os.remove('/tmp/mkcrhomecast.tmp')

checkmktmp()
writePidFile()
mkchromecast.systray.main()

23 changes: 23 additions & 0 deletions mkchromecast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import argparse
from .audiodevices import *
from .terminate import *
import os.path
import pickle


parser = argparse.ArgumentParser(description='Cast mac os x audio to your google cast devices.')
parser.add_argument('-c', '--config', action="store_true", help='Use this option to connect from configuration file')
Expand All @@ -25,3 +28,23 @@
if args.config == True or args.discover == True or args.name == True or args.version == True or args.youtube == True:
print ('This option is not implemented yet.')
terminate()



"""
This is to write a PID file
"""
def writePidFile():
if os.path.exists('/tmp/mkcrhomecast.pid') == True: #This is to verify that pickle tmp file exists
os.remove('/tmp/mkcrhomecast.pid')
pid = str(os.getpid())
f = open('/tmp/mkcrhomecast.pid', 'wb')
pickle.dump(pid, f)
f.close()
return


def checkmktmp():
if os.path.exists('/tmp/mkcrhomecast.tmp') == True: #This is to verify that pickle tmp file exists
os.remove('/tmp/mkcrhomecast.tmp')
return
25 changes: 20 additions & 5 deletions mkchromecast/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

import subprocess
import multiprocessing
import time
import sys
import time, sys, os, signal
from .audiodevices import *
from .cast import *
import psutil
import os
import signal
import psutil, pickle

"""
These functions are used to get up the streaming server.
Expand All @@ -22,15 +20,32 @@
def streaming():
webcast = ['./bin/node', './nodejs/node_modules/webcast-osx-audio/bin/webcast.js']
p = subprocess.Popen(webcast)

f = open('/tmp/mkcrhomecast.pid', 'rb')
pidnumber=int(pickle.load(f))
print (pidnumber)

localpid=getpid()

while p.poll() is None:
try:
time.sleep(0.5)
if psutil.pid_exists(pidnumber) == False: # With this if I ensure that if main app fails, everything
inputint() # will get back to normal
outputint()
parent = psutil.Process(localpid)
for child in parent.children(recursive=True): # or parent.children() for recursive=False
child.kill()
parent.kill()
except KeyboardInterrupt:
print ("Ctrl-c was requested")
sys.exit(0)
except IOError:
print ("I/O Error")
sys.exit(0)
except OSError:
print ("OSError")
sys.exit(0)
else:
print ('Reconnecting streaming...')
relaunch(stream,recasting,kill)
Expand Down
6 changes: 2 additions & 4 deletions mkchromecast/systray.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ def stop_cast(self):
self.parent = psutil.Process(self.parent_pid)
for child in self.parent.children(recursive=True): # or parent.children() for recursive=False
child.kill()
if os.path.exists('/tmp/mkcrhomecast.tmp') == True:
os.remove('/tmp/mkcrhomecast.tmp')
checkmktmp()
self.search_cast()
self.ncast.quit_app()
self.stopped = True
Expand All @@ -212,6 +211,5 @@ def main():
menubar()

if __name__ == '__main__':
if os.path.exists('/tmp/mkcrhomecast.tmp') == True: #This is to verify that pickle tmp file exists
os.remove('/tmp/mkcrhomecast.tmp')
checkmktmp()
main()

0 comments on commit c10660e

Please sign in to comment.