Beispiel #1
0
        public void Run(string arguments, Action<string> onLine)
        {
            var process = new Process();
            _isRunning = true;
            var stdinForwarder = new Thread(() => {
                try {
                    while (_isRunning) {
                        var line = Console.ReadLine();
                        process.Write(line);
                    }
                } catch {
                }
            });
            stdinForwarder.Start();

            Logger.Write("Running script {0} with {1}", _file, arguments);
            arguments = "{global-profile} {local-profile} " + arguments;
            run(
                arguments,
                (m) => {
                    var requestRunner = new RequestRunner(_token);
                    if (requestRunner.IsRequest(m)) {
                        ThreadPool.QueueUserWorkItem((msg) => {
                            var response = requestRunner.Request(msg.ToString());
                            foreach (var content in response)
                                process.Write(content);
                        },
                        m);
                    } else {
                        onLine(m);
                    }
                },
                new[] {
                        new KeyValuePair<string,string>("{global-profile}", "\"" + _globalProfileName + "\""),
                        new KeyValuePair<string,string>("{local-profile}", "\"" + _localProfileName + "\"")
                },
                process);
            _isRunning = false;
            stdinForwarder.Abort();
            Logger.Write("Running script completed {0}", _file);
        }
Beispiel #2
0
        private void run(string arguments, Action<string> onLine,
						 IEnumerable<KeyValuePair<string,string>> replacements)
        {
            var cmd = _file;
            var finalReplacements = new List<KeyValuePair<string,string>>();
            finalReplacements.Add(new KeyValuePair<string,string>("{run-location}", "\"" + _workingDirectory + "\""));
            finalReplacements.AddRange(replacements);
            arguments = "{run-location} " + arguments;
            var proc = new Process();
            _writer = (msg) => {
                try {
                    Logger.Write("Writing to the process " + msg);
                    proc.Write(msg);
                } catch (Exception ex) {
                    Logger.Write(ex);
                }
            };
            var realArguments = arguments;
            proc
                .Query(
                    cmd,
                    arguments,
                    false,
                    _token,
                    (error, line) => {
                            if (error && !line.StartsWith("error|")) {
                                onLine("error|" + line);
                                Logger.Write(line);
                            } else
                                onLine(line);
                        },
                    finalReplacements.ToArray(),
                    (args) => realArguments = args);
            onLine(string.Format("event|builtin command ran \"{0}\" {1}", Name, realArguments));
            _writer = (msg) => {};
        }
Beispiel #3
0
 private bool ask(Process proc, string msg)
 {
     try {
         proc.Write(msg);
         return true;
     } catch {
         return false;
     }
 }
 private void startService()
 {
     _service = new Process();
     try
     {
         var responseDispatcher = new ResponseDispatcher(
             _keyPath,
             _dispatchErrors,
             "rscript-" + Name + " ",
             _outputDispatcher,
             internalDispatch,
             (response) => _service.Write(response)
         );
         _service.SetLogger((logMsg) => Logger.Write(logMsg));
         _service.Query(
             _file,
             "{global-profile} {local-profile}",
             false,
             _keyPath,
             (error, m) => {
                 if (m == null)
                     return;
                 Logger.Write("request doing " + m);
                 if (error) {
                     Logger.Write("rscript-" + Name + " produced an error:");
                     Logger.Write("rscript-" + Name + "-" + m);
                 }
                 responseDispatcher.Handle(error, m);
             },
             new[] {
                 new KeyValuePair<string,string>("{global-profile}", "\"" + _globalProfileName + "\""),
                 new KeyValuePair<string,string>("{local-profile}", "\"" + _localProfileName + "\"")
             });
         Logger.Write("Exiting service script");
     }
     catch (Exception ex)
     {
         internalDispatch("rscript-" + Name + " " + ex.ToString());
         Logger.Write(ex.ToString());
     }
 }
 private void runScript(string message)
 {
     if (_isFaulted)
         return;
     var originalMessage = message;
     message = "{event} {global-profile} {local-profile}";
     Logger.Write("Running: " + _file + " " + message);
     ThreadPool.QueueUserWorkItem((task) => {
         try
         {
             var process = new Process();
             var responseDispatcher = new ResponseDispatcher(
                 _keyPath,
                 _dispatchErrors,
                 "rscript-" + Name + " ",
                 _outputDispatcher,
                 internalDispatch,
                 (response) => process.Write(response)
             );
             process.SetLogger((logMsg) => Logger.Write(logMsg));
             var msg = task.ToString();
             process.Query(
                 _file,
                 msg,
                 false,
                 _keyPath,
                 (error, m) => {
                     if (m == null)
                         return;
                     Logger.Write("request doing " + m);
                     if (error) {
                         Logger.Write("rscript-" + Name + " produced an error:");
                         Logger.Write("rscript-" + Name + "-" + m);
                     }
                     responseDispatcher.Handle(error, m);
                 },
                 new[] {
                     new KeyValuePair<string,string>("{event}", "\"" + originalMessage + "\""),
                     new KeyValuePair<string,string>("{global-profile}", "\"" + _globalProfileName + "\""),
                     new KeyValuePair<string,string>("{local-profile}", "\"" + _localProfileName + "\"")
                 });
             Logger.Write("RScript completed");
         }
         catch (Exception ex)
         {
             internalDispatch("rscript-" + Name + " " + ex.ToString());
             Logger.Write(ex.ToString());
         }
     }, message);
 }