All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.agora.rtm.internal.StreamChannelImpl Maven / Gradle / Ivy

package io.agora.rtm.internal;

import io.agora.rtm.ErrorInfo;
import io.agora.rtm.JoinChannelOptions;
import io.agora.rtm.JoinTopicOptions;
import io.agora.rtm.PublishOptions;
import io.agora.rtm.ResultCallback;
import io.agora.rtm.RtmClient;
import io.agora.rtm.RtmConstants;
import io.agora.rtm.RtmConstants.RtmErrorCode;
import io.agora.rtm.RtmConstants.RtmMessageType;
import io.agora.rtm.StreamChannel;
import io.agora.rtm.SubscribeTopicResult;
import io.agora.rtm.TopicMessageOptions;
import io.agora.rtm.TopicOptions;
import io.agora.rtm.internal.RequestInfo;
import io.agora.rtm.internal.UserList;
import java.util.ArrayList;
import java.util.Arrays;

public class StreamChannelImpl extends StreamChannel {
  private static final String TAG = StreamChannelImpl.class.getSimpleName();

  private long mNativeChannel = 0;
  private RtmClientImpl mRtmClient;

  public StreamChannelImpl(long nativeHandle) {
    mNativeChannel = nativeHandle;
  }

  public synchronized void attach(RtmClient rtmClient) {
    mRtmClient = (RtmClientImpl) rtmClient;
    if (mRtmClient != null) {
      mRtmClient.addChannel(this);
    }
  }

  private synchronized void detach() {
    if (mRtmClient != null) {
      mRtmClient.removeChannel(this);
      // mRtmClient = null;
    }
  }

  @Override
  public synchronized RtmErrorCode release() {
    if (mNativeChannel == 0) {
      return RtmErrorCode.INSTANCE_ALREADY_RELEASED;
    }
    detach();
    int ret = nativeDestroy(mNativeChannel);
    mNativeChannel = 0;
    return RtmErrorCode.getEnum(ret);
  }

