Chromium Code Reviews| Index: mojo/public/go/application/describer.go |
| diff --git a/mojo/public/go/application/describer.go b/mojo/public/go/application/describer.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..58cc85691e851840c9b29a03a58de8977c22c9d1 |
| --- /dev/null |
| +++ b/mojo/public/go/application/describer.go |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package application |
| + |
| +import ( |
| + "fmt" |
| + "log" |
| + |
| + "mojo/public/go/bindings" |
| + |
| + "mojo/public/interfaces/bindings/service_describer" |
| +) |
| + |
| +// ServiceDescriberFactory implements ServiceFactory for the ServiceDescriber. |
|
rudominer
2015/10/22 21:28:48
s/for the ServiceDescriber/for ServiceDescriber/
alexfandrianto
2015/10/22 22:58:11
Done.
|
| +// For cleanup purposes, it is also the implementation of a ServiceDescriber. |
| +type ServiceDescriberFactory struct { |
| + mapping map[string]service_describer.ServiceDescription |
| + stubs []*bindings.Stub |
| + descriptions map[string]*ServiceDescriptionFactory |
|
rudominer
2015/10/22 21:28:48
please rename "desriptions" to something containin
alexfandrianto
2015/10/22 22:58:11
Done.
|
| +} |
| + |
| +func newServiceDescriberFactory(mapping map[string]service_describer.ServiceDescription) *ServiceDescriberFactory { |
| + return &ServiceDescriberFactory{ |
| + mapping: mapping, |
| + descriptions: make(map[string]*ServiceDescriptionFactory), |
| + } |
| +} |
| + |
| +func (sd *ServiceDescriberFactory) Create(request service_describer.ServiceDescriber_Request) { |
| + stub := service_describer.NewServiceDescriberStub(request, sd, bindings.GetAsyncWaiter()) |
| + sd.stubs = append(sd.stubs, stub) |
| + go func() { |
| + for { |
| + if err := stub.ServeRequest(); err != nil { |
| + connectionError, ok := err.(*bindings.ConnectionError) |
| + if !ok || !connectionError.Closed() { |
| + log.Println(err) |
| + } |
| + break |
| + } |
| + } |
| + }() |
| +} |
| + |
| +func (sd *ServiceDescriberFactory) Close() { |
| + for _, stub := range sd.stubs { |
| + stub.Close() |
| + } |
| + for _, description := range sd.descriptions { |
| + for _, stub := range description.stubs { |
| + stub.Close() |
| + } |
| + } |
| +} |
| + |
| +// Helper method for DescribeService |
| +func (sd *ServiceDescriberFactory) getServiceDescriptionFactory(inInterfaceName string) *ServiceDescriptionFactory { |
| + // Assumes the interface name is in the mapping. |
| + if desc, ok := sd.descriptions[inInterfaceName]; ok { |
| + return desc |
| + } |
| + sd.descriptions[inInterfaceName] = &ServiceDescriptionFactory{ |
| + description: sd.mapping[inInterfaceName], |
| + } |
| + return sd.descriptions[inInterfaceName] |
| +} |
| + |
| +func (sd *ServiceDescriberFactory) DescribeService(inInterfaceName string, inDescriptionRequest service_describer.ServiceDescription_Request) (err error) { |
| + if _, ok := sd.mapping[inInterfaceName]; ok { |
| + sd.getServiceDescriptionFactory(inInterfaceName).Create(inDescriptionRequest) |
| + return nil |
| + } |
| + return fmt.Errorf("The interface %s is unknown by this application", inInterfaceName) |
| +} |
| + |
| +// ServiceDescriptionFactory implements ServiceFactory for the ServiceDescription. |
|
rudominer
2015/10/22 21:28:48
s/for the ServiceDescription/for ServiceDescriptio
alexfandrianto
2015/10/22 22:58:11
Done.
|
| +type ServiceDescriptionFactory struct { |
| + stubs []*bindings.Stub |
| + description service_describer.ServiceDescription |
|
rudominer
2015/10/22 21:28:48
Consider renaming description -> impl
alexfandrianto
2015/10/22 22:58:11
Done.
|
| +} |
| + |
| +func (serviceDescriptionFactory *ServiceDescriptionFactory) Create(request service_describer.ServiceDescription_Request) { |
| + stub := service_describer.NewServiceDescriptionStub(request, serviceDescriptionFactory.description, bindings.GetAsyncWaiter()) |
| + serviceDescriptionFactory.stubs = append(serviceDescriptionFactory.stubs, stub) |
| + go func() { |
| + for { |
| + if err := stub.ServeRequest(); err != nil { |
| + connectionError, ok := err.(*bindings.ConnectionError) |
| + if !ok || !connectionError.Closed() { |
| + log.Println(err) |
| + } |
| + break |
| + } |
| + } |
| + }() |
| +} |