Commit 926012e912bf3dfb5bca2e933dc22e410296c9d3

Authored by totoro
1 parent cdd05882

add socket pool

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