00001
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <errno.h>
00020 #include <ctype.h>
00021 #include "gridlabd.h"
00022 #include "object.h"
00023 #include "aggregate.h"
00024 #include "histogram.h"
00025
00026 #define _TAPE_C
00027
00028 #include "tape.h"
00029 #include "file.h"
00030 #include "odbc.h"
00031
00032 #define MAP_DOUBLE(X,LO,HI) {#X,VT_DOUBLE,&X,LO,HI}
00033 #define MAP_INTEGER(X,LO,HI) {#X,VT_INTEGER,&X,LO,HI}
00034 #define MAP_STRING(X) {#X,VT_STRING,X,sizeof(X),0}
00035 #define MAP_END {NULL}
00036
00037 VARMAP varmap[] = {
00038
00039 MAP_STRING(timestamp_format),
00040 MAP_END
00041 };
00042
00043 extern CLASS *player_class;
00044 extern CLASS *shaper_class;
00045 extern CLASS *recorder_class;
00046 extern CLASS *multi_recorder_class;
00047 extern CLASS *collector_class;
00048
00049
00050 #ifdef WIN32
00051 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
00052 #define _WIN32_WINNT 0x0400
00053 #include <windows.h>
00054 #define LIBPREFIX
00055 #ifndef DLEXT
00056 #define DLEXT ".dll"
00057 #endif
00058 #define DLLOAD(P) LoadLibrary(P)
00059 #define DLSYM(H,S) GetProcAddress((HINSTANCE)H,S)
00060 #define snprintf _snprintf
00061 #else
00062 #include "dlfcn.h"
00063 #define LIBPREFIX "lib"
00064 #ifndef DLEXT
00065 #define DLEXT ".so"
00066 #else
00067 #endif
00068 #define DLLOAD(P) dlopen(P,RTLD_LAZY)
00069 #define DLSYM(H,S) dlsym(H,S)
00070 #endif
00071
00072 static TAPEFUNCS *funcs = NULL;
00073 static char1024 tape_gnuplot_path;
00074
00075 typedef int (*OPENFUNC)(void *, char *, char *);
00076 typedef char *(*READFUNC)(void *, char *, unsigned int);
00077 typedef int (*WRITEFUNC)(void *, char *, char *);
00078 typedef int (*REWINDFUNC)(void *);
00079 typedef void (*CLOSEFUNC)(void *);
00080
00081 TAPEFUNCS *get_ftable(char *mode){
00082
00083 char256 modname;
00084 TAPEFUNCS *fptr = funcs;
00085 TAPEOPS *ops = NULL;
00086 void *lib = NULL;
00087 CALLBACKS **c = NULL;
00088 char *tpath = NULL;
00089 while(fptr != NULL){
00090 if(strcmp(fptr->mode, mode) == 0)
00091 return fptr;
00092 fptr = fptr->next;
00093 }
00094
00095 fptr = malloc(sizeof(TAPEFUNCS));
00096 if(fptr == NULL)
00097 {
00098 GL_THROW("get_ftable(char *mode='%s'): out of memory", mode);
00099 return NULL;
00100 }
00101 snprintf(modname, 1024, LIBPREFIX "tape_%s" DLEXT, mode);
00102 tpath = gl_findfile(modname, NULL, 0|4);
00103 if(tpath == NULL){
00104 GL_THROW("unable to locate %s", modname);
00105 return NULL;
00106 }
00107 lib = fptr->hLib = DLLOAD(tpath);
00108 if(fptr->hLib == NULL){
00109 GL_THROW("tape module: unable to load DLL for %s", modname);
00110 return NULL;
00111 }
00112 c = (CALLBACKS **)DLSYM(lib, "callback");
00113 if(c)
00114 *c = callback;
00115
00116 ops = fptr->collector = malloc(sizeof(TAPEOPS));
00117 ops->open = (OPENFUNC)DLSYM(lib, "open_collector");
00118 ops->read = NULL;
00119 ops->write = (WRITEFUNC)DLSYM(lib, "write_collector");
00120 ops->rewind = NULL;
00121 ops->close = (CLOSEFUNC)DLSYM(lib, "close_collector");
00122 ops = fptr->player = malloc(sizeof(TAPEOPS));
00123 ops->open = (OPENFUNC)DLSYM(lib, "open_player");
00124 ops->read = (READFUNC)DLSYM(lib, "read_player");
00125 ops->write = NULL;
00126 ops->rewind = (REWINDFUNC)DLSYM(lib, "rewind_player");
00127 ops->close = (CLOSEFUNC)DLSYM(lib, "close_player");
00128 ops = fptr->recorder = malloc(sizeof(TAPEOPS));
00129 ops->open = (OPENFUNC)DLSYM(lib, "open_recorder");
00130 ops->read = NULL;
00131 ops->write = (WRITEFUNC)DLSYM(lib, "write_recorder");
00132 ops->rewind = NULL;
00133 ops->close = (CLOSEFUNC)DLSYM(lib, "close_recorder");
00134 ops = fptr->histogram = malloc(sizeof(TAPEOPS));
00135 ops->open = (OPENFUNC)DLSYM(lib, "open_histogram");
00136 ops->read = NULL;
00137 ops->write = (WRITEFUNC)DLSYM(lib, "write_histogram");
00138 ops->rewind = NULL;
00139 ops->close = (CLOSEFUNC)DLSYM(lib, "close_histogram");
00140 ops = fptr->shaper = malloc(sizeof(TAPEOPS));
00141 ops->open = (OPENFUNC)DLSYM(lib, "open_shaper");
00142 ops->read = (READFUNC)DLSYM(lib, "read_shaper");
00143 ops->write = NULL;
00144 ops->rewind = (REWINDFUNC)DLSYM(lib, "rewind_shaper");
00145 ops->close = (CLOSEFUNC)DLSYM(lib, "close_shaper");
00146 fptr->next = funcs;
00147 funcs = fptr;
00148 return funcs;
00149 }
00150
00151 EXPORT CLASS *init(CALLBACKS *fntable, void *module, int argc, char *argv[])
00152 {
00153 struct recorder my;
00154 struct collector my2;
00155
00156 if (set_callback(fntable)==NULL)
00157 {
00158 errno = EINVAL;
00159 return NULL;
00160 }
00161
00162
00163 sprintf(tape_gnuplot_path, "c:/Program Files/GnuPlot/bin/wgnuplot.exe");
00164 gl_global_create("tape::gnuplot_path",PT_char1024,&tape_gnuplot_path,NULL);
00165
00166
00167 player_class = gl_register_class(module,"player",sizeof(struct player),PC_PRETOPDOWN);
00168 PUBLISH_STRUCT(player,char32,property);
00169 PUBLISH_STRUCT(player,char1024,file);
00170 PUBLISH_STRUCT(player,char8,filetype);
00171 PUBLISH_STRUCT(player,int32,loop);
00172
00173
00174 shaper_class = gl_register_class(module,"shaper",sizeof(struct shaper),PC_PRETOPDOWN);
00175 PUBLISH_STRUCT(shaper,char1024,file);
00176 PUBLISH_STRUCT(shaper,char8,filetype);
00177 PUBLISH_STRUCT(shaper,char256,group);
00178 PUBLISH_STRUCT(shaper,char32,property);
00179 PUBLISH_STRUCT(shaper,double,magnitude);
00180 PUBLISH_STRUCT(shaper,double,events);
00181
00182
00183 recorder_class = gl_register_class(module,"recorder",sizeof(struct recorder),PC_POSTTOPDOWN);
00184 PUBLISH_STRUCT(recorder,char1024,property);
00185 PUBLISH_STRUCT(recorder,char32,trigger);
00186 PUBLISH_STRUCT(recorder,char1024,file);
00187 PUBLISH_STRUCT(recorder,char1024,multifile);
00188
00189 PUBLISH_STRUCT(recorder,int32,limit);
00190 PUBLISH_STRUCT(recorder,char1024,plotcommands);
00191 PUBLISH_STRUCT(recorder,char32,xdata);
00192 PUBLISH_STRUCT(recorder,char32,columns);
00193
00194 if(gl_publish_variable(recorder_class,
00195 PT_double, "interval[s]", ((char*)&(my.dInterval) - (char *)&my),
00196 PT_enumeration, "output", ((char*)&(my.output) - (char *)&my),
00197 PT_KEYWORD, "SCREEN", SCREEN,
00198 PT_KEYWORD, "EPS", EPS,
00199 PT_KEYWORD, "GIF", GIF,
00200 PT_KEYWORD, "JPG", JPG,
00201 PT_KEYWORD, "PDF", PDF,
00202 PT_KEYWORD, "PNG", PNG,
00203 PT_KEYWORD, "SVG", SVG,
00204 NULL) < 1)
00205 GL_THROW("Could not publish property output for recorder");
00206
00207
00208 multi_recorder_class = gl_register_class(module,"multi_recorder",sizeof(struct recorder),PC_POSTTOPDOWN);
00209 if(gl_publish_variable(multi_recorder_class,
00210 PT_double, "interval[s]", ((char*)&(my.dInterval) - (char *)&my),
00211 PT_char1024, "property", ((char*)&(my.property) - (char *)&my),
00212 PT_char32, "trigger", ((char*)&(my.trigger) - (char *)&my),
00213 PT_char1024, "file", ((char*)&(my.file) - (char *)&my),
00214 PT_char1024, "multifile", ((char*)&(my.multifile) - (char *)&my),
00215 PT_int32, "limit", ((char*)&(my.limit) - (char *)&my),
00216 PT_char1024, "plotcommands", ((char*)&(my.plotcommands) - (char *)&my),
00217 PT_char32, "xdata", ((char*)&(my.xdata) - (char *)&my),
00218 PT_char32, "columns", ((char*)&(my.columns) - (char *)&my),
00219 PT_enumeration, "output", ((char*)&(my.output) - (char *)&my),
00220 PT_KEYWORD, "SCREEN", SCREEN,
00221 PT_KEYWORD, "EPS", EPS,
00222 PT_KEYWORD, "GIF", GIF,
00223 PT_KEYWORD, "JPG", JPG,
00224 PT_KEYWORD, "PDF", PDF,
00225 PT_KEYWORD, "PNG", PNG,
00226 PT_KEYWORD, "SVG", SVG,
00227 NULL) < 1)
00228 GL_THROW("Could not publish property output for multi_recorder");
00229
00230
00231 collector_class = gl_register_class(module,"collector",sizeof(struct collector),PC_POSTTOPDOWN);
00232 PUBLISH_STRUCT(collector,char1024,property);
00233 PUBLISH_STRUCT(collector,char32,trigger);
00234 PUBLISH_STRUCT(collector,char1024,file);
00235
00236 PUBLISH_STRUCT(collector,int32,limit);
00237 PUBLISH_STRUCT(collector,char256,group);
00238 if(gl_publish_variable(collector_class,
00239 PT_double, "interval[s]", ((char*)&(my2.dInterval) - (char *)&my2),
00240 NULL) < 1)
00241 GL_THROW("Could not publish property output for collector");
00242
00243
00244 new_histogram(module);
00245
00246 #if 0
00247 new_loadshape(module);
00248 #endif // zero
00249
00250
00251
00252
00253 return player_class;
00254 }
00255
00256 EXPORT int check(void)
00257 {
00258 unsigned int errcount=0;
00259
00260
00261 { OBJECT *obj=NULL;
00262 FINDLIST *players = gl_find_objects(FL_NEW,FT_CLASS,SAME,"tape",FT_END);
00263 while ((obj=gl_find_next(players,obj))!=NULL)
00264 {
00265 struct player *pData = OBJECTDATA(obj,struct player);
00266 if (gl_findfile(pData->file,NULL,FF_EXIST)==NULL)
00267 {
00268 errcount++;
00269 gl_error("player %s (id=%d) uses the file '%s', which cannot be found", obj->name?obj->name:"(unnamed)", obj->id, pData->file);
00270 }
00271 }
00272 }
00273
00274
00275 { OBJECT *obj=NULL;
00276 FINDLIST *shapers = gl_find_objects(FL_NEW,FT_CLASS,SAME,"shaper",FT_END);
00277 while ((obj=gl_find_next(shapers,obj))!=NULL)
00278 {
00279 struct shaper *pData = OBJECTDATA(obj,struct shaper);
00280 if (gl_findfile(pData->file,NULL,FF_EXIST)==NULL)
00281 {
00282 errcount++;
00283 gl_error("shaper %s (id=%d) uses the file '%s', which cannot be found", obj->name?obj->name:"(unnamed)", obj->id, pData->file);
00284 }
00285 }
00286 }
00287
00288 return errcount;
00289 }
00290
00291 int do_kill()
00292 {
00293
00294 return 0;
00295 }
00296