Exemplo n.º 1
0
        static int Main(string[] args)
        {
            try
            {
                Parameter parameter = new Parameter(args);
                if (parameter.IsHelp == true)
                {
                    Console.WriteLine("manコマンドでspingのヘルプを参照して下さい。");
                    return 1;
                }
                #region Timeoutの設定
                SPing.Properties.Settings setting = new SPing.Properties.Settings();
                if (setting.TimeOut != 0)
                    _timeOut = setting.TimeOut;

                if (parameter.TimeOut != 0)
                    _timeOut = parameter.TimeOut;

                if (parameter.EchoCount != 0)
                    _echoCount = parameter.EchoCount;

                #endregion

                #region パラメータの最大スレッド数だけThreadをプールします。ただし、最大スレッド数は、256個
                //デフォルトは、250、1000⇒32bitだから?少ない?
                if (parameter.MaxThreads != 0)
                    ThreadPool.SetMaxThreads(parameter.MaxThreads, parameter.CompletionPortThreads);

                #endregion

                InitUpdateAddress();

                parameter.ArgumentsExceptOptions.All(arg =>
                {
                    string key = arg.IndexOf(",") >= 0 ? arg.Substring(0, arg.IndexOf(",")) : arg;
                    string hostName = arg.IndexOf(",") >= 0 ? arg.Substring(arg.IndexOf(",") + 1) : null;
                    if (_pingResult.ContainsKey(key) == true)
                    {
                        return true;
                    }
                    else
                    {
                        key = UpdateAddress(key);
                        PingResult pr = new PingResult();
                        pr.HostName = hostName;
                        _pingResult.Add(key, pr);

                        return true;
                    }
                });

                //pingの実行
                _pingResult.All(dic => { ThreadPool.QueueUserWorkItem(new WaitCallback(_Ping), dic); return true; });

                //結果の取得
                if (parameter.IsErrorOnly == true)
                {
                    //全てのチェックを終えてからエラーのみをフィルタする。
                    _pingResult.All(dic => { dic.Value.AutoResetEvent.WaitOne(); return true; });

                    //エラーの実を絞ってクエリ抽出
                    var pingResult = from ret in _pingResult where ret.Value.Exception != null || ret.Value.PingReply.Status != IPStatus.Success select ret;

                    //表示
                    return _ShowResult(pingResult, parameter, false);
                }
                else
                {
                    //結果はパラメータ順に表示する。
                    return _ShowResult(_pingResult, parameter, true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.GetAllMessage());
                return 1;
            }
        }
Exemplo n.º 2
0
        static int _ShowResult(IEnumerable<KeyValuePair<string, PingResult>> ret, Parameter parameter,  bool wait)
        {
            if (ret.Count() == 0) return 1;
            Encoding enc = Encoding.GetEncoding("Shift_JIS");
            var keyLenght = ret.Max(c => c.Key.Length);
            var hostNameLength = ret.Max(c => c.Value.HostName == null ? 0 : enc.GetBytes(c.Value.HostName).Length);

            int flg = 0;//0成功、1失敗
            ret.All(r =>
            {
                string hostName = null;
                hostName = r.Value.HostName == null ? ":" + ("".PadRight(hostNameLength)) : ":" + r.Value.HostName.PadRight(hostNameLength - (enc.GetBytes(r.Value.HostName).Length - r.Value.HostName.Length)) + "";
                if (wait == true)
                {
                    r.Value.AutoResetEvent.WaitOne();
                }
                string result = null;
                if (r.Value.Exception == null)
                {
                    if (r.Value.PingReply.Status == IPStatus.Success) {
                        string detailResult = "";
                        if (parameter.IsDetailResult == true)
                        {
                            detailResult =
                              "(bytes=" + r.Value.PingReply.Buffer.Length.ToString() + ","
                            + "time<" + (r.Value.PingReply.RoundtripTime + 1).ToString() + "ms,"
                            + "TTL=" + r.Value.PingReply.Options.Ttl.ToString() + ")";
                        }
                        if (r.Key.ToUpper() == r.Value.PingReply.Address.ToString())
                        {
                            //IPの表示は行わない。
                        }
                        else
                        {
                            result = " [" + r.Value.PingReply.Address.ToString() + "] ";
                        }
                        result = r.Value.PingReply.Status.ToString() + result + detailResult;

                    }
                    else
                    {
                        result = r.Value.PingReply.Status.ToString();
                        flg = 1;
                    }
                }
                else
                {
                    result = r.Value.Exception.GetAllMessage();
                    flg = 1;
                }

                string timeString = "";
                if (parameter.TimeFormat == TimeFormat.YYYYMMDDHHMMSS)
                    timeString = DateTime.Now.ToString("yyyy/MM/dd(ddd) HH:mm:ss") + ":";

                Console.WriteLine(timeString + r.Key.PadRight(keyLenght) + hostName + ":" + result);
                return true;
            });

            return flg;
        }