Erlang chat room Program (9) theme room 2-room Information Management

Source: Internet
Author: User

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.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.