|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.featureprobe.sdk.server; |
| 19 | + |
| 20 | +import com.google.common.annotations.VisibleForTesting; |
| 21 | +import io.socket.client.IO; |
| 22 | +import io.socket.client.Socket; |
| 23 | +import io.socket.engineio.client.transports.WebSocket; |
| 24 | +import org.slf4j.Logger; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.Future; |
| 30 | + |
| 31 | +public class StreamingSynchronizer implements Synchronizer { |
| 32 | + |
| 33 | + private static final Logger logger = Loggers.SYNCHRONIZER; |
| 34 | + |
| 35 | + private PollingSynchronizer pollingSynchronizer; |
| 36 | + |
| 37 | + @VisibleForTesting |
| 38 | + Socket socket; |
| 39 | + |
| 40 | + StreamingSynchronizer(FPContext context, DataRepository dataRepository) { |
| 41 | + pollingSynchronizer = new PollingSynchronizer(context, dataRepository); |
| 42 | + this.socket = connectSocket(context); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Future<Void> sync() { |
| 47 | + return pollingSynchronizer.sync(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void close() throws IOException { |
| 52 | + synchronized (this) { |
| 53 | + if (socket != null) { |
| 54 | + socket.close(); |
| 55 | + socket = null; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private Socket connectSocket(FPContext context) { |
| 61 | + IO.Options sioOptions = IO.Options.builder() |
| 62 | + .setTransports(new String[]{WebSocket.NAME}) |
| 63 | + .setPath(context.getRealtimeUri().getPath()) |
| 64 | + .build(); |
| 65 | + Socket sio = IO.socket(context.getRealtimeUri(), sioOptions); |
| 66 | + |
| 67 | + sio.on("connect", objects -> { |
| 68 | + logger.info("connect socket-io success"); |
| 69 | + Map<String, String> credentials = new HashMap<>(1); |
| 70 | + credentials.put("key", context.getServerSdkKey()); |
| 71 | + sio.emit("register", credentials); |
| 72 | + }); |
| 73 | + |
| 74 | + sio.on("update", objects -> { |
| 75 | + logger.info("socket-io recv update event"); |
| 76 | + pollingSynchronizer.poll(); |
| 77 | + }); |
| 78 | + |
| 79 | + sio.on("disconnect", objects -> logger.info("socket-io disconnected")); |
| 80 | + |
| 81 | + sio.on("connect_error", objects -> logger.error("socket-io error: {}", objects)); |
| 82 | + |
| 83 | + return sio.connect(); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments