sbotHandler.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "cryptoscope.co/go/muxrpc"
  7. "github.com/pkg/errors"
  8. )
  9. type sbotHandler struct {
  10. remoteID string
  11. }
  12. type retWhoami struct {
  13. ID string `json:"id"`
  14. }
  15. func (h sbotHandler) HandleCall(ctx context.Context, req *muxrpc.Request) {
  16. // TODO: push manifest check into muxrpc
  17. if req.Type == "" {
  18. req.Type = "async"
  19. }
  20. switch m := strings.Join(req.Method, "."); m {
  21. case "whoami":
  22. err := req.Return(ctx, retWhoami{"heinbloed"})
  23. if err != nil {
  24. log.Log("call", "whoami", "err", err)
  25. }
  26. case "gossip.connect":
  27. if len(req.Args) != 1 {
  28. req.Stream.CloseWithError(errors.Errorf("bad request"))
  29. return
  30. }
  31. addr := req.Args[0].(string)
  32. ret := make(map[string]interface{})
  33. ret["addr"] = addr
  34. err := ssbTryGossip(ctx, addr)
  35. if err != nil {
  36. log.Log("try", "gossip.connect", "err", err)
  37. req.Stream.CloseWithError(errors.Wrap(err, "failed gossiping"))
  38. return
  39. } else {
  40. ret["worked"] = true
  41. }
  42. err = req.Return(ctx, ret)
  43. if err != nil {
  44. log.Log("call", "gossip.connect", "err", err)
  45. }
  46. default:
  47. log.Log("warning", "unhandled call", "method", m, "args", fmt.Sprintf("%+v", req.Args))
  48. req.Stream.CloseWithError(errors.Errorf("unhandled call"))
  49. }
  50. }
  51. func (h sbotHandler) HandleConnect(ctx context.Context, e muxrpc.Endpoint) {
  52. /* calling back
  53. ret, err := e.Async(ctx, "str", []string{"whoami"})
  54. if err != nil {
  55. log.Log("handleConnect", "whoami", "err", err)
  56. return
  57. }
  58. */
  59. log.Log("handle", "connect", "Hello", h.remoteID)
  60. }
  61. type retPing struct {
  62. Pong string
  63. }
  64. func (h sbotHandler) GossipPing(timout int) retPing {
  65. return retPing{"test"}
  66. }