1 module hunt.service.remoting.provider.ServiceProviderFactory;
2 
3 import grpc;
4 
5 import hunt.collection.HashMap;
6 import hunt.collection.Map;
7 import hunt.logging;
8 import hunt.service.RegistryConfig;
9 
10 import neton.client.NetonFactory;
11 import neton.client.registry.RegistryService;
12 import neton.client.NetonOption;
13 
14 public class ServiceProviderFactory
15 {
16 
17 	private
18 	{
19 		Server _rpcServer;
20 		RegistryService _register;
21 		string _serviceName;
22 		NetonOption _neton;
23 		string _listenIp;
24 		ushort _listenPort;
25 		bool _useRegistry = false;
26 		RegistryConfig _registryConf;
27 	}
28 
29 	this(string listenIp ,ushort listenPort)
30 	{
31 		_listenIp = listenIp;
32 		_listenPort = listenPort;
33 
34 		_rpcServer = new Server();
35 		_rpcServer.listen(_listenIp, _listenPort);
36 	}
37 
38 	void setRegistry(RegistryConfig conf)
39 	{
40 		assert(conf.serviceName.length > 0);
41 		_useRegistry = true;
42 		_registryConf = conf;
43 		_serviceName = conf.serviceName;
44 		_neton.ip = conf.ip;
45 		_neton.port = conf.port;
46 	}
47 
48 	void addService(GrpcService service)
49 	{
50 		_rpcServer.register(service);
51 	}
52 
53 	void start()
54 	{
55 		if(_useRegistry)
56 		{
57 			_register = NetonFactory.createRegistryService(_neton);
58 			assert(_register.registerInstance(_serviceName,_listenIp,_listenPort));
59 		}
60 
61 		_rpcServer.start();
62 	}
63 }