WhiteBoard Using RMI
WSU CEG 7370 WhiteBoard Using Java RMI
WbServerImpl.java
Go to the documentation of this file.
1 // file: WbServerImpl.java by pmateti@wright.edu
2 
3 package WhiteBoard;
4 
5 import java.io.*;
6 import java.util.*;
7 import java.rmi.*;
8 import java.rmi.server.*;
9 
10 class ABoard {
11  String boardName; // Name of this board
12  Vector <LineCoords> vLines; // all lines on this board
13  Vector <WbClient> vClients; // all clients on this board
14 
15  public ABoard(String brdnm) {
16  boardName = brdnm;
17  vLines = new Vector <LineCoords> ();
18  vClients = new Vector <WbClient> ();
19  }
20 }
21 
22 public class WbServerImpl
23  extends UnicastRemoteObject
24  implements WbServer {
25 
26  private Vector <ABoard> vBoards; // all boards on this server
27  private String myURL;
28 
29  public WbServerImpl(String [] args) throws Exception {
30  // args = [serverID, serverMcnm]
31  vBoards = new Vector <ABoard> ();
32  myURL = Invoke.makeURL('S', args[0]);
33  Naming.rebind(myURL, this); // rmi register ourselves
34  Invoke.myPrint("WbServerImpl", myURL + " started");
35  }
36 
37  private void pleaseDie() {
38  int delay = 5000; // in msec, delayed death
39  Timer timer = new Timer();
40  timer.schedule( new TimerTask(){
41  public void run() {
42  try {Naming.unbind(myURL);}
43  catch (Exception e) {e.printStackTrace();}
44  Invoke.myPrint("WbServerImpl", myURL + " exits");
45  System.exit(0);
46  }
47  }, delay);
48  }
49 
50  private ABoard findAboard(String brdnm) {
51  for (Enumeration e = vBoards.elements(); e.hasMoreElements();) {
52  ABoard b = (ABoard) e.nextElement();
53  if (brdnm.equals(b.boardName))
54  return b;
55  }
56  return null;
57  }
58 
59  public void sendAllLines(WbClient wc, String brdnm)
60  throws java.rmi.RemoteException {
61  ABoard ab = findAboard(brdnm);
62  sendAllLines(wc, ab);
63  }
64 
65  private void sendAllLines(WbClient wc, ABoard ab) {
66  for (Enumeration e = ab.vLines.elements(); e.hasMoreElements(); ) {
67  try {wc.updateBoard((LineCoords) e.nextElement());}
68  catch (Exception x) {x.printStackTrace();}
69  }
70  }
71 
72  public void addClient(WbClient wc, String brdnm)
73  throws java.rmi.RemoteException {
74  ABoard ab = findAboard(brdnm);
75  if (ab == null) {
76  ab = new ABoard(brdnm);
77  vBoards.addElement(ab);
78  } else {
79  sendAllLines(wc, ab); // new client on an old board
80  }
81  ab.vClients.addElement(wc);
82  }
83 
84  public void delClient(WbClient wc, String brdnm)
85  throws java.rmi.RemoteException {
86  ABoard ab = findAboard(brdnm);
87  if (ab == null) return;
88 
89  ab.vClients.removeElement(wc);
90 
91  // If this is the last client in board, delete board
92  if (ab.vClients.size() == 0) vBoards.removeElement(ab);
93 
94  // If this was the last board, terminate this server
95  if (vBoards.size() == 0) pleaseDie();
96  }
97 
98  public void addLine(LineCoords ln, String brdnm)
99  throws java.rmi.RemoteException {
100  ABoard ab = findAboard(brdnm);
101  if (ab == null) return;
102 
103  ab.vLines.addElement(ln);
104 
105  // Broadcast to all the clients on this board
106  for (Enumeration e = ab.vClients.elements(); e.hasMoreElements();) {
107  WbClient wc = (WbClient) e.nextElement();
108  try {wc.updateBoard(ln);}
109  catch (Exception x) {x.printStackTrace();}
110  }
111  }
112 
113  public static void main(String args[]) {
114  try { WbServerImpl wsi = new WbServerImpl(args);}
115  catch (Exception e) {e.printStackTrace();}
116  }
117 }
118 
119 // -eof-
void addLine(LineCoords ln, String brdnm)
Vector< ABoard > vBoards
void sendAllLines(WbClient wc, String brdnm)
ABoard findAboard(String brdnm)
static void main(String args[])
WbServerImpl(String[] args)
void delClient(WbClient wc, String brdnm)
void sendAllLines(WbClient wc, ABoard ab)
void addClient(WbClient wc, String brdnm)
Copyright © 2014 www.wright.edu/~pmateti;   thanks to doxygen