001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake.remote;
020
021
022import org.apache.reef.tang.annotations.DefaultImplementation;
023import org.apache.reef.wake.EventHandler;
024import org.apache.reef.wake.Stage;
025import org.apache.reef.wake.remote.impl.DefaultRemoteManagerImplementation;
026
027/**
028 * Represents all remote connections to and from one process to another.
029 */
030@DefaultImplementation(DefaultRemoteManagerImplementation.class)
031public interface RemoteManager extends Stage {
032  /**
033   * Constructor that takes a Codec<T>
034   */
035
036  /**
037   * Returns an event handler that can be used to send messages of type T to the
038   * given destination.
039   *
040   * @param <T>
041   * @param destinationIdentifier a destination identifier
042   * @param messageType           a message class type
043   * @return an event handler
044   */
045  <T> EventHandler<T> getHandler(final RemoteIdentifier destinationIdentifier, final Class<? extends T> messageType);
046
047  /**
048   * Registers the given EventHandler to be invoked when messages of Type T
049   * arrive from sourceIdentifier.
050   * <p/>
051   * Calling this method twice overrides the initial registration.
052   *
053   * @param <T,              U extends T>
054   * @param sourceIdentifier a source identifier
055   * @param messageType      a message class type
056   * @param theHandler       the event handler
057   * @return the subscription that can be used to unsubscribe later
058   */
059  <T, U extends T> AutoCloseable registerHandler(final RemoteIdentifier sourceIdentifier,
060                                                 final Class<U> messageType,
061                                                 final EventHandler<T> theHandler);
062
063  /**
064   * Registers the given EventHandler to be called for the given message type
065   * from any source.
066   * <p/>
067   * If there is an EventHandler registered for this EventType
068   *
069   * @param <T,         U extends T>
070   * @param messageType a message class type
071   * @param theHandler  the event handler
072   * @return the subscription that can be used to unsubscribe later
073   */
074  <T, U extends T> AutoCloseable registerHandler(final Class<U> messageType,
075                                                 final EventHandler<RemoteMessage<T>> theHandler);
076
077  /**
078   * Register an EventHandler that gets called by Wake in the presence of
079   * errors. Note that user-level errors that need to cross the network need
080   * to be handled as standard messages.
081   *
082   * @param theHandler the exception event handler
083   * @return the subscription that can be used to unsubscribe later
084   */
085  @Deprecated
086  AutoCloseable registerErrorHandler(final EventHandler<Exception> theHandler);
087
088  /**
089   * Access the Identifier of this.
090   *
091   * @return the Identifier of this.
092   */
093  RemoteIdentifier getMyIdentifier();
094}