diff --git a/builtin.go b/builtin.go index ee0a378..acd7ad8 100644 --- a/builtin.go +++ b/builtin.go @@ -23,11 +23,11 @@ func Get(key string) string { func Set(key string, value string) { kv.Set(key, value, cache.NoExpiration) - SendEvent("set", fmt.Sprintf("%s,%s", key, value)) + SendEvent("set", []byte(fmt.Sprintf("%s,%s", key, value))) } func pingCmd(message *Message) { - SendEvent("ping", "ping") + SendEvent("ping", []byte("ping")) } func pingEvt(message *Message) { @@ -36,7 +36,7 @@ func pingEvt(message *Message) { func setEvt(msg *Message) { if msg.From != Self() { - parts := strings.SplitN(msg.Payload, ",", 2) + parts := strings.SplitN(string(msg.Payload), ",", 2) if len(parts) == 2 { log.Printf("Set %s = %s", parts[0], parts[1]) kv.Set(parts[0], parts[1], cache.NoExpiration) @@ -45,7 +45,7 @@ func setEvt(msg *Message) { } func joinCmd(message *Message, nodeIn chan *Node) { - ip := net.ParseIP(message.Payload) + ip := net.ParseIP(string(message.Payload)) service := mdns.ServiceEntry{ Name: message.From, AddrV4: ip, diff --git a/filesync.go b/filesync.go index 8784026..84e4af3 100644 --- a/filesync.go +++ b/filesync.go @@ -42,20 +42,20 @@ func sendFile(file string) { if err != nil { log.Println(err) } else { - SendEventC("fileSync", fmt.Sprintf("%s,%s", file, string(content)), true) + SendEventC("fileSync", []byte(fmt.Sprintf("%s,%s", file, content)), true) } } func removeFile(file string) { - SendEvent("fileRemoved", file) + SendEvent("fileRemoved", []byte(file)) } func onFileRemoved(message *Message) { if message.From == Self() { return } - delete(registeredFiles, message.Payload) - os.Remove(message.Payload) + delete(registeredFiles, string(message.Payload)) + os.Remove(string(message.Payload)) } func onFileChanged(message *Message) { @@ -63,7 +63,7 @@ func onFileChanged(message *Message) { return } - parts := strings.SplitN(message.Payload, ",", 2) + parts := strings.SplitN(string(message.Payload), ",", 2) fileName := parts[0] content := parts[1] hasher := sha256.New() @@ -107,7 +107,7 @@ func DirSync(dirName string) { for _, fileInfo := range files { fileName := fmt.Sprintf("%s/%s", dirName, fileInfo.Name()) - if !strings.HasPrefix(fileName, fmt.Sprintf("%s/.", dirName)) { + if !strings.HasPrefix(fileName, fmt.Sprintf("%s/.", dirName)) && !strings.HasSuffix(fileName, "~") { got[fileName] = true hasher := sha256.New() diff --git a/main.go b/main.go index 10a4c8c..3bc7eef 100644 --- a/main.go +++ b/main.go @@ -41,7 +41,7 @@ func join(nodeIn chan *Node, nodeOut chan string) { Command: true, Event: false, Name: "join", - Payload: config.IPs[0].String(), + Payload: []byte(config.IPs[0].String()), } c := gorpc.NewTCPClient(fmt.Sprintf("%s:%d", config.Join, config.Port)) c.Start() diff --git a/messaging.go b/messaging.go index 358dc36..9e90996 100644 --- a/messaging.go +++ b/messaging.go @@ -14,7 +14,7 @@ type Message struct { From string Name string To string - Payload string + Payload []byte Coalesce bool } @@ -81,11 +81,11 @@ func handle(message *Message) { } } -func SendCommand(to string, name string, payload string) { +func SendCommand(to string, name string, payload []byte) { SendCommandC(to, name, payload, true) } -func SendCommandC(to string, name string, payload string, coalesce bool) { +func SendCommandC(to string, name string, payload []byte, coalesce bool) { log.Printf("Sending command %s to %s\n", name, to) msg := Message{ @@ -100,11 +100,11 @@ func SendCommandC(to string, name string, payload string, coalesce bool) { sendQueue <- &msg } -func SendEvent(name string, payload string) { +func SendEvent(name string, payload []byte) { SendEventC(name, payload, false) } -func SendEventC(name string, payload string, coalesce bool) { +func SendEventC(name string, payload []byte, coalesce bool) { log.Printf("Sending event %s\n", name) msg := Message{ @@ -203,7 +203,7 @@ func startMessaging(nodeIn chan *Node, nodeOut chan string) { c := gorpc.NewTCPClient(fmt.Sprintf("%s:%d", node.Service.AddrV4.String(), config.Port)) c.Start() node.Client = c - SendCommand(node.Service.Name, "ping", "ping") + SendCommand(node.Service.Name, "ping", []byte("ping")) } }() diff --git a/scripting.go b/scripting.go index db83a3d..d66fca2 100644 --- a/scripting.go +++ b/scripting.go @@ -34,7 +34,7 @@ func luaMessage(vm *lua.LState, message *Message) *lua.LTable { table := vm.NewTable() vm.SetField(table, "from", lua.LString(message.From)) vm.SetField(table, "name", lua.LString(message.Name)) - vm.SetField(table, "payload", lua.LString(message.Payload)) + vm.SetField(table, "payload", lua.LString(string(message.Payload))) return table } @@ -42,7 +42,7 @@ func luaSendCmd(vm *lua.LState) int { to := vm.ToString(1) name := vm.ToString(2) payload := vm.ToString(3) - SendCommand(to, name, payload) + SendCommand(to, name, []byte(payload)) return 0 } @@ -51,14 +51,14 @@ func luaSendCmdC(vm *lua.LState) int { name := vm.ToString(2) payload := vm.ToString(3) coalesce := vm.ToBool(4) - SendCommandC(to, name, payload, coalesce) + SendCommandC(to, name, []byte(payload), coalesce) return 0 } func luaSendEvt(vm *lua.LState) int { name := vm.ToString(1) payload := vm.ToString(2) - SendEvent(name, payload) + SendEvent(name, []byte(payload)) return 0 } @@ -129,7 +129,7 @@ func luaSelf(vm *lua.LState) int { return 1 } -func luaTicker(script *Script, vm *lua.LState) int { +func luaTick(script *Script, vm *lua.LState) int { sec := vm.ToInt(1) fun := vm.ToFunction(2) go func() { @@ -178,7 +178,7 @@ func startScript(file string) *Script { return luaOnCmd(&script, vm) })) vm.SetGlobal("tick", vm.NewFunction(func(vm *lua.LState) int { - return tick(&script, vm) + return luaTick(&script, vm) })) vm.SetGlobal("sendEvent", vm.NewFunction(luaSendEvt)) vm.SetGlobal("sendCommand", vm.NewFunction(luaSendCmd))