当前位置: 首页>>代码示例>>Golang>>正文


Golang context.T类代码示例

本文整理汇总了Golang中v/io/v23/context.T的典型用法代码示例。如果您正苦于以下问题:Golang T类的具体用法?Golang T怎么用?Golang T使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了T类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ScanForSG

// 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
		}
	}
}
开发者ID:vanadium,项目名称:croupier,代码行数:26,代码来源:client.go

示例2: sendOneInvite

// sendOneInvite sends invitations to all the addresses in addrs and returns the one that accepted it.
// All addrs are assumed to be equivalent and thus at most one Invite RPC will succeed.
//
// TODO: This is aiming to replicate what the RPC stack does for all the
// addresses a single name resolved to. Should all these addresses discovered
// somehow be encapsulated in a single object name?
func sendOneInvite(ctx *context.T, addrs []string) string {
	// Give at most 1 second for these connections to be made, if they
	// can't be made then consider the peer bad and ignore it.
	// TODO: Should these RPCs also use the "connection timeout" that might be implemented
	// as per proposal: https://docs.google.com/a/google.com/document/d/1prtxGhSR5TaL0lc_iDRC0Q6H1Drbg2T0x7MWVb_ZCSM/edit?usp=sharing
	ctx, cancel := context.WithTimeout(ctx, maxInvitationWaitTime)
	defer cancel()
	ch := make(chan string)
	for _, addr := range addrs {
		go func(addr string) {
			err := spec.ScreenClient(addr).Invite(ctx, options.ServerAuthorizer{security.AllowEveryone()})
			ctx.Infof("Invitation to %v sent, error: %v", addr, err)
			if err == nil {
				ch <- addr
				return
			}
			ch <- ""
		}(addr)
	}
	for i := range addrs {
		if ret := <-ch; len(ret) > 0 {
			// Drain the rest and return
			go func() {
				i++
				for i < len(addrs) {
					<-ch
				}
			}()
			return ret
		}
	}
	return ""
}
开发者ID:asimshankar,项目名称:triangles,代码行数:39,代码来源:net.go

示例3: Link

func (f *impl) Link(ctx *context.T, sc ifc.BridgeLinkServerCall, topics []ifc.Topic) error {
	ctx.Info("Link for topics ", topics)

	done := make(chan error, 2)

	cc, mu, err := mqttConnect()
	if err != nil {
		return err
	}

	// transmitter: do mqtt subscribe, then copy messages to output stream
	go func() {
		done <- transmitter(topics, sc.SendStream(), cc, mu)
	}()

	// receiver: read the input stream and copy to mqtt via cc.Publish
	go func() {
		done <- receiver(sc.RecvStream(), cc, mu)
	}()

	err = <-done

	// Stop sender by closing cc.Incoming
	cc.Disconnect()

	return err
}
开发者ID:jeffallen,项目名称:mqtt,代码行数:27,代码来源:service.go

示例4: Lookup

