Exemplo n.º 1
0
        //  Round-trip demonstrator
        //  While this example runs in a single process, that is just to make
        //  it easier to start and stop the example. The client task signals to
        //  main when it's ready.
        public static void Tripping(string[] args)
        {
            bool verbose = (args.Any(e => e.ToLower().Equals("-v") ||
                                     e.ToLower().Equals("--verbose")));

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

            CancellationTokenSource cancellor = new CancellationTokenSource();

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

            using (ZContext ctx = new ZContext())
            {
                using (var client = new ZActor(ctx, Tripping_ClientTask))
                {
                    (new Thread(() => Tripping_WorkerTask(ctx))).Start();
                    (new Thread(() => Tripping_BrokerTask(ctx))).Start();
                    client.Start();
                    using (var signal = client.Frontend.ReceiveFrame())
                        if (verbose)
                        {
                            signal.ToString().DumpString();
                        }
                }
            }
        }
Exemplo n.º 2
0
        public static void IronhouseServer(string[] args)
        {
            //
            // Hello World server with ironhouse security
            //
            // Author: hawkans
            //

            if (args == null || args.Length < 1)
            {
                Console.WriteLine();
                Console.WriteLine("Usage: ./{0} Ironhouse HWServer [Name]", AppDomain.CurrentDomain.FriendlyName);
                Console.WriteLine();
                Console.WriteLine("    Name   Your name. Default: World");
                Console.WriteLine();
                args = new string[] { "World" };
            }

            string name = args[0];
            // Create or load certificates
            ZCert clientCert = GetOrCreateCert("clienttest");
            ZCert serverCert = GetOrCreateCert("servertest");

            using (var responder = new ZSocket(ZSocketType.REP))
                using (var actor = new ZActor(ZAuth.Action0, null))
                {
                    actor.Start();
                    // send CURVE settings to ZAuth
                    actor.Frontend.Send(new ZFrame("VERBOSE"));
                    actor.Frontend.Send(new ZMessage(new List <ZFrame>()
                    {
                        new ZFrame("ALLOW"), new ZFrame("127.0.0.1")
                    }));
                    actor.Frontend.Send(new ZMessage(new List <ZFrame>()
                    {
                        new ZFrame("CURVE"), new ZFrame(".curve")
                    }));

                    responder.CurvePublicKey = serverCert.PublicKey;
                    responder.CurveSecretKey = serverCert.SecretKey;
                    responder.CurveServer    = true;
                    // Bind
                    responder.Bind("tcp://*:5555");

                    while (true)
                    {
                        // Receive
                        using (ZFrame request = responder.ReceiveFrame())
                        {
                            Console.WriteLine("Received {0}", request.ReadString());

                            // Do some work
                            Thread.Sleep(1);

                            // Send
                            responder.Send(new ZFrame(name));
                        }
                    }
                }
        }
