/// <summary> /// Retrieves command call parameters and creates wrapper for them /// </summary> /// <param name="commandStr">Command string in the form 'service/action/callback/args'</param> /// <returns>New class instance or null of string does not represent PhoneGap command</returns> public static PhoneGapCommandCall Parse(string commandStr) { if (string.IsNullOrEmpty(commandStr)) { return(null); //throw new ArgumentNullException("commandStr"); } string[] split = commandStr.Split('/'); if (split.Length < 3) { return(null); } PhoneGapCommandCall commandCallParameters = new PhoneGapCommandCall(); commandCallParameters.Service = split[0]; commandCallParameters.Action = split[1]; commandCallParameters.CallbackId = split[2]; commandCallParameters.Args = split.Length <= 3 ? String.Empty : String.Join("/", split.Skip(3)); // sanity check for illegal names // was failing with :: // PhoneGapCommandResult :: 1, Device1, {"status":1,"message":"{\"name\":\"XD..... if (commandCallParameters.Service.IndexOfAny(new char[] { '@', '.', ',', '!', ' ' }) > -1) { return(null); } return(commandCallParameters); }
/// <summary> /// Retrieves command call parameters and creates wrapper for them /// </summary> /// <param name="commandStr">Command string in the form 'service/action/callback/args'</param> /// <returns>New class instance or null of string does not represent PhoneGap command</returns> public static PhoneGapCommandCall Parse(string commandStr) { if (string.IsNullOrEmpty(commandStr)) { return null; //throw new ArgumentNullException("commandStr"); } string[] split = commandStr.Split('/'); if (split.Length < 3) { return null; } PhoneGapCommandCall commandCallParameters = new PhoneGapCommandCall(); commandCallParameters.Service = split[0]; commandCallParameters.Action = split[1]; commandCallParameters.CallbackId = split[2]; commandCallParameters.Args = split.Length <= 3 ? String.Empty : String.Join("/", split.Skip(3)); // sanity check for illegal names // was failing with :: // PhoneGapCommandResult :: 1, Device1, {"status":1,"message":"{\"name\":\"XD..... if (commandCallParameters.Service.IndexOfAny(new char[] { '@', ':', ',', '!', ' ' }) > -1) { return null; } return commandCallParameters; }
/// <summary> /// Executes command and returns result back. /// </summary> /// <param name="commandCallParams">Command to execute</param> public void ProcessCommand(PhoneGapCommandCall commandCallParams) { if (commandCallParams == null) { throw new ArgumentNullException("commandCallParams"); } BaseCommand bc = CommandFactory.CreateByServiceName(commandCallParams.Service); if (bc == null) { this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION)); return; } EventHandler <PluginResult> OnCommandResultHandler = delegate(object o, PluginResult res) { this.OnCommandResult(commandCallParams.CallbackId, res); }; bc.OnCommandResult += OnCommandResultHandler; EventHandler <ScriptCallback> OnCustomScriptHandler = delegate(object o, ScriptCallback script) { this.InvokeScriptCallback(script); }; bc.OnCustomScript += OnCustomScriptHandler; // TODO: alternative way is using thread pool (ThreadPool.QueueUserWorkItem) instead of // new thread for every command call; but num threads are not sufficient - 2 threads per CPU core Thread thread = new Thread(func => { try { bc.InvokeMethodNamed(commandCallParams.Action, commandCallParams.Args); } catch (Exception) { bc.OnCommandResult -= OnCommandResultHandler; bc.OnCustomScript -= OnCustomScriptHandler; Debug.WriteLine("failed to InvokeMethodNamed :: " + commandCallParams.Action + " on Object :: " + commandCallParams.Service); this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.INVALID_ACTION)); return; } }); thread.Start(); }
/// <summary> /// Executes command and returns result back. /// </summary> /// <param name="commandCallParams">Command to execute</param> public void ProcessCommand(PhoneGapCommandCall commandCallParams) { if (commandCallParams == null) { throw new ArgumentNullException("commandCallParams"); } BaseCommand bc = CommandFactory.CreateByServiceName(commandCallParams.Service); if (bc == null) { this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION)); return; } EventHandler<PluginResult> OnCommandResultHandler = delegate(object o, PluginResult res) { this.OnCommandResult(commandCallParams.CallbackId, res); }; bc.OnCommandResult += OnCommandResultHandler; EventHandler<ScriptCallback> OnCustomScriptHandler = delegate(object o, ScriptCallback script) { this.InvokeScriptCallback(script); }; bc.OnCustomScript += OnCustomScriptHandler; // TODO: alternative way is using thread pool (ThreadPool.QueueUserWorkItem) instead of // new thread for every command call; but num threads are not sufficient - 2 threads per CPU core Thread thread = new Thread(func => { try { bc.InvokeMethodNamed(commandCallParams.Action, commandCallParams.Args); } catch (Exception) { bc.OnCommandResult -= OnCommandResultHandler; bc.OnCustomScript -= OnCustomScriptHandler; Debug.WriteLine("failed to InvokeMethodNamed :: " + commandCallParams.Action + " on Object :: " + commandCallParams.Service); this.OnCommandResult(commandCallParams.CallbackId, new PluginResult(PluginResult.Status.INVALID_ACTION)); return; } }); thread.Start(); }
/// <summary> /// Retrieves command call parameters and creates wrapper for them /// </summary> /// <param name="commandStr">Command string in the form 'service/action/callback/args'</param> /// <returns>New class instance or null of string does not represent PhoneGap command</returns> public static PhoneGapCommandCall Parse(string commandStr) { if (string.IsNullOrEmpty(commandStr)) { throw new ArgumentNullException("commandStr"); } string[] split = commandStr.Split('/'); if (split.Length < 3) { return null; } PhoneGapCommandCall commandCallParameters = new PhoneGapCommandCall(); commandCallParameters.Service = split[0]; commandCallParameters.Action = split[1]; commandCallParameters.CallbackId = split[2]; commandCallParameters.Args = split.Length <= 3 ? String.Empty : String.Join("/", split.Skip(3)); return commandCallParameters; }