diff --git a/gitso/trunk/ConnectionWindow.py b/gitso/trunk/ConnectionWindow.py index 21aa6d5..9e8dd4d 100644 --- a/gitso/trunk/ConnectionWindow.py +++ b/gitso/trunk/ConnectionWindow.py @@ -1,7 +1,7 @@ #! /usr/bin/env python import wx -import os, sys, signal, os.path +import os, sys, signal, os.path, time, thread import AboutWindow, GitsoThread class ConnectionWindow(wx.Frame): @@ -18,33 +18,31 @@ class ConnectionWindow(wx.Frame): @author: Derek Buranen @author: Aaron Gerber """ + self.ToggleValue = 0 self.paths = paths - self.thread = GitsoThread.GitsoThread(self, self.paths) + self.thread = None + self.threadLock = thread.allocate_lock() if sys.platform.find('linux') != -1: width = 165 height = 350 + xval1 = 155 + xval2 = 250 else: height = 350 width = 175 + xval1 = 180 + xval2 = 265 wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(height,width), style=wx.DEFAULT_FRAME_STYLE & ~ (wx.RESIZE_BORDER | wx.RESIZE_BOX | wx.MAXIMIZE_BOX)) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) - # I don't know if this works in OS X icon = wx.Icon(os.path.join(self.paths['main'], 'icon.ico'), wx.BITMAP_TYPE_ICO) self.SetIcon(icon) if sys.platform == 'win32': self.SetBackgroundColour(wx.Colour(236,233,216)) - if sys.platform.find('linux') != -1: - xval1 = 155 - xval2 = 250 - else: - xval1 = 180 - xval2 = 265 - #Buttons self.connectButton = wx.Button(self, 10, "Start", wx.Point(xval1, 70)) wx.EVT_BUTTON(self, 10, self.ConnectSupport) @@ -83,7 +81,6 @@ class ConnectionWindow(wx.Frame): fileMenu.Append(13, "&Clear History", "Clear History") if sys.platform == 'darwin': - #OS X deals with the help and file menu in wonky way. fileMenu.Append(wx.ID_ABOUT, "&About", "About Gitso") wx.EVT_MENU(self, wx.ID_ABOUT, self.ShowAbout) else: @@ -104,9 +101,9 @@ class ConnectionWindow(wx.Frame): self.SetMenuBar(menuBar) - self.statusBar = self.CreateStatusBar(2) - self.statusBar.SetStatusText("Status:", 0) - self.statusBar.SetStatusWidths([50, 300]) + self.statusBar = self.CreateStatusBar(1) + self.statusBar.SetStatusWidths([350]) + self.setMessage("Idle", False) self.SetDefaultItem(self.hostField) self.hostField.SetFocus() @@ -124,8 +121,10 @@ class ConnectionWindow(wx.Frame): @author: Aaron Gerber """ if self.rb1.GetValue(): + self.ToggleValue = 0 self.hostField.Enable(True) else: + self.ToggleValue = 1 self.hostField.Enable(False) @@ -138,9 +137,7 @@ class ConnectionWindow(wx.Frame): """ if self.rb1.GetValue(): # Get Help if self.validHost(self.hostField.GetValue().strip()) and self.hostField.GetValue() != "Enter/Select Support Address": - self.connectButton.Enable(False) - self.stopButton.Enable(True) - self.statusBar.SetStatusText("Started", 1) + self.setMessage("Connecting...", True) host = self.hostField.GetValue().strip() @@ -153,18 +150,15 @@ class ConnectionWindow(wx.Frame): self.sampleList.append(host) self.hostField.Destroy() self.displayHostBox(self.sampleList, host) - - self.thread.setHost(host) - self.thread.start() + + self.createThread(host) else: - self.statusBar.SetStatusText("Invalid Support Address", 1) + self.setMessage("Invalid Support Address", False) else: # Give Suppport - self.connectButton.Enable(False) - self.stopButton.Enable(True) - self.statusBar.SetStatusText("Started", 1) - self.thread.start() - - + self.setMessage("Starting Server...", True) + self.createThread() + + def ShowAbout(self,e): """ Display About Dialog @@ -229,15 +223,19 @@ class ConnectionWindow(wx.Frame): @author: Derek Buranen @author: Aaron Gerber """ - self.thread.kill() - self.connectButton.Enable(True) - self.stopButton.Enable(False) - self.statusBar.SetStatusText("Idle", 1) + if self.thread <> None: + self.thread.kill() + time.sleep(.5) + self.thread = None + self.setMessage("Idle.", False) return def OnCloseWindow(self, evt): - self.KillPID(self) + if self.thread <> None: + self.thread.kill() + time.sleep(.5) + self.thread = None self.Destroy() @@ -270,3 +268,32 @@ class ConnectionWindow(wx.Frame): self.hostField = wx.ComboBox(self, 30, "", wx.Point(105, 12), wx.Size(230, -1), list, wx.CB_DROPDOWN) self.hostField.SetValue(text) + def setMessage(self, message, status): + self.threadLock.acquire() + + self.statusBar.SetStatusText(message, 0) + if status: + self.connectButton.Enable(False) + self.stopButton.Enable(True) + else: + self.connectButton.Enable(True) + self.stopButton.Enable(False) + + if self.ToggleValue == 0: + self.rb1.SetValue(True) + else: + self.rb2.SetValue(True) + + self.threadLock.release() + + def createThread(self, host=""): + if self.thread <> None: + self.thread.kill() + self.thread = GitsoThread.GitsoThread(self, self.paths) + self.thread.setHost(host) + self.thread.start() + + # If you don't wait 2 seconds, the interface won't reload and it'll freeze. + # Possibly on older systems you should wait longer, it works fine on mine... + time.sleep(2) + diff --git a/gitso/trunk/Gitso.py b/gitso/trunk/Gitso.py index cc3b114..9360c07 100644 --- a/gitso/trunk/Gitso.py +++ b/gitso/trunk/Gitso.py @@ -53,7 +53,6 @@ if len(sys.argv) == 2: elif len(sys.argv) > 2: help_menu() - if sys.platform == "darwin": paths['preferences'] = os.path.join(os.path.expanduser("~"), "Library", "Application Support", "Gitso") if os.path.exists(paths['preferences']) != True: diff --git a/gitso/trunk/GitsoThread.py b/gitso/trunk/GitsoThread.py index 2674b7c..5c03ee0 100755 --- a/gitso/trunk/GitsoThread.py +++ b/gitso/trunk/GitsoThread.py @@ -6,23 +6,74 @@ import Processes class GitsoThread(threading.Thread): def __init__(self, window, paths): - self.window = window - self.paths = paths - self.host = "" + self.window = window + self.paths = paths + self.host = "" + self.error = False + self.pid = 0 + self.running = True self.process = Processes.Processes(paths) threading.Thread.__init__(self) + def run(self): if self.host <> "": - print "In Thread -- Starting getSupport()" - self.process.getSupport(self.host) + self.pid = self.process.getSupport(self.host) + time.sleep(.5) + if self.checkStatus(): + self.window.setMessage("Connected.", True) + else: + self.window.setMessage("Could not connect.", False) + self.error = True else: - print "In Thread -- Starting giveSupport()" - self.process.giveSupport() + self.pid = self.process.giveSupport() + time.sleep(.5) + if self.checkStatus(): + self.window.setMessage("Server running.", True) + else: + self.window.setMessage("Could not start server.", False) + self.error = True + + print "GitsoThread.run(pid: " + str(self.pid) + ") running..." + + while(self.running and self.checkStatus()): + time.sleep(.5) + if not self.error: + self.window.setMessage("Idle.", False) + + self.kill() + + def setHost(self, host=""): self.host = host + def kill(self): self.process.KillPID() + self.pid = 0 + self.running = False + + def checkStatus(self): + if self.pid == 0: + return False + + if sys.platform == 'darwin' or sys.platform.find('linux') != -1: + connection = os.popen('netstat -an | grep 5500 | grep ESTABLISHED').readlines() + listen = os.popen('netstat -an | grep 5500 | grep LISTEN').readlines() + elif sys.platform == 'win32': + #XP PRO only -- Need to fix the case where there is no process, it'll still return 1 line. + #info = os.popen('WMIC PROCESS ' + str(self.pid) + ' get Processid').readlines() + # possibly + connection = os.popen('netstat -a | find "ESTABLISHED" | find "5500"').readlines() + listen = os.popen('netstat -a | find "LISTEN" | find "5500"').readlines() + else: + print 'Platform not detected' + connection = array() + listen = array() + if len(connection) == 0 and len(listen) == 0: + return False + else: + return True + diff --git a/gitso/trunk/Processes.py b/gitso/trunk/Processes.py index a25c86e..d98d407 100644 --- a/gitso/trunk/Processes.py +++ b/gitso/trunk/Processes.py @@ -20,6 +20,7 @@ class Processes: self.returnPID = os.spawnl(os.P_NOWAIT, '%s\\WinVNC.exe' % os.environ['WINDIR'], '%s\\WinVNC.exe' % os.environ['WINDIR'], '-connect', '%s' % host) else: print 'Platform not detected' + return self.returnPID def giveSupport(self): if sys.platform == 'darwin': @@ -31,16 +32,13 @@ class Processes: else: dlg = wx.MessageDialog(self, "We were unable to find X11.app in /Applications/Utilities", "To Give Support you need X11.app", wx.OK|wx.CENTRE|wx.ICON_ERROR) dlg.ShowModal() - #self.statusBar.SetStatusText("X11.app not found.", 1) - #self.connectButton.Enable(True) - #self.stopButton.Enable(False) elif sys.platform.find('linux') != -1: self.returnPID = os.spawnlp(os.P_NOWAIT, 'vncviewer', 'vncviewer', '-listen') elif sys.platform == 'win32': - print 'Launching %s\\vncviewer.exe' % os.environ['WINDIR'] self.returnPID = os.spawnl(os.P_NOWAIT, "%s\\vncviewer.exe" % os.environ['WINDIR'], '%s\\vncviewer.exe' % os.environ['WINDIR'], '-listen' ) else: print 'Platform not detected' + return self.returnPID def KillPID(self): """ @@ -50,6 +48,7 @@ class Processes: @author: Aaron Gerber """ if self.returnPID != 0: + print "Processes.KillPID(" + str(self.returnPID) + ")" if sys.platform == 'win32': #import win32api #handle = win32api.OpenProcess(1, 0, pid)