예제 #1
0
        public Result GetRepositories(string query)
        {
            var watch = Stopwatch.StartNew();

            _answer       = null;
            _repositories = null;

            using (var client = new RequestSocket())
            {
                client.Connect(EndpointProvider.Address);

                byte[] load = Encoding.UTF8.GetBytes(query);
                client.SendFrame(load);
                client.ReceiveReady += ClientOnReceiveReady;

                client.Poll(TimeSpan.FromMilliseconds(3000));

                client.ReceiveReady -= ClientOnReceiveReady;
                client.Disconnect(EndpointProvider.Address);
            }

            watch.Stop();

            return(new Result()
            {
                Answer = _answer ?? GetErrorMessage(),
                DurationMilliseconds = watch.ElapsedMilliseconds,
                Repositories = _repositories ?? new Repository[0]
            });
        }
예제 #2
0
        public TextResponse Request(TextRequest request)
        {
            RequestSocket client = CreateClientSocket();

            client.SendFrame(Encoding.Unicode.GetBytes(request.Text));
            client.Poll(TimeSpan.FromMilliseconds(RequestTimeout));
            TerminateClient(client);
            return(_response);
        }
예제 #3
0
파일: Program.cs 프로젝트: GrabCAD/netmq
        private static void Main()
        {
            using (var worker = new RequestSocket())
            {
                var random = new Random(DateTime.Now.Millisecond);
                var guid = Guid.NewGuid();

                worker.Options.Identity = Encoding.Unicode.GetBytes(guid.ToString());
                worker.Connect(ServerEndpoint);

                worker.ReceiveReady += (s, e) =>
                {
                    // Read and save all frames until we get an empty frame
                    // In this example there is only 1 but it could be more
                    byte[] address = worker.ReceiveFrameBytes();
                    worker.ReceiveFrameBytes(); // empty
                    byte[] request = worker.ReceiveFrameBytes();

                    worker.SendMoreFrame(address);
                    worker.SendMoreFrame(Encoding.Unicode.GetBytes(""));
                    worker.SendFrame(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(request) + " WORLD!"));
                };

                Console.WriteLine("W: {0} worker ready", guid);
                worker.SendFrame(Encoding.Unicode.GetBytes(LRUReady));

                var cycles = 0;
                while (true)
                {
                    cycles += 1;
                    if (cycles > 3 && random.Next(0, 5) == 0)
                    {
                        Console.WriteLine("W: {0} simulating a crash", guid);
                        Thread.Sleep(5000);
                    }
                    else if (cycles > 3 && random.Next(0, 5) == 0)
                    {
                        Console.WriteLine("W: {0} simulating CPU overload", guid);
                        Thread.Sleep(3000);
                    }
                    Console.WriteLine("W: {0} normal reply", guid);

                    worker.Poll(TimeSpan.FromMilliseconds(1000));
                }
            }
        }
예제 #4
0
        private static void Main()
        {
            using (var worker = new RequestSocket())
            {
                var random = new Random(DateTime.Now.Millisecond);
                var guid   = Guid.NewGuid();

                worker.Options.Identity = Encoding.Unicode.GetBytes(guid.ToString());
                worker.Connect(ServerEndpoint);

                worker.ReceiveReady += (s, e) =>
                {
                    // Read and save all frames until we get an empty frame
                    // In this example there is only 1 but it could be more
                    byte[] address = worker.ReceiveFrameBytes();
                    worker.ReceiveFrameBytes(); // empty
                    byte[] request = worker.ReceiveFrameBytes();

                    worker.SendMoreFrame(address);
                    worker.SendMoreFrame(Encoding.Unicode.GetBytes(""));
                    worker.SendFrame(Encoding.Unicode.GetBytes(Encoding.Unicode.GetString(request) + " WORLD!"));
                };

                Console.WriteLine("W: {0} worker ready", guid);
                worker.SendFrame(Encoding.Unicode.GetBytes(LRUReady));

                var cycles = 0;
                while (true)
                {
                    cycles += 1;
                    if (cycles > 3 && random.Next(0, 5) == 0)
                    {
                        Console.WriteLine("W: {0} simulating a crash", guid);
                        Thread.Sleep(5000);
                    }
                    else if (cycles > 3 && random.Next(0, 5) == 0)
                    {
                        Console.WriteLine("W: {0} simulating CPU overload", guid);
                        Thread.Sleep(3000);
                    }
                    Console.WriteLine("W: {0} normal reply", guid);

                    worker.Poll(TimeSpan.FromMilliseconds(1000));
                }
            }
        }
