You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
4 years ago
|
package starmap
|
||
|
|
||
|
import (
|
||
|
"encoding/gob"
|
||
|
"errors"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"b612.me/notify"
|
||
|
)
|
||
|
|
||
|
type RemoteKv struct {
|
||
|
kvmu sync.RWMutex
|
||
|
server *notify.StarNotifyS
|
||
|
client *notify.StarNotifyC
|
||
|
addr string
|
||
|
setKeepAlive bool
|
||
|
TimeOut time.Duration
|
||
|
ErrMsg string
|
||
|
}
|
||
|
|
||
|
func NewServer(addr string) (*RemoteKv, error) {
|
||
|
var kv RemoteKv
|
||
|
var err error
|
||
|
kv.addr = addr
|
||
|
kv.TimeOut = time.Second * 10
|
||
|
kv.server, err = notify.NewNotifyS("tcp", addr)
|
||
|
if err == nil {
|
||
|
kv.bind()
|
||
|
}
|
||
|
return &kv, err
|
||
|
}
|
||
|
|
||
|
func NewClient(addr string) (*RemoteKv, error) {
|
||
|
var err error
|
||
|
var kv RemoteKv
|
||
|
kv.addr = addr
|
||
|
kv.TimeOut = time.Second * 10
|
||
|
kv.client, err = notify.NewNotifyC("tcp", addr)
|
||
|
if err == nil {
|
||
|
kv.setKeepAlive = true
|
||
|
kv.client.UseChannel = false
|
||
|
}
|
||
|
return &kv, err
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) Register(data interface{}) {
|
||
|
gob.Register(data)
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) RegisterAll(data []interface{}) {
|
||
|
for _, v := range data {
|
||
|
gob.Register(v)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) SetKeepAlive(alive bool) {
|
||
|
kv.setKeepAlive = alive
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) GetKeepAlive() bool {
|
||
|
return kv.setKeepAlive
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) reconnect() error {
|
||
|
var err error
|
||
|
if kv.addr != "" {
|
||
|
kv.client, err = notify.NewNotifyC("tcp", kv.addr)
|
||
|
if err == nil {
|
||
|
kv.client.UseChannel = false
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
return errors.New("Client Not Online!")
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) bind() {
|
||
|
kv.server.SetNotify("Get", kv.get)
|
||
|
kv.server.SetNotify("Store", kv.store)
|
||
|
kv.server.SetNotify("Delete", kv.store)
|
||
|
kv.server.SetNotify("GetMap", kv.getMap)
|
||
|
kv.server.SetNotify("StoreMap", kv.storeMap)
|
||
|
kv.server.SetNotify("DeleteMap", kv.deleteMap)
|
||
|
}
|
||
|
|
||
|
func (kv *RemoteKv) ErrTimeOut(err error) bool {
|
||
|
if err.Error() == "Time Exceed" {
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|