org.apache.reef.wake.remote.transport.LinkListener Java Examples

The following examples show how to use org.apache.reef.wake.remote.transport.LinkListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NetworkConnectionServiceImpl.java    From reef with Apache License 2.0 6 votes vote down vote up
@Override
public <T> ConnectionFactory<T> registerConnectionFactory(
    final Identifier connectionFactoryId,
    final Codec<T> codec,
    final EventHandler<Message<T>> eventHandler,
    final LinkListener<Message<T>> linkListener,
    final Identifier localEndPointId) {
  final String id = connectionFactoryId.toString();
  checkBeforeRegistration(id);

  final NetworkConnectionFactory<T> connectionFactory = new NetworkConnectionFactory<>(
      this, connectionFactoryId, codec, eventHandler, linkListener, localEndPointId);
  final Identifier localId = getEndPointIdWithConnectionFactoryId(connectionFactoryId, localEndPointId);
  nameServiceRegisteringStage.onNext(new Tuple<>(localId, (InetSocketAddress) transport.getLocalAddress()));

  if (connFactoryMap.putIfAbsent(id, connectionFactory) != null) {
    throw new NetworkRuntimeException("ConnectionFactory " + connectionFactoryId + " was already registered.");
  }

  LOG.log(Level.INFO, "ConnectionFactory {0} was registered", id);

  return connectionFactory;
}
 
Example #2
Source File: NetworkConnectionFactory.java    From reef with Apache License 2.0 5 votes vote down vote up
NetworkConnectionFactory(
    final NetworkConnectionServiceImpl networkService,
    final Identifier connectionFactoryId,
    final Codec<T> eventCodec,
    final EventHandler<Message<T>> eventHandler,
    final LinkListener<Message<T>> eventListener,
    final Identifier localEndPointId) {
  this.networkService = networkService;
  this.connectionMap = new ConcurrentHashMap<>();
  this.connectionFactoryId = connectionFactoryId;
  this.eventCodec = eventCodec;
  this.eventHandler = eventHandler;
  this.eventListener = eventListener;
  this.localEndPointId = localEndPointId;
}
 
Example #3
Source File: NSConnection.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a connection.
 *
 * @param srcId    a source identifier
 * @param destId   a destination identifier
 * @param listener a link listener
 * @param service  a network service
 */
NSConnection(final Identifier srcId, final Identifier destId,
             final LinkListener<T> listener, final NetworkService<T> service) {
  this.srcId = srcId;
  this.destId = destId;
  this.listener = new NSMessageLinkListener<>(listener);
  this.service = service;
  this.codec = new NSMessageCodec<>(service.getCodec(), service.getIdentifierFactory());
}
 
Example #4
Source File: NetworkConnectionServiceLinkListener.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onSuccess(final NetworkConnectionServiceMessage message) {
  final LinkListener listener = connFactoryMap.get(message.getConnectionFactoryId()).getLinkListener();
  if (listener != null) {
    listener.onSuccess(message);
  }

}
 
Example #5
Source File: NetworkConnectionServiceLinkListener.java    From reef with Apache License 2.0 5 votes vote down vote up
@Override
public void onException(final Throwable cause, final SocketAddress remoteAddress,
                        final NetworkConnectionServiceMessage message) {
  final LinkListener listener = connFactoryMap.get(message.getConnectionFactoryId()).getLinkListener();
  if (listener != null) {
    listener.onException(cause, remoteAddress, message);
  }
}
 
Example #6
Source File: NetworkConnectionFactory.java    From reef with Apache License 2.0 4 votes vote down vote up
LinkListener<Message<T>> getLinkListener() {
  return eventListener;
}
 
Example #7
Source File: NSConnection.java    From reef with Apache License 2.0 4 votes vote down vote up
NSMessageLinkListener(final LinkListener<T> listener) {
}
 
Example #8
Source File: NettyLink.java    From reef with Apache License 2.0 4 votes vote down vote up
NettyChannelFutureListener(final T message, final LinkListener<T> listener) {
  this.message = message;
  this.listener = listener;
}
 
Example #9
Source File: NettyMessagingTransport.java    From reef with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a link for the remote address if cached; otherwise opens, caches and returns.
 * When it opens a link for the remote address, only one attempt for the address is made at a given time
 *
 * @param remoteAddr the remote socket address
 * @param encoder    the encoder
 * @param listener   the link listener
 * @return a link associated with the address
 */
