@@ -2,58 +2,59 @@ package main
22
33import (
44 "fmt"
5+ "log"
6+ "net"
7+ "net/http"
58 "os"
6- "syscall"
79 "os/signal"
8- "github.com/julienschmidt/httprouter"
9- "net"
10- "net/http"
11- "log"
10+ "syscall"
1211 "time"
12+
13+ "github.com/julienschmidt/httprouter"
1314 //"encoding/hex"
1415)
1516
1617var (
18+ // VERSION My current version
1719 VERSION = "0.0.0"
1820)
1921
2022var (
21- SIBLINGS_ADDR = "224.0.0.1:9999"
22- BEACON_FREQ time.Duration = 2 //seconds
23- SIBLING_TTL int64 = 5 //seconds
24- BEACON_INTERFACE = ""
23+ siblingsAddr = "224.0.0.1:9999"
24+ beaconFreq time.Duration = 2 //seconds
25+ siblingTTL int64 = 5 //seconds
26+ beaconInterface = ""
2527
2628 maxDatagramSize = 128
2729
28- CACHE_IP = ""
29- CACHE_PORT = "8080"
30- CACHE_GC_FREQ = 3600
31-
32- CACHE_MODE = "standalone"
33-
34- CACHE_GET_URL = "cache/get"
35- CACHE_SET_URL = "cache/set"
36- CACHE_REMOVE_URL = "cache/remove/:cache_block"
37- CACHE_REMOVE_KEY_URL = "cache/remove/key"
38- CACHE_REMOVE_APP_URL = "cache/remove/application"
39- CACHE_REMOVE_ALL_URL = "cache/remove/all"
40- CACHE_STATS_URL = "cache/stats/:stats_type"
41- CACHE_STATS_LOCAL_URL = "cache/stats/local"
42- CACHE_STATS_ALL_URL = "cache/stats/all"
43- CACHE_IMPORT_URL = "cache/import"
44-
45- ME = ""
46- SIBLINGS_MANAGER * SiblingsManager
30+ cacheIP = ""
31+ cachePort = "8080"
32+ cacheGCFreq = 3600
33+
34+ cacheMode = "standalone"
35+
36+ cacheGetURL = "cache/get"
37+ cacheSetURL = "cache/set"
38+ cacheRemoveURL = "cache/remove/:cache_block"
39+ cacheRemoveKeyURL = "cache/remove/key"
40+ cacheRemoveAppURL = "cache/remove/application"
41+ cacheRemoveAllURL = "cache/remove/all"
42+ cacheStatsURL = "cache/stats/:stats_type"
43+ cacheStatslocalURL = "cache/stats/local"
44+ cacheStatsAllURL = "cache/stats/all"
45+ cacheImportURL = "cache/import"
46+
47+ // ME amongst my siblings
48+ ME = ""
49+ siblingsMgr * siblingsManager
4750)
4851
4952var (
50- CACHE * Cache
53+ mainCache * cacheOperator
5154)
5255
53-
54-
5556func udpBeacon () {
56- addr , err := net .ResolveUDPAddr ("udp" , SIBLINGS_ADDR )
57+ addr , err := net .ResolveUDPAddr ("udp" , siblingsAddr )
5758 if err != nil {
5859 log .Fatal (err )
5960 }
@@ -62,7 +63,7 @@ func udpBeacon() {
6263 c , err := net .DialUDP ("udp" , nil , addr )
6364 for {
6465 c .Write ([]byte (ME ))
65- time .Sleep (BEACON_FREQ * time .Second )
66+ time .Sleep (beaconFreq * time .Second )
6667 }
6768}
6869
@@ -89,13 +90,13 @@ func serveMulticastUDP(a string, iface *net.Interface, callback func(*net.UDPAdd
8990 }
9091}
9192
92- func exportcache (sig_ch chan os.Signal ) {
93+ func exportcache (sigCh chan os.Signal ) {
9394
94- s := <- sig_ch
95+ s := <- sigCh
9596
96- log .Println ("Received signal" ,s )
97+ log .Println ("Received signal" , s )
9798
98- SIBLINGS_MANAGER . DistributeContent ()
99+ siblingsMgr . distributeContent ()
99100
100101 os .Exit (0 )
101102}
@@ -104,68 +105,66 @@ func main() {
104105
105106 ME , _ = os .Hostname ()
106107
107- var beacon_interface * net.Interface
108+ var beaconIface * net.Interface
108109
109110 readConfig ()
110111
111- if BEACON_INTERFACE == "" {
112- beacon_interface = nil
113- BEACON_INTERFACE = "default"
112+ if beaconInterface == "" {
113+ beaconIface = nil
114+ beaconInterface = "default"
114115 } else {
115116 var err error
116- beacon_interface , err = net .InterfaceByName (BEACON_INTERFACE )
117+ beaconIface , err = net .InterfaceByName (beaconInterface )
117118 if err != nil {
118119 log .Fatal (err )
119120 }
120121 }
121122
122- log .Println ("Starting Dcached" , VERSION ,"on" , ME , "[ port" , CACHE_PORT , "]" , CACHE_MODE , "mode" )
123- if CACHE_MODE == "cluster" {
124- log .Println ("Multicast group" , SIBLINGS_ADDR )
125- log .Println ("Beacon interval" , int (BEACON_FREQ ), "seconds" )
126- log .Println ("Siblings TTL" , SIBLING_TTL , "seconds" )
123+ log .Println ("Starting Dcached" , VERSION , "on" , ME , "[ port" , cachePort , "]" , cacheMode , "mode" )
124+ if cacheMode == "cluster" {
125+ log .Println ("Multicast group" , siblingsAddr )
126+ log .Println ("Beacon interval" , int (beaconFreq ), "seconds" )
127+ log .Println ("Siblings TTL" , siblingTTL , "seconds" )
127128 log .Println ("Max.datagram size" , maxDatagramSize )
128- log .Println ("Beacon network interface" , BEACON_INTERFACE )
129+ log .Println ("Beacon network interface" , beaconInterface )
129130 }
130- log .Println ("Garbage collector interval" , CACHE_GC_FREQ , "seconds" )
131+ log .Println ("Garbage collector interval" , cacheGCFreq , "seconds" )
131132
132- CACHE = NewCache ()
133- SIBLINGS_MANAGER = NewSiblingsManager ()
133+ mainCache = newCache ()
134+ siblingsMgr = newSiblingsManager ()
134135
135- sig_ch := make (chan os.Signal , 1 )
136- signal .Notify (sig_ch , os .Interrupt )
137- signal .Notify (sig_ch , syscall .SIGTERM )
136+ sigCh := make (chan os.Signal , 1 )
137+ signal .Notify (sigCh , os .Interrupt )
138+ signal .Notify (sigCh , syscall .SIGTERM )
138139
139- go exportcache (sig_ch )
140+ go exportcache (sigCh )
140141
141- if CACHE_MODE == "cluster" {
142+ if cacheMode == "cluster" {
142143
143144 go udpBeacon ()
144- go serveMulticastUDP (SIBLINGS_ADDR , beacon_interface , SIBLINGS_MANAGER . MsgHandler )
145+ go serveMulticastUDP (siblingsAddr , beaconIface , siblingsMgr . msgHandler )
145146 //go serveMulticastUDP(SIBLINGS_ADDR, nil, SIBLINGS_MANAGER.MsgHandler)
146147 }
147148
148- router := httprouter .New ()
149+ router := httprouter .New ()
149150
150- router .NotFound = NotFoundHandler {}
151- router .MethodNotAllowed = MethodNotAllowedHandler {}
151+ router .NotFound = notFoundHandler {}
152+ router .MethodNotAllowed = methodNotAllowedHandler {}
152153
153- router .POST ("/" + CACHE_GET_URL , CacheGet )
154- router .OPTIONS ("/" + CACHE_GET_URL , CacheGetDoc )
154+ router .POST ("/" + cacheGetURL , cacheGet )
155+ router .OPTIONS ("/" + cacheGetURL , cacheGetDoc )
155156
156- router .POST ("/" + CACHE_SET_URL , CacheSet )
157- router .OPTIONS ("/" + CACHE_SET_URL , CacheSetDoc )
157+ router .POST ("/" + cacheSetURL , cacheSet )
158+ router .OPTIONS ("/" + cacheSetURL , cacheSetDoc )
158159
159- router .OPTIONS ("/" + CACHE_REMOVE_URL , CacheRemoveDoc )
160- router .POST ("/" + CACHE_REMOVE_URL , CacheRemove )
160+ router .OPTIONS ("/" + cacheRemoveURL , cacheRemoveDoc )
161+ router .POST ("/" + cacheRemoveURL , cacheRemove )
161162
162- router .OPTIONS ("/" + CACHE_STATS_URL , CacheStatsHandlerDoc )
163- router .GET ("/" + CACHE_STATS_URL , CacheStatsHandler )
163+ router .OPTIONS ("/" + cacheStatsURL , cacheStatsHandlerDoc )
164+ router .GET ("/" + cacheStatsURL , cacheStatsHandler )
164165
165- router .OPTIONS ("/" + CACHE_IMPORT_URL , CacheImportDoc )
166- router .POST ("/" + CACHE_IMPORT_URL , CacheImport )
166+ router .OPTIONS ("/" + cacheImportURL , cacheImportDoc )
167+ router .POST ("/" + cacheImportURL , cacheImport )
167168
168- log .Fatal (http .ListenAndServe (fmt .Sprintf ("%s:%s" , CACHE_IP , CACHE_PORT ), router ))
169+ log .Fatal (http .ListenAndServe (fmt .Sprintf ("%s:%s" , cacheIP , cachePort ), router ))
169170}
170-
171-
0 commit comments