/// <summary> /// Create a new workspace having the credentials specified and insert /// it in the workspace manager, with the current task Stop. /// </summary> public static Workspace CreateWorkspace(KwsCredentials creds) { try { // Clear the public flag if we already have a public workspace. if (Cd.PublicKwsID != 0) { creds.PublicFlag = false; } // Get a new internal ID. UInt64 internalID = Cd.NextKwsInternalID++; // Register the worskpace in the workspace manager. KwsCoreData kwsCd = new KwsCoreData(); kwsCd.Credentials = creds; Workspace kws = new Workspace(); kws.Relink(internalID, kwsCd); AdjustPublicKwsID(); // Insert the workspace in the workpace list in the database. LocalDbBroker.AddKwsToKwsList(kws.InternalID, kws.Cd.Credentials.KwsName); // The WM state has changed. Wm.OnStateChange(WmStateChange.Permanent); return(kws); } catch (Exception ex) { KBase.HandleException(ex, true); return(null); } }
/// <summary> /// Helper method to start a session. /// </summary> private byte[] StartSession(bool serverSessionFlag, bool supportFlag, int windowHandle, String subject, UInt64 sessionID) { // Throw if we cannot open the session. if (AppVnc.SessionPresentFlag) { throw new Exception("a screen sharing session is already running"); } if (!IsOnlineCapable()) { throw new Exception("the " + KwmStrings.Kws + " is not connected to the server"); } // Remember that we have started a session. SessionPresentFlag = true; // Create the local session object. LocalSession = new VncLocalSession(this); LocalSession.ServerSessionFlag = serverSessionFlag; LocalSession.SupportSessionFlag = supportFlag; LocalSession.WindowHandle = windowHandle; LocalSession.Subject = subject; LocalSession.SessionID = sessionID; // Asynchronously start the session. KBase.ExecInUI(LocalSession.HandleNextSessionStep); Kws.OnStateChange(WmStateChange.Transient); return(LocalSession.SessionUuid); }
/// <summary> /// Start the session, if possible. /// </summary> private void HandleSessionStart() { // Check if the KCD has already sent us the end session event. if (SessionIDTree.ContainsKey(SessionID)) { HandleSessionTrouble(new Exception("the KCD closed the connection unexpectedly")); return; } // Clear the session ID tree. SessionIDTree.Clear(); // Show the overlay window. Overlay = new RunningOverlay(); Overlay.Relink(this); // Configure the inactivity monitor to timeout about 10 minutes. InactivityMonitor = MonitorCreator.CreateInstance(MonitorType.GlobalHookMonitor); InactivityMonitor.MonitorKeyboardEvents = true; InactivityMonitor.MonitorMouseEvents = true; InactivityMonitor.Interval = 600000; InactivityMonitor.Elapsed += delegate(Object sender, ElapsedEventArgs args) { KBase.ExecInUI(OnInactivity); }; InactivityMonitor.Enabled = true; // Start the session. Status = VncSessionStatus.Started; // Notify listeners. PostLocalVncSessionEvent(true, null); Kws.OnStateChange(WmStateChange.Transient); }
/// <summary> /// Deliver notifications if delivery is allowed. /// </summary> private static void TriggerNotif() { while (m_notifLockCount == 0 && m_notifQueue.Count > 0) { // While we are firing, we need to lock notifications so that // we don't end up with two executions of this method. m_notifLockCount++; KwsSmNotif n = m_notifQueue.Dequeue(); Workspace kws = n.Kws; try { if (kws.OnKwsSmNotif != null) { kws.OnKwsSmNotif(kws, n); } } catch (Exception ex) { // We cannot handle failures in notifications. KBase.HandleException(ex, true); } m_notifLockCount--; } }
/// <summary> /// Request the state machine to run, if required. /// </summary> private void RequestRun() { if (WantToRunNow() && m_wakeUpMsg == null) { m_wakeUpMsg = new KmodBrokerWakeUpMsg(this); KBase.ExecInUI(new KBase.EmptyDelegate(m_wakeUpMsg.Run)); } }
/// <summary> /// Notify the WM that something occurred. Assume mutex is locked. /// </summary> private void NotifyWm() { if (m_wmWakeUpMsg == null) { m_wmWakeUpMsg = new KcdWmWakeUpMsg(this); KBase.ExecInUI(new KBase.EmptyDelegate(m_wmWakeUpMsg.Run)); } }
/// <summary> /// Called when the KCD thread completes. /// </summary> public void OnKcdThreadCompletion(Exception ex) { m_thread = null; if (ex != null) { KBase.HandleException(ex, true); } DoOnEvent(); }
/// <summary> /// Delete specified object in the serialization table. /// </summary> public void DeleteSerializedObject(String name) { try { m_db.ExecNQ("DELETE FROM serialization WHERE name = '" + name + "'"); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Remove the EAnp events associated to the workspace specified. /// </summary> public void RemoveEAnpEvents(UInt64 kwsID) { try { m_db.ExecNQ("DELETE FROM eanp_events WHERE kws_id = " + kwsID); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Import the workspace list specified. /// </summary> public static void ImportKwsList(String path) { try { WmKwsImportExport.ImportKwsListFromFile(path); } catch (Exception ex) { KBase.HandleException(ex); } }
/// <summary> /// Call the callback function. /// </summary> private void CallCallback() { try { Callback(this); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Remove the specified workspace from the list of workspaces. /// </summary> public void RemoveKwsFromKwsList(UInt64 id) { try { m_db.ExecNQ("DELETE FROM kws_list WHERE kws_id = " + id); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Return true if the serialized object specified exists. /// </summary> public bool HasSerializedObject(String name) { try { return(m_db.GetCmd("SELECT name FROM serialization WHERE name = '" + name + "'").ExecuteScalar() != null); } catch (Exception ex) { KBase.HandleException(ex, true); return(false); } }
/// <summary> /// This method is executed when the application has started and the /// message loop is running. /// </summary> private static void Bootstrap() { // Determine the other process state. WmOtherProcess other = new WmOtherProcess(); other.FindOtherProcess(); // Another KWM we own is running in our session. if (other.State == WmOtherProcessState.OurInCurrentSession) { // Send the other KWM a message to import the credentials. if (ImportKwsPath != "") { SendMsgToOtherKwm(other, ImportKwsMsgID, ImportKwsPath); } // Show the instance of the other KWM. SendMsgToOtherKwm(other, ForegroundMsgID, ""); // We're done. RequestAppExit(); } // Warn the user. else if (other.State == WmOtherProcessState.NotOurInCurrentSession || other.State == WmOtherProcessState.OurInOtherSession) { String error = (other.State == WmOtherProcessState.NotOurInCurrentSession) ? "A " + KwmStrings.Kwm + " started by another user is already running." : "A " + KwmStrings.Kwm + " started by another user is already running."; WmUi.TellUser(error, "Cannot start " + KwmStrings.Kwm, MessageBoxButtons.OK, MessageBoxIcon.Error); RequestAppExit(); } // Enter the main mode. else { try { if (!EnterMainMode()) { RequestAppExit(); } } catch (Exception ex) { KBase.HandleException(ex, true); } } }
/// <summary> /// Update the status of the KAnp event specified. /// </summary> public void UpdateKAnpEventStatus(UInt64 kwsID, UInt64 msgID, KwsAnpEventStatus status) { try { String s = "UPDATE kanp_events SET status = " + (UInt32)status + " WHERE kws_id = " + kwsID + " AND evt_id = " + msgID; m_db.ExecNQ(s); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Return the serialized object having the name specified, if any. /// </summary> public byte[] GetSerializedObject(String name) { try { DbCommand cmd = m_db.GetCmd("SELECT data FROM serialization WHERE name = '" + name + "'"); return((byte[])cmd.ExecuteScalar()); } catch (Exception ex) { KBase.HandleException(ex, true); return(null); } }
/// <summary> /// Stop the KMOD thread and kill all pending and executing /// transactions. /// </summary> private void Killall(Exception ex) { // Get the list of failing transactions and clear the current data // structures. List <KmodTransaction> list = new List <KmodTransaction>(); list.AddRange(m_transactionQueue); m_transactionQueue.Clear(); if (m_curTransaction != null) { list.Add(m_curTransaction); m_curTransaction = null; } // Mark the transactions as failing. foreach (KmodTransaction transaction in list) { transaction.Status = KmodTransactionStatus.Failing; } // Stop the thread if it is running. StopKmodThread(); // Kill all transactions. foreach (KmodTransaction transaction in list) { if (transaction.Status != KmodTransactionStatus.Failing) { continue; } transaction.Status = KmodTransactionStatus.Finished; transaction.Ex = ex; if (ex != null) { KLogging.LogException(ex); } try { transaction.Run(KmodTransactionReason.Error); } catch (Exception ex2) { KBase.HandleException(ex2, true); } } }
/// <summary> /// Run the broker state machine. Only call RequestRun() to execute /// this method. /// </summary> public void Run() { Debug.Assert(m_wakeUpMsg != null); try { // Loop until our state stabilize. while (WantToRunNow()) { Debug.Assert(m_curTransaction == null); Debug.Assert(m_curCommand == null); // We're disabled. Kill all transactions. if (!m_enabledFlag) { Killall(new Exception("KMOD broker disabled")); } // Execute the next transaction. else { m_curTransaction = m_transactionQueue[0]; m_transactionQueue.RemoveAt(0); Debug.Assert(m_curTransaction.Status == KmodTransactionStatus.Queued); m_curTransaction.Status = KmodTransactionStatus.Executing; // We have to start KMOD. if (m_curThread == null) { StartKmodThread(); } // Execute the current transaction. else { StartCurrentTransaction(); } } } } // We cannot recover from these errors. catch (Exception ex) { KBase.HandleException(ex, true); } m_wakeUpMsg = null; }
/// <summary> /// Get the first unprocessed KAnp event of the workspace specified, if /// any. /// </summary> public AnpMsg GetFirstUnprocessedKAnpEvent(UInt64 kwsID) { try { String s = "SELECT evt_data FROM kanp_events WHERE kws_id = " + kwsID + " AND status = " + (UInt32)KwsAnpEventStatus.Unprocessed + " ORDER BY evt_id LIMIT 1"; return(GetEventFromQuery(s)); } catch (Exception ex) { KBase.HandleException(ex, true); return(null); } }
/// <summary> /// Get the EAnp event specified, if any. /// </summary> public AnpMsg GetEAnpEvent(UInt64 kwsID, UInt64 evtID) { try { String s = "SELECT evt_data FROM eanp_events WHERE kws_id = " + kwsID + " AND evt_id = " + evtID + ";"; return(GetEventFromQuery(s)); } catch (Exception ex) { KBase.HandleException(ex, true); return(null); } }
/// <summary> /// Add the workspace specified to the list of workspaces. /// </summary> public void AddKwsToKwsList(UInt64 id, String name) { try { DbCommand cmd = m_db.GetCmd("INSERT INTO kws_list (kws_id, name) VALUES (?, ?);"); m_db.AddParamToCmd(cmd, id); m_db.AddParamToCmd(cmd, name); cmd.ExecuteNonQuery(); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Store the EAnp event specified. /// </summary> public void StoreEAnpEvent(UInt64 kwsID, AnpMsg msg) { try { DbCommand cmd = m_db.GetCmd("INSERT INTO eanp_events (kws_id, evt_id, evt_data) VALUES (?, ?, ?);"); m_db.AddParamToCmd(cmd, kwsID); m_db.AddParamToCmd(cmd, msg.ID); m_db.AddParamToCmd(cmd, msg.ToByteArray(true)); cmd.ExecuteNonQuery(); } catch (Exception ex) { KBase.HandleException(ex, true); } }
/// <summary> /// Add/replace the specified object in the serialization table. /// </summary> public void AddSerializedObject(String name, byte[] data) { try { m_db.ExecNQ("DELETE FROM serialization WHERE name = '" + name + "'"); DbCommand cmd = m_db.GetCmd("INSERT INTO serialization (name, data) VALUES(?, ?)"); m_db.AddParamToCmd(cmd, name); m_db.AddParamToCmd(cmd, data); cmd.ExecuteNonQuery(); } catch (Exception ex) { KBase.HandleException(ex, true); } }