peer.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package peer
  2. import "os"
  3. import "io/ioutil"
  4. import "net"
  5. import "net/url"
  6. import "src.eruta.nl/beoran/bdjncl/b1"
  7. import "src.eruta.nl/beoran/bdjncl/picol"
  8. import "log"
  9. import "fmt"
  10. import "context"
  11. import "strconv"
  12. import "math/rand"
  13. const (
  14. StateDisconnected = iota
  15. StateConnecting
  16. StateConnected
  17. StateIdle
  18. StateDisconnecting
  19. )
  20. type Peer struct {
  21. Config map[string]string
  22. context.Context
  23. Out chan b1.Message
  24. In chan b1.Message
  25. On *url.URL
  26. Id int64
  27. As *url.URL
  28. }
  29. type LocalPeer struct {
  30. Peer
  31. *picol.Interp
  32. Server net.Listener
  33. Client net.Conn
  34. Peers []Peer
  35. }
  36. func (p *Peer) PicolConfig(i *picol.Interp, argv []string, privdata interface{}) (string, error) {
  37. if len(argv) > 2 {
  38. p.Config[argv[1]] = argv[2]
  39. }
  40. if len(argv) > 1 {
  41. return p.Config[argv[1]], nil
  42. }
  43. return "", picol.ArityErr(i, argv[0], argv)
  44. }
  45. func (p *Peer) PicolEnv(i *picol.Interp, argv []string, privdata interface{}) (string, error) {
  46. if len(argv) > 2 {
  47. os.Setenv(argv[2], argv[3])
  48. }
  49. if len(argv) > 1 {
  50. return os.Getenv(argv[1]), nil
  51. }
  52. return "", picol.ArityErr(i, argv[0], argv)
  53. }
  54. func picolLog(i *picol.Interp, argv []string, privdata interface{}) (string, error) {
  55. if len(argv) < 2 {
  56. return "", picol.ArityErr(i, argv[0], argv)
  57. }
  58. log.Printf(argv[1], argv[2:])
  59. return "", nil
  60. }
  61. func (p *LocalPeer) PrepareInterp() {
  62. p.Interp = picol.InitInterp()
  63. p.Interp.RegisterCoreCommands()
  64. p.Interp.RegisterCommand("conf", p.PicolConfig, p)
  65. p.Interp.RegisterCommand("env", p.PicolEnv, p)
  66. p.Interp.RegisterCommand("log", picolLog, p)
  67. }
  68. func (p *LocalPeer) ReadConfigName(fileName string) error {
  69. p.Config = map[string]string{}
  70. buf, err := ioutil.ReadFile(fileName)
  71. if err != nil {
  72. return err
  73. }
  74. result, err := p.Interp.Eval(string(buf))
  75. if err != nil {
  76. return fmt.Errorf("ERROR: %s: %s", result, err)
  77. }
  78. return nil
  79. }
  80. func (p *Peer) UpdateFromConfig() error {
  81. var err error
  82. if val, ok := p.Config["id"]; ok {
  83. p.Id, err = strconv.ParseInt(val, 0, 64)
  84. if err != nil {
  85. return err
  86. }
  87. } else {
  88. p.Id = rand.Int63()
  89. }
  90. if val, ok := p.Config["on"]; ok {
  91. p.On, err = url.Parse(val)
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. if val, ok := p.Config["as"]; ok {
  97. p.As, err = url.Parse(val)
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. }
  104. /*
  105. func (p *Peer) WriteConfigName(fileName string) error {
  106. fout, err := os.Create(fileName string)
  107. if err != nil {
  108. return err
  109. }
  110. return nil
  111. }
  112. */