Пример #1
0
        public void SendToServer(PipeCommand command, int TimeOut = 5000)
        {
            try
            {
                if (_service.GetStatus() == "stopped")
                {
                    Console.WriteLine("start service first");
                    return;
                }

                var serializedCommand            = JsonConvert.SerializeObject(command);
                NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", _pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);

                pipeStream.Connect(TimeOut);
                StreamString ss = new StreamString(pipeStream);

                ss.SendCommand(serializedCommand);
                var answer = ss.ReadAnswer();

                Console.WriteLine(answer);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + "[" + ex.Source + "] " + ex.Message);
            }
        }
Пример #2
0
        private static void GetShareFolder(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];
            _connector.SendToServer(command);
        }
        private void BtInstall_Click(object sender, RoutedEventArgs e)
        {
            BtInstall.IsEnabled = false;
            _isReschedule       = false;

            if (_app.IsIpuApplication)
            {
                try
                {
                    var pipeCmd = new PipeCommand {
                        Action = "InstallIpuApplication", AppId = _app.Id, AppRevision = _app.Revision
                    };
                    var jsonCmd = JsonConvert.SerializeObject(pipeCmd);
                    new PipeClient().Send(jsonCmd, "3A2CD127-D069-4CD5-994D-C481F4760748");
                }
                catch (Exception ex)
                {
                    Globals.Log.Error(ex.Message);
                }
            }
            else
            {
                CcmUtils.InstallApplication(_app);
            }
        }
Пример #4
0
        private string SetLicenseCode(PipeCommand command)
        {
            int codeBuffer = _settings.LicenseCode;

            try
            {
                _settings.LicenseCode = Int16.Parse(command.args["licensecode"]);
            }
            catch (Exception ex)
            {
                _settings.LicenseCode = codeBuffer;
                return("failed: " + ex.Message);
            }
            _readerWriter.Write();
            if (_reconnectProvider.Reconnect())
            {
                _readerWriter.Write();
                _logger.InfoFormat("license code successfully changed");
                return("license code successfully changed");
            }
            else
            {
                _logger.InfoFormat("license code changed");
                return("license code changed, but connection failed: " + _reconnectProvider.ExceptionMessage);
            }
        }
Пример #5
0
        private Task sendCommandAsync(PipeCommand pipeCommand)
        {
            string jsonSerializedPipeCommand = JsonConvert.SerializeObject(pipeCommand, serializerSettings);

            byte[] serializedPipeCommand = Encoding.UTF8.GetBytes(jsonSerializedPipeCommand);
            return(pipe.WriteAsync(serializedPipeCommand, 0, serializedPipeCommand.Length));
        }
Пример #6
0
        private static void PrintVersion(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];
            _connector.SendToServer(command);
        }
Пример #7
0
        private static void Connect(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];

            if (args.Length > 1)
            {
                string databaseUrl = args[1], login, password;

                Console.Write("login: "******"password: "******"url"]      = databaseUrl;
                command.args["login"]    = login;
                command.args["password"] = password;
            }
            else
            {
                Console.WriteLine("specify database url in the command");
                return;
            }
            Console.WriteLine("connecting to " + command.args["url"]);
            _connector.SendToServer(command);
        }
Пример #8
0
        private static void OnDisconnect(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];
            Console.WriteLine("disconnecting...");
            _connector.SendToServer(command);
        }
Пример #9
0
        public PipeMessage(PipeCommand cmd, PipeFunction fun, int sockid, byte[] data, int extra = 0)
        {
            Header = new PipeMessageHeader {
                Command  = cmd,
                Function = fun,
                SocketId = sockid,
                DataSize = data?.Length ?? 0,
                Extra    = extra,
            };

            Data = data;
        }
Пример #10
0
        private static void SetSharedFolder(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];
            if (args.Length > 1)
            {
                command.args["path"] = args[1];
            }
            else
            {
                Console.WriteLine("specify path in the command");
                return;
            }
            _connector.SendToServer(command);
        }
Пример #11
0
        private static void SetLicenseCode(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];
            if (args.Length > 1)
            {
                command.args["licensecode"] = args[1];
            }
            else
            {
                Console.WriteLine("specify license code in the command");
                return;
            }
            _connector.SendToServer(command);
        }
