1 // Copyright (C) 2018-2019 HuntLabs. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 module hunt.gossip.GossipService; 16 17 import hunt.text.StringUtils; 18 import hunt.logging; 19 import hunt.gossip.event.GossipListener; 20 import hunt.gossip.model.SeedMember; 21 22 import hunt.collection.List; 23 import hunt.gossip.Common; 24 import hunt.Integer; 25 import hunt.gossip.GossipSettings; 26 import hunt.gossip.GossipManager; 27 import std.conv; 28 import hunt.Exceptions; 29 30 public class GossipService { 31 // private static final Logger LOGGER = LoggerFactory.getLogger(GossipService.class); 32 33 public this(string cluster, string ipAddress, Integer port, string id, List!(SeedMember) seedMembers, GossipSettings settings, GossipListener listener) /* throws Exception */ { 34 checkParams(cluster, ipAddress, port, seedMembers); 35 if (isNullOrEmpty(id)) { 36 id = ipAddress ~ (":") ~ (to!string(port)); 37 } 38 GossipManager.getInstance().init(cluster, ipAddress, port, id, seedMembers, settings, listener); 39 } 40 41 public GossipManager getGossipManager() { 42 return GossipManager.getInstance(); 43 } 44 45 public void start() { 46 if (getGossipManager().isWorking()) { 47 logInfo("Cgossip already workinig"); 48 return; 49 } 50 GossipManager.getInstance().start(); 51 } 52 53 public void shutdown() { 54 if (getGossipManager().isWorking()) { 55 GossipManager.getInstance().shutdown(); 56 } 57 } 58 59 private void checkParams(string cluster, string ipAddress, Integer port, List!(SeedMember) seedMembers) /* throws Exception */ { 60 string f = "[%s] is required!"; 61 string who = null; 62 if (isNullOrEmpty(cluster)) { 63 who = "cluster"; 64 } else if (isNullOrEmpty(ipAddress)) { 65 who = "ip"; 66 } else if (isNullOrEmpty(to!string(port))) { 67 who = "port"; 68 } else if (seedMembers is null || seedMembers.isEmpty()) { 69 who = "seed member"; 70 } 71 if (who !is null) { 72 throw new IllegalArgumentException(f, who); 73 } 74 } 75 }