4 * Various JSON-RPC calls will want to maintain open resources within a
5 * session, across multiple calls. We'll provide a standardized way to
6 * maintain those open resources here, with some protection against rogue
10 function _resourcesCreate()
12 /* The being-created resources object */
16 * The maximum number of resources available to a single session. This
17 * should be more than is ever needed (even by reasonable recursive
18 * functions) but limits rogue scripts ability to generate DOS attacks.
20 o.RESOURCE_LIMIT = 100;
22 /* List of current resources */
23 o.resourceList = new Object();
25 /* Resource id values will be constantly incrementing; never reset. */
26 o.resourceList.id = 0;
28 /* We'll maintain our own count of the number of open resources */
29 o.resourceList.count = 0;
33 * Set a new saved resource.
35 function _set(resource, type, error)
37 /* Do they already have the maximum number of resources allocated? */
38 if (this.resourceList.count >= this.RESOURCE_LIMIT)
41 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
42 error.setError(jsonrpc.Constant.ServerError.ResourceError,
43 "Session limit on resources (" +
49 /* Allocate an object to hold the new resource and its type */
52 /* Save the resource and its type */
53 r.resource = resource;
56 /* Add this resource to the list */
57 this.resourceList[this.resourceList.id] = r;
59 /* There's a new resource in the list! */
60 this.resourceList.count++;
63 * Return the index of the resource, its resource id, and advance to
64 * the next resource id for next time.
66 var id = this.resourceList.id;
67 this.resourceList.id++;
73 * Get a previously-saved resource
75 function _get(resourceId, error)
77 /* Does the specified resource id exist? */
78 if (! this.resourceList[resourceId])
81 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
82 error.setError(jsonrpc.Constant.ServerError.ResourceError,
83 "Resource not found.");
87 /* Retrieve the resource */
88 var r = this.resourceList[resourceId];
90 /* Give 'em what they came for! */
96 * Find a previously-saved resource
98 function _find(type, error)
100 /* Does the specified resource id exist? */
101 for (var resourceId in this.resourceList)
103 /* Retrieve the resource */
104 var r = this.resourceList[resourceId];
106 /* Ignore "id" and "count" integer fields */
107 if (typeof(r) == "object")
109 /* Is the specified resource the correct type? */
112 /* Yup, this is the one they want. */
118 /* It wasn't found. */
124 * Release a previously-saved resource, allowing it to be freed
126 function _release(resourceId, error)
128 /* Does the specified resource id exist? */
129 if (! this.resourceList[resourceId])
132 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
133 error.setError(jsonrpc.Constant.ServerError.ResourceError,
134 "Resource not found.");
138 /* It exists. Delete it. */
139 delete this.resourceList[resourceId];
141 /* There's now one fewer resources in the list */
142 this.resourceList.count--;
144 o.release = _release;
147 * Retrieve the list of resources (for debugging) */
149 function _getList(error)
151 return this.resourceList;
153 o.getList = _getList;
158 /* singleton: create session resources list */
159 if (! session.resources)
161 session.resources = _resourcesCreate();