Exemplo n.º 3
0
        //  Round-trip demonstrator
        //  While this example runs in a single process, that is just to make
        //  it easier to start and stop the example. The client task signals to
        //  main when it's ready.
        static void Main(string[] args)
        {
            CancellationTokenSource cancellor = new CancellationTokenSource();

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

            using (ZContext ctx = new ZContext())
            {
                using (var client = new ZActor(ctx, Tripping_ClientTask))
                {
                    (new Thread(() => Tripping_WorkerTask(ctx))).Start();
                    (new Thread(() => Tripping_BrokerTask(ctx))).Start();

                    client.Start();

                    using (var signal = client.Frontend.ReceiveFrame())
                        if (Common.ProgramerHelper.Verbose)
                        {
                            signal.ToString().DumpString();
                        }
                }
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
Exemplo n.º 4
0
        //  Round-trip demonstrator
        //  While this example runs in a single process, that is just to make
        //  it easier to start and stop the example. The client task signals to
        //  main when it's ready.
        public static void Tripping(string[] args)
        {
            CancellationTokenSource cancellor = new CancellationTokenSource();

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

            using (ZContext ctx = new ZContext())
            {
                using (var client = new ZActor(ctx, Tripping_ClientTask))
                {
                    (new Thread(() => Tripping_WorkerTask(ctx))).Start();
                    (new Thread(() => Tripping_BrokerTask(ctx))).Start();
                    client.Start();
                    using (var signal = client.Frontend.ReceiveFrame())
                        if (Verbose)
                        {
                            signal.ToString().DumpString();
                        }
                }
            }
        }
Exemplo n.º 5
0
            public FreelanceClient()
            {
                // Constructor
                this.context = ZContext.Create();

                this.Actor = new ZActor(this.context, FreelanceClient.Agent);
                this.Actor.Start();
            }
Exemplo n.º 6
0
        public static void SuiSnail(IDictionary <string, string> dict, string[] args)
        {
            // The main task simply starts a client and a server, and then
            // waits for the client to signal that it has died:

            using (var context = ZContext.Create())
                using (var pubpipe = new ZActor(context, SuiSnail_Publisher))
                    using (var subpipe = new ZActor(context, SuiSnail_Subscriber))
                    {
                        pubpipe.Start();
                        subpipe.Start();

                        subpipe.Frontend.ReceiveFrame();
                        pubpipe.Frontend.Send(new ZFrame("break"));

                        Thread.Sleep(100);
                    }
        }
Exemplo n.º 7
0
        public static void SuiSnail(string[] args)
        {
            // The main task simply starts a client and a server, and then
            // waits for the client to signal that it has died:

            using (var context = new ZContext())
                using (var pubpipe = new ZActor(context, SuiSnail_Publisher))
                    using (var subpipe = new ZActor(context, SuiSnail_Subscriber))
                    {
                        pubpipe.Start();
                        subpipe.Start();

                        subpipe.Frontend.ReceiveFrame();
                        pubpipe.Frontend.Send(new ZFrame("break"));

                        // wait for the Thread (you'll see how fast it is)
                        pubpipe.Join(5000);
                    }
        }
Exemplo n.º 8
0
            protected void Dispose(bool disposing)
            {
                if (disposing)
                {
                    // Destructor

                    if (this.Actor != null)
                    {
                        this.Actor.Dispose();
                        this.Actor = null;
                    }
                    if (this.context != null)
                    {
                        // Do context.Dispose()

                        this.context.Dispose();
                        this.context = null;
                    }
                }
            }
Exemplo n.º 9
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Slow Subscriber Detection");
            // The main task simply starts a client and a server, and then
            // waits for the client to signal that it has died:

            using (var context = new ZContext())
                using (var pubpipe = new ZActor(context, SuiSnail_Publisher))
                    using (var subpipe = new ZActor(context, SuiSnail_Subscriber))
                    {
                        pubpipe.Start();
                        subpipe.Start();

                        subpipe.Frontend.ReceiveFrame();
                        pubpipe.Frontend.Send(new ZFrame("break"));

                        // wait for the Thread (you'll see how fast it is)
                        pubpipe.Join(5000);
                    }

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
Exemplo n.º 10
0
        //  Titanic service
        //  Implements server side of http://rfc.zeromq.org/spec:9
        public static void Titanic(string[] args)
        {
            bool verbosedeep =
                (args.Any(e => e.ToLower().Equals("-vd") ||
                          e.ToLower().Equals("--verbosedeep")));


            bool verbose = verbosedeep || (args.Any(e => e.ToLower().Equals("-v") ||
                                                    e.ToLower().Equals("--verbose")));

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

            CancellationTokenSource cancellor = new CancellationTokenSource();

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

            ZContext ctx = new ZContext();

            using (var requestPipe = new ZActor(ctx, Titanic_Request, verbosedeep))
            {
                (new Thread(() => Titanic_Reply(ctx, cancellor, verbosedeep))).Start();
                (new Thread(() => Titanic_Close(ctx, cancellor, verbosedeep))).Start();
                ////////////////////
                /// HINT: Use requestPipe.Start instead of requestPipe.Start(cancellor)
                /// => with cancellor consturctor needed frontent pipe will not be initializes!!
                ////////////////////
                requestPipe.Start();
                Thread.Sleep(1500);


                // Main dispatcher loop
                while (true)
                {
                    //continue;
                    if (cancellor.IsCancellationRequested ||
                        (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
                    {
                        ctx.Shutdown();
                    }

                    var      path = Path.Combine(TitanicCommon.TITANIC_DIR, TitanicCommon.QUEUE_FILE);
                    var      p    = ZPollItem.CreateReceiver();
                    ZMessage msg;
                    ZError   error;
                    if (requestPipe.Frontend.PollIn(p, out msg, out error, TimeSpan.FromMilliseconds(1000)))
                    {
                        using (msg)
                        {
                            // Ensure message directory exists
                            Directory.CreateDirectory(TitanicCommon.TITANIC_DIR);

                            // Append UUID to queue, prefixed with '-' for pending
                            var uuid = Guid.Parse(msg.PopString());
                            using (var sw = File.AppendText(path))
                            {
                                sw.Write(TitanicCommon.QUEUE_LINEFORMAT, uuid);
                            }
                        }
                    }
                    else if (error.Equals(ZError.ETERM))
                    {
                        cancellor.Cancel();
                        break;                         // Interrupted
                    }
                    else if (error.Equals(ZError.EAGAIN))
                    //continue;
                    {
                        Thread.Sleep(1);
                    }
                    else
                    {
                        break;                         // Interrupted
                    }
                    // Brute force dispatcher
                    if (File.Exists(path))
                    {
                        using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            int    numBytesRead   = 0;
                            int    numBytesToRead = (new UTF8Encoding().GetBytes(String.Format(TitanicCommon.QUEUE_LINEFORMAT, Guid.NewGuid()))).Length;
                            byte[] readBytes      = new byte[numBytesToRead];
                            while (numBytesToRead > 0)
                            {
                                var n = fs.Read(readBytes, 0, numBytesToRead);
                                if (n == 0)
                                {
                                    break;
                                }
                                var line = (new UTF8Encoding()).GetString(readBytes, 0, n);
                                //  UUID is prefixed with '-' if still waiting
                                if (line.StartsWith("-"))
                                {
                                    var uuid = Guid.Parse(line.Substring(1, Guid.NewGuid().ToString().Length));
                                    if (verbose)
                                    {
                                        "I: processing request {0}".DumpString(uuid);
                                    }
                                    if (Titanic_ServiceSuccess(uuid, cancellor))
                                    {
                                        //  Mark queue entry as processed
                                        var newval = (new UTF8Encoding()).GetBytes("+");
                                        fs.Seek(-n, SeekOrigin.Current);
                                        fs.Write(newval, 0, newval.Length);
                                        fs.Seek(n - newval.Length, SeekOrigin.Current);
                                    }
                                }
                                if (cancellor.IsCancellationRequested)
                                {
                                    break;
                                }

                                numBytesRead  += n;
                                numBytesToRead = n;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
		void SetActor( ZActor* Actor );