erlang获取一个5900——65535之间不重复的一个数据
来源:互联网 发布:c语言水仙花数代码 编辑:程序博客网 时间:2024/06/06 21:40
有这样一个问题,我们要获取一个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一个进程,这个是可以的,但是我不知道使用是不是一件好的事情。
0
上一篇:erlang 进制转换
下一篇:标准I/O的三种缓冲
相关热门文章
- 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...
给主人留下些什么吧!~~
评论热议
0 0
- erlang获取一个5900——65535之间不重复的一个数据
- 获取一个序列中的不重复数据
- 使用gen_fsm分配一个5900到65535之间不重复的数字
- 从5900到65535之间随机分配一个数字——erlang实现
- 获取一个连续不重复的流水号
- C#声明一个100大小的数组 随机生成1-100之间不重复的数
- C#声明一个100大小的数组 随机生成1-100之间不重复的数
- 一个erlang的blog
- 一个产生不重复随机数的算法
- 一个生成不重复随机数的方法
- 一个生成不重复随机数的方法
- 生成一个不重复的随机数组
- 建立一个不重复的随机列表
- 如何产生一个不重复的随机数
- 2017-10-31C#引申的一个例题——不重复抽取元素的组合
- 链表——删除排序链表的重复结点(一个都不保留)
- Hive实现从表中随机抽样得到一个不重复的数据样本
- Hive实现从表中随机抽样得到一个不重复的数据样本
- QT学习笔记
- 共享内存是最快的一种IPC方式
- erlang中的元组与列表的区别与联系
- Erlang App. Management with Rebar
- erlang 进制转换
- erlang获取一个5900——65535之间不重复的一个数据
- 标准I/O的三种缓冲
- iOS——代码自动布局
- arm概念区分
- 实现quartz定时器及quartz定时器原理介绍
- linux下mysql的操作
- 互斥与同步
- 关于c++的迭代器
- linux /dev/console、/dev/tty和/dev/null的区别
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
陆离斑驳
树影斑驳
罪案斑驳
斑驳病图片
斑驳是什么意思
斑驳陆离什么意思
斑驳是什么意思啊
斑驳的夜在说什么歌名
斑驳的意思是什么
斑驳陆离的意思
海贼之兽王降临 斑斑斑斑驳
班驴
斑驴图片
斑鱼图片
斑鱼的做法
青斑鱼
虎斑鱼
斑鱼做法
裸斑鱼为什么有毒
斑鱼火锅
眼斑拟石首鱼
裸斑鱼怎么做好吃
老虎斑鱼
斑鱼火锅连锁店
珍珠斑鱼
青斑鱼多少钱一斤
海参斑鱼
孔雀鱼胎斑高清图片
红星斑鱼多少钱一斤
芝麻斑鱼多少钱一斤
海参斑鱼的家常做法
野生山斑鱼300元一斤
山斑鱼多少钱一斤
斑鱼价格
清蒸斑鱼的做法
东星斑鱼多少钱一斤
七星斑鱼多少钱一斤
冰冻海参斑鱼多少钱一斤
海参斑鱼多少钱一斤
龙记斑鱼府
鱼汁鱼味斑鱼火锅店