예제 #5
0
        private static bool TryRequest(string endpoint, string requestString)
        {
            Console.WriteLine("Trying echo service at {0}", endpoint);

            using (var client = new RequestSocket())
            {
                client.Options.Linger = TimeSpan.Zero;

                client.Connect(endpoint);

                client.SendFrame(requestString);
                client.ReceiveReady += ClientOnReceiveReady;
                bool pollResult = client.Poll(TimeSpan.FromMilliseconds(RequestTimeout));
                client.ReceiveReady -= ClientOnReceiveReady;
                client.Disconnect(endpoint);
                return(pollResult);
            }
        }
예제 #6
0
파일: Program.cs 프로젝트: GrabCAD/netmq
        private static bool TryRequest(string endpoint, string requestString)
        {
            Console.WriteLine("Trying echo service at {0}", endpoint);

            using (var client = new RequestSocket())
            {
                client.Options.Linger = TimeSpan.Zero;

                client.Connect(endpoint);

                client.SendFrame(requestString);
                client.ReceiveReady += ClientOnReceiveReady;
                bool pollResult = client.Poll(TimeSpan.FromMilliseconds(RequestTimeout));
                client.ReceiveReady -= ClientOnReceiveReady;
                client.Disconnect(endpoint);
                return pollResult;
            }
        }
예제 #7
0
파일: Program.cs 프로젝트: xzoth/netmq
        private static void Main()
        {
            Console.Title = "NetMQ LazyPirate Client";

            using (var context = NetMQContext.Create())
            {
                RequestSocket client = CreateServerSocket(context);

                while (s_retriesLeft > 0)
                {
                    s_sequence++;
                    Console.WriteLine("C: Sending ({0})", s_sequence);
                    client.Send(Encoding.Unicode.GetBytes(s_sequence.ToString()));
                    s_expectReply = true;

                    while (s_expectReply)
                    {
                        bool result = client.Poll(TimeSpan.FromMilliseconds(RequestTimeout));

                        if (result)
                        {
                            continue;
                        }

                        s_retriesLeft--;

                        if (s_retriesLeft == 0)
                        {
                            Console.WriteLine("C: Server seems to be offline, abandoning");
                            break;
                        }

                        Console.WriteLine("C: No response from server, retrying...");

                        TerminateClient(client);

                        client = CreateServerSocket(context);
                        client.Send(Encoding.Unicode.GetBytes(s_sequence.ToString()));
                    }
                }

                TerminateClient(client);
            }
        }
예제 #8
0
        static void Main(string[] args)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                RequestSocket client = CreateServerSocket(context);

                while (_retriesLeft > 0)
                {
                    _sequence++;
                    Console.WriteLine("C: Sending ({0})", _sequence);
                    client.Send(Encoding.Unicode.GetBytes(_sequence.ToString()));
                    _expectReply = true;

                    while (_expectReply)
                    {
                        bool result = client.Poll(TimeSpan.FromMilliseconds(REQUEST_TIMEOUT));

                        if (!result)
                        {
                            _retriesLeft--;

                            if (_retriesLeft == 0)
                            {
                                Console.WriteLine("C: Server seems to be offline, abandoning");
                                break;
                            }
                            else
                            {
                                Console.WriteLine("C: No response from server, retrying...");

                                client.Disconnect(SERVER_ENDPOINT);
                                client.Close();
                                client.Dispose();

                                client = CreateServerSocket(context);
                                client.Send(Encoding.Unicode.GetBytes(_sequence.ToString()));
                            }
                        }
                    }
                }
            }
        }