Skip to content

Commit

Permalink
Mailbox getter
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 25, 2019
1 parent 28972fb commit 6ef3ff9
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions x/swingset/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,49 @@ type storageMessage struct {
var myKeeper Keeper
var myContext sdk.Context

func mailboxPeer(key string) (string, error) {
path := strings.Split(key, ".")
if len(path) != 2 || path[0] != "mailbox" {
return "", errors.New("Can only access 'mailbox.PEER'")
}
return path[1], nil
}

func ReceiveFromNode(str string) (string, error) {
msg := new(storageMessage)
err := json.Unmarshal([]byte(str), &msg)
if err != nil {
return "", err
}

// TODO: Actually use generic store, not just mailboxes.
switch msg.Method {
case "set":
path := strings.Split(msg.Key, ".")
if len(path) != 2 || path[0] != "mailbox" {
return "", errors.New("Can only set 'mailbox.PEER'")
peer, err := mailboxPeer(msg.Key)
if err != nil {
return "", err
}
mailbox := NewMailbox()
mailbox.Value = msg.Value
myKeeper.SetMailbox(myContext, path[1], mailbox)
myKeeper.SetMailbox(myContext, peer, mailbox)
return "true", nil
break

case "get":
peer, err := mailboxPeer(msg.Key)
if err != nil {
return "", err
}
mailbox := myKeeper.GetMailbox(myContext, peer)
return mailbox.Value, nil

case "has":
case "keys":
case "entries":
case "values":
case "size":
// TODO: Implement these operations
}
/* TODO: Dispatch based on msg.Method */

return "", errors.New("Unrecognized msg.Method " + msg.Method)
}
Expand Down

0 comments on commit 6ef3ff9

Please sign in to comment.