1 /*
2  * hunt-amqp: AMQP library for D programming language, based on hunt-net.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 module hunt.amqp.impl.ProtonServerImpl;
12 
13 //import io.vertx.core.AsyncResult;
14 //import io.vertx.core.Context;
15 //import io.vertx.core.Future;
16 //import io.vertx.core.Handler;
17 //import io.vertx.core.Vertx;
18 //import io.vertx.core.impl.ContextInternal;
19 //import io.vertx.core.net.NetServer;
20 //import io.vertx.core.net.NetSocket;
21 //import hunt.amqp.ProtonConnection;
22 //import hunt.amqp.ProtonServer;
23 //import hunt.amqp.ProtonServerOptions;
24 //import hunt.amqp.ProtonTransportOptions;
25 //import hunt.amqp.sasl.ProtonSaslAuthenticator;
26 //import hunt.amqp.sasl.ProtonSaslAuthenticatorFactory;
27 //import hunt.proton.amqp.Symbol;
28 //import hunt.proton.engine.Transport;
29 //
30 //import java.net.InetAddress;
31 //import java.net.UnknownHostException;
32 //
33 ///**
34 // * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
35 // */
36 //class ProtonServerImpl implements ProtonServer {
37 //
38 //  private final Vertx vertx;
39 //  private final NetServer server;
40 //  private Handler<ProtonConnection> handler;
41 //  // default authenticator, anonymous
42 //  private ProtonSaslAuthenticatorFactory authenticatorFactory = new DefaultAuthenticatorFactory();
43 //  private boolean advertiseAnonymousRelayCapability = true;
44 //
45 //  private ProtonServerOptions options;
46 //
47 //  public ProtonServerImpl(Vertx vertx) {
48 //    this.vertx = vertx;
49 //    this.server = this.vertx.createNetServer();
50 //
51 //    this.options = new ProtonServerOptions();
52 //  }
53 //
54 //  public ProtonServerImpl(Vertx vertx, ProtonServerOptions options) {
55 //    this.vertx = vertx;
56 //    this.server = this.vertx.createNetServer(options);
57 //
58 //    this.options = options;
59 //  }
60 //
61 //  @Override
62 //  public int actualPort() {
63 //    return server.actualPort();
64 //  }
65 //
66 //  @Override
67 //  public ProtonServerImpl listen(int i) {
68 //    server.listen(i);
69 //    return this;
70 //  }
71 //
72 //  @Override
73 //  public ProtonServerImpl listen() {
74 //    server.listen();
75 //    return this;
76 //  }
77 //
78 //  public boolean isMetricsEnabled() {
79 //    return server.isMetricsEnabled();
80 //  }
81 //
82 //  @Override
83 //  public ProtonServerImpl listen(int port, String host, Handler<AsyncResult<ProtonServer>> handler) {
84 //    server.listen(port, host, convertHandler(handler));
85 //    return this;
86 //  }
87 //
88 //  @Override
89 //  public ProtonServerImpl listen(Handler<AsyncResult<ProtonServer>> handler) {
90 //    server.listen(convertHandler(handler));
91 //    return this;
92 //  }
93 //
94 //  private Handler<AsyncResult<NetServer>> convertHandler(final Handler<AsyncResult<ProtonServer>> handler) {
95 //    return result -> {
96 //      if (result.succeeded()) {
97 //        handler.handle(Future.succeededFuture(ProtonServerImpl.this));
98 //      } else {
99 //        handler.handle(Future.failedFuture(result.cause()));
100 //      }
101 //    };
102 //  }
103 //
104 //  @Override
105 //  public ProtonServerImpl listen(int i, String s) {
106 //    server.listen(i, s);
107 //    return this;
108 //  }
109 //
110 //  @Override
111 //  public ProtonServerImpl listen(int i, Handler<AsyncResult<ProtonServer>> handler) {
112 //    server.listen(i, convertHandler(handler));
113 //    return this;
114 //  }
115 //
116 //  @Override
117 //  public void close() {
118 //    server.close();
119 //  }
120 //
121 //  @Override
122 //  public void close(Handler<AsyncResult<Void>> handler) {
123 //    server.close(handler);
124 //  }
125 //
126 //  @Override
127 //  public Handler<ProtonConnection> connectHandler() {
128 //    return handler;
129 //  }
130 //
131 //  @Override
132 //  public ProtonServer saslAuthenticatorFactory(ProtonSaslAuthenticatorFactory authenticatorFactory) {
133 //    if (authenticatorFactory is null) {
134 //      // restore the default
135 //      this.authenticatorFactory = new DefaultAuthenticatorFactory();
136 //    } else {
137 //      this.authenticatorFactory = authenticatorFactory;
138 //    }
139 //    return this;
140 //  }
141 //
142 //  @Override
143 //  public ProtonServerImpl connectHandler(Handler<ProtonConnection> handler) {
144 //    this.handler = handler;
145 //    server.connectHandler(netSocket -> {
146 //      String hostname = null;
147 //      try {
148 //        hostname = InetAddress.getLocalHost().getHostName();
149 //      } catch (UnknownHostException e) {
150 //        // ignore
151 //      }
152 //
153 //      final ProtonConnectionImpl connection = new ProtonConnectionImpl(vertx, hostname, (ContextInternal) Vertx.currentContext());
154 //      if (advertiseAnonymousRelayCapability) {
155 //        connection.setOfferedCapabilities(new Symbol[] { ProtonConnectionImpl.ANONYMOUS_RELAY });
156 //      }
157 //
158 //      final ProtonSaslAuthenticator authenticator = authenticatorFactory.create();
159 //
160 //      ProtonTransportOptions transportOptions = new ProtonTransportOptions();
161 //      transportOptions.setHeartbeat(this.options.getHeartbeat());
162 //      transportOptions.setMaxFrameSize(this.options.getMaxFrameSize());
163 //
164 //      connection.bindServer(netSocket, new ProtonSaslAuthenticator() {
165 //
166 //        @Override
167 //        public void init(NetSocket socket, ProtonConnection protonConnection, Transport transport) {
168 //          authenticator.init(socket, protonConnection, transport);
169 //        }
170 //
171 //        @Override
172 //        public void process(Handler<Boolean> completionHandler) {
173 //          final Context context = Vertx.currentContext();
174 //
175 //          authenticator.process(complete -> {
176 //            final Context callbackContext = vertx.getOrCreateContext();
177 //            if(context != callbackContext) {
178 //              throw new IllegalStateException("Callback was not made on the original context");
179 //            }
180 //
181 //            if (complete) {
182 //              // The authenticator completed, now check success, do required post processing
183 //              if (succeeded()) {
184 //                handler.handle(connection);
185 //                connection.flush();
186 //              } else {
187 //                // auth failed, flush any pending data and disconnect client
188 //                connection.flush();
189 //                connection.disconnect();
190 //              }
191 //            }
192 //
193 //            completionHandler.handle(complete);
194 //          });
195 //        }
196 //
197 //        @Override
198 //        public boolean succeeded() {
199 //          return authenticator.succeeded();
200 //        }
201 //
202 //      }, transportOptions);
203 //    });
204 //    return this;
205 //  }
206 //
207 //  public void setAdvertiseAnonymousRelayCapability(boolean advertiseAnonymousRelayCapability) {
208 //    this.advertiseAnonymousRelayCapability = advertiseAnonymousRelayCapability;
209 //  }
210 //
211 //  private static class DefaultAuthenticatorFactory implements ProtonSaslAuthenticatorFactory {
212 //    @Override
213 //    public ProtonSaslAuthenticator create() {
214 //      return new ProtonSaslServerAuthenticatorImpl();
215 //    }
216 //  }
217 //}