Skip to content

Commit

Permalink
重构序列化问题,去除了entity中的版本号,使用header的协议版本号控制,添加mongodb数据配置,添加用户登录逻辑,添加消息转发…
Browse files Browse the repository at this point in the history
…MessageHandler。
  • Loading branch information
Tony committed Feb 20, 2015
1 parent de6af28 commit 8349a74
Show file tree
Hide file tree
Showing 49 changed files with 1,869 additions and 844 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
| :----: |:-------:| :-------------:|
| 4byte | 8byte | data length |

**Entity:**<br>

| Version | Properties Content |
| :-----: | :-----------------:|
| 2byte | data length |

> version(); // 当前实体类版本,当属性有改变时可以通过版本号控制序列化<br>
> encode(); // 序列化,以byte[]方式写入到DataBuffer中<br>
> decode(DataBuffer); // 反序列化,通过DataBuffer转换成Entity<br>
**Header:**<br>

> version 协议版本号,当协议有改变,以及entity属性有改变时通过版本号进行序列化操作。<br>
> handlerId 业务分发处理,IMHandlerManager.find(handlerId),通过handler去处理客户端请求命令。<br>
> commandId 命令动作处理,IMHandler.dispatch(IMConnection, IMRequest),对IMRequest处理,IMConnection应答或者kill。<br>
> reserved 数据序列处理,对Actual Content进行加解密类型,以及序列化反序列化方式。<br>
Expand All @@ -27,7 +19,7 @@
##### 服务端应答,通过writeEntity写入对象数据,可以连续写入多个Entity
**IMResponse:**<br>
> writeEntity(entity);<br>
> writeEntity(IMSerializer);<br>
##### 长连接,发送数据内容,控制客户端连接
**IMConnection:**<br>
Expand Down
5 changes: 5 additions & 0 deletions chat-biz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
<artifactId>chat-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
</dependencies>
</project>
27 changes: 27 additions & 0 deletions chat-biz/src/main/java/io/ganguo/chat/biz/ApplicationConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.ganguo.chat.biz;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

/**
* Created by Tony on 2/19/15.
*/
@Configuration
@ComponentScan
@EnableMongoRepositories
public class ApplicationConfig extends AbstractMongoConfiguration {

@Override
public Mongo mongo() throws Exception {
return new MongoClient();
}

@Override
protected String getDatabaseName() {
return "CHAT_DB";
}
}
28 changes: 28 additions & 0 deletions chat-biz/src/main/java/io/ganguo/chat/biz/bean/ClientType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.ganguo.chat.biz.bean;

/**
* Created by Tony on 2/19/15.
*/
public enum ClientType {

WINDOWS(0), MAC(1), LINUX(2), IOS(3), ANDROID(4), WINPHONE(5), UNKNOUWN(-1);

private byte mValue = 0;

public byte getValue() {
return mValue;
}

ClientType(int value) {
mValue = (byte) value;
}

public static ClientType valueOfRaw(byte value) {
for (ClientType clientType : ClientType.values()) {
if (clientType.getValue() == value) {
return clientType;
}
}
return UNKNOUWN;
}
}
35 changes: 16 additions & 19 deletions chat-biz/src/main/java/io/ganguo/chat/biz/bean/Gender.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@
/**
* @author Tony
* @createAt Feb 18, 2015
*
*/
public enum Gender {

MALE(1), FEMALE(0), UNKNOUWN(-1);
MALE(1), FEMALE(2), UNKNOUWN(0);

private byte mValue = 0;
private byte mValue = 0;

public byte getValue() {
return mValue;
}
public byte getValue() {
return mValue;
}

Gender(int value) {
mValue = (byte) value;
};
Gender(int value) {
mValue = (byte) value;
}

public static Gender valueOfRaw(byte value) {
switch (value) {
case 0:
return FEMALE;
case 1:
return MALE;
default:
return UNKNOUWN;
}
}
public static Gender valueOfRaw(byte value) {
for (Gender gender : Gender.values()) {
if (gender.getValue() == value) {
return gender;
}
}
return UNKNOUWN;
}
}
32 changes: 16 additions & 16 deletions chat-biz/src/main/java/io/ganguo/chat/biz/bean/Presence.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

