ranok / open-server-platform

A platform for easily developing multithreaded/replicated servers

This URL has Read+Write access

open-server-platform / header.erl
100644 57 lines (46 sloc) 1.383 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
%% @author AUTHOR
%% @version VSN
%% @doc An OSP servlet
-module(MOD).
-behavior(osp_servlet).
 
% Export OSP server callback
-export([start_mnesia/0, server/1, init/0, cleanup/0, proto/0]).
 
% Import the OSP socket library
-import(osp_socket, [send/2, recv/2, sendf/3, close/1]).
% Import the OSP shared state library
-import(osp_mnesia, [start_atomic/0]).
% Import a configuration helper function
-import(osp, [get_conf/2]).
 
% Define the Mnesia record
-record(osp_table, {key, val}).
 
%% @doc Returns the IP protocol for the servlet
proto() ->
    PROTO.
 
%% @doc Opens a file for reading or writing
fopen(Filename, Flags) ->
    osp_file:fopen(MOD, Filename, Flags).
 
%% @doc Stores a value in the mnesia database
store(Key, Val) ->
    osp_mnesia:store(MOD_table, Key, Val).
 
%% @doc Gets a value from the mnesia database
retrieve(Key) ->
    osp_mnesia:retrieve(MOD_table, Key).
 
%% @doc Flushes the transactional variables
flush() ->
    osp_mnesia:flush(MOD_table).
 
%% @doc The mnesia startup routine
start_mnesia() ->
    case lists:member(mnesia_sup, erlang:registered()) of
true ->
ok;
false ->
mnesia:start()
    end,
    case catch(mnesia:table_info(MOD_table, all)) of
{'EXIT', _} ->
mnesia:create_table(MOD_table, [{record_name, osp_table}, {attributes, record_info(fields, osp_table)}]);
_ ->
ok
    end,
    ok.
 
%% @doc The main server loop