-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.go
86 lines (67 loc) · 1.54 KB
/
contact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"github.com/astaxie/beego/logs"
"sync"
)
const (
CONTACT_STATE_UNKOWN = iota
CONTACT_STATE_REACHABLE
CONTACT_STATE_CALLING
)
type Contact struct {
Id uint64
Account string
Password string
Session uint64
State int
}
func (c *Contact) UpdateContactDb() {
o := orm.NewOrm()
_, err := o.Update(c)
if err != nil {
logs.Error("contact db update error")
}
}
func contactRegist(a string, p string) (int, *Contact) {
o := orm.NewOrm()
contact := new(Contact)
err := o.QueryTable("contact").Filter("account",a).Filter("password",p).One(contact)
if err != nil {
logs.Error(err)
return CONTACT_STATE_UNKOWN, contact
}else {
return CONTACT_STATE_REACHABLE, contact
}
}
var activeContacts = struct {
sync.RWMutex
cm map[string]*Contact
}{cm:make(map[string]*Contact)}
func AddActiveContact(c *Contact) {
activeContacts.Lock()
defer activeContacts.Unlock()
activeContacts.cm[c.Account] = c
}
func GetActiveContact(acc string) (*Contact, bool) {
activeContacts.RLock()
defer activeContacts.RUnlock()
c, ok := activeContacts.cm[acc]
return c, ok
}
type Conversation struct {
}
func init() {
orm.RegisterDriver("mysql", orm.DRMySQL)
orm.RegisterDataBase("default", "mysql", "yaoguoju:Admin@123@/ptpv?charset=utf8")
orm.RegisterModel(new(Contact))
}
func insertTestContacts() {
o := orm.NewOrm()
o.Using("default")
contact1 := new(Contact)
contact1.Account = "1000";
contact1.Password = "1234";
logs.Info(o.Insert(contact1))
}