OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package application |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "log" |
| 10 |
| 11 "mojo/public/go/bindings" |
| 12 |
| 13 "mojo/public/interfaces/bindings/service_describer" |
| 14 ) |
| 15 |
| 16 // ServiceDescriberFactory implements ServiceFactory for ServiceDescriber. |
| 17 // For cleanup purposes, it is also the implementation of a ServiceDescriber. |
| 18 type ServiceDescriberFactory struct { |
| 19 // mapping holds the map from interface names to ServiceDescription's. |
| 20 mapping map[string]service_describer.ServiceDescription |
| 21 // stubs stores the stubs for connections opened to this factory. |
| 22 stubs []*bindings.Stub |
| 23 // descriptionFactories maps interface names to ServiceDescriptionFactor
y's. |
| 24 descriptionFactories map[string]*ServiceDescriptionFactory |
| 25 } |
| 26 |
| 27 func newServiceDescriberFactory(mapping map[string]service_describer.ServiceDesc
ription) *ServiceDescriberFactory { |
| 28 return &ServiceDescriberFactory{ |
| 29 mapping: mapping, |
| 30 descriptionFactories: make(map[string]*ServiceDescriptionFactory
), |
| 31 } |
| 32 } |
| 33 |
| 34 func (sd *ServiceDescriberFactory) Create(request service_describer.ServiceDescr
iber_Request) { |
| 35 stub := service_describer.NewServiceDescriberStub(request, sd, bindings.
GetAsyncWaiter()) |
| 36 sd.stubs = append(sd.stubs, stub) |
| 37 go func() { |
| 38 for { |
| 39 if err := stub.ServeRequest(); err != nil { |
| 40 connectionError, ok := err.(*bindings.Connection
Error) |
| 41 if !ok || !connectionError.Closed() { |
| 42 log.Println(err) |
| 43 } |
| 44 break |
| 45 } |
| 46 } |
| 47 }() |
| 48 } |
| 49 |
| 50 func (sd *ServiceDescriberFactory) Close() { |
| 51 for _, stub := range sd.stubs { |
| 52 stub.Close() |
| 53 } |
| 54 for _, factory := range sd.descriptionFactories { |
| 55 for _, stub := range factory.stubs { |
| 56 stub.Close() |
| 57 } |
| 58 } |
| 59 } |
| 60 |
| 61 // Helper method for DescribeService |
| 62 func (sd *ServiceDescriberFactory) getServiceDescriptionFactory(inInterfaceName
string) *ServiceDescriptionFactory { |
| 63 // Assumes the interface name is in the mapping. |
| 64 if desc, ok := sd.descriptionFactories[inInterfaceName]; ok { |
| 65 return desc |
| 66 } |
| 67 sd.descriptionFactories[inInterfaceName] = &ServiceDescriptionFactory{ |
| 68 impl: sd.mapping[inInterfaceName], |
| 69 } |
| 70 return sd.descriptionFactories[inInterfaceName] |
| 71 } |
| 72 |
| 73 func (sd *ServiceDescriberFactory) DescribeService(inInterfaceName string, inDes
criptionRequest service_describer.ServiceDescription_Request) (err error) { |
| 74 if _, ok := sd.mapping[inInterfaceName]; ok { |
| 75 sd.getServiceDescriptionFactory(inInterfaceName).Create(inDescri
ptionRequest) |
| 76 return nil |
| 77 } |
| 78 return fmt.Errorf("The interface %s is unknown by this application", inI
nterfaceName) |
| 79 } |
| 80 |
| 81 // ServiceDescriptionFactory implements ServiceFactory for ServiceDescription. |
| 82 type ServiceDescriptionFactory struct { |
| 83 // stubs stores the stubs for connections opened to this factory. |
| 84 stubs []*bindings.Stub |
| 85 // impl is the ServiceDescription implementation served by this factory. |
| 86 impl service_describer.ServiceDescription |
| 87 } |
| 88 |
| 89 func (serviceDescriptionFactory *ServiceDescriptionFactory) Create(request servi
ce_describer.ServiceDescription_Request) { |
| 90 stub := service_describer.NewServiceDescriptionStub(request, serviceDesc
riptionFactory.impl, bindings.GetAsyncWaiter()) |
| 91 serviceDescriptionFactory.stubs = append(serviceDescriptionFactory.stubs
, stub) |
| 92 go func() { |
| 93 for { |
| 94 if err := stub.ServeRequest(); err != nil { |
| 95 connectionError, ok := err.(*bindings.Connection
Error) |
| 96 if !ok || !connectionError.Closed() { |
| 97 log.Println(err) |
| 98 } |
| 99 break |
| 100 } |
| 101 } |
| 102 }() |
| 103 } |
OLD | NEW |