Exemple #1
0
        //  Majordomo Protocol worker example
        //  Uses the mdwrk API to hide all MDP aspects
        public static void MDWorker(string[] args)
        {
            bool verbose = (args.Any(e => e.ToLower().Equals("-v") ||
                                     e.ToLower().Equals("--verbose")));

            Console.WriteLine("Verbose: {0}", verbose);

            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, ea) =>
            {
                ea.Cancel = true;
                cts.Cancel();
            };

            using (MajordomoWorker session = new MajordomoWorker("tcp://127.0.0.1:5555", "echo", verbose))
            {
                ZMessage reply = null;
                while (true)
                {
                    ZMessage request = session.Recv(reply, cts);
                    if (request == null)
                    {
                        break;                         // worker was interrupted
                    }
                    reply = request;                   // Echo is complex
                }
            }
        }
Exemple #2
0
        //  .split Titanic request service
        //  The {{titanic.request}} task waits for requests to this service. It writes
        //  each request to disk and returns a UUID to the client. The client picks
        //  up the reply asynchronously using the {{titanic.reply}} service:
        private static void Titanic_Request(ZContext ctx, ZSocket backendpipe, CancellationTokenSource cancellor, object[] args)
        {
            using (MajordomoWorker worker = new MajordomoWorker("tcp://127.0.0.1:5555", "titanic.request", (bool)args[0]))
            {
                ZMessage reply = null;
                while (true)
                {
                    // Send reply if it's not null
                    // And then get next request from broker
                    ZMessage request = worker.Recv(reply, cancellor);
                    if (request == null)
                    {
                        break;                         // Interrupted, exit
                    }
                    // Ensure message directory exists
                    Directory.CreateDirectory(TitanicCommon.TITANIC_DIR);
                    // Generate UUID and save mesage to disk
                    Guid   uuid = TitanicCommon.GenerateUuid();
                    string fn   = TitanicCommon.RequestFilename(uuid);

                    request.SerializeToXml(fn);
                    request.Dispose();

                    // Send UUID through tho message queue
                    reply = new ZMessage();
                    reply.Add(new ZFrame(uuid.ToString()));
                    ZError error;
                    if (!backendpipe.Send(reply, out error))
                    {
                        if (error.Equals(ZError.ETERM))
                        {
                            break;
                        }
                    }
                    //backendpipe.Send(reply);

                    // Now send UUID back to client
                    // Done by the mdwrk_recv() at the top of the loop
                    reply = new ZMessage();
                    reply.Add(new ZFrame("200"));
                    reply.Add(new ZFrame(uuid.ToString()));
                }
            }
        }
Exemple #3
0
        //  Majordomo Protocol worker example
        //  Uses the mdwrk API to hide all MDP aspects
        public static void MDWorker(string[] args)
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            Console.CancelKeyPress += (s, ea) =>
            {
                ea.Cancel = true;
                cts.Cancel();
            };

            using (MajordomoWorker session = new MajordomoWorker("tcp://127.0.0.1:5555", "echo", Verbose))
            {
                ZMessage reply = null;
                while (true)
                {
                    ZMessage request = session.Recv(reply, cts);
                    if (request == null)
                    {
                        break;                         // worker was interrupted
                    }
                    reply = request;                   // Echo is complex
                }
            }
        }
Exemple #4
0
        //  .split Titanic close task
        //  The {{titanic.close}} task removes any waiting replies for the request
        //  (specified by UUID). It's idempotent, so it is safe to call more than
        //  once in a row:
        private static void Titanic_Close(ZContext context, CancellationTokenSource cts, bool verbose)
        {
            using (var worker = new MajordomoWorker("tcp://127.0.0.1:5555", "titanic.close", verbose))
            {
                ZMessage reply = null;
                while (true)
                {
                    ZMessage request = worker.Recv(reply, cts);
                    if (request == null)
                    {
                        break;
                    }

                    var g     = Guid.Parse(request.Pop().ReadString());
                    var reqfn = TitanicCommon.RequestFilename(g);
                    var repfn = TitanicCommon.ReplyFilename(g);
                    File.Delete(reqfn);
                    File.Delete(repfn);
                    request.Dispose();
                    reply = new ZMessage();
                    reply.Add(new ZFrame("200"));
                }
            }
        }
Exemple #5
0
 //  .split Titanic reply service
 //  The {{titanic.reply}} task checks if there's a reply for the specified
 //  request (by UUID), and returns a 200 (OK), 300 (Pending), or 400
 //  (Unknown) accordingly:
 private static void Titanic_Reply(ZContext context, CancellationTokenSource cts, bool verbose)
 {
     using (var worker = new MajordomoWorker("tcp://127.0.0.1:5555", "titanic.reply", verbose))
     {
         ZMessage reply = null;
         while (true)
         {
             var request = worker.Recv(reply, cts);
             if (request == null)
             {
                 break;                         // Interrupted, exit
             }
             var g     = Guid.Parse(request.Pop().ReadString());
             var reqfn = TitanicCommon.RequestFilename(g);
             var repfn = TitanicCommon.ReplyFilename(g);
             if (File.Exists(repfn))
             {
                 reply = repfn.DeserializeFromXml <ZMessage>();
                 reply.Prepend(new ZFrame("200"));
             }
             else
             {
                 reply = new ZMessage();
                 if (File.Exists(reqfn))
                 {
                     reply.Prepend(new ZFrame("300"));                             //Pending
                 }
                 else
                 {
                     reply.Prepend(new ZFrame("400"));                             //Unknown
                 }
             }
             request.Dispose();
         }
     }
 }
Exemple #6
0
 public static void InitialiseWorker(String broker = "tcp://127.0.0.1:5555",
                                     String service = "testservice")
 {
     Task.Factory.StartNew(() =>
                           {
                               var worker = new MajordomoWorker(broker, service);
                               worker.Work();
                           }, Canceller.Token);
 }