Exemple #1
0
        static void WorkerTask()
        {
            Random rand = new Random(DateTime.Now.Millisecond);

            using (Context ctx = new Context(1)) {
                using (Socket worker = ctx.Socket(SocketType.REQ)) {
                    //  We use a string identity for ease here
                    ZHelpers.SetID(worker, Encoding.Unicode);
                    worker.Connect("tcp://localhost:5555");
                    int total = 0;
                    while (true)
                    {
                        //  Tell the router we're ready for work
                        worker.Send("Ready", Encoding.Unicode);

                        //  Get workload from router, until finished
                        string workload = worker.Recv(Encoding.Unicode);
                        if (workload.Equals("END"))
                        {
                            Console.WriteLine("Processed: {0} tasks", total);
                            break;
                        }
                        total++;

                        //  Do some random work
                        Thread.Sleep(rand.Next(1, 1000));
                    }
                }
            }
        }
Exemple #2
0
        private static void ClientTask()
        {
            using (var ctx = new Context(1))
            {
                using (var client = ctx.Socket(SocketType.REQ))
                {
                    ZHelpers.SetID(client, Encoding.Unicode);
                    client.Connect(localFeAddress);

                    while (true)
                    {
                        client.Send("HELLO", Encoding.Unicode);
                        string reply = client.Recv(Encoding.Unicode);

                        if (string.IsNullOrEmpty(reply))
                        {
                            break;
                        }

                        Console.WriteLine("Client: {0}", reply);

                        Thread.Sleep(1000);
                    }
                }
            }
        }
Exemple #3
0
        //  Worker using REQ socket to do LRU routing
        //
        static void WorkerTask()
        {
            using (Context ctx = new Context(1)) {
                using (Socket worker = ctx.Socket(SocketType.REQ)) {
                    ZHelpers.SetID(worker, Encoding.Unicode);
                    worker.Connect("tcp://localhost:5556");

                    //  Tell broker we're ready for work
                    worker.Send("READY", Encoding.Unicode);
                    while (true)
                    {
                        //  Read and save all frames until we get an empty frame
                        //  In this example there is only 1 but it could be more
                        string address = worker.Recv(Encoding.Unicode);
                        string empty   = worker.Recv(Encoding.Unicode);

                        //  Get request, send reply
                        string request = worker.Recv(Encoding.Unicode);
                        Console.WriteLine("Worker: {0}", request);

                        worker.SendMore(address, Encoding.Unicode);
                        worker.SendMore();
                        worker.Send("OK", Encoding.Unicode);
                    }
                }
            }
        }
Exemple #4
0
        private static void ClientTask()
        {
            using (var ctx = new Context(1))
            {
                using (var client = ctx.Socket(SocketType.REQ))
                {
                    ZHelpers.SetID(client, Encoding.Unicode);
                    client.Connect("tcp://localhost:5555");

                    while (true)
                    {
                        //  Send request, get repl
                        client.Send("HELLO", Encoding.Unicode);
                        string reply = client.Recv(Encoding.Unicode);

                        if (string.IsNullOrEmpty(reply))
                        {
                            break;
                        }

                        Console.WriteLine("Client: {0}", reply);
                    }
                }
            }
        }
Exemple #5
0
        private static void WorkerTask()
        {
            using (var ctx = new Context(1))
            {
                using (var worker = ctx.Socket(SocketType.REQ))
                {
                    ZHelpers.SetID(worker, Encoding.Unicode);
                    worker.Connect(localBeAddress);

                    var msg = new ZMessage("READY");
                    msg.Send(worker);

                    while (true)
                    {
                        var recvMsg = new ZMessage(worker);
                        Console.WriteLine("Worker: {0}", recvMsg.BodyToString());

                        Thread.Sleep(1000);

                        recvMsg.StringToBody("OK");
                        recvMsg.Send(worker);
                        //var okmsg = new ZMessage("OK");
                        //okmsg.Send(worker);
                    }
                }
            }
        }
