public bool Equals(Endpoint endpoint) { if (string.IsNullOrEmpty(this.ID)) return false; if (string.IsNullOrEmpty(endpoint.ID)) return false; return this.ID.Equals(endpoint.ID); }
private EndpointSession GetSession(Endpoint endpoint) { switch (endpoint.Type) { case Endpoint.BROKER: { EndpointSession session = null; if (!sessions.TryGetValue(endpoint, out session)) { lock (syncRoot) { if (!sessions.TryGetValue(endpoint, out session)) { session = CreateSession(endpoint); sessions[endpoint] = session; } } } return session; } default: throw new ArgumentException(string.Format("Unknow endpoint type: {0}", endpoint.Type)); } }
public void WriteCommand(Endpoint endpoint, ICommand command, int timeoutInMills) { GetSession(endpoint).Write(command, timeoutInMills); }
public void WriteCommand(Endpoint endpoint, ICommand command) { WriteCommand(endpoint, command, config.EndpointSessionDefaultWrtieTimeoutInMills); }
private Bootstrap CreateBootstrap(Endpoint endpoint, EndpointSession endpointSession) { return new Bootstrap() .Option(SessionOption.SO_KEEPALIVE, true) .Option(SessionOption.CONNECT_TIMEOUT_MILLIS, 5000) .Option(SessionOption.TCP_NODELAY, true) .Option(SessionOption.SO_SNDBUF, config.SendBufferSize) .Option(SessionOption.SO_RCVBUF, config.ReceiveBufferSize) .Option(SessionOption.ANY_IDLE_TIME, config.EndpointSessionMaxIdleTime) .OnSessionDestroyed((session) => this.RemoveSession(endpoint, endpointSession)) .Handler(chain => { chain.AddLast( new ExceptionHandler(), new MagicNumberPrepender(), new LengthFieldPrepender(4), new ProtocolCodecFilter(new CommandCodecFactory())); }) .Handler(new DefaultClientChannelInboundHandler(commandProcessorManager, endpoint, endpointSession, this, config)); }
public void RemoveSession(Endpoint endpoint, EndpointSession endpointSession) { EndpointSession removedSession = null; if (Endpoint.BROKER.Equals(endpoint.Type) && sessions.ContainsKey(endpoint)) { lock (syncRoot) { if (sessions.ContainsKey(endpoint)) { EndpointSession tmp = sessions[endpoint]; if (tmp == endpointSession) { sessions.TryRemove(endpoint, out removedSession); } } } } if (removedSession != null) { log.Info(string.Format("Closing idle connection to broker({0}:{1})", endpoint.Host, endpoint.Port)); removedSession.Close(); } }
private void Connect(Endpoint endpoint, EndpointSession endpointSession) { Debug.WriteLine("producer try connect {0}:{1}", endpoint.Host, endpoint.Port); var future = CreateBootstrap(endpoint, endpointSession) .Connect(new IPEndPoint(IPAddress.Parse(endpoint.Host), endpoint.Port)); future.Complete += (s, e) => { var connectFuture = e.Future as DefaultConnectFuture; if (!endpointSession.IsClosed) { if (!connectFuture.Connected) { Log.Error(string.Format("producer try connect failed at {0}:{1}", endpoint.Host, endpoint.Port)); if (metaService.ContainsEndpoint(endpoint)) { endpointSession.SetSessionFuture(null); System.Threading.Thread.Sleep(config.EndpointSessionAutoReconnectDelay * 1000); Debug.WriteLine("producer try connect failed at {0}:{1}", endpoint.Host, endpoint.Port); Connect(endpoint, endpointSession); } } else { Log.Info(String.Format("Connected to broker {0}", connectFuture.Session.RemoteEndPoint.ToString())); endpointSession.SetSessionFuture(connectFuture); } } else { if (connectFuture.Connected) future.Session.Close(true); } }; }
private EndpointSession CreateSession(Endpoint endpoint) { var session = new EndpointSession(this); Connect(endpoint, session); return session; }
public Meta addEndpoint (Endpoint endpoint) { Endpoints.Add (endpoint.ID, endpoint); return this; }
private void PullMessages(Endpoint endpoint, int timeout, BasePullMessagesTask task) { SettableFuture<PullMessageResultCommandV2> future = SettableFuture<PullMessageResultCommandV2>.Create(); PullMessageCommandV2 cmd = task.CreatePullMessageCommand(timeout); cmd.Header.CorrelationId = task.CorrelationId; cmd.setFuture(future); PullMessageResultCommandV2 ack = null; try { PullMessageResultMonitor.Monitor(cmd); EndpointClient.WriteCommand(endpoint, cmd, timeout); try { ack = future.Get(timeout); } catch { } finally { PullMessageResultMonitor.Remove(cmd); } if (ack != null) { AppendMsgToQueue(ack, task.CorrelationId); task.ResultReceived(ack); } } finally { if (ack != null) { ack.Release(); } } }