添加 PING PONG 模型

This commit is contained in:
2026-01-13 23:14:43 +08:00
parent 99e8356db4
commit 5c2a7fc03b
3 changed files with 213 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
package cc.winboll.studio.authcenterapp.auth;
import cc.winboll.studio.authcenterapp.models.PingPongModel;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2026/01/13 22:51
* @LastEditTime 2026/01/14 07:51
* @Describe APP端PING服务器
*/
public class PING {
// 类属性置顶,按常量、静态变量、单例实例排序
public static final String TAG = "PING";
private static volatile PING sInstance;
private volatile boolean isPinging = false;
// 私有构造器,防止外部实例化
private PING() {}
// 双重校验锁单例Java7兼容线程安全
public static PING getInstance() {
if (sInstance == null) {
synchronized (PING.class) {
if (sInstance == null) {
sInstance = new PING();
LogUtils.d(TAG, "PING 单例初始化完成");
}
}
}
return sInstance;
}
// 核心业务方法,同步锁保证线程安全,完善调试日志
public PingPongModel pong(PingPongModel pingPongModel) {
// 打印传入参数调试信息
LogUtils.d(TAG, "pong() 函数调用,传入参数:" + (pingPongModel == null ? "null" : pingPongModel.toString()));
if (!isPinging) {
isPinging = true;
try {
LogUtils.d(TAG, "开始模拟服务器响应休眠1000ms");
Thread.sleep(1000);
// 模拟服务器返回信息
PingPongModel returnPingPongModel = new PingPongModel("You are wellcome!");
LogUtils.d(TAG, "模拟响应完成,返回数据:" + returnPingPongModel.toString());
isPinging = false;
return returnPingPongModel;
} catch (InterruptedException e) {
LogUtils.d(TAG, "线程休眠被中断", e);
isPinging = false;
}
} else {
LogUtils.d(TAG, "当前正在PING中拒绝重复调用返回null");
}
return null;
}
}

View File

@@ -0,0 +1,59 @@
package cc.winboll.studio.authcenterapp.auth;
import cc.winboll.studio.authcenterapp.models.PingPongModel;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2026/01/13 22:51
* @LastEditTime 2026/01/14 08:01:26
* @Describe 服务器PONG回复APP
*/
public class PONG {
// 排序:常量 → 静态单例实例 → 成员变量,符合规范
public static final String TAG = "PONG";
private static volatile PONG sInstance;
private volatile boolean isPonging = false;
// 私有构造器,禁止外部实例化,保障单例唯一性
private PONG() {}
// 双重校验锁单例Java7兼容线程安全且高效适配API30无兼容问题
public static PONG getInstance() {
if (sInstance == null) {
synchronized (PONG.class) {
if (sInstance == null) {
sInstance = new PONG();
LogUtils.d(TAG, "PONG 单例初始化完成");
}
}
}
return sInstance;
}
// 核心业务方法,完善调试日志,精简冗余信息
public PingPongModel pong(PingPongModel pingPongModel) {
// 打印函数调用及入参信息,关键调试点留存
LogUtils.d(TAG, "pong() 函数调用,传入参数:" + (pingPongModel == null ? "null" : pingPongModel.toString()));
if (!isPonging) {
isPonging = true;
try {
LogUtils.d(TAG, "开始模拟服务器PONG响应休眠1000ms");
Thread.sleep(1000);
// 模拟服务器返回数据
PingPongModel returnPingPongModel = new PingPongModel("Hello, WinBoLL!");
LogUtils.d(TAG, "PONG响应模拟完成返回数据" + returnPingPongModel.toString());
isPonging = false;
return returnPingPongModel;
} catch (InterruptedException e) {
LogUtils.d(TAG, "线程休眠被中断重置pong状态", e);
isPonging = false;
}
} else {
LogUtils.d(TAG, "当前正处于PONG响应中拒绝重复调用返回null");
}
return null;
}
}

View File

@@ -0,0 +1,95 @@
package cc.winboll.studio.authcenterapp.models;
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import cc.winboll.studio.libappbase.LogUtils;
import java.io.IOException;
/**
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2026/01/13 22:53:00
* @LastEditTime 2026/01/14 08:10:36
* @Describe PingPongModel 消息模型,存储授权令牌与创建时间
*/
public class PingPongModel extends BaseBean {
// 常量置顶,属性次之,遵循规范排序
public static final String TAG = "PingPongModel";
private long createDateTime;
private String authToken;
// 构造方法
public PingPongModel(String authToken) {
this.createDateTime = System.currentTimeMillis();
this.authToken = authToken;
LogUtils.d(TAG, "PingPongModel 实例创建authToken" + authToken + ",创建时间戳:" + createDateTime);
}
// Getter & Setter 方法
public void setAuthToken(String authToken) {
LogUtils.d(TAG, "setAuthToken() 调用,传入参数:" + authToken);
this.authToken = authToken;
}
public String getAuthToken() {
return authToken;
}
public void setCreateDateTime(long createDateTime) {
LogUtils.d(TAG, "setCreateDateTime() 调用,传入参数:" + createDateTime);
this.createDateTime = createDateTime;
}
public long getCreateDateTime() {
return createDateTime;
}
// 父类重写方法(按重写优先级排序)
@Override
public String getName() {
String className = PingPongModel.class.getName();
LogUtils.d(TAG, "getName() 调用,返回类名:" + className);
return className;
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
LogUtils.d(TAG, "writeThisToJsonWriter() 函数调用开始写入JSON数据");
super.writeThisToJsonWriter(jsonWriter);
jsonWriter.name("createDateTime").value(getCreateDateTime());
LogUtils.d(TAG, "JSON写入完成createDateTime" + getCreateDateTime());
}
@Override
public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException {
LogUtils.d(TAG, "initObjectsFromJsonReader() 调用传入name" + name);
if (super.initObjectsFromJsonReader(jsonReader, name)) {
LogUtils.d(TAG, "父类已处理字段:" + name);
return true;
}
if ("createDateTime".equals(name)) {
setCreateDateTime(jsonReader.nextLong());
return true;
}
LogUtils.d(TAG, "未匹配处理字段:" + name + "返回false");
return false;
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
LogUtils.d(TAG, "readBeanFromJsonReader() 调用开始解析JSON数据");
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (!initObjectsFromJsonReader(jsonReader, name)) {
LogUtils.d(TAG, "跳过未处理字段:" + name);
jsonReader.skipValue();
}
}
jsonReader.endObject();
LogUtils.d(TAG, "JSON解析完成当前实例createDateTime" + getCreateDateTime() + "authToken" + getAuthToken());
return this;
}
}