Exemple #6
0
        //  ---------------------------------------------------------------------
        //  This is our client task
        //  It connects to the server, and then sends a request once per second
        //  It collects responses as they arrive, and it prints them out. We will
        //  run several client tasks in parallel, each with a different random ID.
        static void ClientTask()
        {
            using (Context ctx = new Context(1)) {
                using (Socket client = ctx.Socket(SocketType.XREQ)) {
                    //  Generate printable identity for the client
                    ZHelpers.SetID(client, Encoding.Unicode);
                    string identity = client.IdentityToString(Encoding.Unicode);
                    client.Connect("tcp://localhost:5570");

                    client.PollInHandler += (skt, revents) => {
                        ZMessage zmsg = new ZMessage(skt);
                        Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString());
                    };

                    int requestNbr = 0;
                    while (true)
                    {
                        //  Tick once per second, pulling in arriving messages
                        for (int centitick = 0; centitick < 100; centitick++)
                        {
                            Context.Poller(new List <Socket>(new Socket[] { client }), 10000);
                        }
                        ZMessage zmsg = new ZMessage("");
                        zmsg.StringToBody(String.Format("request: {0}", ++requestNbr));
                        zmsg.Send(client);
                    }
                }
            }
        }
Exemple #7
0
        //  ---------------------------------------------------------------------
        //  This is our client task
        //  It connects to the server, and then sends a request once per second
        //  It collects responses as they arrive, and it prints them out. We will
        //  run several client tasks in parallel, each with a different random ID.
        public static void ClientTask()
        {
            using (var context = ZmqContext.Create())
            {
                using (ZmqSocket client = context.CreateSocket(SocketType.DEALER))
                {
                    //  Generate printable identity for the client
                    string identity = ZHelpers.SetID(client, Encoding.Unicode);
                    //client.Connect("tcp://localhost:5570");
                    string serviceRepoAddress = ConfigurationSettings.AppSettings["serviceRepoAddressZMQ"];
                    client.Connect(serviceRepoAddress);

                    client.ReceiveReady += (s, e) =>
                    {
                        var zmsg = new ZMessage(e.Socket);
                        Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString());
                    };

                    int requestNumber = 0;

                    var poller = new Poller(new List <ZmqSocket> {
                        client
                    });

                    var zmsg2 = new ZMessage("");

                    JSONMessage jsonMess = new JSONMessage();
                    jsonMess.Service    = "Klient";
                    jsonMess.Function   = "RegisterService";
                    jsonMess.Parameters = new string[] { "Klient", "Location", "binding" };
                    string json = JsonConvert.SerializeObject(jsonMess);
                    zmsg2.StringToBody(json);
                    zmsg2.Send(client);

                    while (true)
                    {
                        //  Tick once per second, pulling in arriving messages
                        for (int centitick = 0; centitick < 100; centitick++)
                        {
                            poller.Poll(TimeSpan.FromMilliseconds(10));
                        }

                        var zmsg = new ZMessage("");

                        jsonMess            = new JSONMessage();
                        jsonMess.Service    = "Klient";
                        jsonMess.Function   = "GetServiceLocation";
                        jsonMess.Parameters = new string[] { "Klient", "binding" };
                        json = JsonConvert.SerializeObject(jsonMess);
                        zmsg.StringToBody(json);
                        zmsg.Send(client);
                    }
                }
            }
        }
Exemple #8
0
        //  Basic request-reply client using REQ socket
        //
        static void ClientTask()
        {
            using (Context ctx = new Context(1)) {
                using (Socket client = ctx.Socket(SocketType.REQ)) {
                    ZHelpers.SetID(client, Encoding.Unicode);
                    client.Connect("tcp://localhost:5555");

                    //  Send request, get reply
                    client.Send("HELLO", Encoding.Unicode);
                    string reply = client.Recv(Encoding.Unicode);
                    Console.WriteLine("Client: {0}", reply);
                }
            }
        }
Exemple #9
0
        static Socket ConnectWorker(Context context)
        {
            Socket worker = context.Socket(SocketType.DEALER);

            //Set random identity to make tracing easier
            ZHelpers.SetID(worker, Encoding.Unicode);
            worker.Connect("tcp://localhost:5556");

            //Tell the queue we're ready for work
            Console.WriteLine("I: worker ready");
            worker.Send(PPP_READY, Encoding.Unicode);

            return(worker);
        }
Exemple #10
0
        private static void ClientTask()
        {
            using (var context = ZmqContext.Create())
            {
                using (ZmqSocket client = context.CreateSocket(SocketType.REQ))
                {
                    ZHelpers.SetID(client, Encoding.Unicode);
                    client.Connect("tcp://localhost:5555");

                    //  Send request, get reply
                    client.Send("HELLO", Encoding.Unicode);
                    string reply = client.Receive(Encoding.Unicode);
                    Console.WriteLine("Client: {0}", reply);
                }
            }
        }
