-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a36c73
commit 990d4c5
Showing
24 changed files
with
752 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 13 additions & 40 deletions
53
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,22 @@ | ||
package com.crossoverjie.cim.client.sdk; | ||
|
||
import com.crossoverjie.cim.client.sdk.logger.Printer; | ||
import com.crossoverjie.cim.client.sdk.vo.req.LoginReqVO; | ||
import com.crossoverjie.cim.client.sdk.vo.res.ServerResVO; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import com.crossoverjie.cim.client.sdk.impl.ClientBuilderImpl; | ||
import java.io.Closeable; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Data | ||
@Builder | ||
@Slf4j | ||
public class Client { | ||
public interface Client extends Closeable { | ||
|
||
private long userId; | ||
private String userName; | ||
|
||
private String routeUrl; | ||
|
||
private int loginFailNumConf; | ||
|
||
private int errorCount; | ||
|
||
private Printer logger; | ||
static ClientBuilder builder() { | ||
return new ClientBuilderImpl(); | ||
} | ||
|
||
void send(String msg) throws Exception; | ||
|
||
private ServerResVO.ServerInfo userLogin() { | ||
LoginReqVO loginReqVO = new LoginReqVO(userId, userName); | ||
ServerResVO.ServerInfo cimServer = null; | ||
try { | ||
// cimServer = routeRequest.getCIMServer(loginReqVO); | ||
// | ||
// //保存系统信息 | ||
// clientInfo.saveServiceInfo(cimServer.getIp() + ":" + cimServer.getCimServerPort()) | ||
// .saveUserInfo(userId, userName); | ||
// TODO: 2024/9/12 messageId | ||
CompletableFuture<Void> sendAync(String msg); | ||
|
||
// log.info("cimServer=[{}]", cimServer.toString()); | ||
} catch (Exception e) { | ||
errorCount++; | ||
ClientState.State getState(); | ||
|
||
if (errorCount >= loginFailNumConf) { | ||
logger.info("The maximum number of reconnections has been reached[{}]times, close cim client!", errorCount); | ||
// todo shutdown | ||
} | ||
log.error("login fail", e); | ||
} | ||
return cimServer; | ||
} | ||
Long getUserId(); | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/ClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.crossoverjie.cim.client.sdk; | ||
|
||
import com.crossoverjie.cim.client.sdk.io.MessageListener; | ||
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import okhttp3.OkHttpClient; | ||
|
||
public interface ClientBuilder { | ||
|
||
Client build(); | ||
|
||
ClientBuilder userId(Long userId); | ||
ClientBuilder userName(String userName); | ||
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); | ||
} |
24 changes: 24 additions & 0 deletions
24
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/ClientState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.crossoverjie.cim.client.sdk; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public abstract class ClientState { | ||
|
||
private static final AtomicReference<State> STATE = new AtomicReference<>(State.Initialized); | ||
|
||
public enum State { | ||
Initialized, // Client is created | ||
Connecting, // Client connecting to server | ||
Ready, | ||
Closed, | ||
Failed, | ||
}; | ||
|
||
public void setState(State s) { | ||
STATE.set(s); | ||
} | ||
|
||
public State getState() { | ||
return STATE.get(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/ConnectionState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.crossoverjie.cim.client.sdk; | ||
|
||
public class ConnectionState { | ||
private static volatile boolean reConnect = false; | ||
|
||
public static void setReConnect(boolean reConnect) { | ||
ConnectionState.reConnect = reConnect; | ||
} | ||
public static boolean getReConnect() { | ||
return ConnectionState.reConnect; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/Event.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.crossoverjie.cim.client.sdk; | ||
|
||
public interface Event { | ||
void debug(String msg, Object... replace); | ||
void info(String msg, Object... replace); | ||
void warn(String msg, Object... replace); | ||
void error(String msg, Object... replace); | ||
void fatal(Client client); | ||
|
||
class DefaultEvent implements Event { | ||
@Override | ||
public void debug(String msg, Object... replace) { | ||
System.out.println(msg); | ||
} | ||
|
||
@Override | ||
public void info(String msg, Object... replace) { | ||
System.out.println(msg); | ||
} | ||
|
||
@Override | ||
public void warn(String msg, Object... replace) { | ||
System.out.println(msg); | ||
} | ||
|
||
@Override | ||
public void error(String msg, Object... replace) { | ||
System.err.println(msg); | ||
} | ||
|
||
@Override | ||
public void fatal(Client client) { | ||
|
||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/RouteLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.crossoverjie.cim.client.sdk; | ||
|
||
import com.crossoverjie.cim.common.core.proxy.RpcProxyManager; | ||
import com.crossoverjie.cim.common.enums.StatusEnum; | ||
import com.crossoverjie.cim.common.exception.CIMException; | ||
import com.crossoverjie.cim.common.res.BaseResponse; | ||
import com.crossoverjie.cim.route.api.RouteApi; | ||
import com.crossoverjie.cim.route.api.vo.req.LoginReqVO; | ||
import com.crossoverjie.cim.route.api.vo.res.CIMServerResVO; | ||
import okhttp3.OkHttpClient; | ||
|
||
public class RouteLookup { | ||
|
||
|
||
private final RouteApi routeApi; | ||
private final Event event; | ||
|
||
public RouteLookup(String routeUrl, OkHttpClient okHttpClient, Event event) { | ||
routeApi = RpcProxyManager.create(RouteApi.class, routeUrl, okHttpClient); | ||
this.event = event; | ||
} | ||
|
||
public CIMServerResVO getServer(LoginReqVO loginReqVO) throws Exception { | ||
BaseResponse<CIMServerResVO> cimServerResVO = routeApi.login(loginReqVO); | ||
|
||
// repeat fail | ||
if (!cimServerResVO.getCode().equals(StatusEnum.SUCCESS.getCode())) { | ||
event.info(cimServerResVO.getMessage()); | ||
|
||
// when client in reConnect state, could not exit. | ||
if (ConnectionState.getReConnect()) { | ||
event.warn("###{}###", StatusEnum.RECONNECT_FAIL.getMessage()); | ||
throw new CIMException(StatusEnum.RECONNECT_FAIL); | ||
} | ||
} | ||
|
||
|
||
return cimServerResVO.getDataBody(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
cim-client-sdk/src/main/java/com/crossoverjie/cim/client/sdk/impl/ClientBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.crossoverjie.cim.client.sdk.impl; | ||
|
||
import com.crossoverjie.cim.client.sdk.Client; | ||
import com.crossoverjie.cim.client.sdk.ClientBuilder; | ||
import com.crossoverjie.cim.client.sdk.Event; | ||
import com.crossoverjie.cim.client.sdk.io.MessageListener; | ||
import com.crossoverjie.cim.client.sdk.io.ReconnectCheck; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import okhttp3.OkHttpClient; | ||
|
||
public class ClientBuilderImpl implements ClientBuilder { | ||
|
||
|
||
private ClientConfigurationData conf; | ||
|
||
public ClientBuilderImpl() { | ||
this(new ClientConfigurationData()); | ||
} | ||
public ClientBuilderImpl(ClientConfigurationData conf) { | ||
this.conf = conf; | ||
} | ||
|
||
@Override | ||
public Client build() { | ||
return new ClientImpl(conf); | ||
} | ||
|
||
@Override | ||
public ClientBuilder userId(Long userId) { | ||
this.conf.setUserId(userId); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder userName(String userName) { | ||
this.conf.setUserName(userName); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder routeUrl(String routeUrl) { | ||
this.conf.setRouteUrl(routeUrl); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder loginRetryCount(int loginRetryCount) { | ||
this.conf.setLoginRetryCount(loginRetryCount); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder event(Event event) { | ||
this.conf.setEvent(event); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder reconnectCheck(ReconnectCheck reconnectCheck) { | ||
this.conf.setReconnectCheck(reconnectCheck); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder okHttpClient(OkHttpClient okHttpClient) { | ||
this.conf.setOkHttpClient(okHttpClient); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder messageListener(MessageListener messageListener) { | ||
this.conf.setMessageListener(messageListener); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ClientBuilder callbackThreadPool(ThreadPoolExecutor callbackThreadPool) { | ||
this.conf.setCallbackThreadPool(callbackThreadPool); | ||
return this; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...lient-sdk/src/main/java/com/crossoverjie/cim/client/sdk/impl/ClientConfigurationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.crossoverjie.cim.client.sdk.impl; | ||
|
||
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.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import okhttp3.OkHttpClient; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class ClientConfigurationData { | ||
|
||
private long userId; | ||
private String userName; | ||
|
||
private String routeUrl; | ||
private int loginRetryCount = 5; | ||
|
||
@JsonIgnore | ||
private Event event = new Event.DefaultEvent(); | ||
|
||
@JsonIgnore | ||
private MessageListener messageListener = | ||
(client, msg) -> System.out.printf("id:[%s] msg:[%s]%n \n", client.getUserId(), msg); | ||
|
||
@JsonIgnore | ||
private OkHttpClient okHttpClient = new OkHttpClient(); | ||
|
||
@JsonIgnore | ||
private ThreadPoolExecutor callbackThreadPool; | ||
|
||
@JsonIgnore | ||
private ReconnectCheck reconnectCheck = (client) -> true; | ||
} |
Oops, something went wrong.