...14// KIND, either express or implied. See the License for the15// specific language governing permissions and limitations16// under the License.17package org.openqa.selenium.events.zeromq;18import org.openqa.selenium.events.EventBus;19import org.openqa.selenium.events.EventListener;20import org.openqa.selenium.events.EventName;21import org.openqa.selenium.grid.config.Config;22import org.openqa.selenium.grid.security.Secret;23import org.openqa.selenium.grid.security.SecretOptions;24import org.openqa.selenium.internal.Require;25import org.openqa.selenium.json.JsonInput;26import org.zeromq.ZContext;27import java.util.function.Consumer;28import static org.openqa.selenium.events.zeromq.UnboundZmqEventBus.REJECTED_EVENT;29/**30 * An {@link EventBus} backed by ZeroMQ.31 */32public class ZeroMqEventBus {33 private static final String EVENTS_SECTION = "events";34 private ZeroMqEventBus() {35 // Use the create method.36 }37 public static EventBus create(ZContext context, String publish, String subscribe, boolean bind, Secret secret) {38 if (bind) {39 return new BoundZmqEventBus(context, publish, subscribe, secret);40 }41 return new UnboundZmqEventBus(context, publish, subscribe, secret);42 }43 public static EventBus create(Config config) {44 String publish = config.get(EVENTS_SECTION, "publish")45 .orElseThrow(() -> new IllegalArgumentException(46 "Unable to find address to publish events to."));47 String subscribe = config.get(EVENTS_SECTION, "subscribe")48 .orElseThrow(() -> new IllegalArgumentException(49 "Unable to find address to subscribe for events from."));50 boolean bind = config.getBool(EVENTS_SECTION, "bind").orElse(false);51 SecretOptions secretOptions = new SecretOptions(config);52 return create(new ZContext(), publish, subscribe, bind, secretOptions.getRegistrationSecret());53 }54 public static EventListener<RejectedEvent> onRejectedEvent(Consumer<RejectedEvent> handler) {55 Require.nonNull("Handler", handler);56 return new EventListener<>(REJECTED_EVENT, RejectedEvent.class, handler);57 }58 public static class RejectedEvent {59 private final EventName name;60 private final Object data;61 RejectedEvent(EventName name, Object data) {62 this.name = name;63 this.data = data;64 }65 public EventName getName() {66 return name;67 }68 public Object getData() {69 return data;70 }71 private static RejectedEvent fromJson(JsonInput input) {72 EventName name = null;73 Object data = null;74 input.beginObject();75 while (input.hasNext()) {76 switch (input.nextName()) {77 case "data":78 data = input.read(Object.class);79 break;80 case "name":81 name = input.read(EventName.class);82 break;83 default:84 input.skipValue();85 break;86 }87 }88 input.endObject();89 return new RejectedEvent(name, data);90 }91 }92}...