| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | public sealed class EndpointFactoryManager |
| | 8 | | { |
| 1 | 9 | | internal EndpointFactoryManager(Instance instance) |
| | 10 | | { |
| 1 | 11 | | _instance = instance; |
| 1 | 12 | | _factories = new List<EndpointFactory>(); |
| 1 | 13 | | } |
| | 14 | |
|
| | 15 | | public void initialize() |
| | 16 | | { |
| 1 | 17 | | foreach (EndpointFactory f in _factories) |
| | 18 | | { |
| 1 | 19 | | f.initialize(); |
| | 20 | | } |
| 1 | 21 | | } |
| | 22 | |
|
| | 23 | | public void add(EndpointFactory factory) |
| | 24 | | { |
| 1 | 25 | | lock (_mutex) |
| | 26 | | { |
| | 27 | | Debug.Assert(!_factories.Any(f => f.type() == factory.type())); |
| 1 | 28 | | _factories.Add(factory); |
| 1 | 29 | | } |
| 1 | 30 | | } |
| | 31 | |
|
| | 32 | | public EndpointFactory get(short type) |
| | 33 | | { |
| 1 | 34 | | lock (_mutex) |
| | 35 | | { |
| 1 | 36 | | foreach (EndpointFactory f in _factories) |
| | 37 | | { |
| 1 | 38 | | if (f.type() == type) |
| | 39 | | { |
| 1 | 40 | | return f; |
| | 41 | | } |
| | 42 | | } |
| 1 | 43 | | return null; |
| | 44 | | } |
| 1 | 45 | | } |
| | 46 | |
|
| | 47 | | public EndpointI create(string str, bool oaEndpoint) |
| | 48 | | { |
| 1 | 49 | | string[] arr = Ice.UtilInternal.StringUtil.splitString(str, " \t\r\n") ?? |
| 1 | 50 | | throw new ParseException($"Failed to parse endpoint '{str}': mismatched quote"); |
| 1 | 51 | | if (arr.Length == 0) |
| | 52 | | { |
| 1 | 53 | | throw new ParseException($"Failed to parse endpoint '{str}': value has no non-whitespace characters"); |
| | 54 | | } |
| | 55 | |
|
| 1 | 56 | | var v = new List<string>(arr); |
| 1 | 57 | | string protocol = v[0]; |
| 1 | 58 | | v.RemoveAt(0); |
| | 59 | |
|
| 1 | 60 | | if (protocol == "default") |
| | 61 | | { |
| 1 | 62 | | protocol = _instance.defaultsAndOverrides().defaultProtocol; |
| | 63 | | } |
| | 64 | |
|
| 1 | 65 | | EndpointFactory factory = null; |
| | 66 | |
|
| 1 | 67 | | lock (_mutex) |
| | 68 | | { |
| 1 | 69 | | for (int i = 0; i < _factories.Count; i++) |
| | 70 | | { |
| 1 | 71 | | EndpointFactory f = _factories[i]; |
| 1 | 72 | | if (f.protocol().Equals(protocol, StringComparison.Ordinal)) |
| | 73 | | { |
| 1 | 74 | | factory = f; |
| | 75 | | } |
| | 76 | | } |
| 1 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | if (factory != null) |
| | 80 | | { |
| 1 | 81 | | EndpointI e = factory.create(v, oaEndpoint); |
| 1 | 82 | | if (v.Count > 0) |
| | 83 | | { |
| 0 | 84 | | throw new ParseException($"Failed to parse endpoint '{str}': unrecognized argument '{v[0]}'"); |
| | 85 | | } |
| 1 | 86 | | return e; |
| | 87 | |
|
| | 88 | | // Code below left in place for debugging. |
| | 89 | |
|
| | 90 | | /* |
| | 91 | | EndpointI e = f.create(s.Substring(m.Index + m.Length), oaEndpoint); |
| | 92 | | BasicStream bs = new BasicStream(_instance, true); |
| | 93 | | e.streamWrite(bs); |
| | 94 | | Buffer buf = bs.getBuffer(); |
| | 95 | | buf.b.position(0); |
| | 96 | | short type = bs.readShort(); |
| | 97 | | EndpointI ue = new Ice.Internal.OpaqueEndpointI(type, bs); |
| | 98 | | System.Console.Error.WriteLine("Normal: " + e); |
| | 99 | | System.Console.Error.WriteLine("Opaque: " + ue); |
| | 100 | | return e; |
| | 101 | | */ |
| | 102 | | } |
| | 103 | |
|
| | 104 | | // |
| | 105 | | // If the stringified endpoint is opaque, create an unknown endpoint, |
| | 106 | | // then see whether the type matches one of the known endpoints. |
| | 107 | | // |
| 1 | 108 | | if (protocol == "opaque") |
| | 109 | | { |
| 1 | 110 | | EndpointI ue = new OpaqueEndpointI(v); |
| 1 | 111 | | if (v.Count > 0) |
| | 112 | | { |
| 1 | 113 | | throw new ParseException($"Failed to parse endpoint '{str}': unrecognized argument '{v[0]}'"); |
| | 114 | | } |
| 1 | 115 | | factory = get(ue.type()); |
| 1 | 116 | | if (factory != null) |
| | 117 | | { |
| | 118 | | // |
| | 119 | | // Make a temporary stream, write the opaque endpoint data into the stream, |
| | 120 | | // and ask the factory to read the endpoint data from that stream to create |
| | 121 | | // the actual endpoint. |
| | 122 | | // |
| 1 | 123 | | var os = new Ice.OutputStream(Ice.Util.currentProtocolEncoding); |
| 1 | 124 | | os.writeShort(ue.type()); |
| 1 | 125 | | ue.streamWrite(os); |
| 1 | 126 | | var iss = |
| 1 | 127 | | new Ice.InputStream(_instance, Ice.Util.currentProtocolEncoding, os.getBuffer(), true); |
| 1 | 128 | | iss.pos(0); |
| 1 | 129 | | iss.readShort(); // type |
| 1 | 130 | | iss.startEncapsulation(); |
| 1 | 131 | | EndpointI e = factory.read(iss); |
| 1 | 132 | | iss.endEncapsulation(); |
| 1 | 133 | | return e; |
| | 134 | | } |
| 1 | 135 | | return ue; // Endpoint is opaque, but we don't have a factory for its type. |
| | 136 | | } |
| | 137 | |
|
| 1 | 138 | | return null; |
| | 139 | | } |
| | 140 | |
|
| | 141 | | public EndpointI read(Ice.InputStream s) |
| | 142 | | { |
| 1 | 143 | | lock (_mutex) |
| | 144 | | { |
| 1 | 145 | | short type = s.readShort(); |
| | 146 | |
|
| 1 | 147 | | EndpointFactory factory = get(type); |
| 1 | 148 | | EndpointI e = null; |
| | 149 | |
|
| 1 | 150 | | s.startEncapsulation(); |
| | 151 | |
|
| 1 | 152 | | if (factory != null) |
| | 153 | | { |
| 1 | 154 | | e = factory.read(s); |
| | 155 | | } |
| | 156 | | // |
| | 157 | | // If the factory failed to read the endpoint, return an opaque endpoint. This can |
| | 158 | | // occur if for example the factory delegates to another factory and this factory |
| | 159 | | // isn't available. In this case, the factory needs to make sure the stream position |
| | 160 | | // is preserved for reading the opaque endpoint. |
| | 161 | | // |
| 1 | 162 | | e ??= new OpaqueEndpointI(type, s); |
| | 163 | |
|
| 1 | 164 | | s.endEncapsulation(); |
| | 165 | |
|
| 1 | 166 | | return e; |
| | 167 | | } |
| 1 | 168 | | } |
| | 169 | |
|
| | 170 | | private readonly Instance _instance; |
| | 171 | | private readonly List<EndpointFactory> _factories; |
| 1 | 172 | | private readonly object _mutex = new(); |
| | 173 | | } |