erlang获取一个5900——65535之间不重复的一个数据

2024-02-17 03:58

本文主要是介绍erlang获取一个5900——65535之间不重复的一个数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

有这样一个问题,我们要获取一个5900~65535之间的一个端口号。对于这个端口号必须是唯一出现的,不能重复,如果分配出去的端口号使用完了之后,还要进行相应的回收,等待下一次的分配。要使用erlang来实现,这个该如何实现呢?
我是这么来实现的,在我的实现当中要使用mnesia数据库。每分配一个端口号出去之后,就将分配的端口号记录在数据库中,并且在数据库中使用一项记录下一次可以分配并且使用的端口号的值。如果下一次请求分配的时候,就直接读取分配出去的那个端口号,然后更新这一项,往数据库中添加已经分配的这个端口号,等下一次如果再次分配的时候,就直接分配。
我的程序代码如下:
  1. %% vncport.erl
  2. %% Author: Sunny
  3. %% Created: 2011-7-25
  4. %% Description: get vncport
  5. -module(vncport).
  6. -date("2011.07.25").

  7. %%
  8. %% Include Files
  9. %%
  10. -include_lib("stdlib/include/qlc.hrl").
  11. -record(vnc_counter,
  12.                 {
  13.                 vncport_num,
  14.                 used_flag
  15.                 }).

  16. %%
  17. %% Exported Functions
  18. %%
  19. -export([init/0, stop/0]).
  20. -export([recovery_one_vncport/1]).
  21. -export([get_next_vncport/0]).

  22. %%
  23. %% Api Functions
  24. %%
  25. init() ->
  26.      mnesia:stop(),
  27.      mnesia:delete_schema([node()]),
  28.      mnesia:create_schema([node()]),
  29.      mnesia:start(),
  30.      mnesia:create_table(vnc_counter,[{attributes, record_info(fields, vnc_counter)}]),
  31.      F = fun(N) ->
  32.              insert_to_table(N)
  33.          end,
  34.      for(5908, 5910, F),
  35.      for(5915, 5917, F),
  36.      mnesia:dirty_update_counter(vnc_counter, mac_id, 5900).

  37. stop() ->
  38.     mnesia:stop().

  39. get_next_vncport() ->
  40.     Query = qlc:q([X#vnc_counter.used_flag || X <- mnesia:table(vnc_counter),
  41.                     X#vnc_counter.vncport_num =:= mac_id]),
  42.     F = fun() ->
  43.             qlc:e(Query)
  44.     end,
  45.     io:format("get the vncport which is not used~n"), %% degug info
  46.     {atomic, [Result]} = mnesia:transaction(F),
  47. %    spawn(fun() -> update_vnc_counter(Result) end),
  48.     update_vnc_counter(Result),
  49.     io:format("get the next vncport is:~p~n", [Result]), %% debug info
  50.     Result.

  51. recovery_one_vncport(N) ->
  52.     Row = {vnc_counter, N},
  53.     F = fun() ->
  54.         mnesia:delete(Row)
  55.     end,
  56.     Result = mnesia:transaction(F),
  57.     io:format("delete ~p successfully~n", [N]), %% debug info
  58.     Result.

  59. %%
  60. %% Local Functions
  61. %%
  62. update_vnc_counter(X) ->
  63.     io:format("spawn one process to update vnc_counter~n"), %% debug info
  64.     insert_to_table(X),
  65.     io:format("add one used vncport to vnc_counter successfully!~n"), %% debug info
  66.     if
  67.         X+1 > 65535 ->
  68.             N=5900;
  69.         X+1 =< 65535 ->
  70.             N=X+1
  71.     end,
  72.     io:format("now begin check whether in vnc_counter~n"), %% debug info
  73.     case check_in_vnc_counter(N) of
  74.         true ->
  75.             io:format("find ~p is in vnc_counter~n", [N]), %% debug info
  76.             NX = find_not_in_vnc_counter(N), %% find one not in vnc_counter
  77.             Row = #vnc_counter{vncport_num=mac_id, used_flag=NX};
  78.         false ->
  79.             io:format("find ~p is not in vnc_counter~n", [N]), %% debug info
  80.             Row = #vnc_counter{vncport_num=mac_id, used_flag=N}
  81.     end,
  82.     F = fun() ->
  83.             mnesia:write(Row)
  84.         end,
  85.     mnesia:transaction(F),
  86.     io:format("update vnc_counter over~n"), %% debug info
  87.     ok.

  88. check_in_vnc_counter(N) ->
  89.     io:format("check ~p is or not in vnc_counter~n", [N]), %% debug info
  90.     Query = qlc:q([X#vnc_counter.vncport_num || X <- mnesia:table(vnc_counter),
  91.                     X#vnc_counter.vncport_num =:= N]),
  92.     F = fun() ->
  93.             qlc:e(Query)
  94.     end,
  95.     Result = mnesia:transaction(F),
  96.     io:format("check result is:~p~n", [Result]), %% debug info
  97.     case Result of
  98.         {atomic, []} ->
  99.             Return = false;
  100.         {atomic, [_]} ->
  101.             Return = true
  102.     end,
  103.     Return.

  104. find_not_in_vnc_counter(N) ->
  105.     if
  106.         N+1 > 65535 ->
  107.             NX = 5900;
  108.         N+1 =< 65535 ->
  109.             NX = N+1
  110.     end,
  111.     case check_in_vnc_counter(NX) of
  112.         true ->
  113.             Result = find_not_in_vnc_counter(N+1);
  114.         false ->
  115.             Result = NX
  116.     end,
  117.     Result.

  118. insert_to_table(N) ->
  119.     Row = #vnc_counter{vncport_num=N, used_flag=yes},
  120.     F = fun() ->
  121.             mnesia:write(Row)
  122.         end,
  123.     mnesia:transaction(F).

  124. for(Max, Max, F) ->
  125.     [F(Max)];
  126. for(Min, Max, F) ->
  127.     [F(Min)|for(Min+1, Max, F)].
这个程序的执行结果是:
  1. 1> c(vncport).
  2. {ok,vncport}
  3. 2> vncport:init().
  4. 5900
  5. 3> vncport:get_next_vncport().
  6. get the vncport which is not used
  7. spawn one process to update vnc_counter
  8. add one used vncport to vnc_counter
  9. now begin check whether in vnc_counter
  10. check 5901 is or not in vnc_counter
  11. check result is:{atomic,[]}
  12. find 5901 is not in vnc_counter
  13. update vnc_counter over
  14. get the next vncport is:5900
  15. 5900
  16. 4> vncport:get_next_vncport().
  17. get the vncport which is not used
  18. spawn one process to update vnc_counter
  19. add one used vncport to vnc_counter
  20. now begin check whether in vnc_counter
  21. check 5902 is or not in vnc_counter
  22. check result is:{atomic,[5902]}
  23. find 5902 is in vnc_counter
  24. check 5903 is or not in vnc_counter
  25. check result is:{atomic,[5903]}
  26. check 5904 is or not in vnc_counter
  27. check result is:{atomic,[5904]}
  28. check 5905 is or not in vnc_counter
  29. check result is:{atomic,[5905]}
  30. check 5906 is or not in vnc_counter
  31. check result is:{atomic,[5906]}
  32. check 5907 is or not in vnc_counter
  33. check result is:{atomic,[5907]}
  34. check 5908 is or not in vnc_counter
  35. check result is:{atomic,[5908]}
  36. check 5909 is or not in vnc_counter
  37. check result is:{atomic,[5909]}
  38. check 5910 is or not in vnc_counter
  39. check result is:{atomic,[5910]}
  40. check 5911 is or not in vnc_counter
  41. check result is:{atomic,[]}
  42. update vnc_counter over
  43. get the next vncport is:5901
  44. 5901
  45. 5> vncport:get_next_vncport().
  46. get the vncport which is not used
  47. spawn one process to update vnc_counter
  48. add one used vncport to vnc_counter
  49. now begin check whether in vnc_counter
  50. check 5912 is or not in vnc_counter
  51. check result is:{atomic,[]}
  52. find 5912 is not in vnc_counter
  53. update vnc_counter over
  54. get the next vncport is:5911
  55. 5911
  56. 6> vncport:get_next_vncport().
  57. get the vncport which is not used
  58. spawn one process to update vnc_counter
  59. add one used vncport to vnc_counter
  60. now begin check whether in vnc_counter
  61. check 5913 is or not in vnc_counter
  62. check result is:{atomic,[5913]}
  63. find 5913 is in vnc_counter
  64. check 5914 is or not in vnc_counter
  65. check result is:{atomic,[5914]}
  66. check 5915 is or not in vnc_counter
  67. check result is:{atomic,[5915]}
  68. check 5916 is or not in vnc_counter
  69. check result is:{atomic,[5916]}
  70. check 5917 is or not in vnc_counter
  71. check result is:{atomic,[5917]}
  72. check 5918 is or not in vnc_counter
  73. check result is:{atomic,[]}
  74. update vnc_counter over
  75. get the next vncport is:5912
  76. 5912
  77. 7> vncport:get_next_vncport().
  78. get the vncport which is not used
  79. spawn one process to update vnc_counter
  80. add one used vncport to vnc_counter
  81. now begin check whether in vnc_counter
  82. check 5919 is or not in vnc_counter
  83. check result is:{atomic,[]}
  84. find 5919 is not in vnc_counter
  85. update vnc_counter over
  86. get the next vncport is:5918
  87. 5918
  88. 8> vncport:get_next_vncport().
  89. get the vncport which is not used
  90. spawn one process to update vnc_counter
  91. add one used vncport to vnc_counter
  92. now begin check whether in vnc_counter
  93. check 5920 is or not in vnc_counter
  94. check result is:{atomic,[]}
  95. find 5920 is not in vnc_counter
  96. update vnc_counter over
  97. get the next vncport is:5919
  98. 5919
  99. 9> vncport:get_next_vncport().
  100. get the vncport which is not used
  101. spawn one process to update vnc_counter
  102. add one used vncport to vnc_counter
  103. now begin check whether in vnc_counter
  104. check 5921 is or not in vnc_counter
  105. check result is:{atomic,[]}
  106. find 5921 is not in vnc_counter
  107. update vnc_counter over
  108. get the next vncport is:5920
  109. 5920
  110. 10>
可能输出的信息比较多些,但是对于本程序来说我们只要关注最后的那个数据就行了。
在我的程序中有一个小的技巧就是在init()函数中使用的
    mnesia:dirty_update_counter(vnc_counter, mac_id, 5900),
    我使用了这个来记录下一次要读取的可以使用的数据,原先使用文件的记录,发现那是有问题的,而且在程序中处理数据库可以使用spawn一个进程,这个是可以的,但是我不知道使用是不是一件好的事情。
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(676) | 评论(0) | 转发(0) |
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...
给主人留下些什么吧!~~
评论热议

这篇关于erlang获取一个5900——65535之间不重复的一个数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/716603

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

poj2406(连续重复子串)

题意:判断串s是不是str^n,求str的最大长度。 解题思路:kmp可解,后缀数组的倍增算法超时。next[i]表示在第i位匹配失败后,自动跳转到next[i],所以1到next[n]这个串 等于 n-next[n]+1到n这个串。 代码如下; #include<iostream>#include<algorithm>#include<stdio.h>#include<math.

poj3261(可重复k次的最长子串)

题意:可重复k次的最长子串 解题思路:求所有区间[x,x+k-1]中的最小值的最大值。求sa时间复杂度Nlog(N),求最值时间复杂度N*N,但实际复杂度很低。题目数据也比较水,不然估计过不了。 代码入下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring

烟火目标检测数据集 7800张 烟火检测 带标注 voc yolo

一个包含7800张带标注图像的数据集,专门用于烟火目标检测,是一个非常有价值的资源,尤其对于那些致力于公共安全、事件管理和烟花表演监控等领域的人士而言。下面是对此数据集的一个详细介绍: 数据集名称:烟火目标检测数据集 数据集规模: 图片数量:7800张类别:主要包含烟火类目标,可能还包括其他相关类别,如烟火发射装置、背景等。格式:图像文件通常为JPEG或PNG格式;标注文件可能为X