Example #1
0
        private void DataListenerProxy(ZContext ctx)
        {
            using (var xsubsocket = new ZSocket(ctx, ZSocketType.XSUB))
                using (var xpubsocket = new ZSocket(ctx, ZSocketType.XPUB))
                    using (var listener = new ZSocket(ctx, ZSocketType.PAIR))
                    {
                        new Thread(() => StatisticListenerTask(ctx)).Start();

                        // Frontend xsubsocket is where the publisher node(s!) sending data
                        string localhost = "tcp://*:5560";
                        Console.WriteLine("I: Connecting to {0}", localhost);
                        xsubsocket.Bind(localhost);
                        xpubsocket.Bind("inproc://databoiler");
                        listener.Bind("inproc://listener");

                        ZError error;
                        if (!ZContext.Proxy(xsubsocket, xpubsocket, listener, out error))
                        {
                            Console.Write("error: {0}", error);
                            if (error == ZError.ETERM)
                            {
                                return;
                            }
                            throw new ZException(error);
                        }
                        // some error handling
                    }
        }
Example #2
0
            public static void WUProxy(string[] args)
            {
                //
                // Weather proxy device
                using (var context = new ZContext())
                    using (var frontend = new ZSocket(context, ZSocketType.XSUB))
                        using (var backend = new ZSocket(context, ZSocketType.XPUB))
                        {
                            // Frontend is where the weather server sits
                            string localhost = "tcp://127.0.0.1:5556";
                            Console.WriteLine("I: Connecting to {0}", localhost);
                            frontend.Connect(localhost);

                            // Backend is our public endpoint for subscribers
                            foreach (IPAddress address in WUProxy_GetPublicIPs())
                            {
                                var tcpAddress = string.Format("tcp://{0}:8100", address);
                                Console.WriteLine("I: Binding on {0}", tcpAddress);
                                backend.Bind(tcpAddress);

                                var epgmAddress = string.Format("epgm://{0};239.192.1.1:8100", address);
                                Console.WriteLine("I: Binding on {0}", epgmAddress);
                                backend.Bind(epgmAddress);
                            }
                            using (var subscription = ZFrame.Create(1))
                            {
                                subscription.Write(new byte[] { 0x1 }, 0, 1);
                                backend.Send(subscription);
                            }

                            // Run the proxy until the user interrupts us
                            ZContext.Proxy(frontend, backend);
                        }
            }
Example #3
0
        public static void MTServer(IDictionary <string, string> dict, string[] args)
        {
            //
            // Multithreaded Hello World server
            //
            // Author: metadings
            //

            // Socket to talk to clients and
            // Socket to talk to workers
            using (var context = ZContext.Create())
                using (var clients = ZSocket.Create(context, ZSocketType.ROUTER))
                    using (var workers = ZSocket.Create(context, ZSocketType.DEALER))
                    {
                        clients.Bind("tcp://*:5555");
                        workers.Bind("inproc://workers");

                        // Launch pool of worker threads
                        for (int i = 0; i < 5; ++i)
                        {
                            var thread = new Thread(() => MTServer_Worker(context));
                            thread.Start();
                        }

                        // Connect work threads to client threads via a queue proxy
                        ZContext.Proxy(clients, workers);
                    }
        }
Example #4
0
        public static void Espresso(string[] args)
        {
            //
            // Espresso Pattern
            // This shows how to capture data using a pub-sub proxy
            //
            // Author: metadings
            //

            using (var context = new ZContext())
                using (var subscriber = new ZSocket(context, ZSocketType.XSUB))
                    using (var publisher = new ZSocket(context, ZSocketType.XPUB))
                        using (var listener = new ZSocket(context, ZSocketType.PAIR))
                        {
                            new Thread(() => Espresso_Publisher(context)).Start();
                            new Thread(() => Espresso_Subscriber(context)).Start();
                            new Thread(() => Espresso_Listener(context)).Start();

                            subscriber.Connect("tcp://127.0.0.1:6000");
                            publisher.Bind("tcp://*:6001");
                            listener.Bind("inproc://listener");

                            ZError error;
                            if (!ZContext.Proxy(subscriber, publisher, listener, out error))
                            {
                                if (error == ZError.ETERM)
                                {
                                    return;             // Interrupted
                                }
                                throw new ZException(error);
                            }
                        }
        }
