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 the ServiceDescriber. | |
rudominer
2015/10/22 21:28:48
s/for the ServiceDescriber/for ServiceDescriber/
alexfandrianto
2015/10/22 22:58:11
Done.
| |
17 // For cleanup purposes, it is also the implementation of a ServiceDescriber. | |
18 type ServiceDescriberFactory struct { | |
19 mapping map[string]service_describer.ServiceDescription | |
20 stubs []*bindings.Stub | |
21 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.
| |
22 } | |
23 | |
24 func newServiceDescriberFactory(mapping map[string]service_describer.ServiceDesc ription) *ServiceDescriberFactory { | |
25 return &ServiceDescriberFactory{ | |
26 mapping: mapping, | |
27 descriptions: make(map[string]*ServiceDescriptionFactory), | |
28 } | |
29 } | |
30 | |
31 func (sd *ServiceDescriberFactory) Create(request service_describer.ServiceDescr iber_Request) { | |
32 stub := service_describer.NewServiceDescriberStub(request, sd, bindings. GetAsyncWaiter()) | |
33 sd.stubs = append(sd.stubs, stub) | |
34 go func() { | |
35 for { | |
36 if err := stub.ServeRequest(); err != nil { | |
37 connectionError, ok := err.(*bindings.Connection Error) | |
38 if !ok || !connectionError.Closed() { | |
39 log.Println(err) | |
40 } | |
41 break | |
42 } | |
43 } | |
44 }() | |
45 } | |
46 | |
47 func (sd *ServiceDescriberFactory) Close() { | |
48 for _, stub := range sd.stubs { | |
49 stub.Close() | |
50 } | |
51 for _, description := range sd.descriptions { | |
52 for _, stub := range description.stubs { | |
53 stub.Close() | |
54 } | |
55 } | |
56 } | |
57 | |
58 // Helper method for DescribeService | |
59 func (sd *ServiceDescriberFactory) getServiceDescriptionFactory(inInterfaceName string) *ServiceDescriptionFactory { | |
60 // Assumes the interface name is in the mapping. | |
61 if desc, ok := sd.descriptions[inInterfaceName]; ok { | |
62 return desc | |
63 } | |
64 sd.descriptions[inInterfaceName] = &ServiceDescriptionFactory{ | |
65 description: sd.mapping[inInterfaceName], | |
66 } | |
67 return sd.descriptions[inInterfaceName] | |
68 } | |
69 | |
70 func (sd *ServiceDescriberFactory) DescribeService(inInterfaceName string, inDes criptionRequest service_describer.ServiceDescription_Request) (err error) { | |
71 if _, ok := sd.mapping[inInterfaceName]; ok { | |
72 sd.getServiceDescriptionFactory(inInterfaceName).Create(inDescri ptionRequest) | |
73 return nil | |
74 } | |
75 return fmt.Errorf("The interface %s is unknown by this application", inI nterfaceName) | |
76 } | |
77 | |
78 // ServiceDescriptionFactory implements ServiceFactory for the ServiceDescriptio n. | |
rudominer
2015/10/22 21:28:48
s/for the ServiceDescription/for ServiceDescriptio
alexfandrianto
2015/10/22 22:58:11
Done.
| |
79 type ServiceDescriptionFactory struct { | |
80 stubs []*bindings.Stub | |
81 description service_describer.ServiceDescription | |
rudominer
2015/10/22 21:28:48
Consider renaming description -> impl
alexfandrianto
2015/10/22 22:58:11
Done.
| |
82 } | |
83 | |
84 func (serviceDescriptionFactory *ServiceDescriptionFactory) Create(request servi ce_describer.ServiceDescription_Request) { | |
85 stub := service_describer.NewServiceDescriptionStub(request, serviceDesc riptionFactory.description, bindings.GetAsyncWaiter()) | |
86 serviceDescriptionFactory.stubs = append(serviceDescriptionFactory.stubs , stub) | |
87 go func() { | |
88 for { | |
89 if err := stub.ServeRequest(); err != nil { | |
90 connectionError, ok := err.(*bindings.Connection Error) | |
91 if !ok || !connectionError.Closed() { | |
92 log.Println(err) | |
93 } | |
94 break | |
95 } | |
96 } | |
97 }() | |
98 } | |
OLD | NEW |