summaryrefslogtreecommitdiff
path: root/java/apps/NexusII/src/Room.java
blob: 4a9a294c6537f7b8909844aa664fc82198019ea2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// RoomThread and Room implement the concept of a chat "room"
// Sumedh Mungee <sumedh@cs.wustl.edu>


import java.util.*;
import java.io.File;

// This class(&thread) is responsible for multicasting
// packets on its incoming "client" queues, onto one or
// more outgoing queues, which are picked up by the client.
class RoomThread implements Runnable, consts {

  private MT_Bounded_Queue rq_;
  private Vector clientlist_;

  public RoomThread(MT_Bounded_Queue rq, Vector clientlist) {
    rq_ = rq;
    clientlist_ = clientlist;
  }
  
  public void run() {
    for(;;) {
      dataPacket d = (dataPacket) rq_.dq(); // Extract packet
      Enumeration e = clientlist_.elements(); // Iterate over clients
      while(e.hasMoreElements()) 
        ((ClientHandler)e.nextElement()).getQ().nq(d); // Enqueue packet
    }
  }
}


public class Room implements consts {
  
  private String name_;         // name of this "room" 
  private String last_image_ = new String("NexusII.gif"); // filename of the last image broadcast
  private Thread roomthread_;
  private  MT_Bounded_Queue rq_ = new MT_Bounded_Queue();
  private Vector clientlist_ = new Vector();
  
  // Constructors
  public Room(String name) {
    int i;
    name_ = new String(name);
    roomthread_ = new Thread(new RoomThread(rq_, clientlist_));
    roomthread_.start();
  } 
 
  // Client management methods follow..

  public synchronized void addClient(ClientHandler client) {
    clientlist_.addElement(client);
  }
  // Returns true if this room has now become empty
  public synchronized boolean delClient(ClientHandler client) {
    clientlist_.removeElement(client);
    return clientlist_.isEmpty();
  }
  
  public synchronized boolean checkClient(ClientHandler client) {
    return clientlist_.contains(client);
  }
  
  public synchronized Enumeration clientList() {
    return clientlist_.elements();
  }

  public String getName() {
    return name_;
  }

  public MT_Bounded_Queue getQ() {
    return rq_;
  }

  public synchronized String getLastImageName() {
    return last_image_;
  }

  public synchronized void putNextImageName(String s) {
    last_image_ = s;
  }

  protected void finalize() {
    roomthread_.stop();
    File f = new File(last_image_);
    if(f.exists()) 
      f.delete();
    roomthread_ = null;
  }
}