Example #5
0
        static void AsyncSrv_ServerTask(ZContext context)
        {
            // This is our server task.
            // It uses the multithreaded server model to deal requests out to a pool
            // of workers and route replies back to clients. One worker can handle
            // one request at a time but one client can talk to multiple workers at
            // once.

            using (var frontend = ZSocket.Create(context, ZSocketType.ROUTER))
                using (var backend = ZSocket.Create(context, ZSocketType.DEALER))
                {
                    // Frontend socket talks to clients over TCP
                    frontend.Bind("tcp://*:5570");
                    // Backend socket talks to workers over inproc
                    backend.Bind("inproc://backend");

                    // Launch pool of worker threads, precise number is not critical
                    for (int i = 0; i < 5; ++i)
                    {
                        int j = i; new Thread(() => AsyncSrv_ServerWorker(context, j)).Start();
                    }

                    // Connect backend to frontend via a proxy
                    ZError error;
                    if (!ZContext.Proxy(frontend, backend, out error))
                    {
                        if (error == ZError.ETERM)
                        {
                            return;                     // Interrupted
                        }
                        throw new ZException(error);
                    }
                }
        }
Example #6
0
        private void DataBoilerProxy(ZContext ctx)
        {
            using (var sub = new ZSocket(ctx, ZSocketType.SUB))
                using (var pub = new ZSocket(ctx, ZSocketType.PUB))         // dont needed anymore
                {
                    sub.Connect("inproc://databoiler");
                    sub.SubscribeAll();             //"asdf");//All();


                    /***//*
                    *  ZError error;
                    *  ZMessage msg;
                    *  while (null != (msg = sub.ReceiveMessage(out error)))
                    *  {
                    *
                    *       msg.frame to msgcountcountainer.
                    *       parse by uid.
                    *       store current value by uid
                    *  }
                    *//***/
                    pub.Bind("inproc://datafeed");

                    // like syncpub
                    ZError error;
                    if (!ZContext.Proxy(sub, pub, out error))
                    {
                        Console.Write("error: {0}", error);
                        if (error == ZError.ETERM)
                        {
                            return;
                        }
                        throw new ZException(error);
                    }

                    /*
                     * ZMessage msg;
                     * ZError error;
                     * while (true)
                     *      if (null == (msg = sub.ReceiveMessage(out error)))
                     *      {
                     *              Console.Write(string.Format("error: {0}", error));
                     *              if (error == ZError.ETERM)
                     *                      break;
                     *              throw new ZException(error);
                     *      }
                     *      else
                     *              pub.Send(msg);
                     */
                }
        }
Example #7
0
        public void Start()
        {
            Listener.Bind();
            Sender.Bind();

            ZError error;

            if (!ZContext.Proxy(Listener.Socket, Sender.Socket, out error))
            {
                //TODO : Unbind and Dispose of Sockets????
                if (error == ZError.ETERM)
                {
                    return;             // Interrupted
                }
                throw new ZException(error);
            }
        }
Example #8
0
        //  .split broker task
        //  Here is the broker task. It uses the {{zmq_proxy}} function to switch
        //  messages between frontend and backend:
        static void Tripping_BrokerTask(ZContext ctx)
        {
            using (var frontend = new ZSocket(ctx, ZSocketType.DEALER))
                using (var backend = new ZSocket(ctx, ZSocketType.DEALER))
                {
                    frontend.Bind("tcp://*:5555");
                    backend.Bind("tcp://*:5556");

                    ZError error;
                    if (!ZContext.Proxy(frontend, backend, out error))
                    {
                        if (Equals(error, ZError.ETERM))
                        {
                            return; // Interrupted
                        }
                        throw new ZException(error);
                    }
                }
        }
Example #9
0
        static void Main(string[] args)
        {
            using (var ctx = new ZContext())
                using (var clients = new ZSocket(ctx, ZSocketType.ROUTER))
                    using (var workers = new ZSocket(ctx, ZSocketType.DEALER))
                    {
                        clients.Bind("tcp://*:10000");
                        workers.Bind("inproc://workers");

                        // Launch pool of worker threads
                        for (int i = 0; i < 5; ++i)
                        {
                            new Thread(() => MTServer_Worker(ctx)).Start();
                        }

                        // Connect work threads to client threads via a queue proxy
                        ZContext.Proxy(clients, workers);
                    }
        }
Example #10
0
        private void Listen()
        {
            while (_context != null && _socket != null && !disposedValue)
            {
                // Launch pool of worker threads, precise number is not critical

                for (int i = 0; i < 5; ++i)
                {
                    int j = i;
                    _workerThreads.Add(new WorkerThread
                    {
                        ThreadID = j,
                        Thread   = new Thread(() => AsyncSrv_ServerWorker(this, _context, j))
                    }
                                       );
                }
                foreach (var workerThread in _workerThreads)
                {
                    workerThread.Thread.Start();
                }
                // Connect backend to frontend via a proxy
                ZError error;
                if (!ZContext.Proxy(_socket, _worker, out error))
                {
                    if (error == ZError.ETERM)
                    {
                        return; // Interrupted
                    }
                    throw new ZException(error);
                }

/*                using (ZFrame request = _socket.ReceiveFrame())
 *              {
 *
 *                  var reqString = request.ReadString();
 *                  Console.WriteLine("Listener Received {0}", reqString);
 *                  MessageReceived.DynamicInvoke(this, reqString);
 *              }
 */
            }
        }
Example #11
0
        public void Start()
        {
            if (state == ServerHostState.OPEN)
            {
                // don't open again
                return;
            }

            Console.WriteLine("[HOST] Started...");
            state = ServerHostState.OPEN;

            // create our sockets
            _serverFrontendPublishSocket = new ZSocket(_context, ZSocketType.PUB);
            _serverFrontendReceiveSocket = new ZSocket(_context, ZSocketType.ROUTER);

            // bind both sockets
            _serverFrontendReceiveSocket.Bind("tcp://*:5555");
            _serverFrontendPublishSocket.Bind("tcp://*:5556");

            ZContext.Proxy(_serverFrontendReceiveSocket, _serverFrontendPublishSocket);
        }
Example #12
0
        public static void MsgQueue(IDictionary <string, string> dict, string[] args)
        {
            //
            // Simple message queuing broker
            // Same as request-reply broker but using QUEUE device
            //
            // Author: metadings
            //

            // Socket facing clients and
            // Socket facing services
            using (var context = new ZContext())
                using (var frontend = new ZSocket(context, ZSocketType.ROUTER))
                    using (var backend = new ZSocket(context, ZSocketType.DEALER))
                    {
                        frontend.Bind("tcp://*:5559");
                        backend.Bind("tcp://*:5560");

                        // Start the proxy
                        ZContext.Proxy(frontend, backend);
                    }
        }
Example #13
0
 /// <summary>
 /// 这是代理
 /// </summary>
 /// <param name="context"></param>
 static void AsyncSrv_ServerTask(ZContext context)
 {
     using (var frontend = new ZSocket(context, ZSocketType.ROUTER))
         using (var backend = new ZSocket(context, ZSocketType.DEALER))
         {
             frontend.Bind("tcp://*:5570");
             backend.Bind("inproc://backend");
             for (int i = 0; i < 1; ++i)
             {
                 int j = i; new Thread(() => AsyncSrv_ServerWorker(context, j)).Start();
             }
             if (ZContext.Proxy(frontend, backend, out var error))
             {
                 return;
             }
             if (Equals(error, ZError.ETERM))
             {
                 return;
             }
             throw new ZException(error);
         }
 }
