Skip to content

Commit

Permalink
use cim-client-sdk implement client
Browse files Browse the repository at this point in the history
  • Loading branch information
crossoverJie committed Sep 14, 2024
1 parent c98d904 commit 05ffa2d
Show file tree
Hide file tree
Showing 60 changed files with 442 additions and 1,638 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.crossoverjie.cim.client.sdk;

import com.crossoverjie.cim.client.sdk.impl.ClientBuilderImpl;
import com.crossoverjie.cim.client.sdk.impl.ClientConfigurationData;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.route.api.vo.req.P2PReqVO;
import com.crossoverjie.cim.route.api.vo.res.CIMServerResVO;
Expand Down Expand Up @@ -29,7 +30,7 @@ default void sendGroup(String msg) throws Exception{

ClientState.State getState();

Long getUserId();
ClientConfigurationData.Auth getAuth();

Set<CIMUserInfo> getOnlineUser() throws Exception;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.crossoverjie.cim.client.sdk;

import com.crossoverjie.cim.client.sdk.impl.ClientConfigurationData;
import com.crossoverjie.cim.client.sdk.io.MessageListener;
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck;
import java.util.concurrent.ThreadPoolExecutor;
import okhttp3.OkHttpClient;

/**
* @author crossoverJie
*/
public interface ClientBuilder {

Client build();

ClientBuilder userId(Long userId);
ClientBuilder userName(String userName);
ClientBuilder auth(ClientConfigurationData.Auth auth);
ClientBuilder routeUrl(String routeUrl);
ClientBuilder loginRetryCount(int loginRetryCount);
ClientBuilder event(Event event);
ClientBuilder reconnectCheck(ReconnectCheck reconnectCheck);

ClientBuilder okHttpClient(OkHttpClient okHttpClient);
ClientBuilder messageListener(MessageListener messageListener);
ClientBuilder callbackThreadPool(ThreadPoolExecutor callbackThreadPool);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public CompletableFuture<Void> sendGroupMsg(ChatReqVO chatReqVO) {
});
}

