KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chipchat > Channel


1 /*
2  * Created on 2004. 3. 9.
3  *
4  */

5 package chipchat;
6
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /**
12  * @author Mr.Lee
13  */

14 public final class Channel {
15    /** Chipchat Instance.. */
16    private ChipChat parent;
17
18    /*
19     * Rooms Operations...
20     */

21
22    /** Rooms */
23    private Map JavaDoc rooms = new HashMap JavaDoc();
24
25    /**
26     * Make new room.
27     * @param roomname The name of room
28     * @param maxman The maxmum number of person in room
29     * @param passwd Password
30     * @param master Administrator id number
31     * @return New Room Numbers.
32     * @throws Exception When Room is full.
33     */

34    public Long JavaDoc makeRoom(
35       final String JavaDoc roomname,
36       final int maxman,
37       final String JavaDoc passwd,
38       final int master)
39       throws Exception JavaDoc {
40       Long JavaDoc num = getUniqueNumber();
41       Room room = new Room(this, num, roomname, maxman, passwd, master);
42       synchronized (rooms) {
43          rooms.put(num, room);
44       }
45       return num;
46    }
47
48    /**
49     * Remove Room.
50     * @param num Unique number of room.
51     */

52    public void removeRoom(final Long JavaDoc num) {
53       synchronized (rooms) {
54          rooms.remove(num);
55       }
56    }
57
58    /**
59     * Get Room.
60     * @param num Unique number of room.
61     * @return Room.
62     */

63    public Room getRoom(final Long JavaDoc num) {
64       synchronized (rooms) {
65          return (Room) rooms.get(num);
66       }
67    }
68
69    /*
70     * Make Unique Number..
71     */

72    /** Lock object for unique number. */
73    private Object JavaDoc uniqueNumberLock = new Object JavaDoc();
74    /** Number of last maked. */
75    private long uniqueNum = 0;
76
77    /**
78     * Make unique number.
79     * @return Unique number
80     */

81    private Long JavaDoc getUniqueNumber() {
82       long r;
83       synchronized (uniqueNumberLock) {
84          r = uniqueNum++;
85       }
86       return new Long JavaDoc(r);
87    }
88
89    /*
90     * Room Lists....
91     */

92    /** Temporary List of room for listing. */
93    private RoomInfo[] roomList;
94    /** Whether rooms are changed or not. */
95    private boolean listChanged = true;
96
97    /**
98     * Set that rooms are changed.
99     */

100    protected void setListChanged() {
101       listChanged = true;
102    }
103
104    /**
105     * Get temporary list of rooms.
106     * @return Temporary list of rooms.
107     */

108    public RoomInfo[] getRoomList() {
109       if (listChanged) {
110          makeRoomList();
111       }
112       return roomList;
113    }
114
115    /**
116     * Make new temporary list of rooms.
117     */

118    private void makeRoomList() {
119       synchronized (rooms) {
120          if (listChanged) {
121             RoomInfo[] tmpRoomList = new RoomInfo[rooms.size()];
122             int c = 0;
123             Iterator JavaDoc i = rooms.values().iterator();
124             while (i.hasNext()) {
125                Object JavaDoc value = i.next();
126                tmpRoomList[c] = new RoomInfo((Room) value);
127                c++;
128             }
129             roomList = tmpRoomList;
130          }
131       }
132    }
133 }
134
Popular Tags