Debian Codegraph @

terps/tads/tads3/vmhttpsrv.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2010 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmhttpsrv.h - CVmObjHTTPServer object
12 Function
13
14 Notes
15
16 Modified
17 04/22/10 MJRoberts - Creation
18 */
19
20 #ifndef VMHTTPSRV_H
21 #define VMHTTPSRV_H
22
23 #include "t3std.h"
24 #include "vmtype.h"
25 #include "vmglob.h"
26 #include "vmobj.h"
27 #include "vmundo.h"
28 #include "vmnet.h"
29
30
31 /* ------------------------------------------------------------------------ */
32 /*
33 * HTTP server objects have no image file representation, because these
34 * objects are inherently transient.
35 */
36
37
38 /* ------------------------------------------------------------------------ */
39 /*
40 * Our in-memory extension data structure. This is usually a 'struct'
41 * that contains the same information as in the image file, but using
42 * native types.
43 */
44 struct vm_httpsrv_ext
45 {
46 /* allocate the structure */
47 static vm_httpsrv_ext *alloc_ext(VMG_ class CVmObjHTTPServer *self);
48
49 /* the listener object */
50 class TadsListener *l;
51
52 /* original requested listener address */
53 char *addr;
54 };
55
56
57 /* ------------------------------------------------------------------------ */
58 /*
59 * CVmObjHTTPServer intrinsic class definition
60 */
61
62 class CVmObjHTTPServer: public CVmObject
63 {
64 friend class CVmMetaclassHTTPServer;
65
66 public:
67 /* metaclass registration object */
68 static class CVmMetaclass *metaclass_reg_;
69 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
70
71 /* am I of the given metaclass? */
72 virtual int is_of_metaclass(class CVmMetaclass *meta) const
73 {
74 /* try my own metaclass and my base class */
75 return (meta == metaclass_reg_
76 || CVmObject::is_of_metaclass(meta));
77 }
78
79 /* is this a CVmObjHTTPServer object? */
80 static int is_httpsrv_obj(VMG_ vm_obj_id_t obj)
81 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
82
83 /* create dynamically using stack arguments */
84 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
85 uint argc);
86
87 /*
88 * call a static property - we don't have any of our own, so simply
89 * "inherit" the base class handling
90 */
91 static int call_stat_prop(VMG_ vm_val_t *result,
92 const uchar **pc_ptr, uint *argc,
93 vm_prop_id_t prop)
94 {
95 return CVmObject::
96 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
97 }
98
99 /* notify of deletion */
100 void notify_delete(VMG_ int in_root_set);
101
102 /* set a property */
103 void set_prop(VMG_ class CVmUndo *undo,
104 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
105
106 /* get a property */
107 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
108 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
109
110 /*
111 * receive savepoint notification - we don't keep any
112 * savepoint-relative records, so we don't need to do anything here
113 */
114 void notify_new_savept() { }
115
116 /* apply an undo record - we don't keep any undo */
117 void apply_undo(VMG_ struct CVmUndoRecord *) { }
118
119 /* discard an undo record */
120 void discard_undo(VMG_ struct CVmUndoRecord *) { }
121
122 /* mark our undo record references - we don't have undo or references */
123 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
124
125 /* mark our references - we don't have any to mark */
126 void mark_refs(VMG_ uint) { }
127
128 /* remove weak references - we don't have any weak refs */
129 void remove_stale_weak_refs(VMG0_) { }
130 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
131
132 /* load from an image file */
133 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
134
135 /* reload from an image file */
136 void reload_from_image(VMG_ vm_obj_id_t self,
137 const char *ptr, size_t siz);
138
139 /* rebuild for image file */
140 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
141
142 /* save to a file */
143 void save_to_file(VMG_ class CVmFile *fp);
144
145 /* restore from a file */
146 void restore_from_file(VMG_ vm_obj_id_t self,
147 class CVmFile *fp, class CVmObjFixup *fixups);
148
149 /*
150 * determine if we've been changed since loading - assume we have (if
151 * we haven't, the only harm is the cost of unnecessarily reloading or
152 * saving)
153 */
154 int is_changed_since_load() const { return TRUE; }
155
156 /*
157 * Get my listening address and port number. 'host' is the host name
158 * original specified in the constructor, and 'ip' is the listener IP
159 * address in decimal 1.2.3.4 notation. 'port' is the actual listening
160 * port number. The 'host' and 'ip' strings must be deleted by the
161 * caller via delete[].
162 */
163 int get_listener_addr(char *&addr, char *&ip, int &port) const;
164
165 protected:
166 /* get my extension data */
167 vm_httpsrv_ext *get_ext() const
168 { return (vm_httpsrv_ext *)ext_; }
169
170 /* load or reload image data */
171 void load_image_data(VMG_ const char *ptr, size_t siz);
172
173 /* create a with no initial contents */
174 CVmObjHTTPServer() { ext_ = 0; }
175
176 /* create with contents ('flag' is just for overloading resolution) */
177 CVmObjHTTPServer(VMG_ int flag);
178
179 /* property evaluator - undefined function */
180 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
181
182 /* shut down the server */
183 int getp_shutdown(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
184
185 /* get the port number */
186 int getp_get_port(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
187
188 /* get the original listening address */
189 int getp_get_addr(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
190
191 /* get the listening address in IP format */
192 int getp_get_ip(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc);
193
194 /* property evaluation function table */
195 static int (CVmObjHTTPServer::*func_table_[])(VMG_ vm_obj_id_t self,
196 vm_val_t *retval, uint *argc);
197 };
198
199
200 /* ------------------------------------------------------------------------ */
201 /*
202 * CVmObjHTTPServer metaclass registration table object
203 */
204 class CVmMetaclassHTTPServer: public CVmMetaclass
205 {
206 public:
207 /* get the global name */
208 const char *get_meta_name() const { return "http-server/030000"; }
209
210 /* create from image file */
211 void create_for_image_load(VMG_ vm_obj_id_t id)
212 {
213 new (vmg_ id) CVmObjHTTPServer();
214 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
215 G_obj_table->set_obj_transient(id);
216 }
217
218 /* create from restoring from saved state */
219 void create_for_restore(VMG_ vm_obj_id_t id)
220 {
221 new (vmg_ id) CVmObjHTTPServer();
222 G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE);
223 G_obj_table->set_obj_transient(id);
224 }
225
226 /* create dynamically using stack arguments */
227 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
228 { return CVmObjHTTPServer::create_from_stack(vmg_ pc_ptr, argc); }
229
230 /* call a static property */
231 int call_stat_prop(VMG_ vm_val_t *result,
232 const uchar **pc_ptr, uint *argc,
233 vm_prop_id_t prop)
234 {
235 return CVmObjHTTPServer::
236 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
237 }
238 };
239
240 #endif /* VMHTTPSRV_H */
241
242 /*
243 * Register the class
244 */
245 VM_REGISTER_METACLASS(CVmObjHTTPServer)