Skip to content

Commit

Permalink
新增支持多端登陆
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Aug 19, 2016
1 parent 7b72092 commit afde33c
Show file tree
Hide file tree
Showing 38 changed files with 639 additions and 292 deletions.
1 change: 1 addition & 0 deletions conf/reference.conf
Expand Up @@ -184,5 +184,6 @@ mp {

spi {
thread-pool-factory:"com.mpush.tools.thread.pool.DefaultThreadPoolFactory"
dns-mapping-manager:"com.mpush.common.net.HttpProxyDnsMappingManager"
}
}
Expand Up @@ -19,6 +19,8 @@

package com.mpush.api.connection;

import com.mpush.api.router.ClientType;

/**
* Created by ohun on 2015/12/22.
*
Expand All @@ -29,8 +31,10 @@ public final class SessionContext {
public String osVersion;
public String clientVersion;
public String deviceId;
public String userId;
public int heartbeat;
public Cipher cipher;
private int clientType;

public void changeCipher(Cipher cipher) {
this.cipher = cipher;
Expand All @@ -56,6 +60,11 @@ public SessionContext setDeviceId(String deviceId) {
return this;
}

public SessionContext setUserId(String userId) {
this.userId = userId;
return this;
}

public void setHeartbeat(int heartbeat) {
this.heartbeat = heartbeat;
}
Expand All @@ -64,6 +73,13 @@ public boolean handshakeOk() {
return deviceId != null && deviceId.length() > 0;
}

public int getClientType() {
if (clientType == 0) {
clientType = ClientType.find(osName).type;
}
return clientType;
}

@Override
public String toString() {
return "SessionContext [osName=" + osName
Expand Down
14 changes: 8 additions & 6 deletions mpush-api/src/main/java/com/mpush/api/push/PushSender.java
Expand Up @@ -19,11 +19,13 @@

package com.mpush.api.push;

import com.mpush.api.router.ClientLocation;
import com.mpush.api.service.Service;
import com.mpush.api.spi.SpiLoader;
import com.mpush.api.spi.client.PusherFactory;

import java.util.Collection;
import java.util.concurrent.FutureTask;

/**
* Created by ohun on 2015/12/30.
Expand All @@ -38,19 +40,19 @@ static PushSender create() {

void send(String content, Collection<String> userIds, Callback callback);

void send(String content, String userId, Callback callback);
FutureTask<Boolean> send(String content, String userId, Callback callback);

void send(byte[] content, Collection<String> userIds, Callback callback);

void send(byte[] content, String userId, Callback callback);
FutureTask<Boolean> send(byte[] content, String userId, Callback callback);

interface Callback {
void onSuccess(String userId);
void onSuccess(String userId, ClientLocation location);

void onFailure(String userId);
void onFailure(String userId, ClientLocation location);

void onOffline(String userId);
void onOffline(String userId, ClientLocation location);

void onTimeout(String userId);
void onTimeout(String userId, ClientLocation location);
}
}
77 changes: 58 additions & 19 deletions mpush-api/src/main/java/com/mpush/api/router/ClientLocation.java
Expand Up @@ -19,6 +19,7 @@

package com.mpush.api.router;

import com.mpush.api.connection.Connection;
import com.mpush.api.connection.SessionContext;

/**
Expand Down Expand Up @@ -48,16 +49,15 @@ public final class ClientLocation {
*/
private String deviceId;

/**
* 链接ID
*/
private String connId;

public String getDeviceId() {
return deviceId;
}

public ClientLocation setDeviceId(String deviceId) {
this.deviceId = deviceId;
return this;
}

/**
* 客户端类型
*/
private transient int clientType;

