func (ei *EntityIndex) Verify() []error { var errs []error for k, v := range ei.byid { if k != v.Id() { errs = append(errs, fmt.Error("Key %v does not match entity id %v for entity %v", k, v.Id(), v.Name())) } } for k, v := range ei.byname { if k != v.Name() { errs = append(errs, fmt.Error("Key %v does not match entity name %v for entity %v", k, v.Name(), v.Id())) } } return errs }
func (bus *Bus) startListener() (net.Listener, error) { bus.Lock() defer bus.Unlock() if bus.listener != nil { return nil, fmt.Error("Bus already listening") } l, err := net.Listen(bus.config.Net, fmt.String("%v:%v", bus.config.Address, bus.config.Port)) if err != nil { return nil, fmt.Error("Error starting listener: %v", err) } bus.listener = l return l, nil }
func (g *Geometry) AddLayout(l Layout) error { if g.LayoutExists(l.Id()) { return fmt.Error("layout %v already exists", l.Id()) } g.layouts[l.Id()] = l return nil }
func (node *chanTransport) Receive() (*Msg, error) { msg, ok := <-node.rx if ok { return msg, nil } return nil, fmt.Error("EOF") }
func (client *Client) Receive() (*Msg, error) { msg, err := client.transport.Receive() if err != nil { client.bus.onClientError(client, fmt.Error("Receive: %v", err)) } return msg, err }
func (client *Client) Send(msg *Msg) error { err := client.transport.Send(msg) if err != nil { client.bus.onClientError(client, fmt.Error("Send: %v", err)) return err } return nil }
func (ctx *context) stop() error { if ctx.status != ctxStarted { return fmt.Error("Service %v not started", ctx.Name()) } ctx.status = ctxStopping err := ctx.service.Stop(ctx) ctx.status = ctxStopped return err }
func (r *contextList) Get(name string) (*context, error) { l := r.GetAll() for _, ctx := range l { if ctx.name == name { return ctx, nil } } return nil, fmt.Error("No context with name %v", name) }
func (bus *bus) Subscribe(topic string, h Handler) Closer { if h == nil { panic(fmt.Error("h is nil")) } return bus.subscribe(&handler{ id: bus.id, topic: topic, handler: h, }) }
func (node *chanTransport) Send(msg *Msg) (err error) { defer func() { e := recover() if e != nil { err = fmt.Error("%v", e) } }() node.tx <- msg return }
func (node *chanTransport) Close() (err error) { defer func() { e := recover() if e != nil { err = fmt.Error("%v", e) } }() close(node.rx) close(node.tx) return }
func (ei *EntityIndex) CreateId(name string) (int, string, error) { if ei.NameExists(name) { return -1, "", fmt.Error("Name %v already exists") } id := ei.nextid for ; ei.IdExists(id); id++ { } ei.nextid = id + 1 ei.byid[id] = nil ei.byname[name] = nil return id, name, nil }
func parseFile(r *bufio.Reader) (*Config, error) { cfg := newConfig() s := cfg.section("") lineNo := 0 for { lineNo++ line, err := r.ReadString('\n') if err == io.EOF { return cfg, nil } else if err != nil { return nil, err } line = strings.TrimSpace(line) if isEmpty(line) || isComment(line) { continue } if name, ok := trySectionHeader(line); ok { if _, isdup := cfg.sections[name]; isdup { return nil, fmt.Error("Duplicate section header [%v] at line %v", name, lineNo) } else { s = newNestedSection(cfg.sections[""]) s.set("__name__", name) cfg.sections[name] = s } continue } if key, val, ok := tryKeyValuePair(line); ok { s.set(key, val) continue } return nil, fmt.Error("Error parsing configuration at line %v", lineNo) } }
func (c *carrier) Resize(size int) error { l := len(c.children) if size == l { return nil // nothing to do... } if !canResize(c.children, size) { return fmt.Error("Resizing would drop entities") } old := c.children c.children = make([]Entity, size) copy(c.children, old) return nil }
func (ctx *context) start() error { if ctx.status != ctxStopped { err := fmt.Error("Service %v not stopped", ctx.Name()) ctx.log.Printf("Error starting service: %v: %v", ctx.Name(), err) return err } ctx.status = ctxStarting err := ctx.service.Start(ctx) if err != nil { ctx.log.Printf("Error starting service %v: %v", ctx.Name(), err) ctx.status = ctxStopped return err } ctx.status = ctxStarted return nil }
func FromFile(path string) (*Config, error) { path, err := filepath.Abs(path) if err != nil { return nil, err } f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() if cfg, err := FromReader(f); err != nil { return nil, fmt.Error("Error parsing file %v: %v", path, err) } else { return cfg, nil } }
func (client *Client) pumpMessages() { client.routines.Add(1) defer func() { recovered := recover() client.routines.Done() if recovered != nil { err := fmt.Error("pumpMessages: %v", recovered) client.bus.onClientError(client, err) } client.Close() }() for client.running() { msg, err := client.Receive() if err != nil { return } client.bus.Handle(client, msg) } }
func FromArgs() (*Config, error) { set := flag.NewFlagSet("cfg", flag.ContinueOnError) var rawCfgArgs argcfg set.Var(&rawCfgArgs, "cfg", "comma-separated list of configuration key-value pairs") err := set.Parse(os.Args[1:]) if err != nil { return nil, err } cfg := newConfig() for _, rawkv := range rawCfgArgs { kv := strings.TrimSpace(rawkv) if key, val, ok := tryKeyValuePair(kv); ok { cfg.set("", key, val) } else { return nil, fmt.Error("Invalid commandline key-value pair: '%v'", kv) } } return cfg, nil }
func (m *Model) AddStaticCarrier(name string, x, y, rows, cols int) (Carrier, error) { if m.entityExists(name) { return nil, fmt.Error("Entity with name %v already exists", name) } c, err := m.newCarrier(name, rows*cols) if err != nil { return nil, err } carrierLayout := m.geometry.newGridLayout(rows, cols, m.gridSize()) carrierLayout.id = c.Id() m.geometry.AddLayout(carrierLayout) b := m.grid.getBoundingBox(x, y, rows, cols) m.sitelayout.set(c.Index(), *b) m.site.add(c) return c, nil }
func (s *Server) Refresh(c *Command) { fmt.Printline("Received a refresh") cmd := new(Refresh) err := json.Unmarshal(c.MsgBody, cmd) if err != nil { s.InvalidCommand(c, err) return } m, ok := s.models[cmd.ModelName] if !ok { s.InvalidCommand(c, fmt.Error("Unknown model %v", cmd.ModelName)) return } s.Respond(c, &ModelCreated{ Name: m.Name(), Rows: m.grid.rows, Cols: m.grid.cols, GridX: m.grid.size.W, GridY: m.grid.size.L, }) }
func (msg *GetConfig) Wait() (Config, error) { if cfg, ok := <-msg.config; ok { return cfg, nil } return nil, fmt.Error("Config %v not found", msg.name) }
func (core *core) Handle(msg Msg) { switch msg := msg.(type) { case *registerService: if msg.service == nil { return } core.ctx.Log().Printf("Registering service %v", msg.service.Name()) core.Register(msg.service) msg.Success() case *unregisterService: core.ctx.Log().Printf("Unregistering service %v", msg.Name) core.ctx.Run(func() { msg.Error(fmt.Error("Not implemented")) }) case *startService: core.ctx.Log().Printf("Starting service %v", msg.Name) core.ctx.Run(func() { ctx, err := core.contexts.Get(msg.Name) if err == nil { ctx.receive(msg) } else { msg.Error(err) } }) case *stopService: core.ctx.Run(func() { ctx, err := core.contexts.Get(msg.Name) if err == nil { core.ctx.Log().Printf("Stopping service %v", msg.Name) ctx.receive(msg) } else { msg.Error(err) } }) case *startAllServices: core.ctx.Log().Print("Starting all services") core.ctx.Run(func() { contexts := core.contexts.GetAll() for _, ctx := range contexts { if autostart, ok := ctx.Cfg().Bool("autostart"); ok && autostart { core.ctx.Log().Printf("Starting service %v", ctx.Name()) result := NewResult() ctx.receive(&startService{Name: ctx.Name(), Result: result}) err := result.Wait() if err != nil { msg.Error(err) return } } } msg.Success() }) case *listServices: core.ctx.Log().Print("Listing services") contexts := core.contexts.GetAll() list := make([]ServiceState, 0, len(contexts)) timestamp := time.Now() for _, ctx := range contexts { list = append(list, ServiceState{ Id: ctx.id, Name: ctx.name, Status: ctx.status, Timestamp: timestamp, }) } msg.Result <- list case *stopAllServices: core.ctx.Log().Print("Stopping all services") core.ctx.Run(func() { contexts := core.contexts.GetAll() lastindex := len(contexts) - 1 for i := range contexts { ctx := contexts[lastindex-i] result := NewResult() core.ctx.Log().Printf("Stopping service %v", ctx.Name()) ctx.receive(&stopService{Name: ctx.Name(), Result: result}) err := result.Wait() if err != nil { msg.Error(err) return } } msg.Success() }) default: core.ctx.Log().Printf("Unhandled message %T %+v", msg, msg) } }