📄 rmilistserverimpl.java
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.rmi.*;import java.util.*;import java.rmi.server.*;import java.rmi.registry.*;public class RMIListServerImpl extends UnicastRemoteObject implements RMIListServer { protected IDList idList; protected Vector clients; public RMIListServerImpl () throws RemoteException { idList = new IDList (); clients = new Vector (); } public synchronized IDList register (RMIListClient client) { clients.addElement (client); return idList; } public synchronized void deregister (RMIListClient client) { clients.removeElement (client); } public synchronized void addElement (Object element) { ID id = idList.allocateID (); if (idList.addElement (id, element)) { for (int i = 0; i < clients.size (); ++ i) { try { ((RMIListClient) clients.elementAt (i)).elementAdded (id, element); } catch (RemoteException ex) { clients.removeElementAt (i --); } } } } public synchronized void updateElement (ID id, Object element) { ID newID = idList.allocateID (); if (idList.updateElement (id, newID, element)) { for (int i = 0; i < clients.size (); ++ i) { try { ((RMIListClient) clients.elementAt (i)).elementUpdated (id, newID, element); } catch (RemoteException ex) { clients.removeElementAt (i --); } } } } public synchronized void replaceElement (ID id, Object element) { ID newID = idList.allocateID (); if (idList.replaceElement (id, newID, element)) { for (int i = 0; i < clients.size (); ++ i) { try { ((RMIListClient) clients.elementAt (i)).elementReplaced (id, newID, element); } catch (RemoteException ex) { clients.removeElementAt (i --); } } } } public synchronized void removeElement (ID id) { if (idList.removeElement (id)) { for (int i = 0; i < clients.size (); ++ i) { try { ((RMIListClient) clients.elementAt (i)).elementRemoved (id); } catch (RemoteException ex) { clients.removeElementAt (i --); } } } } public static void main (String[] args) throws RemoteException, AlreadyBoundException { if (args.length != 2) throw new IllegalArgumentException ("Syntax: RMIListServerImpl <port> <service>"); RMIListServerImpl listServer = new RMIListServerImpl (); Registry registry = LocateRegistry.createRegistry (Integer.parseInt (args[0])); registry.bind (args[1], listServer); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -