summaryrefslogtreecommitdiff
path: root/services/resources.esp
blob: d4a77f790729c64e46aefe8cbb82fb3095e3e4f2 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<%

/*
 * Various JSON-RPC calls will want to maintain open resources within a
 * session, across multiple calls.  We'll provide a standardized way to
 * maintain those open resources here, with some protection against rogue
 * scripts.
 */

function _resourcesCreate()
{
    /* The being-created resources object */
    var o = new Object();

    /*
     * The maximum number of resources available to a single session.  This
     * should be more than is ever needed (even by reasonable recursive
     * functions) but limits rogue scripts ability to generate DOS attacks.
     */
    o.RESOURCE_LIMIT = 100;

    /* List of current resources */
    o.resourceList = new Object();

    /* Resource id values will be constantly incrementing; never reset. */
    o.resourceList.id = 0;

    /* We'll maintain our own count of the number of open resources */
    o.resourceList.count = 0;

    /*
     * Resource types
     */
    o.Type = new Object();
    o.Type.ldb          = 1;    /* database handle */
    o.Type.tid          = 2;    /* tree id */
    o.Type.fid          = 3;    /* file id */
    /* etc., etc., etc. */


    /*
     * Set a new saved resource.
     */
    function _set(resource, type, error)
    {
        /* Do they already have the maximum number of resources allocated? */
        if (this.resourceList.count >= this.RESOURCE_LIMIT)
        {
            /* Yup. */
            error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
            error.setError(JsonRpcError_ResourceError,
                           "Session limit on resources (" +
                           RESOURCE_LIMIT +
                           ") exceeded.");
            return error;
        }

        /* Allocate an object to hold the new resource and its type */
        var r = new Object();

        /* Save the resource and its type */
        r.resource = resource;
        r.type = type;

        /* Add this resource to the list */
        this.resourceList[this.resourceList.id] = r;

        /* There's a new resource in the list! */
        this.resourceList.count++;

        /*
         * Return the index of the resource, its resource id, and advance to
         * the next resource id for next time.
         */
        var id = this.resourceList.id;
        this.resourceList.id++;
        return id;
    }
    o.set = _set;

    /*
     * Get a previously-saved resource
     */
    function _get(resourceId, type, error)
    {
        /* Does the specified resource id exist? */
        if (! this.resourceList[resourceId])
        {
            /* Nope. */
            error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
            error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
                           "Resource not found.");
            return error;
        }

        /* Retrieve the resource */
        var r = this.resourceList[resourceId];

        /* Is the specified resource the correct type? */
        if (r.type != type)
        {
            /* Nope. */
            error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
            error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
                           "Incorrect type for specified resource id.");
            return error;
        }

        /* Give 'em what they came for! */
        return r.resource;
    }
    o.get = _get;

    /*
     * Release a previously-saved resource, allowing it to be freed
     */
    function _release(resourceId, error)
    {
        /* Does the specified resource id exist? */
        if (! this.resourceList[resourceId])
        {
            /* Nope. */
            error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
            error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
                           "Resource not found.");
            return error;
        }

        /* It exists.  Delete it. */
        delete this.resourceList[resourceId];

        /* There's now one fewer resources in the list */
        this.resourceList.count--;
    }
    o.release = _release;

    /*
     * Retrieve the list of resources (for debugging) */
     */
    function _getList(error)
    {
        return this.resourceList;
    }
    o.getList = _getList;

    return o;
}

/* singleton: create session resources list */
if (! session.resources)
{
    session.resources = _resourcesCreate();
}


/*
 * Local Variables:
 * mode: c
 * End:
 */
%>