From 926012e912bf3dfb5bca2e933dc22e410296c9d3 Mon Sep 17 00:00:00 2001 From: totoro Date: Sun, 13 Oct 2013 16:20:24 +0200 Subject: [PATCH] add socket pool --- network/socket_pool.anubis | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+), 0 deletions(-) create mode 100644 network/socket_pool.anubis diff --git a/network/socket_pool.anubis b/network/socket_pool.anubis new file mode 100644 index 0000000..05cec46 --- /dev/null +++ b/network/socket_pool.anubis @@ -0,0 +1,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. + -- libgit2 0.21.4