Пример #1
0
 /// <summary>
 /// 发送心跳
 /// </summary>
 private static void SendHeartbeat()
 {
     while (true)
     {
         Framework.Network.Synchronous.Client client = new Framework.Network.Synchronous.Client(Dns.GetHostName(),
     8600);
         string information = Process.GetCurrentProcess().Id + "|" + _servicePort + "?" + DateTime.UtcNow.ToString();
         Packet packet = new Packet(Encoding.UTF8.GetBytes(information));
         packet.WaiteCallBack = false;
         client.Send<bool>(packet);
         Thread.Sleep(30 * 1000);
     }
 }
Пример #2
0
        private string Query(string keywords, string queryType, string data)
        {
            IEnumerable<string> workers = GetAllWorkers();
            string result = string.Empty;
            var enumerable = workers as string[] ?? workers.ToArray();
            Console.WriteLine("Get Worker count:" + enumerable.Count());
            if (workers != null)
            {
                result = GetGlobalCache<string>(keywords);
                Console.WriteLine("query:[{0}] in cache is {1}", keywords, result);
                if (string.IsNullOrEmpty(result))
                {
                    string timestamp = DateTime.UtcNow.ToLongDateString();
                    SetGlobalCache(queryType, timestamp);
                    SetGlobalCache(timestamp, keywords);
                    int sendIndex = 9000;
                    List<string> cacheStore = new List<string>();
                    foreach (string worker in enumerable)
                    {
                        string[] workerInfo = worker.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        int endFlagIndex = workerInfo[0].LastIndexOf("/", StringComparison.Ordinal) + 1;
                        string ip = workerInfo[0].Substring(endFlagIndex, workerInfo[0].Length - endFlagIndex);

                        try
                        {
                            sendIndex += (int.Parse(workerInfo[1]) % 100);
                            Client client = new Client(ip, sendIndex);
                            Packet dataPacket = new Packet(System.Text.Encoding.UTF8.GetBytes(data))
                            {
                                WaiteCallBack = true
                            };
                            client.Send<bool>(dataPacket);
                            cacheStore.Add(ip + "," + sendIndex);
                            sendIndex = 9000;
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                        }
                    }

                    foreach (string ca in cacheStore)
                    {
                        try
                        {
                            string outputResult = ca + keywords;
                            Console.WriteLine(outputResult);
                            string record = GetGlobalCache<string>(outputResult);
                            result += record;
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                        }

                    }
                }

            }
            else
            {
                Console.WriteLine("Not found any workers!");
            }
            return result;
        }