wildfire chat server source initial submit

This commit is contained in:
heavyrain2012
2019-03-16 17:04:23 +08:00
parent e59217bd10
commit cab3ca0916
1466 changed files with 528862 additions and 15919 deletions
@@ -0,0 +1,49 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.push;
public class PushMessage {
public String sender;
public String senderName;
public int convType;
public String target;
public String targetName;
public int line;
public int cntType;
public long serverTime;
//消息的类型,普通消息通知栏;voip要透传。
public int pushMessageType;
//推送类型,android推送分为小米/华为/魅族等。ios分别为开发和发布。
public int pushType;
public String pushContent;
public int unReceivedMsg;
public int mentionedType;
public String packageName;
public String deviceToken;
public String voipDeviceToken;
public boolean isHiddenDetail;
public String language;
public PushMessage(String sender, int conversationType, String target, int line, int messageContentType, long serverTime, String senderName, String targetName, int unReceivedMsg, int mentionedType, boolean isHiddenDetail, String language) {
this.sender = sender;
this.convType = conversationType;
this.target = target;
this.senderName = senderName;
this.targetName = targetName;
this.line = line;
this.cntType = messageContentType;
this.serverTime = serverTime;
this.unReceivedMsg = unReceivedMsg;
this.pushMessageType = (cntType == 400 ? PushServer.PushMessageType.PUSH_MESSAGE_TYPE_VOIP_INVITE : (cntType == 402 ? PushServer.PushMessageType.PUSH_MESSAGE_TYPE_VOIP_BYE : PushServer.PushMessageType.PUSH_MESSAGE_TYPE_NORMAL));
this.mentionedType = mentionedType;
this.isHiddenDetail = isHiddenDetail;
this.language = language;
}
}
@@ -0,0 +1,103 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.push;
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.proto.WFCMessage;
import com.google.gson.Gson;
import io.moquette.persistence.MemorySessionStore;
import io.moquette.server.config.IConfig;
import io.moquette.spi.ClientSession;
import io.moquette.spi.ISessionsStore;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import win.liyufan.im.HttpUtils;
import win.liyufan.im.Utility;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static io.moquette.BrokerConstants.PUSH_ANDROID_SERVER_ADDRESS;
import static io.moquette.BrokerConstants.PUSH_IOS_SERVER_ADDRESS;
public class PushServer {
private static final Logger LOG = LoggerFactory.getLogger(PushServer.class);
public interface PushMessageType {
int PUSH_MESSAGE_TYPE_NORMAL = 0;
int PUSH_MESSAGE_TYPE_VOIP_INVITE = 1;
int PUSH_MESSAGE_TYPE_VOIP_BYE = 2;
}
private static PushServer INSTANCE = new PushServer();
private ISessionsStore sessionsStore;
private static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*5);
private String androidPushServerUrl;
private String iOSPushServerUrl;
private PushServer() {
}
public static PushServer getServer() {
return INSTANCE;
}
public void init(IConfig config, ISessionsStore sessionsStore) {
this.sessionsStore = sessionsStore;
this.androidPushServerUrl = config.getProperty(PUSH_ANDROID_SERVER_ADDRESS);
this.iOSPushServerUrl = config.getProperty(PUSH_IOS_SERVER_ADDRESS);
}
public void pushMessage(PushMessage pushMessage, String deviceId, String pushContent) {
LOG.info("try to delivery push diviceId = {}, pushContent", deviceId, pushContent);
executorService.execute(() ->{
try {
pushMessageInternel(pushMessage, deviceId, pushContent);
} catch (Exception e) {
e.printStackTrace();
Utility.printExecption(LOG, e);
}
});
}
private void pushMessageInternel(PushMessage pushMessage, String deviceId, String pushContent) {
if (pushMessage.pushMessageType == PushMessageType.PUSH_MESSAGE_TYPE_NORMAL && StringUtil.isNullOrEmpty(pushContent)) {
LOG.info("push content empty, deviceId {}", deviceId);
return;
}
MemorySessionStore.Session session = sessionsStore.getSession(deviceId);
int badge = session.getUnReceivedMsgs();
if (badge <= 0) {
badge = 1;
}
if (StringUtil.isNullOrEmpty(session.getDeviceToken())) {
LOG.warn("Device token is empty for device {}", deviceId);
return;
}
pushMessage.packageName = session.getAppName();
pushMessage.pushType = session.getPushType();
pushMessage.pushContent = pushContent;
pushMessage.deviceToken = session.getDeviceToken();
pushMessage.unReceivedMsg = badge;
if (session.getPlatform() == ProtoConstants.Platform.Platform_iOS || session.getPlatform() == ProtoConstants.Platform.Platform_Android) {
String url = androidPushServerUrl;
if (session.getPlatform() == ProtoConstants.Platform.Platform_iOS) {
url = iOSPushServerUrl;
pushMessage.voipDeviceToken = session.getVoipDeviceToken();
}
HttpUtils.httpJsonPost(url, new Gson().toJson(pushMessage, pushMessage.getClass()));
} else {
LOG.info("Not mobile platform {}", session.getPlatform());
}
}
}
@@ -0,0 +1,34 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.server;
import win.liyufan.im.Utility;
import java.util.Base64;
public class MechineCode {
public static void main(String[] args) throws Exception {
for (String arg: args
) {
System.out.println(arg);
}
if (args.length != 2) {
System.out.println("Usage: ip port");
System.exit(-1);
}
String mac = Utility.getMacAddress();
mac += "|";
mac += args[0];
mac += "|";
mac += args[1];
mac = Base64.getEncoder().encodeToString(mac.getBytes());
System.out.println(mac);
}
}
@@ -0,0 +1,17 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.server;
import java.io.IOException;
public class Server {
public static void main(String[] args) throws IOException {
io.moquette.server.Server.start(args);
}
}
@@ -0,0 +1,48 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package cn.wildfirechat.server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadPoolExecutorWrapper {
private static final Logger LOG = LoggerFactory.getLogger(ThreadPoolExecutorWrapper.class);
private final ScheduledExecutorService executor;
private final int count;
private final AtomicInteger runCounter;
private final String name;
public ThreadPoolExecutorWrapper(ScheduledExecutorService executor, int count, String name) {
this.executor = executor;
this.count = count;
this.runCounter = new AtomicInteger();
this.name = name;
}
public void execute(Runnable task) {
int startCount = runCounter.incrementAndGet();
LOG.info("Submit task and current taskcount {}", startCount);
final long startTime = System.currentTimeMillis();
executor.execute(() -> {
try {
task.run();
} finally {
int endCount = runCounter.decrementAndGet();
LOG.info("Finish task and current taskcount {} use time {}", endCount, System.currentTimeMillis()-startTime);
}
});
}
public void shutdown() {
executor.shutdown();
}
}