Ejemplo n.º 1
0
        public RestResponse Process(string clusterName, string strUrl, string reqdata, string encoding, Method method)
        {
            ESNode node = ESNodeManager.Instance.GetThriftNode(clusterName);

            using (var esSession = new ESSession(node.ConnectionProvider))
            {
                var restRequest = new RestRequest();
                restRequest.Method = method;
                restRequest.Uri    = strUrl;

                if (!string.IsNullOrEmpty(reqdata))
                {
                    restRequest.Body = Encoding.UTF8.GetBytes(reqdata);
                }

                //				restRequest.Parameters = new Dictionary<string, string>();
                //				restRequest.Parameters.Add("pretty", "true");
                restRequest.Headers = new Dictionary <string, string>();
                restRequest.Headers.Add("Content-Type", "application/json");
                //				restRequest.Headers.Add("charset", encoding);

                RestResponse response = esSession.GetClient().execute(restRequest);
                if (response.Status != Status.OK && response.Status != Status.CREATED)
                {
                    var formatedMessage = string.Format("Method:{5},Status:{0},Url:{1}{2},ReqData{3},Response:{4}",
                                                        response.Status, esSession.CurrentServer, strUrl, reqdata,
                                                        response.GetBody(), method);


                    ExceptionHandler.HandleExceptionResponse(response.GetBody(), formatedMessage);
                }
                return(response);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 测试eSight是否能够连通。
 /// </summary>
 /// <param name="hostIP">主机IP</param>
 /// <param name="port">端口</param>
 /// <param name="userAccount">用户账户</param>
 /// <param name="passowrd">密码</param>
 /// <param name="timeoutSec">超时时间默认为:ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC</param>
 /// <returns>失败返回错误码,成功返回为空字符</returns>
 public string TestESSession(string hostIP, int port, string userAccount, string passowrd, int timeoutSec = ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC)
 {
     try
     {
         using (ESSession eSSession = new ESSession())
         {
             eSSession.SetHttpMode(_isHttps);
             eSSession.InitESight(hostIP, port, userAccount, passowrd, timeoutSec);
             eSSession.Open();
         }
     }
     catch (ESSessionExpceion ess)
     {
         LogUtil.HWLogger.API.Error(ess);
         if (ess.Code == "1")
         {
             return(ConstMgr.ErrorCode.SYS_USER_LOGING);
         }
         else
         {
             return(ess.Code);
         }
     }
     catch (Exception se)
     {
         LogUtil.HWLogger.API.Error(se);
         return(ConstMgr.ErrorCode.SYS_UNKNOWN_ERR);
     }
     return("");
 }
Ejemplo n.º 3
0
        public void IsTimeoutTest()
        {
            ESSession esSession = new ESSession();

            esSession.InitESight(_hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
            Assert.AreEqual(true, esSession.IsTimeout());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 返回IESSession
        /// </summary>
        /// <param name="hostIP">eSight IP</param>
        /// <param name="aliasName">别名</param>
        /// <param name="port">eSight 端口</param>
        /// <param name="userAccount">对应的账号</param>
        /// <param name="passowrd">对应的密码</param>
        /// <param name="timeoutSec">连接超时时间,默认:ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC</param>
        /// <returns></returns>
        public IESSession SaveNewESSession(string hostIP, string aliasName, int port, string userAccount, string passowrd, int timeoutSec = ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC)
        {
            //测试连接...
            using (ESSession eSSession = new ESSession())
            {
                eSSession.SetHttpMode(_isHttps);
                eSSession.InitESight(hostIP, port, userAccount, passowrd, timeoutSec);
                eSSession.Open();
            }

            IESSession iESSession = null;

            iESSession = FindESSession(hostIP); //查找已有的eSesssion,防止重复
            if (iESSession == null)             //没有找到已有的eSight.
            {
                iESSession = new ESSession();
                iESSession.SetHttpMode(_isHttps);                                      //设置默认协议为全局。
            }
            iESSession.InitESight(hostIP, port, userAccount, passowrd, timeoutSec);    //初始化eSight.
            iESSession.HWESightHost.AliasName = aliasName;                             //初始化eSight别名.
            iESSession.Open();                                                         //尝试打开连接,可能会抛出异常。

            iESSession.SaveToDB();                                                     //保存到数据库
            lock (eSightSessions)                                                      //锁定向量,防止并发
            {
                eSightSessions[iESSession.HWESightHost.HostIP.ToUpper()] = iESSession; //存储到缓存。
            }
            return(iESSession);
        }
Ejemplo n.º 5
0
        public void HandleException1Test()
        {
            ESSession          esSession = new ESSession();
            AggregateException ae        = new AggregateException("1");

            esSession.HandleException(ae);
            List <Exception> exs = new List <Exception>();
        }
Ejemplo n.º 6
0
 public static void ClassInitialize(TestContext context)
 {
     _hwESightHost              = new HWESightHost();
     _hwESightHost.HostIP       = "127.0.0.1";
     _hwESightHost.HostPort     = 32102;
     _hwESightHost.LoginAccount = "test";
     _hwESightHost.LoginPwd     = "test";
     _esSession = new ESSession();
     _esSession.SetHttpMode(true);
     _esSession.InitESight(_hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
 }
Ejemplo n.º 7
0
 public void SaveToDBTest()
 {
     using (ESSession esSession = new ESSession())
     {
         esSession.SetHttpMode(true);
         esSession.InitESight(_hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
         bool isSccu = esSession.SaveToDB();
         Assert.IsTrue(isSccu);
         Assert.IsTrue(esSession.HWESightHost.ID > 0);
     }
 }
Ejemplo n.º 8
0
        public void ChkOpenResultTest()
        {
            ESSession    esSession    = new ESSession();
            HWESightHost hwESightHost = new HWESightHost();

            hwESightHost.HostIP       = "127.0.0.1";
            hwESightHost.HostPort     = 32102;
            hwESightHost.LoginAccount = "test";
            hwESightHost.LoginPwd     = "test";
            esSession = new ESSession();
            esSession.SetHttpMode(true);
            esSession.InitESight(hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
            JObject result = JsonUtil.DeserializeObject <JObject>("{\"code\":1,\"data\":\"bfec0163 - dd56 - 473e-b11f - 6d5845e1b684\", \"description\":\"Operation success.\"}");

            esSession.ChkOpenResult(result);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 查询已有的连接列表
        /// </summary>
        /// <returns>返回已有的连接列表</returns>
        public IList <IESSession> ListESSessions()
        {
            lock (eSightSessions)
            {
                IList <IESSession>   retList  = new List <IESSession>();
                IList <HWESightHost> hostList = ListESHost();
                Dictionary <string, HWESightHost> tmpSessions = new Dictionary <string, HWESightHost>();
                foreach (HWESightHost hwESightHost in hostList)
                {
                    tmpSessions[hwESightHost.HostIP.ToUpper()] = hwESightHost;
                    if (eSightSessions.ContainsKey(hwESightHost.HostIP.ToUpper()))//Already exists...
                    {
                        IESSession iESession = eSightSessions[hwESightHost.HostIP.ToUpper()];
                        if (IsSameESightHost(hwESightHost, iESession.HWESightHost))
                        {
                            retList.Add(eSightSessions[hwESightHost.HostIP.ToUpper()]);
                        }
                        else//If not same reinit. 将list增加给retlist
                        {
                            iESession.InitESight(hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
                            retList.Add(iESession);
                        }
                    }
                    else
                    {//Create new...
                        IESSession iESSession = new ESSession();
                        iESSession.SetHttpMode(_isHttps);
                        iESSession.InitESight(hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
                        eSightSessions[hwESightHost.HostIP.ToUpper()] = iESSession;

                        retList.Add(iESSession);
                    }
                }
                //Clean unused sessions.
                IList <IESSession> existsList = new List <IESSession>(eSightSessions.Values);
                foreach (IESSession ieSSession in existsList)
                {
                    if (!tmpSessions.ContainsKey(ieSSession.HWESightHost.HostIP.ToUpper()))
                    {
                        eSightSessions.Remove(ieSSession.HWESightHost.HostIP.ToUpper());
                    }
                }
                return(retList);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 初始化所有的eSight连接的配置信息。
        /// 注意,并没有open。
        /// </summary>
        /// <param name="timeoutSec">连接超时时间,默认为ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC</param>
        public void InitESSessions(int timeoutSec = ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC)
        {
            IHWESightHostDal     hwESightHostDal = HWESightHostDal.Instance;
            IList <HWESightHost> hostList        = hwESightHostDal.GetList("1=1");//获取eSight

            foreach (HWESightHost hwESightHost in hostList)
            {
                lock (eSightSessions)                                               //开锁
                {
                    if (!eSightSessions.ContainsKey(hwESightHost.HostIP.ToUpper())) //判断是否已经在内存中存在,防止反复初始化。
                    {
                        IESSession iESSession = new ESSession();
                        iESSession.SetHttpMode(_isHttps);
                        iESSession.InitESight(hwESightHost, timeoutSec);
                        eSightSessions[hwESightHost.HostIP.ToUpper()] = iESSession;
                    }
                }
            }
        }
Ejemplo n.º 11
0
        public void HandleException2Test()
        {
            ESSession esSession      = new ESSession();
            var       expected       = "response content";
            var       expectedBytes  = Encoding.UTF8.GetBytes(expected);
            var       responseStream = new MemoryStream();

            responseStream.Write(expectedBytes, 0, expectedBytes.Length);
            responseStream.Seek(0, SeekOrigin.Begin);

            var response = new Mock <HttpWebResponse>();

            response.Setup(c => c.GetResponseStream()).Returns(responseStream);

            WebException we1 = new WebException("tset", new Exception("xx"), WebExceptionStatus.ConnectionClosed, response.Object);

            WebException       we2 = new WebException("tset2");
            AggregateException ae  = new AggregateException("1", we1, we2);

            esSession.HandleException(ae);
        }
Ejemplo n.º 12
0
        public void DisposeESightTest()
        {
            ESSession    esSession    = new ESSession();
            HWESightHost hwESightHost = new HWESightHost();

            hwESightHost.HostIP       = "127.0.0.1";
            hwESightHost.HostPort     = 32102;
            hwESightHost.LoginAccount = "test";
            hwESightHost.LoginPwd     = "test";
            esSession = new ESSession();
            esSession.SetHttpMode(true);
            esSession.InitESight(hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);
            esSession.Open();
            bool isSccu = esSession.SaveToDB();

            Assert.IsTrue(isSccu);

            esSession.HClient = null;
            //esSession.HWESightHost.HostIP = "xxx.xxx.xxx";

            esSession.Dispose();
        }
Ejemplo n.º 13
0
        public void DeleteESightTestZero()
        {
            ESSession    esSession    = new ESSession();
            HWESightHost hwESightHost = new HWESightHost();

            hwESightHost.HostIP       = "127.0.0.1";
            hwESightHost.HostPort     = 32102;
            hwESightHost.LoginAccount = "test";
            hwESightHost.LoginPwd     = "test";
            esSession = new ESSession();
            esSession.SetHttpMode(true);
            esSession.InitESight(hwESightHost, ConstMgr.HWESightHost.DEFAULT_TIMEOUT_SEC);

            bool isSccu = esSession.SaveToDB();

            Assert.IsTrue(isSccu);
            int oldId = esSession.HWESightHost.ID;

            esSession.HWESightHost.ID = 0;
            isSccu = esSession.DeleteESight();
            Assert.IsTrue(isSccu);
            esSession.HWESightHost.ID = oldId;
            esSession.DeleteESight();
        }
Ejemplo n.º 14
0
		public RestResponse Process(string clusterName,string strUrl, string reqdata, string encoding, Method method)
		{
			ESNode node = ESNodeManager.Instance.GetThriftNode(clusterName);
			using (var esSession = new ESSession(node.ConnectionProvider))
			{
				var restRequest = new RestRequest();
				restRequest.Method = method;
				restRequest.Uri = strUrl;

				if (!string.IsNullOrEmpty(reqdata))
				{
					restRequest.Body = Encoding.UTF8.GetBytes(reqdata);
				}

				//				restRequest.Parameters = new Dictionary<string, string>();
				//				restRequest.Parameters.Add("pretty", "true");
				restRequest.Headers = new Dictionary<string, string>();
				restRequest.Headers.Add("Content-Type", "application/json");
				//				restRequest.Headers.Add("charset", encoding);

				RestResponse response = esSession.GetClient().execute(restRequest);
				if (response.Status != Status.OK && response.Status != Status.CREATED)
				{
					var formatedMessage = string.Format("Method:{5},Status:{0},Url:{1}{2},ReqData{3},Response:{4}",
								  response.Status, esSession.CurrentServer, strUrl, reqdata,
								  response.GetBody(), method);


					ExceptionHandler.HandleExceptionResponse(response.GetBody(), formatedMessage);



				}
				return response;
			}
		}
Ejemplo n.º 15
0
        public void ChkUnInitESightTest()
        {
            ESSession esSession = new ESSession();

            esSession.TryOpen();
        }