Beispiel #1
0
        protected override void DoAction()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(IPAddress.Loopback, SubJobAction.PortNumber);
            ClientStream = new SocketStream(socket);
            try
            {
                //recieve vars, if any
                var varsToReceive = CentipedeSerializer.Deserialize <int>(ClientStream);

                var received = new List <object>(varsToReceive);
                for (int i = 0; i < varsToReceive; i++)
                {
                    received.Add(CentipedeSerializer.Deserialize(ClientStream));
                }

                if (!String.IsNullOrWhiteSpace(this.InputVars))
                {
                    var enumerable = InputVars.Split(',').Select(s => s.Trim()).ToList();

                    if (varsToReceive != enumerable.Count)
                    {
                        throw new FatalActionException(
                                  string.Format("Wrong number of variables, expected {0}, got {1}",
                                                enumerable.Count,
                                                varsToReceive),
                                  this);
                    }

                    foreach (var tuple in enumerable.Zip(received, Tuple.Create))
                    {
                        Variables[tuple.Item1] = tuple.Item2;
                    }
                }
                else
                {
                    if (varsToReceive != 0)
                    {
                        throw new ActionException(
                                  string.Format(
                                      "Recieved {0} {1} from parent job, was not expecting any.",
                                      varsToReceive,
                                      "variable".Pluralize(varsToReceive)),
                                  this);
                    }
                }
            }
            catch (ObjectDisposedException e)
            {
                throw new FatalActionException("Getting variables from parent job failed.", e, this);
            }
        }
Beispiel #2
0
        protected override void DoAction()
        {
            MemoryStream stream = new MemoryStream();

            StreamWriter sw = new StreamWriter(stream);

            sw.Write(Variables[InVar]);
            sw.Flush();

            stream.Seek(0, SeekOrigin.Begin);

            Variables[OutVar] = CentipedeSerializer.Deserialize(stream);
        }
Beispiel #3
0
        protected override void DoAction()
        {
            this._process = new Process
            {
                StartInfo =
                {
                    FileName        = this.JobFileName,

                    Verb            = "Run",
                    UseShellExecute = true
                }
            };

            this._process.Start();

            this._serverStream = new Socket(AddressFamily.InterNetwork,
                                            SocketType.Stream,
                                            ProtocolType.IP);
            this._serverStream.Bind(new IPEndPoint(IPAddress.Loopback, PortNumber));

            this._bgw = new BackgroundWorker();
            this._bgw.WorkerSupportsCancellation = true;
            this._bgw.DoWork += BgwOnDoWork;

            this._bgw.RunWorkerAsync();


            Socket connection = null;

            try
            {
                this._serverStream.Listen(0);

                connection = this._serverStream.Accept();

                var ss = new SocketStream(connection);


                if (!String.IsNullOrWhiteSpace(this.InputVars))
                {
                    List <string> variables =
                        this.InputVars.Split(',').Select(s => s.Trim()).ToList();

                    //send number of variables
                    CentipedeSerializer.Serialize(ss, variables.Count);

                    try
                    {
                        foreach (var variable in variables)
                        {
                            object o = this.Variables[variable];
                            try
                            {
                                CentipedeSerializer.Serialize(ss, o);
                            }
                            catch (SerializationException e)
                            {
                                throw new FatalActionException(
                                          string.Format(
                                              "Cannot send variable {0} to subjob, type {1} is not supported.",
                                              variable,
                                              o.GetType()),
                                          e,
                                          this);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new ActionException(e, this);
                    }
                }
                else
                {
                    //no variables to send
                    CentipedeSerializer.Serialize(ss, 0);
                }

                var subJobSuccess = (bool)CentipedeSerializer.Deserialize(ss);

                if (!subJobSuccess)
                {
                    throw new ActionException("Subjob was not completed", this);
                }


                //recieve vars, if any
                var varsToRecieve = CentipedeSerializer.Deserialize <int>(ss);

                var received = new List <object>(varsToRecieve);

                for (int i = 0; i < varsToRecieve; i++)
                {
                    received.Add(CentipedeSerializer.Deserialize(ss));
                }

                if (!String.IsNullOrWhiteSpace(this.OutputVars))
                {
                    List <string> outputVars =
                        this.OutputVars.Split(',').Select(s => s.Trim()).ToList();

                    if (varsToRecieve != outputVars.Count)
                    {
                        throw new FatalActionException(
                                  string.Format("Wrong number of variables, expected {0}, got {1}",
                                                outputVars.Count,
                                                varsToRecieve));
                    }

                    foreach (var tuple in outputVars.Zip(received, Tuple.Create))
                    {
                        this.Variables[tuple.Item1] = tuple.Item2;
                    }
                }
                else
                {
                    if (varsToRecieve != 0)
                    {
                        throw new ActionException(
                                  string.Format("Recieved {0} {1} from subjob, was not expecting any.",
                                                varsToRecieve,
                                                "variable".Pluralize(varsToRecieve)));
                    }
                }
            }
            finally
            {
                try
                {
                    this._serverStream.Close();
                    if (connection != null)
                    {
                        connection.Close();
                    }
                    this._messagePipe.Close();
                    if (!this._process.HasExited)
                    {
                        this._process.CloseMainWindow();
                        this._process.Close();
                        if (!this._process.WaitForExit(10000))
                        {
                            this._process.Kill();
                            this._process = null;
                        }
                    }
                }
                catch (Exception e)
                {
                    this.OnMessage(new MessageEventArgs
                    {
                        Message = e.Message,
                        Level   = MessageLevel.Debug
                    });
                }
            }
        }