Пример #12
0
 private string SetDelay(PipeCommand command)
 {
     try
     {
         _settings.Timeout = Double.Parse(command.args["delay"]);
         CreateNewListener();
         _readerWriter.Write();
         _logger.InfoFormat("delay successfully changed");
         return("delay successfully changed");
     }
     catch (FormatException ex)
     {
         _logger.InfoFormat("failed: " + ex.Message);
         return("failed: " + ex.Message);
     }
 }
Пример #13
0
        private static void SetDelay(string[] args)
        {
            PipeCommand command = new PipeCommand();

            command.commandName = args[0];
            if (args.Length > 1)
            {
                command.args["delay"] = args[1];
            }
            else
            {
                Console.WriteLine("specify time out in the command");
                return;
            }

            _connector.SendToServer(command);
        }
Пример #14
0
        private bool runnerLifetime()
        {
            bool succeed = false;

            using (BinaryReader br = new BinaryReader(pipeServer))
                using (BinaryWriter bw = new BinaryWriter(pipeServer))
                {
                    /* handshake */
                    PipeCommand helloCommand = PipeUtil.ReadCommand(br);
                    if (helloCommand.Command != "Hello")
                    {
                        throw new Exception("Handshake failed.");
                    }
                    logger.Info("Pipe: Handshake signal received.");
                    PipeUtil.WriteCommand(bw, new PipeCommand("Hello"));
                    logger.Info("Pipe: Handshake signal sent.");

                    /* transmit composition */
                    transmitComposition(bw);
                    PipeUtil.WriteCommand(bw, new PipeCommand("RunSimulation"));

                    /* waiting for simulation finish */
                    do
                    {
                        PipeCommand command = PipeUtil.ReadCommand(br);
                        logger.Info("Pipe: Received command: " + command.Command);
                        switch (command.Command)
                        {
                        case "Progress":
                            TaskProgressChangedHandler(this, command.Parameters);
                            break;

                        case "Completed":
                        case "Failed":
                            PipeUtil.WriteCommand(bw, new PipeCommand("Halt"));
                            succeed    = (command.Command == "Completed");
                            isReleased = true;
                            break;
                        }
                    } while (!isReleased);
                }
            runnerProcess.WaitForExit();
            return(succeed);
        }
Пример #15
0
        private string Connect(PipeCommand command)
        {
            if (_settings.SharePath == "")
            {
                return("Revit shared folder path is not set");
            }

            if (_reconnectProvider.Reconnect(command))
            {
                if (_revitShareListener == null)
                {
                    CreateNewListener();
                }
                _readerWriter.Write();
                return("connection is successful");
            }
            else
            {
                _logger.InfoFormat("connection failed: " + _reconnectProvider.ExceptionMessage);
                return("connection failed: " + _reconnectProvider.ExceptionMessage);
            }
        }
        public bool Reconnect(PipeCommand command)
        {
            object[] dataBuffer = new object[] { _settings.ServerUrl, _settings.DbName, _settings.Login, _settings.Password };
            string[] subs       = SplitUrl(command.args["url"]);
            if (subs == null)
            {
                subs = new string[] { command.args["url"], "" }; //if database name wasn't typed
            }

            _settings.ServerUrl = subs[0];
            _settings.DbName    = subs[1];
            _settings.Login     = command.args["login"];
            _settings.Password  = command.args["password"].EncryptAes();

            if (!Reconnect())
            {
                _settings.ServerUrl = (string)dataBuffer[0];
                _settings.DbName    = (string)dataBuffer[1];
                _settings.Login     = (string)dataBuffer[2];
                _settings.Password  = (string)dataBuffer[3];
                return(false);
            }
            return(true);
        }
