public interface ComputeNode { double calculate(double x); } public class ComputeNodeImpl implements ComputeNode { @Override public double calculate(double x) { return Math.sin(x); } } public class CentralNode { public static void main(String[] args) { ComputeNode computeNode = new ComputeNodeImpl(); ComputeNode stub = (ComputeNode) UnicastRemoteObject.exportObject(computeNode, 0); Registry registry = LocateRegistry.createRegistry(1099); registry.rebind("compute", stub); } } public class ClientNode { public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry("localhost", 1099); ComputeNode computeNode = (ComputeNode) registry.lookup("compute"); double result = computeNode.calculate(3.14); } catch (Exception e) { e.printStackTrace(); } } } public interface StorageNode { void storeFile(String filename, byte[] fileData); byte[] readFile(String filename); } public class StorageNodeImpl implements StorageNode { private Map<String, byte[]> files = new HashMap<>(); @Override public void storeFile(String filename, byte[] fileData) { files.put(filename, fileData); } @Override public byte[] readFile(String filename) { return files.get(filename); } } public class ServerNode { public static void main(String[] args) { try { StorageNode storageNode = new StorageNodeImpl(); StorageNode stub = (StorageNode) UnicastRemoteObject.exportObject(storageNode, 0); Registry registry = LocateRegistry.createRegistry(1099); registry.rebind("storage", stub); } catch (Exception e) { e.printStackTrace(); } } } public class ClientNode { public static void main(String[] args) { try { Registry registry = LocateRegistry.getRegistry("localhost", 1099); StorageNode storageNode = (StorageNode) registry.lookup("storage"); storageNode.storeFile("sample.txt", fileData); byte[] downloadedData = storageNode.readFile("sample.txt"); String downloadedFile = new String(downloadedData); } catch (Exception e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文