socket_pool.anubis
2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Created by PyramIDE.
* User: Totoro
* Date: 13/08/2013
* Time: 00:11
*
*/
read tools/basis.anubis
public type Socket_pool:
pool(String pk, Var(List(RWStream)) socket).
public type Pool_list:
pool_list(Var(List(Socket_pool)) p_list).
define Maybe(Socket_pool)
get_pool
(
List(Socket_pool) list,
String pk
)=
//if p_list is pool_list(list) then
if list is
{
[] then failure,
[ h . t ] then
if h is pool(_pk, _) then
if _pk = pk then //we found the pool
success(h)
else
get_pool(t, pk)
}.
define Socket_pool
get_or_create_pool
(
Var(List(Socket_pool)) p_list,
// List(Socket_pool) p_list,
String pk,
List(Socket_pool) current
)=
if current is
{
[] then
with new_entry = pool(pk, var([])),
p_list <- [ new_entry . *p_list];
new_entry,
[ h . t ] then
if h is pool(_pk, _) then
if _pk = pk then
h
else
get_or_create_pool(p_list, pk, t)
}.
public define One
add_socket_to_pool
(
Pool_list pool_list, //list of pools
String pk, //primary key of the pool
RWStream new_socket //new socket to add to pool
)=
//println("Add socket to pool ["+pk+"]");
with pool = get_or_create_pool(p_list(pool_list), pk,*p_list(pool_list)),
socket(pool) <- [ new_socket . *socket(pool)];
println("add socket pool ["+pk+"] length "+length(*socket(pool)))
.
public define Maybe(RWStream)
get_socket_from_pool
(
Pool_list pool_list, //list of pools
String pk //primary key of the pool
)=
if get_pool(*p_list(pool_list), pk) is
{
failure then failure, //the pool doesn't exists !!!
success(pool) then
if *socket(pool) is
{
[] then println("No more socket in pool "+pk);failure, // no more socket in queue
[h . t] then
socket(pool) <- t;
println("get socket pool ["+pk+"] length "+length(t));
success(h)
}
}.
public define Maybe(RWStream)
get_socket_from_pool
(
Pool_list pool_list, //list of pools
String pk, //primary key of the pool
Int try
)=
if try > 0 then
if get_socket_from_pool(pool_list, pk) is
{
failure then
sleep(1000);
get_socket_from_pool(pool_list, pk, try-1), //the pool doesn't exists !!!
success(sock) then success(sock)
}
else
println("get_socket_from_pool No available socket");
failure.