@Override
public <T> Link<T> open(final SocketAddress remoteAddr, final Encoder<? super T> encoder,
                        final LinkListener<? super T> listener) throws IOException {

  Link<T> link = null;

  for (int i = 0; i <= this.numberOfTries; ++i) {
    LinkReference linkRef = this.addrToLinkRefMap.get(remoteAddr);

    if (linkRef != null) {
      link = (Link<T>) linkRef.getLink();
      if (LOG.isLoggable(Level.FINE)) {
        LOG.log(Level.FINE, "Link {0} for {1} found", new Object[]{link, remoteAddr});
      }
      if (link != null) {
        return link;
      }
    }

    if (i == this.numberOfTries) {
      // Connection failure
      throw new ConnectException("Connection to " + remoteAddr + " refused");
    }

    LOG.log(Level.FINE, "No cached link for {0} thread {1}",
        new Object[]{remoteAddr, Thread.currentThread()});

    // no linkRef
    final LinkReference newLinkRef = new LinkReference();
    final LinkReference prior = this.addrToLinkRefMap.putIfAbsent(remoteAddr, newLinkRef);
    final AtomicInteger flag = prior != null ?
        prior.getConnectInProgress() : newLinkRef.getConnectInProgress();

    synchronized (flag) {
      if (!flag.compareAndSet(0, 1)) {
        while (flag.get() == 1) {
          try {
            flag.wait();
          } catch (final InterruptedException ex) {
            LOG.log(Level.WARNING, "Wait interrupted", ex);
          }
        }
      }
    }

    linkRef = this.addrToLinkRefMap.get(remoteAddr);
    link = (Link<T>) linkRef.getLink();

    if (link != null) {
      return link;
    }

    ChannelFuture connectFuture = null;
    try {
      connectFuture = this.clientBootstrap.connect(remoteAddr);
      connectFuture.syncUninterruptibly();

      link = new NettyLink<>(connectFuture.channel(), encoder, listener);
      linkRef.setLink(link);

      synchronized (flag) {
        flag.compareAndSet(1, 2);
        flag.notifyAll();
      }
      break;
    } catch (final Exception e) {
      if (e instanceof ConnectException) {
        LOG.log(Level.WARNING, "Connection refused. Retry {0} of {1}",
            new Object[]{i + 1, this.numberOfTries});
        synchronized (flag) {
          flag.compareAndSet(1, 0);
          flag.notifyAll();
        }

        if (i < this.numberOfTries) {
          try {
            Thread.sleep(retryTimeout);
          } catch (final InterruptedException interrupt) {
            LOG.log(Level.WARNING, "Thread {0} interrupted while sleeping", Thread.currentThread());
          }
        }
      } else {
        throw e;
      }
    }
  }

  return link;
}
 
Example #10
Source File: NetworkConnectionService.java    From reef with Apache License 2.0 2 votes vote down vote up
/**
 * Registers an instance of ConnectionFactory corresponding to the connectionFactoryId.
 * Binds Codec, EventHandler, LinkListener and localEndPointId to the ConnectionFactory.
 * ConnectionFactory can create multiple connections between other NetworkConnectionServices.
 * The connectionFactoryId is used to distinguish the type of connection and the localEndPointId
 * is the contact point, which is registered to NameServer through this method.
 *
 * @param connectionFactoryId a connection factory id
 * @param codec a codec for type T
 * @param eventHandler an event handler for type T
 * @param linkListener a link listener
 * @param localEndPointId a local end point id
 * @return the registered connection factory
 */
<T> ConnectionFactory<T> registerConnectionFactory(Identifier connectionFactoryId,
                                   Codec<T> codec,
                                   EventHandler<Message<T>> eventHandler,
                                   LinkListener<Message<T>> linkListener,
                                   Identifier localEndPointId);
 
Example #11
Source File: NettyLink.java    From reef with Apache License 2.0 2 votes vote down vote up
/**
 * Constructs a link.
 *
 * @param channel  the channel
 * @param encoder  the encoder
 * @param listener the link listener
 */
public NettyLink(final Channel channel, final Encoder<? super T> encoder, final LinkListener<? super T> listener) {
  this.channel = channel;
  this.encoder = encoder;
  this.listener = listener;
}