| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package streamclient | 5 package streamclient |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "strings" | 9 "strings" |
| 10 "sync" | 10 "sync" |
| 11 ) | 11 ) |
| 12 | 12 |
| 13 var ( | |
| 14 // DefaultRegistry is the default protocol registry. | |
| 15 DefaultRegistry = &Registry{} | |
| 16 ) | |
| 17 | |
| 18 // ClientFactory is a generator function that is invoked by the Registry when a | 13 // ClientFactory is a generator function that is invoked by the Registry when a |
| 19 // new Client is requested for its protocol. | 14 // new Client is requested for its protocol. |
| 20 type ClientFactory func(string) (Client, error) | 15 type ClientFactory func(string) (Client, error) |
| 21 | 16 |
| 22 // Registry maps protocol prefix strings to their Client generator functions. | 17 // Registry maps protocol prefix strings to their Client generator functions. |
| 23 // | 18 // |
| 24 // This allows multiple Butler stream protocols (e.g., "unix:", "net.pipe:", | 19 // This allows multiple Butler stream protocols (e.g., "unix:", "net.pipe:", |
| 25 // etc.) to be parsed from string. | 20 // etc.) to be parsed from string. |
| 26 type Registry struct { | 21 type Registry struct { |
| 27 // lock protects the fields in Registry. | 22 // lock protects the fields in Registry. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 if r.protocols == nil { | 41 if r.protocols == nil { |
| 47 r.protocols = make(map[string]ClientFactory) | 42 r.protocols = make(map[string]ClientFactory) |
| 48 } | 43 } |
| 49 r.protocols[name] = f | 44 r.protocols[name] = f |
| 50 } | 45 } |
| 51 | 46 |
| 52 // NewClient invokes the protocol ClientFactory generator for the | 47 // NewClient invokes the protocol ClientFactory generator for the |
| 53 // supplied protocol/address string, returning the generated Client. | 48 // supplied protocol/address string, returning the generated Client. |
| 54 func (r *Registry) NewClient(path string) (Client, error) { | 49 func (r *Registry) NewClient(path string) (Client, error) { |
| 55 parts := strings.SplitN(path, ":", 2) | 50 parts := strings.SplitN(path, ":", 2) |
| 56 » params := "" | 51 » value := "" |
| 57 if len(parts) == 2 { | 52 if len(parts) == 2 { |
| 58 » » params = parts[1] | 53 » » value = parts[1] |
| 59 } | 54 } |
| 60 | 55 |
| 61 r.lock.Lock() | 56 r.lock.Lock() |
| 62 defer r.lock.Unlock() | 57 defer r.lock.Unlock() |
| 63 | 58 |
| 64 if f, ok := r.protocols[parts[0]]; ok { | 59 if f, ok := r.protocols[parts[0]]; ok { |
| 65 » » return f(params) | 60 » » return f(value) |
| 66 } | 61 } |
| 67 return nil, fmt.Errorf("streamclient: no protocol registered for [%s]",
parts[0]) | 62 return nil, fmt.Errorf("streamclient: no protocol registered for [%s]",
parts[0]) |
| 68 } | 63 } |
| 69 | 64 |
| 65 // defaultRegistry is the default protocol registry. |
| 66 var defaultRegistry = &Registry{} |
| 67 |
| 68 // GetDefaultRegistry returns the default client registry instance. |
| 69 func GetDefaultRegistry() *Registry { |
| 70 return defaultRegistry |
| 71 } |
| 72 |
| 70 // registerProtocol registers a protocol with the default (global) protocol | 73 // registerProtocol registers a protocol with the default (global) protocol |
| 71 // registry. | 74 // registry. |
| 72 func registerProtocol(name string, f ClientFactory) { | 75 func registerProtocol(name string, f ClientFactory) { |
| 73 » DefaultRegistry.Register(name, f) | 76 » defaultRegistry.Register(name, f) |
| 74 } | 77 } |
| OLD | NEW |