コード例 #1
0
        static void SendProgramResponse(string id, HttpListenerResponse response)
        {
            response.StatusCode = 200;

            ProgramResult res = null;

            lock (results)
            {
                res = results[id];
            }

            byte[] bytes = Encoding.UTF8.GetBytes(jss.Serialize(res));
            response.OutputStream.Write(bytes, 0, bytes.Length);
        }
コード例 #2
0
        public ProgramResult RunProgram(string programName, string args, string workingDir, IDictionary <string, string> env)
        {
            string progId = agent.StartProgram(programName, workingDir, args, env);

            while (true)
            {
                Bizarrefish.VMAgent.ProgramResult result = agent.GetProgramResult(progId);
                if (result.Complete == true)
                {
                    return(new ProgramResult()
                    {
                        ExitCode = result.ExitCode,
                        StandardOutput = result.StandardOutput,
                        StandardError = result.StandardError
                    });
                }
                // Wait a sec
                Thread.Sleep(1000);
            }
        }
コード例 #3
0
        static void StartProgramHandler(HttpListenerRequest request, HttpListenerResponse response)
        {
            string programPath = "";
            string args = "";
            string workingDir = null;
            var env = new List<Tuple<string, string>>();

            var query = request.QueryString;
            foreach(var itemName in query.AllKeys)
            {
                var itemValue = query[itemName];
                if(itemName == "target")
                    programPath = itemValue;
                else if(itemName == "workingDir")
                    workingDir = itemValue;
                else if(itemName == "args")
                    args = itemValue;

                else if(itemName.StartsWith("env."))
                    env.Add(Tuple.Create (itemName.Replace ("env.",""), itemValue));
            }

            Log ("StartProgram: " + programPath);
            Log ("	Args: " + args);
            Log ("	Working Directory: " + workingDir);

            ProcessStartInfo ps = new ProcessStartInfo(programPath, args);

            foreach(var e in env)
            {
                Log ("	Environment: " + e.Item1 + " = " + e.Item2);
                ps.EnvironmentVariables[e.Item1] = e.Item2;
            }

            if(workingDir == null)
                workingDir = Path.GetDirectoryName(programPath);

            ps.WorkingDirectory = workingDir;
            ps.RedirectStandardError = ps.RedirectStandardOutput = true;
            ps.UseShellExecute = false;

            Process p = new Process();
            p.StartInfo = ps;
            p.EnableRaisingEvents = true;
            p.Start ();
            string id = p.Id.ToString();

            Log ("	Id: " + id);

            ProgramResult result = new ProgramResult()
            {
                Complete = false,
                ProgramId = id
            };

            results[id] = result;

            p.Exited += (sender, e) =>
            {
                lock(results)
                {
                    Log ("Program Exited: " + programPath + " (" + p.ExitCode + ")");
                    result.StandardOutput = p.StandardOutput.ReadToEnd();
                    result.StandardError = p.StandardError.ReadToEnd ();
                    result.ExitCode = p.ExitCode;
                    result.Complete = true;
                }
            };

            Thread.Sleep(QuickieSleepTime);

            SendProgramResponse(id, response);
        }
コード例 #4
0
        static void StartProgramHandler(HttpListenerRequest request, HttpListenerResponse response)
        {
            string programPath = "";
            string args        = "";
            string workingDir  = null;
            var    env         = new List <Tuple <string, string> >();

            var query = request.QueryString;

            foreach (var itemName in query.AllKeys)
            {
                var itemValue = query[itemName];
                if (itemName == "target")
                {
                    programPath = itemValue;
                }
                else if (itemName == "workingDir")
                {
                    workingDir = itemValue;
                }
                else if (itemName == "args")
                {
                    args = itemValue;
                }

                else if (itemName.StartsWith("env."))
                {
                    env.Add(Tuple.Create(itemName.Replace("env.", ""), itemValue));
                }
            }

            Log("StartProgram: " + programPath);
            Log("	Args: "+ args);
            Log("	Working Directory: "+ workingDir);

            ProcessStartInfo ps = new ProcessStartInfo(programPath, args);

            foreach (var e in env)
            {
                Log("	Environment: "+ e.Item1 + " = " + e.Item2);
                ps.EnvironmentVariables[e.Item1] = e.Item2;
            }

            if (workingDir == null)
            {
                workingDir = Path.GetDirectoryName(programPath);
            }

            ps.WorkingDirectory      = workingDir;
            ps.RedirectStandardError = ps.RedirectStandardOutput = true;
            ps.UseShellExecute       = false;

            Process p = new Process();

            p.StartInfo           = ps;
            p.EnableRaisingEvents = true;
            p.Start();
            string id = p.Id.ToString();

            Log("	Id: "+ id);

            ProgramResult result = new ProgramResult()
            {
                Complete  = false,
                ProgramId = id
            };

            results[id] = result;

            p.Exited += (sender, e) =>
            {
                lock (results)
                {
                    Log("Program Exited: " + programPath + " (" + p.ExitCode + ")");
                    result.StandardOutput = p.StandardOutput.ReadToEnd();
                    result.StandardError  = p.StandardError.ReadToEnd();
                    result.ExitCode       = p.ExitCode;
                    result.Complete       = true;
                }
            };

            Thread.Sleep(QuickieSleepTime);

            SendProgramResponse(id, response);
        }