public enum Presence {

UNAVAILABLE(0), AVAILABLE(1);
UNAVAILABLE(0), AVAILABLE(1);

private byte mValue = 0;
private byte mValue = 0;

public byte getValue() {
return mValue;
}
public byte getValue() {
return mValue;
}

Presence(int value) {
mValue = (byte) value;
};
Presence(int value) {
mValue = (byte) value;
}

public static Presence valueOfRaw(byte value) {
switch (value) {
case 1:
return AVAILABLE;
default:
return UNAVAILABLE;
}
}
public static Presence valueOfRaw(byte value) {
for (Presence presence : Presence.values()) {
if (presence.getValue() == value) {
return presence;
}
}
return UNAVAILABLE;
}
}
39 changes: 39 additions & 0 deletions chat-biz/src/main/java/io/ganguo/chat/biz/entity/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.ganguo.chat.biz.entity;

import org.springframework.data.annotation.Id;

import java.math.BigInteger;

/**
* Created by Tony on 2/19/15.
*/
public abstract class BaseEntity {

@Id
protected String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

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

BaseEntity that = (BaseEntity) o;

if (id != null ? !id.equals(that.id) : that.id != null) return false;

return true;
}

@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}
49 changes: 49 additions & 0 deletions chat-biz/src/main/java/io/ganguo/chat/biz/entity/Login.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.ganguo.chat.biz.entity;

import io.ganguo.chat.biz.bean.ClientType;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

/**
* Created by Tony on 2/20/15.
*/
@Document
public class Login extends BaseEntity {
@Indexed(unique = true)
private long uin;
private String authToken;
private long activeTime;

public long getUin() {
return uin;
}

public void setUin(long uin) {
this.uin = uin;
}

public String getAuthToken() {
return authToken;
}

public void setAuthToken(String authToken) {
this.authToken = authToken;
}

public long getActiveTime() {
return activeTime;
}

public void setActiveTime(long activeTime) {
this.activeTime = activeTime;
}

@Override
public String toString() {
return "Login{" +
"uin=" + uin +
", authToken='" + authToken + '\'' +
", activeTime=" + activeTime +
'}';
}
}
94 changes: 94 additions & 0 deletions chat-biz/src/main/java/io/ganguo/chat/biz/entity/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.ganguo.chat.biz.entity;


import org.springframework.data.mongodb.core.mapping.Document;

/**
* Created by Tony on 2/20/15.
*/
@Document
public class Message extends BaseEntity {
// uin
private long to;
private long from;
private byte type;
private String message;
private long createAt;

public long getTo() {
return to;
}

public void setTo(long to) {
this.to = to;
}

public long getFrom() {
return from;
}

public void setFrom(long from) {
this.from = from;
}

public byte getType() {
return type;
}

public void setType(byte type) {
this.type = type;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public long getCreateAt() {
return createAt;
}

public void setCreateAt(long createAt) {
this.createAt = createAt;
}

@Override
public String toString() {
return "Message{" +
"to=" + to +
", from=" + from +
", type=" + type +
", message='" + message + '\'' +
", createAt=" + createAt +
'}';
}

public enum Type {
SESSION_MSG(0), // 临时会话消息
BUDDY_MSG(1), // 好友消息
GROUP_MSG(2); // 群消息

private byte mValue = 0;

public byte getValue() {
return mValue;
}

Type(int value) {
mValue = (byte) value;
}

public static Type valueOfRaw(byte value) {
for (Type type : Type.values()) {
if (type.getValue() == value) {
return type;
}
}
return SESSION_MSG;
}
}

}
Loading

0 comments on commit 8349a74

Please sign in to comment.