Exemple #11
0
        //  Worker using REQ socket to do LRU routing
        //
        static void WorkerTask()
        {
            using (Context ctx = new Context(1)) {
                using (Socket worker = ctx.Socket(SocketType.REQ)) {
                    ZHelpers.SetID(worker, Encoding.Unicode);
                    worker.Connect("tcp://localhost:5556");

                    //  Tell broker we're ready for work
                    worker.Send("READY", Encoding.Unicode);

                    while (true)
                    {
                        ZMessage zmsg = new ZMessage(worker);
                        Console.WriteLine("Worker: {0}", Encoding.Unicode.GetString(zmsg.Body));
                        zmsg.StringToBody("OK");
                        zmsg.Send(worker);
                    }
                }
            }
        }
Exemple #12
0
        public static void WorkerTask()
        {
            var randomizer = new Random(DateTime.Now.Millisecond);

            using (var context = ZmqContext.Create())
            {
                using (ZmqSocket worker = context.CreateSocket(SocketType.REQ))
                {
                    //  We use a string identity for ease here
                    ZHelpers.SetID(worker, Encoding.Unicode);
                    worker.Connect("tcp://localhost:5555");

                    int total = 0;

                    bool end = false;
                    while (!end)
                    {
                        //  Tell the router we're ready for work
                        worker.Send("Ready", Encoding.Unicode);

                        //  Get workload from router, until finished
                        string workload = worker.Receive(Encoding.Unicode);

                        if (workload.Equals("END"))
                        {
                            end = true;
                        }
                        else
                        {
                            total++;

                            Thread.Sleep(randomizer.Next(1, 1000)); //  Simulate 'work'
                        }
                    }

                    Console.WriteLine("ID ({0}) processed: {1} tasks", worker.Identity, total);
                }
            }
        }
Exemple #13
0
        //  ---------------------------------------------------------------------
        //  This is our client task
        //  It connects to the server, and then sends a request once per second
        //  It collects responses as they arrive, and it prints them out. We will
        //  run several client tasks in parallel, each with a different random ID.
        public static void ClientTask()
        {
            using (var context = ZmqContext.Create())
            {
                using (ZmqSocket client = context.CreateSocket(SocketType.DEALER))
                {
                    //  Generate printable identity for the client
                    string identity = ZHelpers.SetID(client, Encoding.Unicode);
                    client.Connect("tcp://localhost:5570");

                    client.ReceiveReady += (s, e) =>
                    {
                        var zmsg = new ZMessage(e.Socket);
                        Console.WriteLine("{0} : {1}", identity, zmsg.BodyToString());
                    };

                    int requestNumber = 0;

                    var poller = new Poller(new List <ZmqSocket> {
                        client
                    });

                    while (true)
                    {
                        //  Tick once per second, pulling in arriving messages
                        for (int centitick = 0; centitick < 100; centitick++)
                        {
                            poller.Poll(TimeSpan.FromMilliseconds(10));
                        }
                        var zmsg = new ZMessage("");
                        zmsg.StringToBody(String.Format("request: {0}", ++requestNumber));
                        zmsg.Send(client);
                    }
                }
            }
        }
Exemple #14
0
        static void Main(string[] args)
        {
            using (var context = ZmqContext.Create())
            {
                using (var worker = context.CreateSocket(SocketType.REQ))
                {
                    var randomizer = new Random(DateTime.Now.Millisecond);
                    var identity   = ZHelpers.SetID(worker, Encoding.Unicode);
                    worker.Connect("tcp://localhost:5556");

                    Console.WriteLine("I: {0} worker ready", identity);
                    worker.Send("READY", Encoding.Unicode);

                    var cycles = 0;
                    while (true)
                    {
                        var zmsg = new ZMessage(worker);

                        cycles += 1;
                        if (cycles > 3 && randomizer.Next(0, 5) == 0)
                        {
                            Console.WriteLine("I: {0} simulating a crash", identity);
                            break;
                        }
                        else if (cycles > 3 && randomizer.Next(0, 5) == 0)
                        {
                            Console.WriteLine("I: {0} simulating CPU overload", identity);
                            System.Threading.Thread.Sleep(3000);
                        }
                        Console.WriteLine("I: {0} normal reply", identity);
                        System.Threading.Thread.Sleep(1000);
                        zmsg.Send(worker);
                    }
                }
            }
        }