func (v23pd *dispatcher) Lookup(ctx *context.T, suffix string) (interface{}, security.Authorizer, error) {
	ctx.Infof("dispatcher.Lookup for suffix: %s", suffix)
	return fakeService{
		appctx: v23pd.appctx,
		suffix: suffix,
		ids:    bindings.NewCounter(),
	}, security.AllowEveryone(), nil
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:8,代码来源:serverproxy.go

示例5: Invoke

// Invoke calls the mojom service based on the suffix and converts the mojom
// results (a struct) to Vanadium results (a slice of *vom.RawBytes).
// Note: The argptrs from Prepare are reused here. The vom bytes should have
// been decoded into these argptrs, so there are actual values inside now.
func (fs fakeService) Invoke(ctx *context.T, call rpc.StreamServerCall, method string, argptrs []interface{}) (results []interface{}, _ error) {
	// fs.suffix consists of the mojo url and the application/interface name.
	// The last part should be the name; everything else is the url.
	parts := strings.Split(fs.suffix, "/")
	mojourl := strings.Join(parts[:len(parts)-1], "/") // e.g., mojo:go_remote_echo_server. May be defined in a BUILD.gn file.
	mojoname := parts[len(parts)-1]                    // e.g., mojo::examples::RemoteEcho. Defined from the interface + module.

	// Create the generic message pipe. r is a bindings.InterfaceRequest, and
	// p is a bindings.InterfacePointer.
	r, p := bindings.CreateMessagePipeForMojoInterface()
	v := v23ServiceRequest{
		request: r,
		name:    mojoname,
	} // v is an application.ServiceRequest with mojoname

	// Connect to the mojourl.
	fs.appctx.ConnectToApplication(mojourl).ConnectToService(&v)

	// Then assign a new router the FakeService.
	// This will never conflict because each FakeService is only invoked once.
	fs.router = bindings.NewRouter(p.PassMessagePipe(), bindings.GetAsyncWaiter())
	defer fs.Close_Proxy()

	ctx.Infof("Fake Service Invoke (Remote Signature: %q -- %q)", mojourl, mojoname)

	// Vanadium relies on type information, so we will retrieve that first.
	mojomInterface, desc, err := fs.callRemoteSignature(mojourl, mojoname)
	if err != nil {
		return nil, err
	}

	ctx.Infof("Fake Service Invoke Signature %v", mojomInterface)
	ctx.Infof("Fake Service Invoke (Remote Method: %v)", method)

	// With the type information, we can make the method call to the remote interface.
	methodResults, err := fs.callRemoteMethod(ctx, method, mojomInterface, desc, argptrs)
	if err != nil {
		ctx.Errorf("Method called failed: %v", err)
		return nil, err
	}

	ctx.Infof("Fake Service Invoke Results %v", methodResults)

	// Convert methodResult to results.
	results = make([]interface{}, len(methodResults))
	for i := range methodResults {
		results[i] = &methodResults[i]
	}
	return results, nil
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:54,代码来源:serverproxy.go

示例6: Invite

func (nm *networkManager) Invite(ctx *context.T, call rpc.ServerCall) error {
	inviter := call.RemoteEndpoint().Name()
	response := make(chan error)
	nm.inviteRPCs <- Invitation{
		Name:      inviter,
		Color:     selectColor(call.Security().RemoteBlessings().PublicKey()),
		Response:  response,
		Withdrawn: ctx.Done(),
	}
	if err := <-response; err != nil {
		return err
	}
	blessings, rejected := security.RemoteBlessingNames(ctx, call.Security())
	ctx.Infof("Accepted invitation from %[email protected]%v (rejected blessings: %v)", blessings, inviter, rejected)
	return nil
}
开发者ID:asimshankar,项目名称:triangles,代码行数:16,代码来源:net.go

示例7: channel2rpc

func channel2rpc(ctx *context.T, src <-chan *spec.Triangle, dst string, errch chan<- error, myScreen chan<- *spec.Triangle) {
	for t := range src {
		// This is an "interactive" game, if an RPC doesn't succeed in say
		ctxTimeout, cancel := context.WithTimeout(ctx, maxTriangleGiveTime)
		if err := spec.ScreenClient(dst).Give(ctxTimeout, *t, options.ServerAuthorizer{security.AllowEveryone()}); err != nil {
			cancel()
			returnTriangle(t, myScreen)
			ctx.Infof("%q.Give failed: %v, aborting connection with remote screen", dst, err)
			errch <- err
			break
		}
		cancel()
	}
	for t := range src {
		returnTriangle(t, myScreen)
	}
	ctx.VI(1).Infof("Exiting goroutine with connection to %q", dst)
}
开发者ID:asimshankar,项目名称:triangles,代码行数:18,代码来源:net.go

示例8: Give

func (nm *networkManager) Give(ctx *context.T, call rpc.ServerCall, t spec.Triangle) error {
	if ctx.V(3) {
		blessings, rejected := security.RemoteBlessingNames(ctx, call.Security())
		ctx.Infof("Took a triangle from %[email protected]%v (rejected blessings: %v)", blessings, call.RemoteEndpoint().Name(), rejected)
	}
	// Transform from sender's coordinates to our coordinates.
	// The assumption is that if the triangle was to the left of the
	// sender's coordinate system, then it will appear on our right and
	// vice-versa.
	switch {
	case t.X < -1:
		t.X += 2
	case t.X > 1:
		t.X -= 2
	}
	nm.myScreen <- &t
	return nil
}
开发者ID:asimshankar,项目名称:triangles,代码行数:18,代码来源:net.go

示例9: callRemoteWithResponse

// A helper function that sends a remote message that expects a response.
func (fs fakeService) callRemoteWithResponse(ctx *context.T, message *bindings.Message) (outMessage *bindings.Message, err error) {
	ctx.Infof("callRemoteGeneric: Send message along the router")

	readResult := <-fs.router.AcceptWithResponse(message)
	if err = readResult.Error; err != nil {
		return
	}

	ctx.Infof("callRemoteGeneric: Audit response message header flag")
	// The message flag we receive back must be a bindings.MessageIsResponseFlag
	if readResult.Message.Header.Flags != bindings.MessageIsResponseFlag {
		err = &bindings.ValidationError{bindings.MessageHeaderInvalidFlags,
			fmt.Sprintf("invalid message header flag: %v", readResult.Message.Header.Flags),
		}
		return
	}

	ctx.Infof("callRemoteGeneric: Audit response message header type")
	// While the mojo service we called into will return a header whose
	// type must match our outgoing one.
	if got, want := readResult.Message.Header.Type, message.Header.Type; got != want {
		err = &bindings.ValidationError{bindings.MessageHeaderUnknownMethod,
			fmt.Sprintf("invalid method in response: expected %v, got %v", want, got),
		}
		return
	}

	return readResult.Message, nil
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:30,代码来源:serverproxy.go

示例10: Advertise

// 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()
	}
}
开发者ID:vanadium,项目名称:croupier,代码行数:24,代码来源:server.go

示例11: sendInvites

func sendInvites(ctx *context.T, disc discovery.T, notify chan<- string) {
	ctx.Infof("Scanning for peers to invite")
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	updates, err := disc.Scan(ctx, fmt.Sprintf("v.InterfaceName=%q", interfaceName))
	if err != nil {
		ctx.Panic(err)
	}
	for u := range updates {
		if u.IsLost() {
			continue
		}
		ctx.Infof("Sending invitations to %+v", u.Addresses())
		if addr := sendOneInvite(ctx, u.Addresses()); len(addr) > 0 {
			notify <- addr
			go func() {
				for range updates {
				}
			}()
			return
		}
	}
	ctx.Infof("Stopped scanning for peers to invite without finding one")
}
开发者ID:asimshankar,项目名称:triangles,代码行数:24,代码来源:net.go

示例12: seekInvites

func seekInvites(ctx *context.T, disc discovery.T, server rpc.Server, updates <-chan bool) {
	var (
		ad = &discovery.Advertisement{
			InterfaceName: interfaceName,
			Attributes: discovery.Attributes{
				"OS": runtime.GOOS,
			},
		}
		cancel    func()
		chStopped <-chan struct{}
		start     = func() {
			// Start the advertisement, update cancelCtx, cancel and chStopped
			var err error
			var advCtx *context.T
			advCtx, cancel = context.WithCancel(ctx)
			if chStopped, err = discutil.AdvertiseServer(advCtx, disc, server, "", ad, nil); err != nil {
				cancel()
				ctx.Infof("Failed to advertise %#v: %v", *ad, err)
				return
			}
			ctx.Infof("Started advertising: %#v", *ad)
		}
		stop = func() {
			if chStopped == nil {
				return
			}
			cancel()
			<-chStopped
			ctx.Infof("Stopped advertising: %#v", *ad)
			chStopped = nil
		}
	)
	start()
	go func() {
		for shouldStart := range updates {
			if shouldStart {
				start()
				continue
			}
			stop()
		}
	}()
}
开发者ID:asimshankar,项目名称:triangles,代码行数:43,代码来源:net.go

示例13: btAndDiscoveryFunc

func btAndDiscoveryFunc(ctx *context.T, w io.Writer) error {
	bothf := func(ctx *context.T, w io.Writer, format string, args ...interface{}) {
		fmt.Fprintf(w, format, args...)
		ctx.Infof(format, args...)
	}
	defer bothf(ctx, w, "finishing!")

	dis, err := v23.NewDiscovery(ctx)
	if err != nil {
		bothf(ctx, w, "Can't create discovery %v", err)
		return err
	}

	ctx = v23.WithListenSpec(ctx, rpc.ListenSpec{Addrs: rpc.ListenAddrs{{Protocol: "bt", Address: "/0"}}})
	_, server, err := v23.WithNewServer(ctx, "", &echoServer{}, security.AllowEveryone())
	if err != nil {
		bothf(ctx, w, "Can't create server %v", err)
		return err
	}
	ctx.Infof("Server listening on %v", server.Status().Endpoints)
	ctx.Infof("Server listen errors: %v", server.Status().ListenErrors)

	interfaces := []string{
		"v.io/x/jni/impl/google/services/vango/Echo",
		"v.io/x/jni/impl/google/services/vango/Echo2",
		"v.io/x/jni/impl/google/services/vango/Echo3",
		"v.io/x/jni/impl/google/services/vango/Echo4",
	}
	type adstate struct {
		ad   *discovery.Advertisement
		stop func()
	}
	ads := []adstate{}
	for _, name := range interfaces {
		ad := &discovery.Advertisement{
			InterfaceName: name,
			Attributes: discovery.Attributes{
				"one":   "A value of some kind",
				"two":   "Yet another value",
				"three": "More and more",
				"four":  "This is insane",
			},
		}
		nctx, ncancel := context.WithCancel(ctx)
		ch, err := libdiscovery.AdvertiseServer(nctx, dis, server, "", ad, nil)
		if err != nil {
			bothf(nctx, w, "Can't advertise server %v", err)
			return err
		}

		stop := func() {
			ncancel()
			<-ch
		}
		ads = append(ads, adstate{ad, stop})
	}

	type updateState struct {
		ch   <-chan discovery.Update
		stop func()
	}
	var updates []updateState
	for _, name := range interfaces {
		nctx, ncancel := context.WithCancel(ctx)
		u, err := dis.Scan(nctx, `v.InterfaceName="`+name+`"`)
		if err != nil {
			bothf(nctx, w, "Can't scan %v", err)
			return err
		}
		stop := func() {
			ncancel()
		}
		updates = append(updates, updateState{u, stop})
	}

	for _, u := range updates[1:] {
		go func(up updateState) {
			for _ = range up.ch {
			}
		}(u)
	}

	makeopt := func(ad discovery.Advertisement) options.Preresolved {
		me := &naming.MountEntry{
			IsLeaf: true,
		}
		for _, a := range ad.Addresses {
			addr, _ := naming.SplitAddressName(a)
			me.Servers = append(me.Servers, naming.MountedServer{
				Server: addr,
			})
		}
		return options.Preresolved{Resolution: me}
	}

	alive := map[discovery.AdId]options.Preresolved{}
	ticker := time.NewTicker(time.Second)
	for {
		select {
		case <-ticker.C:
//.........这里部分代码省略.........
开发者ID:vanadium,项目名称:go.jni,代码行数:101,代码来源:funcs.go

示例14: AllFunc

// AllFunc runs a server, advertises it, scans for other servers and makes an
// Echo RPC to every advertised remote server.
func AllFunc(ctx *context.T, output io.Writer) error {
	ls := rpc.ListenSpec{Proxy: "proxy"}
	addRegisteredProto(&ls, "tcp", ":0")
	addRegisteredProto(&ls, "bt", "/0")
	fmt.Fprintf(output, "Listening on: %+v (and proxy)\n", ls.Addrs)
	ctx, server, err := v23.WithNewServer(
		v23.WithListenSpec(ctx, ls),
		mountName(ctx, "all"),
		&echoServer{},
		security.AllowEveryone())
	if err != nil {
		return err
	}
	ad := &discovery.Advertisement{
		InterfaceName: interfaceName,
		Attributes: discovery.Attributes{
			"Hello": "There",
		},
	}
	d, err := v23.NewDiscovery(ctx)
	if err != nil {
		return err
	}
	stoppedAd, err := libdiscovery.AdvertiseServer(ctx, d, server, "", ad, nil)
	if err != nil {
		return err
	}
	updates, err := d.Scan(ctx, "v.InterfaceName=\""+interfaceName+"\"")
	if err != nil {
		return err
	}
	var (
		status      = server.Status()
		counter     = 0
		peerByAdId  = make(map[discovery.AdId]*peer)
		lastCall    = make(map[discovery.AdId]time.Time)
		callResults = make(chan string)
		activeCalls = 0
		quit        = false
		myaddrs     = serverAddrs(status)
		ticker      = time.NewTicker(time.Second)
		call        = func(p *peer) {
			counter++
			activeCalls++
			lastCall[p.adId] = time.Now()
			go func(msg string) {
				summary, err := p.call(ctx, msg)
				if err != nil {
					ctx.Infof("Failed to call [%v]: %v", p.description, err)
					callResults <- ""
					return
				}
				callResults <- summary
			}(fmt.Sprintf("Hello #%d", counter))
		}
		statRequest = make(chan chan<- string)
	)
	defer ticker.Stop()
	stats.NewStringFunc(vangoStat, func() string {
		r := make(chan string)
		statRequest <- r
		return <-r
	})
	defer stats.Delete(vangoStat)
	fmt.Fprintln(output, "My AdID:", ad.Id)
	fmt.Fprintln(output, "My addrs:", myaddrs)
	ctx.Infof("SERVER STATUS: %+v", status)
	for !quit {
		select {
		case <-ctx.Done():
			quit = true
		case <-status.Dirty:
			status = server.Status()
			newaddrs := serverAddrs(status)
			changed := len(newaddrs) != len(myaddrs)
			if !changed {
				for i := range newaddrs {
					if newaddrs[i] != myaddrs[i] {
						changed = true
						break
					}
				}
			}
			if changed {
				myaddrs = newaddrs
				fmt.Fprintln(output, "My addrs:", myaddrs)
			}
			ctx.Infof("SERVER STATUS: %+v", status)
		case u, scanning := <-updates:
			if !scanning {
				fmt.Fprintln(output, "SCANNING STOPPED")
				quit = true
				break
			}
			if u.IsLost() {
				if p, ok := peerByAdId[u.Id()]; ok {
					fmt.Fprintln(output, "LOST:", p.description)
				}
//.........这里部分代码省略.........
开发者ID:vanadium,项目名称:go.jni,代码行数:101,代码来源:funcs.go

示例15: MethodSignature

// The fake service knows nothing about method signatures.
func (fs fakeService) MethodSignature(ctx *context.T, call rpc.ServerCall, method string) (signature.Method, error) {
	ctx.Infof("Fake Service Method Signature???")
	return signature.Method{}, nil
}
开发者ID:vanadium,项目名称:mojo.v23proxy,代码行数:5,代码来源:serverproxy.go


注:本文中的v/io/v23/context.T类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。