public String getHost() {
return host;
Expand All @@ -72,26 +72,64 @@ public String getOsName() {
return osName;
}

public ClientLocation setOsName(String osName) {
public void setOsName(String osName) {
this.osName = osName;
return this;
}

public String getClientVersion() {
return clientVersion;
}

public ClientLocation setClientVersion(String clientVersion) {
public void setClientVersion(String clientVersion) {
this.clientVersion = clientVersion;
return this;
}

public static ClientLocation from(SessionContext context) {
ClientLocation config = new ClientLocation();
config.osName = context.osName;
config.clientVersion = context.clientVersion;
config.deviceId = context.deviceId;
return config;
public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public String getConnId() {
return connId;
}

public void setConnId(String connId) {
this.connId = connId;
}

public int getClientType() {
if (clientType == 0) {
clientType = ClientType.find(osName).type;
}
return clientType;
}

public static ClientLocation from(Connection connection) {
SessionContext context = connection.getSessionContext();
ClientLocation location = new ClientLocation();
location.osName = context.osName;
location.clientVersion = context.clientVersion;
location.deviceId = context.deviceId;
location.connId = connection.getId();
return location;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ClientLocation location = (ClientLocation) o;

return clientType == location.clientType;
}

@Override
public int hashCode() {
return Integer.hashCode(clientType);
}

@Override
Expand All @@ -101,6 +139,7 @@ public String toString() {
", osName='" + osName + '\'' +
", clientVersion='" + clientVersion + '\'' +
", deviceId='" + deviceId + '\'' +
", connId='" + connId + '\'' +
'}';
}
}
58 changes: 58 additions & 0 deletions mpush-api/src/main/java/com/mpush/api/router/ClientType.java
@@ -0,0 +1,58 @@
/*
* (C) Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* ohun@live.cn (夜色)
*/

package com.mpush.api.router;

import java.util.Arrays;

/**
* Created by ohun on 16/8/18.
*
* @author ohun@live.cn (夜色)
*/
public enum ClientType {
MOBILE(1, "android", "ios"),
PC(2, "windows", "mac", "linux"),
WEB(3, "web", "h5"),
UNKNOWN(-1);

public final int type;
public final String[] os;

ClientType(int type, String... os) {
this.type = type;
this.os = os;
}

public boolean contains(String osName) {
return Arrays.stream(os).anyMatch(s -> s.equalsIgnoreCase(osName));
}

public static boolean isSameClient(String osName1, String osName2) {
if (osName1.equals(osName2)) return true;
return find(osName1).contains(osName2);
}

public static ClientType find(String osName) {
for (ClientType type : values()) {
if (type.contains(osName)) return type;
}
return UNKNOWN;
}
}
16 changes: 14 additions & 2 deletions mpush-api/src/main/java/com/mpush/api/router/RouterManager.java
Expand Up @@ -19,6 +19,8 @@

package com.mpush.api.router;

import java.util.Set;

/**
* Created by ohun on 2015/12/23.
*
Expand All @@ -39,15 +41,25 @@ public interface RouterManager<R extends Router> {
* 删除路由
*
* @param userId
* @param clientType
* @return
*/
boolean unRegister(String userId, int clientType);

/**
* 查询路由
*
* @param userId
* @return
*/
boolean unRegister(String userId);
Set<R> lookupAll(String userId);

/**
* 查询路由
*
* @param userId
* @param clientType
* @return
*/
R lookup(String userId);
R lookup(String userId, int clientType);
}
Expand Up @@ -17,7 +17,7 @@
* ohun@live.cn (夜色)
*/

package com.mpush.tools.config.data;
package com.mpush.api.spi.net;


import java.net.URL;
Expand Down
Expand Up @@ -20,11 +20,18 @@
package com.mpush.api.spi.net;

import com.mpush.api.service.Service;
import com.mpush.api.spi.SpiLoader;

/**
* Created by yxx on 2016/5/23.
*
* @author ohun@live.cn (夜色)
*/
public interface DnsMappingManager extends Service {

static DnsMappingManager create() {
return SpiLoader.load(DnsMappingManager.class);
}

DnsMapping lookup(String origin);
}
Expand Up @@ -19,7 +19,9 @@

package com.mpush.bootstrap.job;

import com.mpush.common.net.DnsMappingManager;
import com.mpush.api.spi.SpiLoader;
import com.mpush.api.spi.net.DnsMappingManager;
import com.mpush.common.net.HttpProxyDnsMappingManager;
import com.mpush.tools.config.CC;

/**
Expand All @@ -31,7 +33,7 @@ public class HttpProxyBoot extends BootJob {
@Override
void run() {
if (CC.mp.http.proxy_enabled) {
DnsMappingManager.I.start();
SpiLoader.load(DnsMappingManager.class, CC.mp.spi.dns_mapping_manager).start();
}
next();
}
Expand Down

0 comments on commit afde33c

Please sign in to comment.