Skip to content

Commit

Permalink
first commit for e-watch protocol updated
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinwsz committed May 9, 2016
1 parent fafbb96 commit cd585db
Show file tree
Hide file tree
Showing 28 changed files with 2,618 additions and 854 deletions.
32 changes: 17 additions & 15 deletions base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,42 @@ import (
type ChannelMap map[string]*ChannelState
type SessionMap map[string]*libnet.Session

type AckMap map[string]map[string]bool
type AckMap map[string]map[string]int

const COMM_PREFIX = "IM"

var ChannleList []string

func init() {
ChannleList = []string{protocol.SYSCTRL_CLIENT_STATUS, protocol.SYSCTRL_TOPIC_STATUS, protocol.SYSCTRL_TOPIC_SYNC,
ChannleList = []string{protocol.SYSCTRL_CLIENT_STATUS, protocol.SYSCTRL_TOPIC_STATUS, protocol.SYSCTRL_TOPIC_SYNC,
protocol.SYSCTRL_SEND, protocol.SYSCTRL_MONITOR, protocol.STORE_CLIENT_INFO, protocol.STORE_TOPIC_INFO}
}

type ChannelState struct {
ChannelName string
Channel *libnet.Channel
ClientIDlist []string
ChannelName string
Channel *libnet.Channel
ClientIDlist []string
}

func NewChannelState(channelName string, channel *libnet.Channel) *ChannelState {
return &ChannelState {
ChannelName : channelName,
Channel : channel,
ClientIDlist : make([]string, 0),
return &ChannelState{
ChannelName: channelName,
Channel: channel,
ClientIDlist: make([]string, 0),
}
}

type SessionState struct {
ClientID string
Alive bool
ClientID string
Alive bool
ClientType string
}

func NewSessionState(alive bool, cid string) *SessionState {
return &SessionState {
ClientID : cid,
Alive : alive,
func NewSessionState(alive bool, cid string, clienttype string) *SessionState {
return &SessionState{
ClientID: cid,
Alive: alive,
ClientType: clienttype,
}
}

Expand Down
290 changes: 180 additions & 110 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,149 +16,219 @@
package common

import (
"time"
"math/rand"
"github.com/oikomi/FishChatServer/log"
"time"

"github.com/oikomi/FishChatServer/base"
"github.com/oikomi/FishChatServer/log"
"github.com/oikomi/FishChatServer/storage/mongo_store"
"github.com/oikomi/FishChatServer/storage/redis_store"
)

const KeyPrefix string = base.COMM_PREFIX

var DefaultRedisConnectTimeout uint32 = 2000
var DefaultRedisReadTimeout uint32 = 1000
var DefaultRedisWriteTimeout uint32 = 1000

var DefaultRedisOptions redis_store.RedisStoreOptions = redis_store.RedisStoreOptions {
Network : "tcp",
Address : ":6379",
ConnectTimeout : time.Duration(DefaultRedisConnectTimeout)*time.Millisecond,
ReadTimeout : time.Duration(DefaultRedisReadTimeout)*time.Millisecond,
WriteTimeout : time.Duration(DefaultRedisWriteTimeout)*time.Millisecond,
Database : 1,
KeyPrefix : base.COMM_PREFIX,
var DefaultRedisReadTimeout uint32 = 1000
var DefaultRedisWriteTimeout uint32 = 1000

var DefaultRedisOptions redis_store.RedisStoreOptions = redis_store.RedisStoreOptions{
Network: "tcp",
Address: ":6379",
ConnectTimeout: time.Duration(DefaultRedisConnectTimeout) * time.Millisecond,
ReadTimeout: time.Duration(DefaultRedisReadTimeout) * time.Millisecond,
WriteTimeout: time.Duration(DefaultRedisWriteTimeout) * time.Millisecond,
Database: 1,
KeyPrefix: base.COMM_PREFIX,
}

//Just use random to select msg_server
func SelectServer(serverList []string, serverNum int) string {
return serverList[rand.Intn(serverNum)]
}

func GetSessionFromCID(storeOp interface{}, ID string) (*redis_store.SessionCacheData, error) {
func GetSessionFromCID(storeOp interface{}, ID string) (interface{}, error) {
switch storeOp.(type) {
case *redis_store.SessionCache:
session ,err := storeOp.(*redis_store.SessionCache).Get(ID)

if err != nil {
log.Warningf("no ID : %s", ID)
return nil, err
}
if session != nil {
log.Info(session)
}

return session, nil

case *redis_store.SessionCache:
// return (*redis_store.SessionCacheData)
SessionCacheData, err := storeOp.(*redis_store.SessionCache).Get(ID)

if err != nil {
log.Warningf("no ID : %s", ID)
return nil, err
}
if SessionCacheData != nil {
log.Info(SessionCacheData)
}

return SessionCacheData, nil
case *mongo_store.MongoStore:
// return (*mongo_store.sessionStoreData)
sessionStoreData, err := storeOp.(*mongo_store.MongoStore).GetSessionFromCid(ID)

if err != nil {
log.Warningf("no ID : %s", ID)
return nil, err
}
if sessionStoreData != nil {
log.Info(sessionStoreData)
}

return sessionStoreData, nil

}

return nil, NOTFOUNT

// session ,err := sessionCache.Get(ID)

// if err != nil {
// log.Warningf("no ID : %s", ID)
// return nil, err
// }
// if session != nil {
// log.Info(session)
// }

// return session, nil
}

// session ,err := sessionCache.Get(ID)

// if err != nil {
// log.Warningf("no ID : %s", ID)
// return nil, err
// }
// if session != nil {
// log.Info(session)
// }

// return session, nil
}

func DelSessionFromCID(storeOp interface{}, ID string) error {
func DelSessionFromCID(storeOp interface{}, ID string) error {
switch storeOp.(type) {
case *redis_store.SessionCache:
err := storeOp.(*redis_store.SessionCache).Delete(ID)
if err != nil {
log.Warningf("no ID : %s", ID)
return err
}
case *redis_store.SessionCache:
err := storeOp.(*redis_store.SessionCache).Delete(ID)

if err != nil {
log.Warningf("no ID : %s", ID)
return err
}
}
// err := sessionCache.Delete(ID)
// if err != nil {
// log.Warningf("no ID : %s", ID)
// return err
// }

// err := sessionCache.Delete(ID)

// if err != nil {
// log.Warningf("no ID : %s", ID)
// return err
// }

return NOTFOUNT
}

func GetTopicFromTopicName(storeOp interface{}, topicName string) (*redis_store.TopicCacheData, error) {
func GetTopicFromTopicName(storeOp interface{}, topicName string) (interface{}, error) {
switch storeOp.(type) {
case *redis_store.TopicCache:
topic ,err := storeOp.(*redis_store.TopicCache).Get(topicName)

if err != nil {
log.Warningf("no topicName : %s", topicName)
return nil, err
}
if topic != nil {
log.Info(topic)
}

return topic, nil
case *redis_store.TopicCache:
// return (*redis_store.TopicCacheData)
TopicCacheData, err := storeOp.(*redis_store.TopicCache).Get(topicName)

if err != nil {
log.Warningf("no topicName : %s", topicName)
return nil, err
}
if TopicCacheData != nil {
log.Info(TopicCacheData)
}

return TopicCacheData, nil
case *mongo_store.MongoStore:
// return (*mongo_store.TopicStoreData)
TopicStoreData, err := storeOp.(*mongo_store.MongoStore).GetTopicFromCid(topicName)

if err != nil {
log.Warningf("no topicName : %s", topicName)
return nil, err
}
if TopicStoreData != nil {
log.Info(TopicStoreData)
}

return TopicStoreData, nil
}

return nil, NOTFOUNT

// topic ,err := topicCache.Get(topicName)

// if err != nil {
// log.Warningf("no topicName : %s", topicName)
// return nil, err
// }
// if topic != nil {
// log.Info(topic)
// }

// return topic, nil

// topic ,err := topicCache.Get(topicName)

// if err != nil {
// log.Warningf("no topicName : %s", topicName)
// return nil, err
// }
// if topic != nil {
// log.Info(topic)
// }

// return topic, nil
}

func StoreData(storeOp interface{}, data interface{}) error {
switch data.(type) {
case *redis_store.SessionCacheData:
err := storeOp.(*redis_store.SessionCache).Set(data.(*redis_store.SessionCacheData))
//err := (*redis_store.SessionCache)(storeOp).Set(data)
if err != nil {
return err
log.Error("error:", err)
}
log.Info("cache sesion success")

return nil
case *redis_store.TopicCacheData:
err := storeOp.(*redis_store.TopicCache).Set(data.(*redis_store.TopicCacheData))
if err != nil {
return err
log.Error("error:", err)
}
log.Info("cache topic success")

return nil
case *mongo_store.SessionStoreData:
err := storeOp.(*mongo_store.MongoStore).Set(data)
if err != nil {
return err
log.Error("error:", err)
}
log.Info("store session success")

return nil

case *mongo_store.TopicStoreData:
err := storeOp.(*mongo_store.MongoStore).Set(data)
if err != nil {
return err
log.Error("error:", err)
}
log.Info("store topic success")

return nil
}
return nil
}

func GetOfflineMsgFromOwnerName(storeOp interface{}, ownerName string) (*redis_store.OfflineMsgCacheData, error) {
func GetOfflineMsgFromOwnerName(storeOp interface{}, ownerName string) (*redis_store.OfflineMsgCacheData, error) {
switch storeOp.(type) {
case *redis_store.OfflineMsgCache:
o ,err := storeOp.(*redis_store.OfflineMsgCache).Get(ownerName)
if err != nil {
log.Warningf("no ownerName : %s", ownerName)
return nil, err
}
if o != nil {
log.Info(o)
}
return o, nil
case *redis_store.OfflineMsgCache:
o, err := storeOp.(*redis_store.OfflineMsgCache).Get(ownerName)

if err != nil {
log.Warningf("no ownerName : %s", ownerName)
return nil, err
}
if o != nil {
log.Info(o)
}

return o, nil
}



return nil, NOTFOUNT

// o ,err := offlineMsgCache.Get(ownerName)

// if err != nil {
// log.Warningf("no ownerName : %s", ownerName)
// return nil, err
// }
// if o != nil {
// log.Info(o)
// }

// return o, nil
}

// o ,err := offlineMsgCache.Get(ownerName)

// if err != nil {
// log.Warningf("no ownerName : %s", ownerName)
// return nil, err
// }
// if o != nil {
// log.Info(o)
// }

// return o, nil
}
Loading

0 comments on commit cd585db

Please sign in to comment.