本文主要是介绍erlang获取一个5900——65535之间不重复的一个数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
有这样一个问题,我们要获取一个5900~65535之间的一个端口号。对于这个端口号必须是唯一出现的,不能重复,如果分配出去的端口号使用完了之后,还要进行相应的回收,等待下一次的分配。要使用erlang来实现,这个该如何实现呢?
我是这么来实现的,在我的实现当中要使用mnesia数据库。每分配一个端口号出去之后,就将分配的端口号记录在数据库中,并且在数据库中使用一项记录下一次可以分配并且使用的端口号的值。如果下一次请求分配的时候,就直接读取分配出去的那个端口号,然后更新这一项,往数据库中添加已经分配的这个端口号,等下一次如果再次分配的时候,就直接分配。
我的程序代码如下:
- %% vncport.erl
- %% Author: Sunny
- %% Created: 2011-7-25
- %% Description: get vncport
- -module(vncport).
- -date("2011.07.25").
-
- %%
- %% Include Files
- %%
- -include_lib("stdlib/include/qlc.hrl").
- -record(vnc_counter,
- {
- vncport_num,
- used_flag
- }).
-
- %%
- %% Exported Functions
- %%
- -export([init/0, stop/0]).
- -export([recovery_one_vncport/1]).
- -export([get_next_vncport/0]).
-
- %%
- %% Api Functions
- %%
- init() ->
- mnesia:stop(),
- mnesia:delete_schema([node()]),
- mnesia:create_schema([node()]),
- mnesia:start(),
- mnesia:create_table(vnc_counter,[{attributes, record_info(fields, vnc_counter)}]),
- F = fun(N) ->
- insert_to_table(N)
- end,
- for(5908, 5910, F),
- for(5915, 5917, F),
- mnesia:dirty_update_counter(vnc_counter, mac_id, 5900).
-
- stop() ->
- mnesia:stop().
-
- get_next_vncport() ->
- Query = qlc:q([X#vnc_counter.used_flag || X <- mnesia:table(vnc_counter),
- X#vnc_counter.vncport_num =:= mac_id]),
- F = fun() ->
- qlc:e(Query)
- end,
- io:format("get the vncport which is not used~n"), %% degug info
- {atomic, [Result]} = mnesia:transaction(F),
- % spawn(fun() -> update_vnc_counter(Result) end),
- update_vnc_counter(Result),
- io:format("get the next vncport is:~p~n", [Result]), %% debug info
- Result.
-
- recovery_one_vncport(N) ->
- Row = {vnc_counter, N},
- F = fun() ->
- mnesia:delete(Row)
- end,
- Result = mnesia:transaction(F),
- io:format("delete ~p successfully~n", [N]), %% debug info
- Result.
-
- %%
- %% Local Functions
- %%
- update_vnc_counter(X) ->
- io:format("spawn one process to update vnc_counter~n"), %% debug info
- insert_to_table(X),
- io:format("add one used vncport to vnc_counter successfully!~n"), %% debug info
- if
- X+1 > 65535 ->
- N=5900;
- X+1 =< 65535 ->
- N=X+1
- end,
- io:format("now begin check whether in vnc_counter~n"), %% debug info
- case check_in_vnc_counter(N) of
- true ->
- io:format("find ~p is in vnc_counter~n", [N]), %% debug info
- NX = find_not_in_vnc_counter(N), %% find one not in vnc_counter
- Row = #vnc_counter{vncport_num=mac_id, used_flag=NX};
- false ->
- io:format("find ~p is not in vnc_counter~n", [N]), %% debug info
- Row = #vnc_counter{vncport_num=mac_id, used_flag=N}
- end,
- F = fun() ->
- mnesia:write(Row)
- end,
- mnesia:transaction(F),
- io:format("update vnc_counter over~n"), %% debug info
- ok.
-
- check_in_vnc_counter(N) ->
- io:format("check ~p is or not in vnc_counter~n", [N]), %% debug info
- Query = qlc:q([X#vnc_counter.vncport_num || X <- mnesia:table(vnc_counter),
- X#vnc_counter.vncport_num =:= N]),
- F = fun() ->
- qlc:e(Query)
- end,
- Result = mnesia:transaction(F),
- io:format("check result is:~p~n", [Result]), %% debug info
- case Result of
- {atomic, []} ->
- Return = false;
- {atomic, [_]} ->
- Return = true
- end,
- Return.
-
- find_not_in_vnc_counter(N) ->
- if
- N+1 > 65535 ->
- NX = 5900;
- N+1 =< 65535 ->
- NX = N+1
- end,
- case check_in_vnc_counter(NX) of
- true ->
- Result = find_not_in_vnc_counter(N+1);
- false ->
- Result = NX
- end,
- Result.
-
- insert_to_table(N) ->
- Row = #vnc_counter{vncport_num=N, used_flag=yes},
- F = fun() ->
- mnesia:write(Row)
- end,
- mnesia:transaction(F).
-
- for(Max, Max, F) ->
- [F(Max)];
- for(Min, Max, F) ->
- [F(Min)|for(Min+1, Max, F)].
这个程序的执行结果是:
- 1> c(vncport).
- {ok,vncport}
- 2> vncport:init().
- 5900
- 3> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5901 is or not in vnc_counter
- check result is:{atomic,[]}
- find 5901 is not in vnc_counter
- update vnc_counter over
- get the next vncport is:5900
- 5900
- 4> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5902 is or not in vnc_counter
- check result is:{atomic,[5902]}
- find 5902 is in vnc_counter
- check 5903 is or not in vnc_counter
- check result is:{atomic,[5903]}
- check 5904 is or not in vnc_counter
- check result is:{atomic,[5904]}
- check 5905 is or not in vnc_counter
- check result is:{atomic,[5905]}
- check 5906 is or not in vnc_counter
- check result is:{atomic,[5906]}
- check 5907 is or not in vnc_counter
- check result is:{atomic,[5907]}
- check 5908 is or not in vnc_counter
- check result is:{atomic,[5908]}
- check 5909 is or not in vnc_counter
- check result is:{atomic,[5909]}
- check 5910 is or not in vnc_counter
- check result is:{atomic,[5910]}
- check 5911 is or not in vnc_counter
- check result is:{atomic,[]}
- update vnc_counter over
- get the next vncport is:5901
- 5901
- 5> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5912 is or not in vnc_counter
- check result is:{atomic,[]}
- find 5912 is not in vnc_counter
- update vnc_counter over
- get the next vncport is:5911
- 5911
- 6> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5913 is or not in vnc_counter
- check result is:{atomic,[5913]}
- find 5913 is in vnc_counter
- check 5914 is or not in vnc_counter
- check result is:{atomic,[5914]}
- check 5915 is or not in vnc_counter
- check result is:{atomic,[5915]}
- check 5916 is or not in vnc_counter
- check result is:{atomic,[5916]}
- check 5917 is or not in vnc_counter
- check result is:{atomic,[5917]}
- check 5918 is or not in vnc_counter
- check result is:{atomic,[]}
- update vnc_counter over
- get the next vncport is:5912
- 5912
- 7> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5919 is or not in vnc_counter
- check result is:{atomic,[]}
- find 5919 is not in vnc_counter
- update vnc_counter over
- get the next vncport is:5918
- 5918
- 8> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5920 is or not in vnc_counter
- check result is:{atomic,[]}
- find 5920 is not in vnc_counter
- update vnc_counter over
- get the next vncport is:5919
- 5919
- 9> vncport:get_next_vncport().
- get the vncport which is not used
- spawn one process to update vnc_counter
- add one used vncport to vnc_counter
- now begin check whether in vnc_counter
- check 5921 is or not in vnc_counter
- check result is:{atomic,[]}
- find 5921 is not in vnc_counter
- update vnc_counter over
- get the next vncport is:5920
- 5920
- 10>
可能输出的信息比较多些,但是对于本程序来说我们只要关注最后的那个数据就行了。
在我的程序中有一个小的技巧就是在init()函数中使用的
mnesia:dirty_update_counter(vnc_counter, mac_id, 5900),
我使用了这个来记录下一次要读取的可以使用的数据,原先使用文件的记录,发现那是有问题的,而且在程序中处理数据库可以使用spawn一个进程,这个是可以的,但是我不知道使用是不是一件好的事情。
相关热门文章
- python 自动化测试平台 Robot ...
- python 自动化测试平台 Robot ...
- python snmp 自动化2-在python...
- 自动化测试详细测试计划 模板...
- python snmp 自动化3-修改pyth...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
这篇关于erlang获取一个5900——65535之间不重复的一个数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!