  @Override
  public synchronized void join(JoinChannelOptions options, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED, RtmConstants.JOIN_API_STR);
      return;
    }
    if (options == null) {
      options = new JoinChannelOptions();
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeJoin(mNativeChannel, options, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode == RtmErrorCode.OK) {
        mRtmClient.mJoinCallback.put(requestInfo.requestId, resultCallback);
      } else {
        mRtmClient.processFailureCallback(resultCallback, errorCode, RtmConstants.JOIN_API_STR);
      }
    }
  }

  @Override
  public synchronized void renewToken(String token, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED, RtmConstants.RENEW_TOKEN_API_STR);
      return;
    }

    int ret = nativeRenewToken(mNativeChannel, token);
    mRtmClient.processCallback(ret, resultCallback, RtmConstants.RENEW_TOKEN_API_STR);
  }

  @Override
  public synchronized void leave(ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED, RtmConstants.LEAVE_API_STR);
      return;
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      mRtmClient.mLeaveCallback.offerLast(resultCallback);
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeLeave(mNativeChannel, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode != RtmErrorCode.OK) {
        mRtmClient.mLeaveCallback.pollLast();
        mRtmClient.processFailureCallback(resultCallback, errorCode, RtmConstants.LEAVE_API_STR);
      }
    }
  }

  @Override
  public synchronized String getChannelName() {
    if (mNativeChannel == 0) {
      return "";
    }
    return nativeGetChannelName(mNativeChannel);
  }

  @Override
  public synchronized void joinTopic(
      String topicName, JoinTopicOptions options, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED, RtmConstants.JOIN_TOPIC_API_STR);
      return;
    }
    if (options == null) {
      options = new JoinTopicOptions();
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      mRtmClient.mJoinTopicCallback.offerLast(resultCallback);
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeJoinTopic(mNativeChannel, topicName, options, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode != RtmErrorCode.OK) {
        mRtmClient.mJoinTopicCallback.pollLast();
        mRtmClient.processFailureCallback(
            resultCallback, errorCode, RtmConstants.JOIN_TOPIC_API_STR);
      }
    }
  }

  @Override
  public synchronized void publishTopicMessage(String topicName, byte[] message,
      TopicMessageOptions options, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED,
          RtmConstants.PUBLISH_TOPIC_MESSAGE_API_STR);
      return;
    }
    if (message == null) {
      mRtmClient.processFailureCallback(resultCallback, RtmErrorCode.CHANNEL_INVALID_MESSAGE,
          RtmConstants.PUBLISH_TOPIC_MESSAGE_API_STR);
      return;
    }
    if (options == null) {
      options = new TopicMessageOptions();
    }

    int ret = nativePublishTopicBinaryMessage(mNativeChannel, topicName, message, options);
    if (resultCallback == null) {
      return;
    }
    RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
    if (errorCode == RtmErrorCode.OK) {
      resultCallback.onSuccess(null);
    } else {
      mRtmClient.processFailureCallback(
          resultCallback, errorCode, RtmConstants.PUBLISH_TOPIC_MESSAGE_API_STR);
    }
  }

  @Override
  public synchronized void publishTopicMessage(String topicName, String message,
      TopicMessageOptions options, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED,
          RtmConstants.PUBLISH_TOPIC_MESSAGE_API_STR);
      return;
    }
    if (options == null) {
      options = new TopicMessageOptions();
    }

    int ret = nativePublishTopicStringMessage(mNativeChannel, topicName, message, options);
    if (resultCallback == null) {
      return;
    }
    RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
    if (errorCode == RtmErrorCode.OK) {
      resultCallback.onSuccess(null);
    } else {
      mRtmClient.processFailureCallback(
          resultCallback, errorCode, RtmConstants.PUBLISH_TOPIC_MESSAGE_API_STR);
    }
  }

  @Override
  public synchronized void leaveTopic(String topicName, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(
          resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED, RtmConstants.LEAVE_TOPIC_API_STR);
      return;
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      mRtmClient.mLeaveTopicCallback.offerLast(resultCallback);
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeLeaveTopic(mNativeChannel, topicName, requestInfo);
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode != RtmErrorCode.OK) {
        mRtmClient.mLeaveTopicCallback.pollLast();
        mRtmClient.processFailureCallback(
            resultCallback, errorCode, RtmConstants.LEAVE_TOPIC_API_STR);
      }
    }
  }

  @Override
  public synchronized void subscribeTopic(
      String topicName, TopicOptions options, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      if (resultCallback != null) {
        resultCallback.onFailure(new ErrorInfo(RtmErrorCode.INSTANCE_ALREADY_RELEASED,
            mRtmClient.getErrorReason(RtmErrorCode.INSTANCE_ALREADY_RELEASED),
            RtmConstants.SUBSCRIBE_TOPIC_API_STR));
      }
      return;
    }
    if (options == null) {
      options = new TopicOptions();
    }

    synchronized (mRtmClient.mRtmCallbackLock) {
      mRtmClient.mSubscribeTopicCallback.offerLast(resultCallback);
      RequestInfo requestInfo = new RequestInfo();
      int ret = nativeSubscribeTopic(mNativeChannel, topicName, options, requestInfo);
      if (resultCallback == null) {
        return;
      }
      RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
      if (errorCode != RtmErrorCode.OK) {
        mRtmClient.mSubscribeTopicCallback.pollLast();
        resultCallback.onFailure(new ErrorInfo(
            errorCode, mRtmClient.getErrorReason(ret), RtmConstants.SUBSCRIBE_TOPIC_API_STR));
      }
    }
  }

  @Override
  public synchronized void unsubscribeTopic(
      String topicName, TopicOptions options, ResultCallback resultCallback) {
    if (mNativeChannel == 0) {
      mRtmClient.processFailureCallback(resultCallback, RtmErrorCode.INSTANCE_ALREADY_RELEASED,
          RtmConstants.UNSUBSCRIBE_TOPIC_API_STR);
      return;
    }
    if (topicName == null) {
      mRtmClient.processFailureCallback(resultCallback, RtmErrorCode.CHANNEL_INVALID_TOPIC_NAME,
          RtmConstants.UNSUBSCRIBE_TOPIC_API_STR);
      return;
    }
    if (options == null) {
      options = new TopicOptions();
    }

    int ret = nativeUnsubscribeTopic(mNativeChannel, topicName, options);
    mRtmClient.processCallback(ret, resultCallback, RtmConstants.UNSUBSCRIBE_TOPIC_API_STR);
  }

  @Override
  public synchronized void getSubscribedUserList(
      String topicName, ResultCallback> resultCallback) {
    if (resultCallback == null) {
      return;
    }
    if (mNativeChannel == 0) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.INSTANCE_ALREADY_RELEASED,
          mRtmClient.getErrorReason(RtmErrorCode.INSTANCE_ALREADY_RELEASED),
          RtmConstants.GET_SUBSCRIBED_USER_LIST_API_STR));
    }
    if (topicName == null) {
      resultCallback.onFailure(new ErrorInfo(RtmErrorCode.CHANNEL_INVALID_TOPIC_NAME,
          mRtmClient.getErrorReason(RtmErrorCode.CHANNEL_INVALID_TOPIC_NAME),
          RtmConstants.GET_SUBSCRIBED_USER_LIST_API_STR));
    }

    UserList userList = new UserList();
    int ret = nativeGetSubscribedUserList(mNativeChannel, topicName, userList);
    RtmErrorCode errorCode = RtmErrorCode.getEnum(ret);
    if (errorCode == RtmErrorCode.OK) {
      resultCallback.onSuccess(new ArrayList(userList.users));
    } else {
      resultCallback.onFailure(new ErrorInfo(errorCode, mRtmClient.getErrorReason(ret),
          RtmConstants.GET_SUBSCRIBED_USER_LIST_API_STR));
    }
  }

  private static native int nativeDestroy(long handle);

  private native int nativeJoin(
      long nativeStreamChannelAndroid, JoinChannelOptions options, RequestInfo requestInfo);

  private native int nativeLeave(long nativeStreamChannelAndroid, RequestInfo requestInfo);

  private native int nativeRenewToken(long nativeStreamChannelAndroid, String token);

  private native String nativeGetChannelName(long nativeStreamChannelAndroid);

  private native int nativeJoinTopic(long nativeStreamChannelAndroid, String topicName,
      JoinTopicOptions options, RequestInfo requestInfo);

  private native int nativePublishTopicBinaryMessage(long nativeStreamChannelAndroid,
      String topicName, byte[] message, TopicMessageOptions options);

  private native int nativePublishTopicStringMessage(long nativeStreamChannelAndroid,
      String topicName, String message, TopicMessageOptions options);

  private native int nativeLeaveTopic(
      long nativeStreamChannelAndroid, String topicName, RequestInfo requestInfo);

  private native int nativeSubscribeTopic(long nativeStreamChannelAndroid, String topicName,
      TopicOptions options, RequestInfo requestInfo);

  private native int nativeUnsubscribeTopic(
      long nativeStreamChannelAndroid, String topicName, TopicOptions options);

  private native int nativeGetSubscribedUserList(
      long nativeStreamChannelAndroid, String topicName, UserList users);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy