Exemple #1
0
 private void InitBus()
 {
     _contex = new Context(1);
     _subscriberSocket = _contex.Socket(SocketType.SUB);
     _subscriberSocket.Subscribe("",Encoding.Unicode);
     _subscriberSocket.Connect("tcp://127.0.0.1:5556");
 }
        public AnnaHttpServer(string busControllerUrl, string rpcHostUrl, string httpUrl)
        {
            if (string.IsNullOrEmpty(busControllerUrl))
            {
                throw new ArgumentNullException("busControllerUrl");
            }

            if (string.IsNullOrEmpty(rpcHostUrl))
            {
                throw new ArgumentNullException("rpcHostUrl");
            }

            if (string.IsNullOrEmpty(httpUrl))
            {
                throw new ArgumentNullException("httpUrl");
            }

            LoggerProvider.EnvironmentLogger.Info("Starting HTTP Server...");

            broadcastSocket.Connect(busControllerUrl);
            broadcastSocket.Subscribe("STOP-HTTPD", Encoding.UTF8);

            if (!httpUrl.EndsWith("/"))
            {
                httpUrl += '/';
            }

            this.httpHostUrl = httpUrl;
            LoggerProvider.EnvironmentLogger.Info(String.Format("HTTP Listen: [{0}]", httpUrl));

            this.rpcReqUrl = rpcHostUrl;
            LoggerProvider.EnvironmentLogger.Info("Connecting to MQ: [" + this.rpcReqUrl + "]");
        }
Exemple #3
0
        public static void ProcessingLoop()
        {
            if (!SlipstreamEnvironment.Initialized)
            {
                throw new InvalidOperationException(
                          "Unable to start PRC-Handler thread, please initialize the environment first.");
            }

            var broadcastUrl  = SlipstreamEnvironment.Settings.BroadcastUrl;
            var rpcHandlerUrl = SlipstreamEnvironment.Settings.RpcHandlerUrl;
            var id            = Guid.NewGuid();

            LoggerProvider.EnvironmentLogger.Debug(
                () => string.Format("Starting RpcHandler thread[{0}] URL=[{1}] ...", id, rpcHandlerUrl));

            using (var broadcastSocket = new ZMQ.Socket(ZMQ.SocketType.SUB))
                using (var receiver = new ZMQ.Socket(ZMQ.SocketType.REP))
                {
                    broadcastSocket.Connect(broadcastUrl);
                    broadcastSocket.Subscribe("STOP-RPC", Encoding.UTF8);
                    LoggerProvider.EnvironmentLogger.Debug(
                        () => string.Format("RpcHandler thread[{0}] is connected to the Commander URL[{1}]", id, broadcastUrl));

                    receiver.Connect(rpcHandlerUrl);

                    var items = new PollItem[2];
                    items[0] = broadcastSocket.CreatePollItem(IOMultiPlex.POLLIN);
                    items[0].PollInHandler += new PollHandler(BusControllerPollInHandler);

                    items[1] = receiver.CreatePollItem(IOMultiPlex.POLLIN);
                    items[1].PollInHandler += new PollHandler(ReceiverPollInHandler);

                    lock (s_lockObj)
                    {
                        s_running = true;
                    }

                    //  Process messages from both sockets
                    while (s_running)
                    {
                        Context.Poller(items, -1);
                    }

                    LoggerProvider.EnvironmentLogger.Debug(
                        () => string.Format("The RpcHandler thread[{0}] is stopped", id));
                }
        }
Exemple #4
0
        /**
        Send a Pulse message so the registry knows we're alive.
        **/
        private void SendPulse()
        {
            if (_lastPulse.AddSeconds(_pulseTimestep) < DateTime.Now) {
                // Serialize to binary
                MemoryStream stream = new MemoryStream();
                // Write message type
                stream.WriteByte((byte)RegistryMessageType.Pulse);
                // Write service info
                StreamUtil.Write(stream, _applicationID);

                // Send to registry and await results.

                _registryRPCSocket.Send(stream.GetBuffer());
                byte[] message = _registryRPCSocket.Recv(1000);
                if (message != null)
                {
                    MemoryStream reply = StreamUtil.CreateStream(message);
                    bool ack = StreamUtil.Read<bool>(reply);
                    if (!ack)
                    {
                        //The registry server did not recognise our current application ID.
                        //Recieve new application ID.
                        _applicationID = StreamUtil.Read<ulong>(reply);
                        //Re register our current services with the registry.
                        Console.WriteLine("Registry did not recognise our AppID! - Recovering with new appid:" + _applicationID);
                        ReRegisterAll();
                    }
                }
                else
                {
                    {
                        if (!_serverAlternatives.Where(x => (x.ServerID != _serverID)).Any())
                            throw new Exception("Connection with registry failed without any alternitives present.");
                        if (_serverAlternatives.Where(x => (x.ServerID == _serverID)).Any())
                            _serverAlternatives.Remove(_serverAlternatives.Where(x => (x.ServerID == _serverID)).Single());
                        AlternateRegistryServer alt = _serverAlternatives.Where(x => (x.ServerID != _serverID)).First();
                        _serverID = alt.ServerID;
                        _registryRPCAddress = alt.ServerRPCAddress;
                        _registryPublishAddress = alt.ServerPubAddress;
                        _registryRPCSocket = _context.Socket(SocketType.REQ);
                        _registryRPCSocket.Connect(_registryRPCAddress);
                        _registryPublishSocket = _context.Socket(SocketType.SUB);
                        _registryPublishSocket.Connect(_registryPublishAddress);
                        _registryPublishSocket.Subscribe(new byte[0]);
                        Console.WriteLine("Failing registry detected. Transitioned to alternate registry server. New server id: " + _serverID);
                    }
                }
                _lastPulse = DateTime.Now;
            }
        }
Exemple #5
0
 /**
 Connects to the registry server publish socket and subscribes for published messages.
 **/
 private void ConnectRegistryPublish()
 {
     if(_registryPublishSocket != null) return;
     _registryPublishSocket = _context.Socket(SocketType.SUB);
     _registryPublishSocket.Connect(_registryPublishAddress);
     _registryPublishSocket.Subscribe(new byte[0]);
 }
        public void Start()
        {
            context = new Context(1);
            receiver = context.Socket(SocketType.SUB);
            receiver.Bind(ZmqUrl);
            receiver.Subscribe("", Encoding.Unicode);
            Console.WriteLine("The server was started");
            items[0] = receiver.CreatePollItem(IOMultiPlex.POLLIN);
            items[0].PollInHandler += new PollHandler(MessageReceiver_PollInHandler);

            StartSubscriber();
            Thread thr = new Thread(StartReceiver);
            hiloServer = new Thread(new ParameterizedThreadStart(InicioWebserver));
            thr.Start(new StartParameters { context = this.context, pollitems = this.items });
            hiloServer.Start(webport);
        }