-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
259 lines (217 loc) · 6.27 KB
/
worker.go
File metadata and controls
259 lines (217 loc) · 6.27 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (c) 2010 Digaku.com
* writen by Robin Syihab (r[at]nosql.asia)
*
* License MIT
*
* Copyright (c) 2009 The Go Authors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package worker
import (
//"fmt"
"strings"
"os"
"net"
"sync"
"container/vector"
"./_obj/ndayak"
)
const (
CMD_QUIT = "/q"
CMD_PROCESS = "/p" // /p [POST-ID]
CMD_RMPOST = "/rm" // /rm [POST-ID]
CMD_UPPOST = "/up" // /up [POST-ID]
CMD_BPTALL = "/bptall" // /bptall [POST-ID]
CMD_HI = "/hi" // /hi there
CMD_SRV = "/srv" // /srv [NDAYAK-IP] [down|up]
)
var (
con net.PacketConn
asLoadBalancer bool
loadbalServers vector.StringVector
loadbalServersCount int
loadbalConns vector.Vector
redirectorMutex *sync.Mutex
)
func Init(_con net.PacketConn, asLoadbal bool, loadbalsrv string){
con = _con
asLoadBalancer = asLoadbal
// if in load balancher mode then
// create exporter and importer
if asLoadbal == true{
redirectorMutex = new(sync.Mutex)
servers := strings.Split(loadbalsrv,",",1000)
for _, server := range servers{
if len(strings.Trim(server," \n\t\r")) > 0{
loadbalServers.Push(server)
}
}
loadbalServersCount = loadbalServers.Len()
for i := 0; i < loadbalServersCount; i++ {
conn, err := net.Dial("udp4","",loadbalServers[i])
if err != nil{
ndayak.Error("worker.Init: Cannot dial to udp4 for load bal server: `%s`\n", loadbalServers[i])
}
ndayak.Info2("Send hi to `%v`\n", loadbalServers[i])
conn.Write([]byte("/hi"))
loadbalConns.Push(conn)
}
}
}
func Close(){
ndayak.Info2("Closing...\n")
if asLoadBalancer == true{
for i := 0; i < loadbalServersCount; i++ {
ndayak.Info2("Closing server `%s`...\n", loadbalServers[i])
loadbalConns[i].(net.Conn).Close()
}
}
}
func Worker(ch chan string){
for {
rv := <- ch
//fmt.Println("worker received new task...")
//fmt.Println("working...")
d := strings.Split(rv, " ", 2)
var arg string = ""
cmdstr := d[0]
if cmdstr[0] != '/'{
ndayak.Warn("Invalid command\n")
continue
}
if asLoadBalancer == true && cmdstr != CMD_SRV{
redirectCmd(rv)
continue
}
if len(d) > 1{
arg = d[1]
}
switch cmdstr{
case CMD_PROCESS:
post_id := arg
ndayak.Info("Building index for post_id: %v...\n", post_id)
ndayak.ProcessPost(post_id)
case CMD_QUIT:
ndayak.Info("Quit command received. Realy quiting now...\n")
con.Close()
os.Exit(0)
case CMD_RMPOST:
post_id := arg
ndayak.Info("Removing stream for post id: `%s`...\n", post_id)
ndayak.RmPostStream(post_id)
case CMD_UPPOST:
post_id := arg
ndayak.Info("Top up stream for post id: `%s`...\n", post_id)
ndayak.TopUpPost(post_id)
case CMD_BPTALL:
post_id := arg
ndayak.Info("Broadcast all post with id `%s`\n", post_id)
ndayak.BroadcastAll(post_id)
case CMD_SRV:
if asLoadBalancer == false{
ndayak.Warn("%s command not supported for non load balancer mode\n", cmdstr)
continue
}
d := strings.Fields(arg)
if len(d) < 2{ ndayak.Warn("Invalid `%s` command\n", cmdstr); continue; }
addr := d[0]
state := d[1]
if len(strings.Trim(addr," \n\t\r")) == 0{
ndayak.Warn("Invalid ndayak ip")
continue
}
if state == "up"{
addNewNdayak(addr)
ndayak.Info("Ndayak instance for addr `%s` has been added.\n", addr)
}else{
rmNdayak(addr)
ndayak.Info("Ndayak instance for addr `%s` has been removed.\n", addr)
}
case CMD_HI:
if asLoadBalancer == true{
redirectCmd(rv)
}else{
ndayak.Info("Got \"hi\" message from Ndayaks\n")
}
default:
ndayak.Info("Unknown command `%s`\n", cmdstr)
}
//fmt.Println("worker finished task.")
}
}
var currentChExIndex = 0;
func ndayakExists(addr string) bool{
for _, s := range loadbalServers{
if s == addr{
return true
}
}
return false
}
func addNewNdayak(addr string){
redirectorMutex.Lock()
defer redirectorMutex.Unlock()
if ndayakExists(addr) == true{
ndayak.Warn("Ndayak with addr `%s` already exists\n", addr)
return
}
conn, err := net.Dial("udp4","",addr)
if err != nil{
ndayak.Error("addNewNdayak: Cannot connect to new ndayak addr `%v`\n", addr)
return
}
loadbalServers.Push(addr)
loadbalConns.Push(conn)
}
func rmNdayak(addr string){
redirectorMutex.Lock()
defer redirectorMutex.Unlock()
if ndayakExists(addr) == false{
ndayak.Warn("Ndayak with addr `%s` doesn't exists\n", addr)
return
}
for i, s := range loadbalServers{
if s == addr{
loadbalServers.Delete(i)
loadbalConns.At(i).(net.Conn).Close()
loadbalConns.Delete(i)
break
}
}
}
func redirectCmd(cmd string){
redirectorMutex.Lock()
defer redirectorMutex.Unlock()
if loadbalServers.Len() == 0 || loadbalConns.Len() == 0{
ndayak.Warn("redirectCmd: No route to any ndayak instance. Please add first.\n")
return
}
ndayak.Info("Redirect command `%s` to `%v`\n", cmd, loadbalServers)
if currentChExIndex >= loadbalServers.Len(){
currentChExIndex = 0;
}
n, err := loadbalConns.At(currentChExIndex).(net.Conn).Write([]byte(cmd))
if err != nil{
ndayak.Error("redirectCmd: Cannot send data to ladbal server `%s`. %s\n", loadbalServers.At(currentChExIndex), err)
}
ndayak.Info2("Success send to loadbal server `%s`, packet for %d bytes\n", loadbalServers.At(currentChExIndex), n)
currentChExIndex += 1;
}