peer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "src.eruta.nl/beoran/bdjncl/config"
  9. import "log"
  10. import "fmt"
  11. import "context"
  12. import "strconv"
  13. import "math/rand"
  14. import "time"
  15. const (
  16. StateDisconnected = iota
  17. StateConnecting
  18. StateConnected
  19. StateIdle
  20. StateDisconnecting
  21. )
  22. type Peer struct {
  23. Conf map[string]string
  24. context.Context
  25. Out chan b1.Message
  26. In chan b1.Message
  27. On *url.URL
  28. Cmd *url.URL
  29. Id int64
  30. As *url.URL
  31. }
  32. type LocalPeer struct {
  33. config.Config
  34. Peer
  35. *picol.Interp
  36. Server net.Listener
  37. Command net.Listener
  38. Client net.Conn
  39. Peers []Peer
  40. }
  41. func (l LocalPeer) Start() error {
  42. var err error
  43. l.In = make(chan b1.Message)
  44. l.Out = make(chan b1.Message)
  45. l.Server, err = net.Listen("tcp", "0.0.0.0:8390")
  46. if err != nil {
  47. return err
  48. }
  49. go l.Serve()
  50. l.Command, err = net.Listen("unix", "/tmp/bdjncl")
  51. if err != nil {
  52. return err
  53. }
  54. go l.Commander()
  55. go l.Connect()
  56. return nil
  57. }
  58. func (l LocalPeer) Connect() {
  59. for {
  60. time.Sleep(1 * time.Second)
  61. conn, err := net.Dial("tcp", "localhost:8390")
  62. if err != nil {
  63. log.Printf("Could not accept connection: %s", err)
  64. }
  65. l.OnConnected(conn)
  66. }
  67. }
  68. func (l LocalPeer) Serve() {
  69. for {
  70. conn, err := l.Server.Accept()
  71. if err != nil {
  72. log.Printf("Could not accept connection: %s", err)
  73. time.Sleep(1 * time.Second)
  74. } else {
  75. go l.OnConnected(conn)
  76. }
  77. }
  78. }
  79. func (l LocalPeer) OnConnected(conn net.Conn) {
  80. for {
  81. log.Printf("Connected: %s", conn.RemoteAddr())
  82. time.Sleep(1 * time.Second)
  83. }
  84. }
  85. func (l LocalPeer) Commander() {
  86. for {
  87. time.Sleep(1 * time.Second)
  88. }
  89. }
  90. func (p *Peer) PicolConfig(i *picol.Interp, argv []string, privdata interface{}) (string, error) {
  91. if len(argv) > 2 {
  92. p.Conf[argv[1]] = argv[2]
  93. }
  94. if len(argv) > 1 {
  95. return p.Conf[argv[1]], nil
  96. }
  97. return "", picol.ArityErr(i, argv[0], argv)
  98. }
  99. func (p *Peer) PicolEnv(i *picol.Interp, argv []string, privdata interface{}) (string, error) {
  100. if len(argv) > 2 {
  101. os.Setenv(argv[2], argv[3])
  102. }
  103. if len(argv) > 1 {
  104. return os.Getenv(argv[1]), nil
  105. }
  106. return "", picol.ArityErr(i, argv[0], argv)
  107. }
  108. func picolLog(i *picol.Interp, argv []string, privdata interface{}) (string, error) {
  109. if len(argv) < 2 {
  110. return "", picol.ArityErr(i, argv[0], argv)
  111. }
  112. log.Printf(argv[1], argv[2:])
  113. return "", nil
  114. }
  115. func (p *LocalPeer) PrepareInterp() {
  116. p.Interp = picol.InitInterp()
  117. p.Interp.RegisterCoreCommands()
  118. p.Interp.RegisterCommand("conf", p.PicolConfig, p)
  119. p.Interp.RegisterCommand("env", p.PicolEnv, p)
  120. p.Interp.RegisterCommand("log", picolLog, p)
  121. }
  122. func (p *LocalPeer) ReadConfigName(fileName string) error {
  123. p.Conf = map[string]string{}
  124. buf, err := ioutil.ReadFile(fileName)
  125. if err != nil {
  126. return err
  127. }
  128. result, err := p.Interp.Eval(string(buf))
  129. if err != nil {
  130. return fmt.Errorf("ERROR: %s: %s", result, err)
  131. }
  132. return nil
  133. }
  134. func (p *Peer) UpdateFromConfig() error {
  135. var err error
  136. if val, ok := p.Conf["id"]; ok {
  137. p.Id, err = strconv.ParseInt(val, 0, 64)
  138. if err != nil {
  139. return err
  140. }
  141. } else {
  142. p.Id = rand.Int63()
  143. }
  144. if val, ok := p.Conf["on"]; ok {
  145. p.On, err = url.Parse(val)
  146. if err != nil {
  147. return err
  148. }
  149. }
  150. if val, ok := p.Conf["as"]; ok {
  151. p.As, err = url.Parse(val)
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. return nil
  157. }
  158. /*
  159. func (p *Peer) WriteConfigName(fileName string) error {
  160. fout, err := os.Create(fileName string)
  161. if err != nil {
  162. return err
  163. }
  164. return nil
  165. }
  166. */