Exemple #1
0
        public async void StopServer()
        {
            if (ServerSupport.IsLocalIP(Model.ServerIP))
            {
                var message = new ConfirmationMessage(
                    "AmatsukazeServerを終了しますか?",
                    "AmatsukazeServer",
                    System.Windows.MessageBoxImage.Information,
                    System.Windows.MessageBoxButton.OKCancel,
                    "Confirm");

                await Messenger.RaiseAsync(message);

                if (message.Response == true)
                {
                    await Model.Server?.EndServer();
                }
            }
            else
            {
                //
            }
        }
Exemple #2
0
        public async Task Exec()
        {
            string srcpath = option.FilePath;

            byte[] hash = null;

            if (string.IsNullOrEmpty(option.NasDir) == false)
            {
                if (File.Exists(option.FilePath) == false)
                {
                    throw new Exception("入力ファイルが見つかりません");
                }

                if (option.ClearSucceeded)
                {
                    // succeededを空にする
                    var succeeded = option.NasDir + Path.DirectorySeparatorChar + "succeeded";
                    if (Directory.Exists(succeeded))
                    {
                        foreach (var file in Directory.GetFiles(succeeded))
                        {
                            File.Delete(file);
                        }
                    }
                }
                // NASにコピー
                var remotepath = option.NasDir + Path.DirectorySeparatorChar + Path.GetFileName(srcpath);
                hash = await HashUtil.CopyWithHash(option.FilePath, remotepath);

                srcpath = remotepath;
                if (option.WithRelated)
                {
                    var body   = Path.GetFileNameWithoutExtension(option.FilePath);
                    var tsext  = Path.GetExtension(option.FilePath);
                    var srcDir = Path.GetDirectoryName(option.FilePath);
                    foreach (var ext in ServerSupport.GetFileExtentions(null, true))
                    {
                        string srcPath = srcDir + Path.DirectorySeparatorChar + body + ext;
                        string dstPath = option.NasDir + Path.DirectorySeparatorChar + body + ext;
                        if (File.Exists(srcPath))
                        {
                            File.Copy(srcPath, dstPath);
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(option.RemoteDir) == false)
            {
                // これはサーバからのアクセス用パスなので"\\"で区切る
                srcpath = option.RemoteDir + "\\" + Path.GetFileName(srcpath);
            }

            Console.WriteLine(srcpath + " を追加します");

            // リクエストを生成
            request = new AddQueueRequest()
            {
                DirPath = Path.GetDirectoryName(srcpath),
                Outputs = new List <OutputInfo>()
                {
                    new OutputInfo()
                    {
                        DstPath  = option.OutPath,
                        Profile  = option.Profile,
                        Priority = option.Priority,
                    }
                },
                Targets = new List <AddQueueItem>()
                {
                    new AddQueueItem()
                    {
                        Path = srcpath, Hash = hash
                    }
                },
                Mode        = ProcMode.AutoBatch,
                RequestId   = UniqueId(),
                AddQueueBat = option.AddQueueBat
            };

            server = new CUIServerConnection(this);
            bool isLocal  = !IsRunningOnMono() && ServerSupport.IsLocalIP(option.ServerIP);
            int  maxRetry = isLocal ? 3 : 5;

            for (int i = 0; i < maxRetry; ++i)
            {
                if (i > 0)
                {
                    Console.WriteLine("再試行します・・・");
                }

                try
                {
                    // サーバに接続
                    server.Connect(option.ServerIP, option.ServerPort);
                }
                catch (Exception e0)
                {
                    Console.WriteLine(e0.StackTrace);
                    // サーバに繋がらなかった

                    // ローカルの場合は、起動を試みる
                    if (isLocal)
                    {
                        await ServerSupport.TerminateStandalone(option.AmatsukazeRoot);

                        ServerSupport.LaunchLocalServer(option.ServerPort, option.AmatsukazeRoot);

                        // 10秒待つ
                        await Task.Delay(10 * 1000);
                    }
                    else
                    {
                        // リモートの場合は、Wake On Lanする
                        if (option.MacAddress == null)
                        {
                            throw new Exception("リモートサーバへの接続に失敗しました。");
                        }

                        Console.WriteLine("Wake On Lanで起動を試みます。");

                        WakeOnLan.Wake(
                            IPAddress.Parse(option.ServerIP),
                            IPAddress.Parse(option.Subnet),
                            option.MacAddress);

                        // 40秒待つ
                        await Task.Delay(40 * 1000);
                    }

                    continue;
                }

                try
                {
                    // サーバにタスク登録
                    await server.AddQueue(request);

                    // リクエストIDの完了通知ゲット or タイムアウトしたら終了
                    var timeout = Task.Delay(30 * 1000);
                    while (okReceived == false)
                    {
                        var recv = server.ProcOneMessage();
                        if (await Task.WhenAny(recv, timeout) == timeout)
                        {
                            Console.WriteLine("サーバのリクエスト受理を確認できませんでした。");
                            throw new Exception();
                        }
                    }
                }
                catch (Exception e1)
                {
                    Console.WriteLine(e1.StackTrace);
                    // なぜか失敗した
                    continue;
                }

                if (string.IsNullOrEmpty(option.NasDir) == false && !option.NoMove)
                {
                    // NASにコピーしたファイルはtransferredフォルダに移動
                    string trsDir = Path.GetDirectoryName(option.FilePath) + Path.DirectorySeparatorChar + "transferred";
                    Directory.CreateDirectory(trsDir);
                    string trsFile = trsDir + Path.DirectorySeparatorChar + Path.GetFileName(option.FilePath);
                    if (File.Exists(option.FilePath))
                    {
                        if (File.Exists(trsFile))
                        {
                            // 既に存在している同名ファイルは削除
                            File.Delete(trsFile);
                        }
                        File.Move(option.FilePath, trsFile);
                    }
                    if (option.WithRelated)
                    {
                        var body   = Path.GetFileNameWithoutExtension(option.FilePath);
                        var tsext  = Path.GetExtension(option.FilePath);
                        var srcDir = Path.GetDirectoryName(option.FilePath);
                        foreach (var ext in ServerSupport.GetFileExtentions(null, true))
                        {
                            string srcPath = srcDir + Path.DirectorySeparatorChar + body + ext;
                            string dstPath = trsDir + Path.DirectorySeparatorChar + body + ext;
                            if (File.Exists(srcPath))
                            {
                                if (File.Exists(dstPath))
                                {
                                    // 既に存在している同名ファイルは削除
                                    File.Delete(dstPath);
                                }
                                File.Move(srcPath, dstPath);
                            }
                        }
                    }
                }

                break;
            }
            server.Finish();
        }