-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkListOperation.go
178 lines (166 loc) · 4.72 KB
/
linkListOperation.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"strconv"
)
// first the linklist
type Node struct {
client *Client
next *Node
}
type headNode struct {
sm *sendMsg
rm *recvMsg
logchan chan *sendMsg
signalChan chan []byte
ips string
mideng int // forbiden multiple 111Err111
next *Node
ScheduleMap int
listCount int
}
type SameIdsLinkList struct {
Head *headNode
}
func newNode(client *Client, next *Node) *Node {
return &Node{
client: client,
next: next,
}
}
func NewSocketList(msg *msg) *SameIdsLinkList {
head := &headNode{
sm: msg.sm,
rm: msg.rm,
logchan: make(chan *sendMsg),
signalChan: make(chan []byte),
ips: "",
next: nil,
ScheduleMap: BEFORECREATE,
listCount: 0,
}
return &SameIdsLinkList{head}
}
// check whether the link is empty
func (list *SameIdsLinkList) isEmpty() bool {
return list.Head.next == nil
}
// append list
func (list *SameIdsLinkList) Append(node *Node) {
head := list.Head // *headNode
/*
one head only allow one node at the same time, except admin
*/
if head.next == nil {
head.next = node
head.listCount++
} else {
if head.listCount == 1 {
if node.client.Admin == head.next.client.Admin {
// replace one different
head.next.client.addr = ""
close(head.next.client.send)
close(head.next.client.sendLog)
head.next = node
} else {
head.next.next = node
head.listCount++
}
} else if head.listCount == 2 {
if node.client.Admin == head.next.client.Admin {
head.next.client.addr = ""
close(head.next.client.send)
close(head.next.client.sendLog)
head.next = node
} else if node.client.Admin == head.next.next.client.Admin {
head.next.next.client.addr = ""
close(head.next.next.client.send)
close(head.next.next.client.sendLog)
head.next.next = node
}
} else {
Error.Printf("[%d, %d]: have >2 clients node\n", head.rm.Content.IDs.Uid, head.rm.Content.IDs.Tid)
return
}
}
}
// delete
func (list *SameIdsLinkList) Remove(client *Client) error {
empty := list.isEmpty() // have node rather than only head
if empty {
return errors.New("this is an empty list")
}
head := list.Head // *headNode
if head.listCount == 1 {
if head.next.client == client {
last := head.next.next // node or nil
client.addr = ""
/*close(client.send)
close(client.sendLog)*/
head.next = last
head.listCount--
Trace.Printf("[%d, %d](a:%v): %s logged out\n", client.userIds.Uid, client.userIds.Tid, client.Admin, client.addr)
}
} else if head.listCount == 2 {
if head.next.client == client {
client.addr = ""
/*close(client.send)
close(client.sendLog)*/
head.next = head.next.next
head.listCount--
Trace.Printf("[%d, %d](a:%v): %s logged out\n", client.userIds.Uid, client.userIds.Tid, client.Admin, client.addr)
} else if head.next.next.client == client {
client.addr = ""
/* close(client.send)
close(client.sendLog)*/
head.next.next = nil
head.listCount--
Trace.Printf("[%d, %d](a:%v): %s logged out\n", client.userIds.Uid, client.userIds.Tid, client.Admin, client.addr)
}
} else {
Trace.Printf("[%d, %d]: %s not in the right list, or >2 client in one head\n", client.userIds.Uid, client.userIds.Tid, client.addr)
}
list.PrintList()
return nil
}
// print
func (list *SameIdsLinkList) PrintList() {
empty := list.isEmpty()
if empty {
Trace.Printf("[%d, %d]:This is an empty list\n", list.Head.rm.Content.IDs.Uid, list.Head.rm.Content.IDs.Tid)
return
}
if list.Head == nil {
Trace.Printf("no clients in head\n")
return
}
current := list.Head.next
if list.Head.listCount > 2 {
Error.Printf("[%d, %d]: already have two client node\n", list.Head.rm.Content.IDs.Uid, list.Head.rm.Content.IDs.Tid)
return
}
Trace.Printf("----------------[%d, %d]----------------\n", list.Head.rm.Content.IDs.Uid, list.Head.rm.Content.IDs.Tid)
for i := 0; i < list.Head.listCount; i++ {
Trace.Printf("[%d, %d](a:%v): %s exists\n", current.client.userIds.Uid, current.client.userIds.Tid, current.client.Admin, current.client.addr)
current = current.next
}
Trace.Printf("----------------[%d, %d]----------------\n", list.Head.rm.Content.IDs.Uid, list.Head.rm.Content.IDs.Tid)
}
func (list *SameIdsLinkList) linklistRun() {
for true {
select {
case msgs := <-list.Head.logchan:
if list.Head.listCount == 1 {
if !(list.Head.next.client.Admin) {
list.Head.next.client.sendLog <- []byte(strconv.Itoa(msgs.Type))
}
} else if list.Head.listCount == 2 {
if !(list.Head.next.client.Admin) {
list.Head.next.client.sendLog <- []byte(strconv.Itoa(msgs.Type))
} else if !(list.Head.next.next.client.Admin) {
list.Head.next.next.client.sendLog <- []byte(strconv.Itoa(msgs.Type))
}
}
}
}
}