// Searches for new syncgroups being advertised, sends found syncgroups to sgChan func ScanForSG(ctx *context.T, quit chan bool, u *uistate.UIState) { mdns, err := mdns.New("") if err != nil { ctx.Fatalf("Plugin failed: %v", err) } ds := ldiscovery.NewWithPlugins([]ldiscovery.Plugin{mdns}) fmt.Printf("Start scanning...\n") ch, err := ds.Scan(ctx, fmt.Sprintf("v.InterfaceName = \"%s\"", util.CroupierInterface)) if err != nil { ctx.Fatalf("Scan failed: %v", err) } instances := make(map[string]string) loop: for { select { case update := <-ch: GetSG(instances, update, u) view.LoadDiscoveryView(u) case <-signals.ShutdownOnSignals(ctx): break loop case <-quit: break loop } } }
func serve(ctx *context.T, env *cmdline.Env, args []string) error { name := "" if len(args) > 0 { name = args[0] fmt.Printf("mounting under: %s\n", name) } _, server, err := v23.WithNewServer(ctx, name, defaultMediaServer(), security.AllowEveryone()) if err != nil { return err } fmt.Printf("Listening at: %s", server.Status().Endpoints[0].Name()) <-signals.ShutdownOnSignals(ctx) return nil }
func runLockD(ctx *context.T, env *cmdline.Env, args []string) error { if len(configDir) == 0 { return errors.New("--config-dir must be specified") } if finfo, err := os.Stat(configDir); os.IsNotExist(err) { if err := os.MkdirAll(configDir, os.FileMode(0700)); err != nil { return fmt.Errorf("could not create configuration directory %v: %v", configDir, err) } } else if err != nil || !finfo.IsDir() { return fmt.Errorf("--config-dir=%v is not a directory", configDir) } shutdown, err := startServer(ctx, configDir) if err != nil { return fmt.Errorf("failed to start server: %v", err) } <-signals.ShutdownOnSignals(ctx) shutdown() return nil }
// Advertises a set of game log and game settings syncgroups func Advertise(logAddress, settingsAddress, gameStartData string, quit chan bool, ctx *context.T) { ctx, stop := context.WithCancel(ctx) mdns, err := mdns.New("") if err != nil { ctx.Fatalf("mDNS failed: %v", err) } discoveryService := ldiscovery.NewWithPlugins([]ldiscovery.Plugin{mdns}) gameService := discovery.Service{ InstanceName: "A sample game service", InterfaceName: util.CroupierInterface, Attrs: map[string]string{"settings_sgname": settingsAddress, "game_start_data": gameStartData}, Addrs: []string{logAddress}, } if _, err := discoveryService.Advertise(ctx, &gameService, nil); err != nil { ctx.Fatalf("Advertise failed: %v", err) } select { case <-signals.ShutdownOnSignals(ctx): stop() case <-quit: stop() } }
func main() { ctx, shutdown := v23.Init() defer shutdown() // Run server if *to == "" { // We are not the caller, so make the RPC available for the // caller to call in on. bsrv := ifc.BridgeServer(makeImpl()) _, server, err := v23.WithNewServer(ctx, *serviceName, bsrv, security.DefaultAuthorizer()) if err != nil { ctx.Error("Error serving service: ", err) return } endpoint := server.Status().Endpoints[0] fmt.Printf("Listening at: %v\n", endpoint) // Wait forever. <-signals.ShutdownOnSignals(ctx) } else if *to != "" && *from != "" { // pipe mode ifct := ifcTopics(*topics) tmout := options.ChannelTimeout(*timeout) leftc := ifc.BridgeClient(*to) rightc := ifc.BridgeClient(*from) leftcc, err := leftc.Link(ctx, ifct, tmout) if err != nil { ctx.Error(err) return } rightcc, err := rightc.Link(ctx, ifct, tmout) if err != nil { ctx.Error(err) return } errCh := make(chan error, 2) wg := &sync.WaitGroup{} wg.Add(2) go linkToLink(leftcc.RecvStream(), rightcc.SendStream(), errCh, wg) go linkToLink(rightcc.RecvStream(), leftcc.SendStream(), errCh, wg) wg.Wait() select { case err := <-errCh: log.Print("pipe error: ", err) default: // don't block on channel read } } else { cc, mu, err := mqttConnect() if err != nil { ctx.Error("mqtt connect: ", err) return } bc := ifc.BridgeClient(*to) ifct := ifcTopics(*topics) bcc, err := bc.Link(ctx, ifct, options.ChannelTimeout(*timeout)) if err != nil { ctx.Error(err) return } done := make(chan error, 2) go func() { done <- transmitter(ifct, bcc.SendStream(), cc, mu) println("send done") }() go func() { done <- receiver(bcc.RecvStream(), cc, mu) println("recv done") }() err = <-done log.Print("Stopped with error ", err) // Stop sender by closing cc.Incoming cc.Disconnect() } }