public void offLine(Long userId) throws Exception {
public void offLine(Long userId) {
ChatReqVO vo = new ChatReqVO(userId, "offLine");
routeApi.offLine(vo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.crossoverjie.cim.client.sdk.Event;
import com.crossoverjie.cim.client.sdk.io.MessageListener;
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck;
import com.crossoverjie.cim.common.util.StringUtil;
import java.util.concurrent.ThreadPoolExecutor;
import okhttp3.OkHttpClient;

Expand All @@ -26,19 +27,19 @@ public Client build() {
}

@Override
public ClientBuilder userId(Long userId) {
this.conf.setUserId(userId);
return this;
}

@Override
public ClientBuilder userName(String userName) {
this.conf.setUserName(userName);
public ClientBuilder auth(ClientConfigurationData.Auth auth) {
if (auth.getUserId() <= 0 || StringUtil.isEmpty(auth.getUserName())){
throw new IllegalArgumentException("userId and userName must be set");
}
this.conf.setAuth(auth);
return this;
}

@Override
public ClientBuilder routeUrl(String routeUrl) {
if (StringUtil.isEmpty(routeUrl)) {
throw new IllegalArgumentException("routeUrl must be set");
}
this.conf.setRouteUrl(routeUrl);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.concurrent.ThreadPoolExecutor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import okhttp3.OkHttpClient;

Expand All @@ -18,8 +18,15 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class ClientConfigurationData {

private long userId;
private String userName;
private Auth auth;

@Data
@AllArgsConstructor
@Builder
public static class Auth{
private long userId;
private String userName;
}

private String routeUrl;
private int loginRetryCount = 5;
Expand All @@ -29,7 +36,7 @@ public class ClientConfigurationData {

@JsonIgnore
private MessageListener messageListener =
(client, msg) -> System.out.printf("id:[%s] msg:[%s]%n \n", client.getUserId(), msg);
(client, msg) -> System.out.printf("id:[%s] msg:[%s]%n \n", client.getAuth(), msg);

@JsonIgnore
private OkHttpClient okHttpClient = new OkHttpClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.crossoverjie.cim.common.kit.HeartBeatHandler;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.common.protocol.CIMRequestProto;
import com.crossoverjie.cim.common.util.StringUtil;
import com.crossoverjie.cim.route.api.vo.req.ChatReqVO;
import com.crossoverjie.cim.route.api.vo.req.LoginReqVO;
import com.crossoverjie.cim.route.api.vo.req.P2PReqVO;
Expand Down Expand Up @@ -66,13 +65,7 @@ public class ClientImpl extends ClientState implements Client {

public ClientImpl(ClientConfigurationData conf) {
this.conf = conf;
if (this.conf.getUserId() <= 0 || StringUtil.isEmpty(this.conf.getUserName())) {
throw new IllegalArgumentException("userId and userName must be set");
}

if (StringUtil.isEmpty(this.conf.getRouteUrl())) {
throw new IllegalArgumentException("routeUrl must be set");
}
if (this.conf.getCallbackThreadPool() == null) {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(CALLBACK_QUEUE_SIZE);
ThreadFactory factory = new ThreadFactoryBuilder()
Expand All @@ -87,7 +80,7 @@ public ClientImpl(ClientConfigurationData conf) {
routeManager = new RouteManager(conf.getRouteUrl(), conf.getOkHttpClient(), conf.getEvent());

heartBeatPacket = CIMRequestProto.CIMReqProtocol.newBuilder()
.setRequestId(this.conf.getUserId())
.setRequestId(this.conf.getAuth().getUserId())
.setReqMsg("ping")
.setType(Constants.CommandType.PING)
.build();
Expand Down Expand Up @@ -131,7 +124,7 @@ private CompletableFuture<Boolean> doConnectServer() {
this.serverInfo = cimServer;
future.complete(true);
}, () -> {
this.conf.getEvent().error("Login fail!");
this.conf.getEvent().error("Login fail!, cannot get server info!");
this.conf.getEvent().fatal(this);
future.complete(false);
});
Expand All @@ -144,8 +137,8 @@ private CompletableFuture<Boolean> doConnectServer() {
* @return Server info
*/
private Optional<CIMServerResVO> userLogin(CompletableFuture<Boolean> future) {
LoginReqVO loginReqVO = new LoginReqVO(conf.getUserId(),
conf.getUserName());
LoginReqVO loginReqVO = new LoginReqVO(conf.getAuth().getUserId(),
conf.getAuth().getUserName());

CIMServerResVO cimServer = null;
try {
Expand Down Expand Up @@ -182,8 +175,8 @@ private void doConnectServer(CIMServerResVO cimServer, CompletableFuture<Boolean
*/
private void loginServer() {
CIMRequestProto.CIMReqProtocol login = CIMRequestProto.CIMReqProtocol.newBuilder()
.setRequestId(this.conf.getUserId())
.setReqMsg(this.conf.getUserName())
.setRequestId(this.conf.getAuth().getUserId())
.setReqMsg(this.conf.getAuth().getUserName())
.setType(Constants.CommandType.LOGIN)
.build();
channel.writeAndFlush(login)
Expand All @@ -205,7 +198,7 @@ public void reconnect() throws Exception {
}
this.serverInfo = null;
// clear route information.
this.routeManager.offLine(this.getUserId());
this.routeManager.offLine(this.getConf().getAuth().getUserId());

this.conf.getEvent().info("cim trigger reconnecting....");

Expand All @@ -226,24 +219,25 @@ public void close() {
channel.close();
channel = null;
}
this.routeManager.offLine(this.getAuth().getUserId());
}

@Override
public CompletableFuture<Void> sendP2PAsync(P2PReqVO p2PReqVO) {
CompletableFuture<Void> future = new CompletableFuture<>();
p2PReqVO.setUserId(this.conf.getUserId());
p2PReqVO.setUserId(this.conf.getAuth().getUserId());
return routeManager.sendP2P(future, p2PReqVO);
}

@Override
public CompletableFuture<Void> sendGroupAsync(String msg) {
// TODO: 2024/9/12 return messageId
return this.routeManager.sendGroupMsg(new ChatReqVO(this.conf.getUserId(), msg));
return this.routeManager.sendGroupMsg(new ChatReqVO(this.conf.getAuth().getUserId(), msg));
}

@Override
public Long getUserId() {
return this.conf.getUserId();
public ClientConfigurationData.Auth getAuth() {
return this.conf.getAuth();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc

@Override
public void channelActive(ChannelHandlerContext ctx) {
ClientImpl.getClient().getConf().getEvent().info("ChannelActive");
ClientImpl.getClient().getConf().getEvent().debug("ChannelActive");
ClientImpl.getClient().setState(ClientState.State.Ready);
}

Expand Down
Loading

0 comments on commit 05ffa2d

Please sign in to comment.