Пример #17
0
        public void Run()
        {
            DateTime lastReport;

            logger.Info("TaskRunner is initializing...");
            pipeClient = new NamedPipeClientStream(".", PipeName,
                                                   PipeDirection.InOut, PipeOptions.WriteThrough,
                                                   TokenImpersonationLevel.Impersonation);
            pipeClient.Connect();
            logger.Info("Pipe connected.");
            bool isReleased = false;

            try
            {
                using (BinaryReader br = new BinaryReader(pipeClient))
                    using (BinaryWriter bw = new BinaryWriter(pipeClient))
                    {
                        /* handshake */
                        PipeUtil.WriteCommand(bw, new PipeCommand("Hello"));
                        logger.Info("Pipe: Handshake signal sent.");
                        PipeCommand helloCommand = PipeUtil.ReadCommand(br);
                        if (helloCommand.Command != "Hello")
                        {
                            throw new Exception("Handshake failed.");
                        }
                        logger.Info("Pipe: Handshake signal received.");
                        composition = new CompositionManager();
                        do
                        {
                            string      modelId;
                            PipeCommand command = PipeUtil.ReadCommand(br);
                            logger.Info("Pipe: Received command: " + command.Command);
                            switch (command.Command)
                            {
                            case "AddModel":
                                modelId = command.Parameters["modelId"];
                                string workingDirectory  = command.Parameters["workingDirectory"];
                                string assemblyPath      = command.Parameters["assemblyPath"];
                                string linkableComponent = command.Parameters["linkableComponent"];
                                Model  model             = new Model();
                                model.Create(modelId, workingDirectory, assemblyPath, linkableComponent);
                                composition.AddModel(model);
                                logger.Info("Created model " + modelId + ": " + linkableComponent);
                                break;

                            case "SetModelProperties":
                                modelId = command.Parameters["modelId"];
                                foreach (KeyValuePair <string, string> entry in command.Parameters)
                                {
                                    logger.Info("Model " + modelId + " Property: [Key=" + entry.Key + ", Value=" + entry.Value + "]");
                                }
                                composition.GetModel(modelId).Init(command.Parameters);
                                break;

                            case "AddLink":
                                string linkId           = command.Parameters["linkId"];
                                string sourceModelId    = command.Parameters["sourceModelId"];
                                string targetModelId    = command.Parameters["targetModelId"];
                                string sourceQuantity   = command.Parameters["sourceQuantity"];
                                string targetQuantity   = command.Parameters["targetQuantity"];
                                string sourceElementSet = command.Parameters["sourceElementSet"];
                                string targetElementSet = command.Parameters["targetElementSet"];
                                logger.Info("Link " + linkId + " Property: [sourceModelId=" + sourceModelId + ", targetModelId=" + targetModelId + ", sourceQuantity=" + sourceQuantity + ", targetQuantity=" + targetQuantity + ", sourceElementSet=" + sourceElementSet + ", targetElementSet=" + targetElementSet + "]");
                                composition.AddLink(linkId, sourceModelId, targetModelId, sourceQuantity, targetQuantity, sourceElementSet, targetElementSet);
                                break;

                            case "SetSimulationProperties":
                                DateTime triggerInvokeTime = DateTime.Parse(command.Parameters["triggerInvokeTime"]);
                                bool     parallelized      = Boolean.Parse(command.Parameters["parallelized"]);
                                progressReportInterval        = Int32.Parse(command.Parameters["progressReportInterval"]);
                                composition.TriggerInvokeTime = triggerInvokeTime;
                                composition.Parallelized      = parallelized;
                                break;

                            case "RunSimulation":
                                lastReport    = DateTime.Now;
                                modelProgress = new Dictionary <string, string>();
                                composition.CompositionModelProgressChangedHandler += new CompositionModelProgressChangedDelegate(delegate(object sender, string cmGuid, string progress)
                                {
                                    lock (this)
                                    {
                                        modelProgress[cmGuid] = progress;
                                        if ((DateTime.Now - lastReport).TotalSeconds > progressReportInterval)
                                        {
                                            lastReport = DateTime.Now;
                                            PipeUtil.WriteCommand(bw, new PipeCommand("Progress", modelProgress));
                                        }
                                    }
                                });
                                RunSimulation(delegate(object sender, bool succeed)
                                {
                                    logger.Info("Simulation finished, succeed=" + succeed);
                                    PipeUtil.WriteCommand(bw, new PipeCommand("Progress", modelProgress));
                                    PipeUtil.WriteCommand(bw, new PipeCommand(succeed ? "Completed" : "Failed"));
                                });
                                break;

                            case "Halt":
                                isReleased = true;
                                return;
                            }
                        } while (true);
                    }
            }
            catch (Exception e)
            {
                if (!isReleased)
                {
                    logger.Crit("Exception occured during task lifetime: " + e.ToString());
                }
            }
            finally
            {
                pipeClient.Close();
                pipeClient.Dispose();
            }
        }
Пример #18
0
 public PipeMessage(PipeCommand cmd, PipeFunction fun = 0, byte[] data = null) : this(cmd, fun, 0, data, 0)
 {
 }