diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-04-10 19:05:28 +0200 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-04-10 19:05:28 +0200 |
commit | cdf87ca3be8bccf8026fda928b30e959771cbaa4 (patch) | |
tree | b2c38684e45bc63fd60a34e25beaa749cccf2f40 /directory/src/main | |
parent | e100caf587fb7e9db626cf59c6f9dbf9114d72f7 (diff) |
Convert Directory service to using a protobuffer based service.
Diffstat (limited to 'directory/src/main')
-rw-r--r-- | directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java | 13 | ||||
-rw-r--r-- | directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java | 32 |
2 files changed, 36 insertions, 9 deletions
diff --git a/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java b/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java index 7a05894..667d4f0 100644 --- a/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java +++ b/directory/src/main/java/com/orbekk/same/directory/DirectoryApp.java @@ -3,6 +3,7 @@ package com.orbekk.same.directory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.orbekk.protobuf.SimpleProtobufServer; import com.orbekk.same.discovery.DirectoryService; import com.orbekk.same.http.JettyServerBuilder; import com.orbekk.same.http.ServerContainer; @@ -12,13 +13,9 @@ public class DirectoryApp { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(DirectoryApp.class); - ServerContainer server = new JettyServerBuilder(DISCOVERY_PORT) - .withService(new DirectoryServiceImpl(), DirectoryService.class) - .build(); - try { - server.start(); - } catch (Exception e) { - logger.error("Unable to start server.", e); - } + SimpleProtobufServer server = + SimpleProtobufServer.create(DISCOVERY_PORT); + server.registerService(new DirectoryServiceImpl()); + server.start(); } } diff --git a/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java b/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java index 307c0ca..9c38446 100644 --- a/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java +++ b/directory/src/main/java/com/orbekk/same/directory/DirectoryServiceImpl.java @@ -7,9 +7,15 @@ import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.protobuf.RpcCallback; +import com.google.protobuf.RpcController; import com.orbekk.same.discovery.DirectoryService; +import com.orbekk.same.Services; +import com.orbekk.same.Services.Empty; +import com.orbekk.same.Services.MasterState; +import com.orbekk.same.Services.NetworkDirectory; -public class DirectoryServiceImpl implements DirectoryService { +public class DirectoryServiceImpl extends Services.Directory implements DirectoryService { private Logger logger = LoggerFactory.getLogger(getClass()); public final static long EXPIRE_TIME = 15 * 60l * 1000; // 15 minutes List<NetworkEntry> networkList = new ArrayList<NetworkEntry>(); @@ -45,4 +51,28 @@ public class DirectoryServiceImpl implements DirectoryService { networkList.remove(entry); networkList.add(entry); } + + @Override + public void registerNetwork(RpcController controller, MasterState request, + RpcCallback<Empty> done) { + try { + registerNetwork(request.getNetworkName(), request.getMasterUrl()); + } catch (Exception e) { + // No RPC call here. + } + done.run(Empty.getDefaultInstance()); + } + + @Override + public void getNetworks(RpcController controller, Empty request, + RpcCallback<NetworkDirectory> done) { + NetworkDirectory.Builder directory = NetworkDirectory.newBuilder(); + for (NetworkEntry e : networkList) { + directory.addNetwork(MasterState.newBuilder() + .setMasterUrl(e.masterUrl) + .setNetworkName(e.networkName) + .build()); + } + done.run(directory.build()); + } } |