Commits

Alvaro Videla committed 9763c2c

first commit

Comments (0)

Files changed (2)

ErlangSessionHandler.class.php

+<?php
+
+class ErlangSessionHandler
+{
+  protected $host = 'server@127.0.0.1';
+  protected $erlang_cookie = 'ABCDEFG';
+  protected $conn_timeout = 5;
+  
+  protected $link = null;
+  
+  public function open($save_path, $session_name)
+  {
+    if(null === $this->link)
+    {
+      $this->link = peb_connect($this->host, $this->erlang_cookie, $this->conn_timeout);
+      if(!$this->link)
+      {
+        throw new Exception(sprintf("Can't connect to the erlang node %s using erlang_cookie %s", $this->host, $this->erlang_cookie));
+      }
+    }
+    return $this->link;
+  }
+  
+  public function close()
+  {
+    if(is_resource($this->link))
+    {
+      peb_close($this->link);
+    }
+  }
+  
+  public function read($session_id)
+  {
+    $x = peb_encode("[~s]", array(array($session_id)));
+    $result = peb_rpc("session_handler", "read", $x, $this->link);
+    $rs = peb_decode($result);
+    $data = $rs[0];
+    return is_array($data) ? '' : $data;
+  }
+  
+  public function write($session_id, $session_data)
+  {
+    $x = peb_encode("[~s, ~s]", array(array($session_id, $session_data))); 
+    $result = peb_rpc("session_handler", "write", $x, $this->link);
+    unset($result);
+    return true;
+  }
+  
+  public function destroy($session_id)
+  {
+    $x = peb_encode("[~s]", array(array($session_id)));
+    $result = peb_rpc("session_handler", "destroy", $x, $this->link);
+    unset($result);
+    return true;
+  }
+  
+  public function gc($max_expirte_time)
+  {
+    $x = peb_encode('[~i]', array(array($max_expirte_time)));
+    $result = peb_rpc("session_handler", "gc", $x, $this->link);
+    $rs = peb_decode($result);
+    return $rs;
+  }
+}
+% See 
+
+-module(session_handler).
+-author("Alvaro Videla videlalvaro at gmail.com").
+
+-behaviour(gen_server).
+
+-export([start/0, stop/0]).
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+  terminate/2, code_change/3]).
+
+-export([read/1, write/2, destroy/1, gc/1]).
+
+
+start() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+stop()  -> gen_server:cast(?MODULE, stop).
+
+read(SessId) -> gen_server:call(?MODULE, {read, SessId}).
+write(SessId, Data) -> gen_server:call(?MODULE, {write, SessId, Data}).
+destroy(SessId) -> gen_server:call(?MODULE, {destroy, SessId}).
+gc(Timeout) -> gen_server:call(?MODULE, {gc, Timeout}).
+
+-record(state, {table}).
+
+init([]) -> 
+  TableId = ets:new(session, [set]),
+  {ok, #state{table=TableId}}.
+
+handle_call({read, SessId}, _From, #state{table=TableId}=State) -> 
+  error_logger:info_msg("read called ~p.~n", [SessId]),
+  Reply = 
+  case ets:lookup(TableId, list_to_binary(SessId)) of
+    [{_, Data}] -> Data;
+    _ -> ""
+  end,
+  {reply, Reply, State};
+
+handle_call({write, SessId, Data}, _From, #state{table=TableId}=State) ->
+  error_logger:info_msg("write called ~p ~p.~n", [SessId, Data]),
+  Reply = ets:insert(TableId, {list_to_binary(SessId), Data}),
+  {reply, Reply, State};
+
+handle_call({destroy, SessId}, _From, #state{table=TableId}=State) ->
+  error_logger:info_msg("destroy called ~p.~n", [SessId]),
+  Reply = ets:delete(TableId, list_to_binary(SessId)),
+  {reply, Reply, State};
+
+handle_call({gc, Timeout}, _From, State) ->
+  error_logger:info_msg("gc called ~p.~n", [Timeout]),
+  Reply = "GC",
+  {reply, Reply, State}.
+
+handle_cast(stop, State) -> {stop, normal, State}.
+handle_info(_Info, State) -> 
+  error_logger:info_msg("handle_info ~p ~p.~n", [_Info, State]),
+  {noreply, State}.
+terminate(_Reason, _State) -> ok.
+code_change(_OldVsn, State, _Extra) -> {ok, State}.