-
-
Save greg5600k/6836422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" | |
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>com.plex.pms</string> | |
<key>OnDemand</key> | |
<false/> | |
<key>ProgramArguments</key> | |
<array> | |
<string>/Applications/PMS.app/Contents/MacOS/PMS</string> | |
</array> | |
<key>RunAtLoad</key> | |
<true/> | |
<key>UserName</key> | |
<string>dubz</string> | |
<key>WorkingDirectory</key> | |
<string>/Applications</string> | |
<key>ServiceDescription</key> | |
<string>PMS</string> | |
</dict> | |
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
PlexConnect | |
Sources: | |
inter-process-communication (queue): http://pymotw.com/2/multiprocessing/communication.html | |
""" | |
import time | |
time.sleep (.5) | |
import sys, time | |
from os import sep | |
import socket | |
from multiprocessing import Process, Pipe | |
import signal, errno | |
import DNSServer, WebServer | |
import Settings | |
from Debug import * # dprint() | |
def getIP_self(): | |
cfg = param['CSettings'] | |
if cfg.getSetting('enable_plexconnect_autodetect')=='True': | |
# get public ip of machine running PlexConnect | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.connect(('1.2.3.4', 1000)) | |
IP = s.getsockname()[0] | |
dprint('PlexConnect', 0, "IP_self: "+IP) | |
else: | |
# manual override from "settings.cfg" | |
IP = cfg.getSetting('ip_plexconnect') | |
dprint('PlexConnect', 0, "IP_self (from settings): "+IP) | |
return IP | |
procs = {} | |
pipes = {} | |
param = {} | |
running = False | |
def startup(): | |
global procs | |
global pipes | |
global param | |
global running | |
# Settings | |
cfg = Settings.CSettings() | |
param['CSettings'] = cfg | |
# Logfile | |
if cfg.getSetting('logpath').startswith('.'): | |
# relative to current path | |
logpath = sys.path[0] + sep + cfg.getSetting('logpath') | |
else: | |
# absolute path | |
logpath = cfg.getSetting('logpath') | |
param['LogFile'] = logpath + sep + 'PlexConnect.log' | |
param['LogLevel'] = cfg.getSetting('loglevel') | |
dinit('PlexConnect', param, True) # init logging, new file, main process | |
# more Settings | |
param['IP_self'] = getIP_self() | |
param['HostToIntercept'] = 'trailers.apple.com' | |
param['HostOfPlexConnect'] = 'atv.plexconnect' | |
running = True | |
# init DNSServer | |
if cfg.getSetting('enable_dnsserver')=='True': | |
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-DNSServer | |
proc = Process(target=DNSServer.Run, args=(slave, param)) | |
proc.start() | |
time.sleep(0.1) | |
if proc.is_alive(): | |
procs['DNSServer'] = proc | |
pipes['DNSServer'] = master | |
else: | |
dprint('PlexConnect', 0, "DNSServer not alive. Shutting down.") | |
running = False | |
# init WebServer | |
if running: | |
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer | |
proc = Process(target=WebServer.Run, args=(slave, param)) | |
proc.start() | |
time.sleep(0.1) | |
if proc.is_alive(): | |
procs['WebServer'] = proc | |
pipes['WebServer'] = master | |
else: | |
dprint('PlexConnect', 0, "WebServer not alive. Shutting down.") | |
running = False | |
# init WebServer_SSL | |
if running: | |
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer | |
proc = Process(target=WebServer.Run_SSL, args=(slave, param)) | |
proc.start() | |
time.sleep(0.1) | |
if proc.is_alive(): | |
procs['WebServer_SSL'] = proc | |
pipes['WebServer_SSL'] = master | |
else: | |
dprint('PlexConnect', 0, "WebServer_SSL not alive. Shutting down.") | |
running = False | |
# not started successful - clean up | |
if not running: | |
cmdShutdown() | |
shutdown() | |
return running | |
def run(): | |
while running: | |
# do something important | |
try: | |
time.sleep(60) | |
except IOError as e: | |
if e.errno == errno.EINTR and not running: | |
pass # mask "IOError: [Errno 4] Interrupted function call" | |
else: | |
raise | |
def shutdown(): | |
for slave in procs: | |
procs[slave].join() | |
dprint('PlexConnect', 0, "shutdown") | |
def cmdShutdown(): | |
global running | |
running = False | |
# send shutdown to all pipes | |
for slave in pipes: | |
pipes[slave].send('shutdown') | |
dprint('PlexConnect', 0, "Shutting down.") | |
def sighandler_shutdown(signum, frame): | |
signal.signal(signal.SIGINT, signal.SIG_IGN) # we heard you! | |
cmdShutdown() | |
if __name__=="__main__": | |
signal.signal(signal.SIGINT, sighandler_shutdown) | |
signal.signal(signal.SIGTERM, sighandler_shutdown) | |
dprint('PlexConnect', 0, "***") | |
dprint('PlexConnect', 0, "PlexConnect") | |
dprint('PlexConnect', 0, "Press CTRL-C to shut down.") | |
dprint('PlexConnect', 0, "***") | |
success = startup() | |
if success: | |
run() | |
shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment