package peer import "os" import "io/ioutil" import "net" import "net/url" import "src.eruta.nl/beoran/bdjncl/b1" import "src.eruta.nl/beoran/bdjncl/picol" import "log" import "fmt" import "context" import "strconv" import "math/rand" const ( StateDisconnected = iota StateConnecting StateConnected StateIdle StateDisconnecting ) type Peer struct { Config map[string]string context.Context Out chan b1.Message In chan b1.Message On *url.URL Id int64 As *url.URL } type LocalPeer struct { Peer *picol.Interp Server net.Listener Client net.Conn Peers []Peer } func (p *Peer) PicolConfig(i *picol.Interp, argv []string, privdata interface{}) (string, error) { if len(argv) > 2 { p.Config[argv[1]] = argv[2] } if len(argv) > 1 { return p.Config[argv[1]], nil } return "", picol.ArityErr(i, argv[0], argv) } func (p *Peer) PicolEnv(i *picol.Interp, argv []string, privdata interface{}) (string, error) { if len(argv) > 2 { os.Setenv(argv[2], argv[3]) } if len(argv) > 1 { return os.Getenv(argv[1]), nil } return "", picol.ArityErr(i, argv[0], argv) } func picolLog(i *picol.Interp, argv []string, privdata interface{}) (string, error) { if len(argv) < 2 { return "", picol.ArityErr(i, argv[0], argv) } log.Printf(argv[1], argv[2:]) return "", nil } func (p *LocalPeer) PrepareInterp() { p.Interp = picol.InitInterp() p.Interp.RegisterCoreCommands() p.Interp.RegisterCommand("conf", p.PicolConfig, p) p.Interp.RegisterCommand("env", p.PicolEnv, p) p.Interp.RegisterCommand("log", picolLog, p) } func (p *LocalPeer) ReadConfigName(fileName string) error { p.Config = map[string]string{} buf, err := ioutil.ReadFile(fileName) if err != nil { return err } result, err := p.Interp.Eval(string(buf)) if err != nil { return fmt.Errorf("ERROR: %s: %s", result, err) } return nil } func (p *Peer) UpdateFromConfig() error { var err error if val, ok := p.Config["id"]; ok { p.Id, err = strconv.ParseInt(val, 0, 64) if err != nil { return err } } else { p.Id = rand.Int63() } if val, ok := p.Config["on"]; ok { p.On, err = url.Parse(val) if err != nil { return err } } if val, ok := p.Config["as"]; ok { p.As, err = url.Parse(val) if err != nil { return err } } return nil } /* func (p *Peer) WriteConfigName(fileName string) error { fout, err := os.Create(fileName string) if err != nil { return err } return nil } */