Example #14
0
        static void Main(string[] args)
        {
            //
            // Espresso Pattern
            // This shows how to capture data using a pub-sub proxy
            //
            // Author: metadings
            //

            using (var context = new ZContext())
                using (var frontend = new ZSocket(context, ZSocketType.XSUB))
                    using (var backend = new ZSocket(context, ZSocketType.XPUB))
                        using (var listener = new ZSocket(context, ZSocketType.PAIR))
                        {
                            //new Thread(() => Espresso_Publisher(context)).Start();
                            //new Thread(() => Espresso_Subscriber(context)).Start();
                            new Thread(() => Espresso_Listener(context)).Start();

                            frontend.Connect("tcp://127.0.0.1:60000");
                            backend.Bind("tcp://*:60001");

                            listener.Bind("inproc://listener");

                            ZError error;
                            if (!ZContext.Proxy(frontend, backend, listener, out error))
                            {
                                if (error == ZError.ETERM)
                                {
                                    return; // Interrupted
                                }
                                throw new ZException(error);
                            }
                        }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
Example #15
0
        static void Server(ZContext context, ReadWrite readwrite)
        {
            using (var frontend = new ZSocket(context, ZSocketType.ROUTER))
                using (var backend = new ZSocket(context, ZSocketType.DEALER))
                {
                    frontend.Bind("tcp://*:5569");
                    backend.Bind("inproc://backend");

                    for (int i = 0; i < 5; ++i)
                    {
                        new Thread(() => ProgramStart(context, readwrite)).Start();
                    }

                    ZError error;
                    if (!ZContext.Proxy(frontend, backend, out error))
                    {
                        if (error == ZError.ETERM)
                        {
                            return; // Interrupted
                        }
                        throw new ZException(error);
                    }
                }
        }
Example #16
0
        static void Main(string[] args)
        {
            using (var db = new LiteDatabase("mydb.db"))
            {
                if (db.CollectionExists("DbLogger"))
                {
                    var collection = db.GetCollection <DBLog>("DbLogger");
                    IEnumerable <DBLog> loggerlist = collection.FindAll();
                    foreach (DBLog log in loggerlist)
                    {
                        dblogger.Add(log);
                    }
                }
                else
                {
                    var collection = db.GetCollection <DBLog>("DbLogger");
                    var log        = new DBLog
                    {
                        userID     = "9999",
                        ipAddress  = "0000000",
                        EncType    = "NULL",
                        fileData   = null,
                        insertDate = DateTime.Now
                    };
                    collection.Insert(log);
                }
                if (db.CollectionExists("DbUsers"))
                {
                    var collection = db.GetCollection <DBUsers>("DbUsers");
                    IEnumerable <DBUsers> userlist = collection.FindAll();
                    foreach (DBUsers user in userlist)
                    {
                        dbusers.Add(user);
                    }
                }
                else
                {
                    var collection1 = db.GetCollection <DBUsers>("DbUsers");
                    var user        = new DBUsers
                    {
                        ID   = "101",
                        user = "******"
                    };
                    collection1.Insert(user);
                }
            }
            Console.WriteLine("Active Users");
            foreach (DBUsers user in dbusers)
            {
                Console.WriteLine(user.id + " - " + user.ID + " - " + user.user);
            }

            using (var ctx = new ZContext())
                using (var clients = new ZSocket(ctx, ZSocketType.ROUTER))
                    using (var workers = new ZSocket(ctx, ZSocketType.DEALER))
                    {
                        clients.Bind("tcp://*:10000");
                        workers.Bind("inproc://workers");

                        // Launch pool of worker threads
                        for (int i = 0; i < 5; ++i)
                        {
                            new Thread(() => MTServer_Worker(ctx)).Start();
                        }

                        // Connect work threads to client threads via a queue proxy
                        ZContext.Proxy(clients, workers);
                    }
        }
Example #17
0
        static void Main(string[] args)
        {
            var options = new Options();
            var parser  = new CommandLineParser(new CommandLineParserSettings(Console.Error));

            if (!parser.ParseArguments(args, options))
            {
                Console.ReadLine();
                throw new ArgumentException();
            }
            //
            // Weather proxy device
            //
            // Author: metadings
            //

            using (var context = new ZContext())
                using (var frontend = new ZSocket(context, ZSocketType.XSUB))
                    using (var backend = new ZSocket(context, ZSocketType.XPUB))
                    {
                        // Frontend is where the weather server sits
                        foreach (var frontendBindEndPoint in options.FrontendBindEndPoints)
                        {
                            var ipPortProtocal = frontendBindEndPoint.Split(':');
                            var ip             = ipPortProtocal[0];
                            var port           = ipPortProtocal[1];
                            var protocal       = ipPortProtocal[2];

                            switch (protocal.ToLower())
                            {
                            case "pgm":
                                var pgmAddress = string.Format("pgm://;239.192.1.1:{0}", port);
                                Console.WriteLine("I: Connecting to FrontEndPoint {0}...", pgmAddress);
                                frontend.Connect(pgmAddress);
                                break;

                            case "epgm":
                                var epgmAddress = string.Format("epgm://;239.192.1.1:{0}", port);
                                Console.WriteLine("I: Connecting to FrontEndPoint {0}...", epgmAddress);
                                frontend.Connect(epgmAddress);
                                break;

                            default:
                            {
                                string frontHost = string.Format("{0}://{1}:{2}", protocal, ip, port);
                                Console.WriteLine("I: Connecting to FrontEndPoint: {0}", frontHost);
                                frontend.Connect(frontHost);
                            }
                            break;
                            }
                        }


                        // Backend is our public endpoint for subscribers
                        //Proxy 还可以装换协议,把前边的unicast转换为 multicast
                        foreach (var backendBindEndPoint in options.BackendBindEndPoints)
                        {
                            var ipPortProtocal = backendBindEndPoint.Split(':');
                            var ip             = ipPortProtocal[0];
                            var port           = ipPortProtocal[1];
                            var protocal       = ipPortProtocal[2];

                            var tcpAddress = string.Format("tcp://{0}:{1}", ip, port);
                            var pgmAddress = string.Format("pgm://{0};239.192.1.1:{1}", ip, port);

                            var epgmAddress = string.Format("epgm://{0};239.192.1.1:{1}", ip, port);


                            switch (protocal.ToLower())
                            {
                            case "pgm":
                                Console.WriteLine("I: Binding on {0}", epgmAddress);
                                backend.Bind(epgmAddress);
                                break;

                            case "epgm":
                                Console.WriteLine("I: Binding on {0}", epgmAddress);
                                backend.Bind(epgmAddress);
                                break;

                            case "tcp":
                            {
                                Console.WriteLine("I: Binding on {0}", tcpAddress);
                                backend.Bind(tcpAddress);
                            }
                            break;

                            default:
                            {
                                Console.WriteLine("I: Binding on {0}", epgmAddress);
                                backend.Bind(epgmAddress);

                                Console.WriteLine("I: Binding on {0}", tcpAddress);
                                backend.Bind(tcpAddress);
                                break;
                            }
                            }
                        }

                        using (var newFrame = ZFrame.Create(1))
                        {
                            newFrame.Write(new byte[] { 0x1 }, 0, 1);

                            backend.Send(newFrame);
                        }

                        //here, it's more like a bridge
                        // Run the proxy until the user interrupts us
                        ZContext.Proxy(frontend, backend);
                    }
        }