Skip to content

Commit

Permalink
add hand shake
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Dec 25, 2015
1 parent a625922 commit 0bb4d35
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 177 deletions.
4 changes: 2 additions & 2 deletions mpush-api/src/main/java/com/shinemo/mpush/api/Connection.java
Expand Up @@ -9,9 +9,9 @@
*/
public interface Connection {

void setClientInfo(ClientInfo info);
void setSessionInfo(SessionInfo info);

ClientInfo getClientInfo();
SessionInfo getSessionInfo();

String getId();

Expand Down
Expand Up @@ -3,14 +3,14 @@
/**
* Created by ohun on 2015/12/22.
*/
public class ClientInfo {
public class SessionInfo {
public final String osName;
public final String osVersion;
public final String clientVersion;
public final String deviceId;
public final String desKey;

public ClientInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) {
public SessionInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) {
this.osName = osName;
this.osVersion = osVersion;
this.clientVersion = clientVersion;
Expand Down
226 changes: 113 additions & 113 deletions mpush-core/src/main/java/com/shinemo/mpush/core/NettyConnection.java
Expand Up @@ -8,124 +8,124 @@
import io.netty.channel.ChannelFutureListener;

import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.ClientInfo;
import com.shinemo.mpush.api.SessionInfo;
import com.shinemo.mpush.api.protocol.Packet;

/**
* Created by ohun on 2015/12/22.
*/
public class NettyConnection implements Connection {

private static final Logger log = LoggerFactory.getLogger(NettyConnection.class);

private ClientInfo info;
private Channel channel;
private int status = 0;

private int hbTimes;
@Override
public void init(Channel channel) {
this.channel = channel;
}

@Override
public void setClientInfo(ClientInfo info) {
this.info = info;
}

@Override
public String getId() {
return channel.id().asLongText();
}

@Override
public void send(final Packet packet) {
if (packet != null) {
if (channel.isWritable()) {
ChannelFuture wf = channel.writeAndFlush(packet);
wf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(!future.isSuccess()){
if(!channel.isActive()){
log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet+",channel is not active");
ConnectionManager.INSTANCE.remove(channel);
}
log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet);
}else{
log.warn("send msg success:"+channel.remoteAddress().toString()+","+packet);
}
}
});
}else{
log.warn("send msg false:"+channel.remoteAddress().toString()+","+packet+", channel is not writable");
}
}
}

@Override
public boolean isClosed() {
return false;
}

@Override
public boolean isOpen() {
return false;
}

public ClientInfo getClientInfo() {
return info;
}

public Channel getChannel() {
return channel;
}

public int increaseAndGetHbTimes(){
return ++hbTimes;
}
public void resetHbTimes(){
hbTimes = 0;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}


public void setChannel(Channel channel) {
this.channel = channel;
}

@Override
public void close() {
this.channel.close();
}

@Override
public int getHbTimes() {
return hbTimes;
}
@Override
public boolean isConnected(){
return channel.isActive();
}
@Override
public boolean isEnable(){
return channel.isWritable();
}

@Override
public String remoteIp() {
return channel.remoteAddress().toString();
}
private static final Logger log = LoggerFactory.getLogger(NettyConnection.class);

private SessionInfo info;
private Channel channel;
private int status = 0;

private int hbTimes;

@Override
public void init(Channel channel) {
this.channel = channel;
}

@Override
public void setSessionInfo(SessionInfo info) {
this.info = info;
}

@Override
public SessionInfo getSessionInfo() {
return info;
}

@Override
public String getId() {
return channel.id().asLongText();
}

@Override
public void send(final Packet packet) {
if (packet != null) {
if (channel.isWritable()) {
ChannelFuture wf = channel.writeAndFlush(packet);
wf.addListener(new ChannelFutureListener() {

@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
if (!channel.isActive()) {
log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet + ",channel is not active");
ConnectionManager.INSTANCE.remove(channel);
}
log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet);
} else {
log.warn("send msg success:" + channel.remoteAddress().toString() + "," + packet);
}
}
});
} else {
log.warn("send msg false:" + channel.remoteAddress().toString() + "," + packet + ", channel is not writable");
}
}
}

@Override
public boolean isClosed() {
return false;
}

@Override
public boolean isOpen() {
return false;
}

public Channel getChannel() {
return channel;
}

public void setChannel(Channel channel) {
this.channel = channel;
}

public int increaseAndGetHbTimes() {
return ++hbTimes;
}

public void resetHbTimes() {
hbTimes = 0;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

@Override
public void close() {
this.channel.close();
}

@Override
public int getHbTimes() {
return hbTimes;
}

@Override
public boolean isConnected() {
return channel.isActive();
}

@Override
public boolean isEnable() {
return channel.isWritable();
}

@Override
public String remoteIp() {
return channel.remoteAddress().toString();
}
}
@@ -1,6 +1,6 @@
package com.shinemo.mpush.core.handler;

import com.shinemo.mpush.api.ClientInfo;
import com.shinemo.mpush.api.SessionInfo;
import com.shinemo.mpush.api.Constants;
import com.shinemo.mpush.api.Request;
import com.shinemo.mpush.api.protocol.Packet;
Expand Down Expand Up @@ -38,8 +38,8 @@ public void handle(HandShakeMessage body, Request request) {
String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE);
String clientKey = body.clientKey;
String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥
ClientInfo info = new ClientInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey);
request.getConnection().setInfo(info);
SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey);
request.getConnection().setClientInfo(info);
ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info);
ReusableTokenManager.INSTANCE.saveToken(token);
Map<String, Serializable> resp = new HashMap<String, Serializable>();
Expand Down
@@ -1,6 +1,6 @@
package com.shinemo.mpush.core.security;

import com.shinemo.mpush.api.ClientInfo;
import com.shinemo.mpush.api.SessionInfo;
import com.shinemo.mpush.tools.crypto.MD5Utils;

/**
Expand All @@ -19,7 +19,7 @@ public ReusableToken getToken() {
return new ReusableToken();
}

public ReusableToken genToken(ClientInfo info) {
public ReusableToken genToken(SessionInfo info) {
/**
* 先生成key,需要保证半个周期内同一个设备生成的key是相同的
*/
Expand Down

0 comments on commit 0bb4d35

Please sign in to comment.