Skip to content

Commit

Permalink
make payload []byte and do not sync "~" files
Browse files Browse the repository at this point in the history
  • Loading branch information
diogok committed Apr 12, 2017
1 parent 034f742 commit 7e4af2a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions filesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ 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) {
if message.From == Self() {
return
}

parts := strings.SplitN(message.Payload, ",", 2)
parts := strings.SplitN(string(message.Payload), ",", 2)
fileName := parts[0]
content := parts[1]
hasher := sha256.New()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Message struct {
From string
Name string
To string
Payload string
Payload []byte
Coalesce bool
}

Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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"))
}
}()

Expand Down
12 changes: 6 additions & 6 deletions scripting.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ 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
}

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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 7e4af2a

Please sign in to comment.