-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (138 loc) · 4.37 KB
/
main.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
/*
* 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 main
import (
"fmt"
"net"
"os"
"strings"
"flag"
"./_obj/ndayak"
"./worker"
sp "./simpleconfig"
)
var (
VERSION string = "v0.4 alpha"
laddr *net.UDPAddr
con net.PacketConn
err os.Error
)
func banner(){
fmt.Printf("Ndayak %s\n",VERSION)
}
type Config struct{
ListenPort int
DbServer string
DbPort int
DbName string
Verbosity int
AsLoadBalancer bool
ConfigFile string
Servers string
}
var listenPort = flag.Int("port",50105,"Listen port")
var DbServer = flag.String("dbserver","127.0.0.1","Database/collection server")
var DbPort = flag.Int("dbport",27017,"Database/collection port")
var DbName = flag.String("dbname","test","Database name")
var verbosity = flag.Int("v",0,"Verbosity level")
var asLoadBalancer = flag.Bool("as-load-balancer",false,"Run Ndayak as load balancer")
var configFile = flag.String("config","ndayak.conf","Configuration file")
func main(){
flag.Parse()
banner()
f, err := os.Open(*configFile,os.O_RDONLY,0666)
if err != nil{
fmt.Printf("Cannot open configuration file `%s`\n", *configFile)
os.Exit(1)
return
}
defer f.Close()
var config Config
err = sp.Unmarshal(&config, f)
if err != nil{
fmt.Printf("Cannot parse configuraition from file `%s`\n", *configFile)
os.Exit(1)
return
}
if *listenPort != 50105{
config.ListenPort = *listenPort
}
if *verbosity != 0{
config.Verbosity = *verbosity
}
fmt.Printf("config.AsLoadBalancer: %v\n", config.AsLoadBalancer)
if config.AsLoadBalancer == true{
*asLoadBalancer = true
}
fmt.Printf("options:\n\tdb_server: %s:%d\n\tdb_name: %s\n", config.DbServer, config.DbPort, config.DbName)
fmt.Printf("\tverbosity: %d\n", config.Verbosity)
var listen_addr string = fmt.Sprintf("0.0.0.0:%d",config.ListenPort)
//laddr, err = net.ResolveUDPAddr(listen_addr);
//if err != nil{fmt.Println("Error in resolve... ",err); os.Exit(1);}
con, err = net.ListenPacket("udp4", listen_addr)
if err != nil{fmt.Println("Error in listen..."); os.Exit(2);}
if *asLoadBalancer == true{
fmt.Println("Run as load balancer")
// split servers
servers := strings.Split(config.Servers, ",", 10)
for _, s := range servers{
if len(strings.Trim(s," \n\t\r")) == 0{
fmt.Println("[WARNING] No servers found for load balancer")
servers = []string{}
break
}
}
fmt.Printf("Servers (%d): %v\n", len(servers),servers)
}
fmt.Println("Listening at " + listen_addr + "...")
fmt.Println("Ready for connection.")
resp := make(chan string)
st := ndayak.Settings{config.DbServer,config.DbPort,config.DbName}
ndayak.Init(con, &st, config.Verbosity)
worker.Init(con, *asLoadBalancer, config.Servers)
go worker.Worker(resp)
go worker.Worker(resp)
go worker.Worker(resp)
go stream_reader(resp,1)
go stream_reader(resp,2)
stream_reader(resp,3) // main stream-reader
worker.Close()
fmt.Println("Done.")
}
func stream_reader(resp chan string,id int){
var buf[1000] byte;
for{
n, _, err := con.ReadFrom(buf[0:128]);
if err != nil{fmt.Println("Error in read..."); os.Exit(3);}
ndayak.Info2("[sr-%d] received %d bytes\n", id, n)
go func(ch chan string){
var d string = string(buf[:n]);
ndayak.Info2("Got: %s\n",d)
ch <- d
}(resp)
}
}