Next, the previous article deals with the problem of "maintaining currently available topic room information. Each time the startchild promoter process is used, the current room information is saved to the ETS table. More importantly, the corresponding information must be updated every time the theme room exits abnormally or restarts.
Modify the content in room_manager.erl:
Create a function to initialize the room information table:
Inittab ()->
ETS: New (Roominfo ,[ Public ,
Ordered_set,
Named_table,
{Keypos, # roominfo. ID}
])
.
Add the following operations to the room table:
Addroominfo (record)->
# Roominfo {name = name, type = type} = record,
Case Isroomuniq ([], name, type)
{Found, _ id}->
{Fail, room_exists };
{Not_found}->
Roomid = id_generator: getnewid ("room "),
Clienttablename = list_to_atom (? Prifix ++ integer_to_list (roomid )),
Info = record # roominfo {id = roomid, tablename = clienttablename },
ETS: insert (roominfo, Info ),
{Success, info}
End
.
setroomavailble (ID)->
case ETS: Lookup (roominfo, ID) of
record->
ETS: update_element (roominfo, ID, {# roominfo. status, 1}),
OK;
[]->
OK
end
.
Setroomunavailble (ID)->
CaseETS: Lookup (roominfo, ID)
Record->
ETS: update_element (roominfo, ID, {# roominfo. Status, 0 }),
OK;
[]->
OK
End
.
Removeroom (ID)->
ETS: delete (roominfo, ID)
.
To facilitate subsequent operations, two methods are provided, one is to obtain the corresponding id based on the given attributes, and the other is to determine whether the room with the corresponding ID exists:
Getroomproperty (ID, proper)->
Case ETS: Lookup (roominfo, ID)
Record->
# Roominfo {name = Name,
Type = type,
Unum = unum,
Tablename = tab,
Status = status,
Creationdate = crdate} = record,
Case Proper
# Roominfo. Name->
{OK, name };
# Roominfo. Type->
{OK, type };
# Roominfo. unum->
{OK, unum };
# Roominfo. tablename->
{OK, Tab };
# Roominfo. Status->
{OK, status };
# Roominfo. creationdate->
{OK, crdate };
_ Els->
{Fail, illegal_property}
End;
_ Els->
{Fail, not_found}
End
.
Isroomuniq ([], name, type)->
Case ETS: All (roominfo)
[R | L]->
# Roominfo {id = ID, name = thename, type = thetype} = r,
Case String: equal ("name", thename) and string: equal (type, thetype)
True ->
{Found, Id };
False ->
Isroomuniq ([L], name, type)
End;
[]->
{Not_found}
End
;
Isroomuniq ([L], name, type)->
Case ETS: All (roominfo)
[R | L]->
# Roominfo {id = ID, name = thename, type = thetype} = r,
Case String: equal ("name", thename) and string: equal (type, thetype)
True ->
{Found, Id };
False ->
Isroomuniq ([L], name, type)
End;
[]->
{Not_found}
End
.
Modify the terminate of chat_room and delete the corresponding records from the table:
Terminate (_ reason, state)->
# Roominfo {id = ID} = state,
Room_manager: removeroom (ID)
.
Modify the initialization method when the room starts. The room ID and name are passed in from the outside:
Init ([para])->
Id_generator: start_link (),
# Roominfo {id = roomid, tablename = clienttablename} = para,
Client_manager: Init (clienttablename ),
Case Is_record (para, roominfo)
True ->
{OK, # roominfo {unum = 0} = para, tablename = clienttablename };
Els->
{OK, # roominfo {id = roomid, unum = 0, name = "room" ++ integer_to_list (roomid), tablename = clienttablename, type = "def "}}
End
.
Temporarily incorrectCodePerform the test and wait until the overall modification is complete before testing.