Exemplo n.º 1
0
 private void status()
 {
     foreach (Task T in allTask)
     {
         AMIServer server = (AMIServer)T.AsyncState;
         Console.WriteLine("Service {0} is running", server.Name);
     }
 }
Exemplo n.º 2
0
 private void shutdown()
 {
     foreach (Task T in allTask)
     {
         AMIServer server = (AMIServer)T.AsyncState;
         Console.WriteLine("Service {0} is stoping", server.Name);
         server.State = AMIServer.state_enum.stop;
     }
     shutdownStatus = true;
 }
Exemplo n.º 3
0
 private void stop(String service)
 {
     foreach (Task T in allTask)
     {
         AMIServer server = (AMIServer)T.AsyncState;
         if (server.Name == service)
         {
             server.State = AMIServer.state_enum.stop;
             allTask.Remove(T);
             return;
         }
     }
     Console.WriteLine("Service {0} has not run yet", service);
 }
Exemplo n.º 4
0
        private void start(String service, String address)
        {
            Console.WriteLine("Starting Service : " + service + ", address : " + address);
            String[]  settings  = address.Split(',');
            String[]  host_port = settings[(int)AMIServer.config_en.address].Split(':');
            AMIServer amiServer = new AMIServer(service, host_port[(int)AMIServer.config_en.hostname], int.Parse(host_port[(int)AMIServer.config_en.port]), int.Parse(settings[(int)AMIServer.config_en.durationTime]));

            list_AMIServer.Add(amiServer);
            amiServer.State = AMIServer.state_enum.running;
            Task T = new Task(checkAMIServer, amiServer);

            allTask.AddLast(T);
            T.Start();
        }
Exemplo n.º 5
0
 private void WriteLog(AMIServer amiServer, string log)
 {
     //Console.WriteLine(log);
     try
     {
         cultureEN = new CultureInfo("en-US");
         string str  = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss >> ", cultureEN);
         string str2 = log.Replace("\r\n", "\r\n" + str);
         if (!Directory.Exists("Log"))
         {
             Directory.CreateDirectory("Log");
         }
         System.IO.File.AppendAllText(string.Concat(new object[] { "Log/AMI_Monitor.", amiServer.Name, '-', amiServer.Host, "-", amiServer.Port, ".", DateTime.Now.ToString("yyyyMMdd", cultureEN), ".log" }), str + str2 + "\r\n");
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error while writting log file : " + ex.Message);
     }
 }
Exemplo n.º 6
0
        private void sendLineNotification(AMIServer amiServer, String argMsg)
        {
            var request  = (HttpWebRequest)WebRequest.Create("https://notify-api.line.me/api/notify");
            var postData = string.Format("message={0}", argMsg);
            var data     = Encoding.UTF8.GetBytes(postData);

            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            String tokenStr = "Bearer " + lineToken;

            request.Headers.Add("Authorization", tokenStr); //KlPhgOKMqBYSuLsZBLAY7uUCXD1s0jEjwHfbUPbQE0I
            //request.Headers.Add("Authorization", "Bearer TRp6byyCsJG7S2poh5ON3zdH88SSm3LMffZ1fXy8o1H"); //KlPhgOKMqBYSuLsZBLAY7uUCXD1s0jEjwHfbUPbQE0I
            using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            var response       = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            Console.WriteLine("Sent line notification... \r\n{0} \r\n", responseString);
        }
Exemplo n.º 7
0
        private void checkAMIServer(object obj)
        {
            AMIServer  amiServer = (AMIServer)obj;
            bool       flag      = false;
            IPEndPoint remoteEP;

            this.WriteLog(amiServer, "Initiailize Service=" + amiServer.Name + ", Host:" + amiServer.Host + ", Port=" + amiServer.Port + ", DurationTime=" + amiServer.DurationTime);
            while (amiServer.State == AMIServer.state_enum.running)
            {
                AvailableInfo info = null;
                Thread.Sleep(500);
                DateTime dateTime  = DateTime.Now;
                double   diff      = dateTime.Subtract(amiServer.TimeStmap).TotalSeconds;
                TimeSpan timeSpand = dateTime.TimeOfDay;
                if (diff >= amiServer.DurationTime &&
                    !(amiServer.Exceptional_start_time < timeSpand && timeSpand < amiServer.Exceptional_end_time)
                    )
                {
                    Console.WriteLine("{0} < {1} < {2}", amiServer.Exceptional_start_time, timeSpand, amiServer.Exceptional_end_time);
                    amiServer.TimeStmap = DateTime.Now;
                    Console.WriteLine("Servcie {0} sending request", amiServer.Name);
                    try
                    {
                        remoteEP = new IPEndPoint(IPAddress.Parse(amiServer.Host), amiServer.Port);
                    }
                    catch (Exception ex)
                    {
                        this.WriteLog(amiServer, ex.Message);
                        return;
                    }

                    using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                    {
                        try
                        {
                            int millisecondsTimeout = 0xbb8;
                            socket.SendTimeout    = millisecondsTimeout;
                            socket.ReceiveTimeout = millisecondsTimeout;
                            if (!socket.BeginConnect(remoteEP, null, null).AsyncWaitHandle.WaitOne(millisecondsTimeout, false))
                            {
                                throw new SocketException(0x274c);
                            }
                            this.WriteLog(amiServer, "Connect: Success");
                        }
                        catch (Exception exception)
                        {
                            flag = true;
                            this.WriteLog(amiServer, "Connect: Error " + exception.Message);
                        }
                        if (!flag)
                        {
                            try
                            {
                                string s     = "000100,0";
                                byte[] bytes = Encoding.UTF8.GetBytes(s);
                                socket.Send(bytes, bytes.Length, SocketFlags.None);
                                try
                                {
                                    bytes = new byte[1024];
                                    int    count = socket.Receive(bytes);
                                    string str2  = Encoding.UTF8.GetString(bytes, 0, count);
                                    this.WriteLog(amiServer, "Response: " + str2);
                                    info = JsonConvert.DeserializeObject <AvailableInfo>(str2);
                                }
                                catch (Exception exception2)
                                {
                                    this.WriteLog(amiServer, "Response: Error " + exception2.Message);
                                }
                            }
                            catch (Exception exception3)
                            {
                                flag = true;
                                this.WriteLog(amiServer, "Request: Error " + exception3.Message);
                            }
                            try
                            {
                                socket.Shutdown(SocketShutdown.Both);
                                this.WriteLog(amiServer, "Disconnect: Success");
                            }
                            catch (Exception exception4)
                            {
                                this.WriteLog(amiServer, "Disconnect: Error " + exception4.Message);
                            }
                        }
                    }

                    if (info == null)
                    {
                        info = new AvailableInfo
                        {
                            ReturnCode    = "X0003",
                            ReturnMessage = "Can't connect to AMI Connector",
                            Available     = false
                        };
                    }

                    String warningMessage = "[AMIChecker]: " + amiServer.Name + " " + amiServer.Host + ":" + amiServer.Port + " : " + "(" + info.ReturnCode + ") " + info.ReturnMessage;
                    Console.WriteLine(warningMessage);

                    // Validate RetunCode and notify to line
                    if (info.ReturnCode == "X0003" || info.ReturnCode == "X0001")
                    {
                        sendLineNotification(amiServer, warningMessage);
                        this.WriteLog(amiServer, warningMessage);
                    }
                }
            }
            Console.WriteLine("Service {0} {1}:{2} stoped", amiServer.Name, amiServer.Host, amiServer.Port);
            Thread.CurrentThread.Abort();
        }