summaryrefslogtreecommitdiff
path: root/NET/worlds/network/RoomMgr.java
blob: 24d366f77209ec4711eee0dfb4ffd21a49ae8ff9 (plain) (blame)
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
package NET.worlds.network;

import NET.worlds.core.Std;
import java.util.Enumeration;
import java.util.Hashtable;

class RoomMgr {
   Hashtable<Integer, NetworkRoom> _roomFRoomID = new Hashtable<Integer, NetworkRoom>();
   Hashtable<String, NetworkRoom> _requestList = null;
   int _requestCount = 0;

   public RoomMgr() {
   }

   public synchronized void addRequest(String roomName, NetworkRoom room) {
      if (this._requestCount == 0) {
         this._requestList = new Hashtable<String, NetworkRoom>();
      }

      assert this._requestList.get(roomName) == null;

      this._requestList.put(roomName, room);
      this._requestCount++;
   }

   public synchronized void delRequest(String roomName) {
      if (this._requestList == null) {
         System.out.println(this + ": delRequest() - " + roomName);
         new Exception().printStackTrace(System.out);
      } else {
         this._requestCount--;
         this._requestList.remove(roomName);
         if (this._requestCount == 0) {
            this._requestList = null;
         }
      }
   }

   public synchronized NetworkRoom getRequest(String roomName) {
      if (this._requestList == null) {
         return null;
      } else {
         NetworkRoom tmpRoom = this._requestList.get(roomName);
         if (tmpRoom != null) {
            this.delRequest(roomName);
         }

         return tmpRoom;
      }
   }

   public void regRoomID(int roomID, NetworkRoom room) {
      NetworkRoom noRoom = this._roomFRoomID.get(new Integer(roomID));
      if (noRoom != null && noRoom != room) {
         System.out.println("[" + Std.getRealTime() + "]: Weird - room is already registered.");
         System.out.println("new roomID = " + roomID + ", new room = " + room);
         System.out.println(room.debugStuff());
         System.out.println("old room is " + noRoom);
         System.out.println(noRoom.debugStuff());
         new Exception().printStackTrace(System.out);
      }

      this._roomFRoomID.put(new Integer(roomID), room);
   }

   public void delRoomID(int roomID, NetworkRoom delRoom) {
      NetworkRoom room = this._roomFRoomID.remove(new Integer(roomID));
      if (room == null) {
         System.out.println("Error - deleting a bad roomID: " + roomID);
         System.out.println(delRoom.debugStuff());
         new Exception().printStackTrace(System.out);
      }
   }

   public NetworkRoom getRoom(int roomID) {
      return roomID == 0 ? null : this._roomFRoomID.get(new Integer(roomID));
   }

   public Enumeration<NetworkRoom> rooms() {
      return this._roomFRoomID.elements();
   }
}