public bool tryLaunchHaggle() { DialogResult res = MessageBox.Show("Haggle does not seem to be running. Start Haggle now?", "Haggle Error", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); if (res == DialogResult.Yes) { int ret = HaggleHandle.SpawnDaemon(); if (ret != 0) { MessageBox.Show("Could not launch Haggle daemon, error=" + ret); return(false); } return(true); // Give Haggle some time to launch //Thread.Sleep(4000); } return(false); }
private bool connectToHaggle() { bool try_again = true; while (try_again) { try_again = false; if (HaggleHandle.DaemonPid() == 0) { if (!tryLaunchHaggle()) { return(false); } } try { hh = new HaggleHandle("PhotoShare"); interestListHandler = new HaggleEventHandler(hh, HaggleEvent.INTEREST_LIST, new HaggleCallback(this.onInterestList)); hh.RequestInterests(); updatedNeighborHandler = new HaggleEventHandler(hh, HaggleEvent.NEIGHBOR_UPDATE, new HaggleCallback(this.onNeighborUpdate)); shutdownHandler = new HaggleEventHandler(hh, HaggleEvent.SHUTDOWN, new HaggleCallback(this.onHaggleShutdown)); newDataObjectHandler = new HaggleEventHandler(hh, HaggleEvent.NEW_DATAOBJECT, new HaggleCallback(this.onNewDataObject)); Utility.Vibration vib = new Utility.Vibration(); Debug.WriteLine("PhotoShare successfully connected to Haggle\n"); return(true); } catch (Haggle.HaggleHandle.IPCException ex) { if (ex.GetError() == HaggleHandle.HAGGLE_BUSY_ERROR) { // FIXME: Should really check if there is another photoshare application running... HaggleHandle.Unregister("PhotoShare"); Thread.Sleep(2000); try_again = true; } else { Debug.WriteLine("Haggle Error: " + ex.ToString() + " errnum=" + ex.GetError()); uint pid = HaggleHandle.DaemonPid(); if (pid > 0) { MessageBox.Show("A Haggle PID file exists, but could still not connect to Haggle"); } else if (pid == 0) { if (!tryLaunchHaggle()) { HaggleHandle.Unregister("PhotoShare"); return(false); } } else { DialogResult res = MessageBox.Show("Could not connect to Haggle.", "Haggle Error", MessageBoxButtons.OK, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); return(false); } } } catch (Haggle.HaggleEventHandler.EventHandlerException ex) { MessageBox.Show(ex.ToString() + " Error=" + ex.GetError()); throw ex; } } Application.Exit(); return(false); }