/* * 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.