func scan(d mojom.Discovery, query string) (<-chan mojom.Update_Pointer, func(), error) {
	ch := make(chan mojom.Update_Pointer, 10)
	handler := &mockScanHandler{ch}
	req, ptr := mojom.CreateMessagePipeForScanHandler()
	stub := mojom.NewScanHandlerStub(req, handler, bindings.GetAsyncWaiter())

	closer, e1, e2 := d.Scan(query, ptr)
	if e1 != nil {
		close(ch)
		return nil, nil, errors.New(e1.Msg)
	}
	if e2 != nil {
		close(ch)
		return nil, nil, e2
	}

	wg := new(sync.WaitGroup)
	wg.Add(1)
	go func() {
		defer wg.Done()
		for {
			if err := stub.ServeRequest(); err != nil {
				connErr, ok := err.(*bindings.ConnectionError)
				if !ok || !connErr.Closed() {
					log.Println(err)
				}
				break
			}
		}
	}()

	stop := func() {
		p := mojom.NewCloserProxy(*closer, bindings.GetAsyncWaiter())
		p.Close()
		p.Close_Proxy()
		stub.Close()
		wg.Wait()
		close(ch)
	}
	return ch, stop, nil
}
Beispiel #2
0
func (d *delegate) Initialize(ctx application.Context) {
	req, ptr := discovery.CreateMessagePipeForDiscovery()
	ctx.ConnectToApplication("https://mojo.v.io/discovery.mojo").ConnectToService(&req)

	scanHandlerReq, scanHandlerPtr := discovery.CreateMessagePipeForScanHandler()
	scanHandlerStub := discovery.NewScanHandlerStub(scanHandlerReq, &scanHandler{}, bindings.GetAsyncWaiter())

	dProxy := discovery.NewDiscoveryProxy(ptr, bindings.GetAsyncWaiter())
	closerPtr, e1, e2 := dProxy.Scan(`v.InterfaceName="v.io/discovery.T"`, scanHandlerPtr)
	if e1 != nil || e2 != nil {
		log.Printf("Failed to scan: %v, %v", e1, e2)
		scanHandlerStub.Close()
		dProxy.Close_Proxy()
		return
	}

	go func() {
		for {
			if err := scanHandlerStub.ServeRequest(); err != nil {
				connErr, ok := err.(*bindings.ConnectionError)
				if !ok || !connErr.Closed() {
					log.Println(err)
				}
				break
			}
		}
	}()

	d.stop = func() {
		cProxy := discovery.NewCloserProxy(*closerPtr, bindings.GetAsyncWaiter())
		cProxy.Close()
		cProxy.Close_Proxy()

		scanHandlerStub.Close()
		dProxy.Close_Proxy()
	}
}