0001 #!/usr/bin/pscript
0002
0003 //
0004 // TODO: Change the linker options for OSX, etc... (RT Lib).
0005 //
0006 userlog("Generate go server");
0007
0008 //Include wizard base.
0009 compilestring(getwizardbase())();
0010
0011 //Provision class
0012 class CClient extends WizardBase {
0013
0014 libs = "-latmisrvinteg -latmi -lubf -lnstd -lpthread -lrt -ldl -lm";
0015
0016 constructor()
0017 {
0018 base.constructor();
0019
0020 if ("DARWIN"==getosname())
0021 {
0022 libs = "-latmisrvinteg -latmi -lubf -lnstd -lpthread -ldl -lm";
0023 }
0024 }
0025
0026
0027 //Configure it:
0028 </ order=0, name = "XATMI Server Name (binary)", type = "string", min=1, max=512 />
0029 srvname = "testsv";
0030
0031 </ order=1, name = "Service name", type = "string", min=1, max=30/>
0032 svcnm = "TESTSV";
0033
0034 </ order=2, name = "Use UBF?", type = "yn"/>
0035 useubf = "y";
0036
0037 </ order=3, name = "UBF include folder name (will be done ../<name>)",
0038 type = "path" depend="(::prov.useubf==\"y\")" />
0039 ubfname = "ubftab";
0040
0041 </ order=4, name = "Gen makefile", type = "yn"/>
0042 genmake = "y";
0043
0044 </ order=5, name = "INI File section (optional, will read config if set)",
0045 type = "string", depend="(::prov.useubf==\"y\")", min=0/>
0046 config = "";
0047
0048 goClientFile = "";
0049
0050 makeFile = "";
0051
0052 function getOutputFiles()
0053 {
0054 goClientFile=appHome+"/"+srvname+".c";
0055 makeFile=appHome+"/Makefile";
0056 }
0057
0058 ////////////////////////////////////////////////////////////////////////
0059 //Build C Server code
0060 ////////////////////////////////////////////////////////////////////////
0061 cClientVal = "";
0062 function buildServer()
0063 {
0064
0065 cClientVal = @"#include <string.h>
0066 #include <stdio.h>
0067 #include <stdlib.h>
0068 #include <memory.h>
0069 #include <math.h>
0070
0071 #include <ndebug.h>
0072 #include <atmi.h>
0073
0074 "+(useubf=="n"?"":@"
0075 #include <ubf.h>
0076 #include <Exfields.h>
0077 #include <test.fd.h>")+@"
0078 /*---------------------------Externs------------------------------------*/
0079 /*---------------------------Macros-------------------------------------*/
0080
0081 #ifndef SUCCEED
0082 #define SUCCEED 0
0083 #endif
0084
0085 #ifndef FAIL
0086 #define FAIL -1
0087 #endif
0088
0089 "+(config==""?"":@"
0090 #define PROGSECTION """+config+@""" /* configuration section */
0091 #define CONFIG_SERVER ""@CCONF""
0092 #define KEY_VAL_BUFFSZ 1024
0093 ")+@"
0094
0095 /*---------------------------Enums--------------------------------------*/
0096 /*---------------------------Typedefs-----------------------------------*/
0097 /*---------------------------Globals------------------------------------*/
0098 /*---------------------------Statics------------------------------------*/
0099 /*---------------------------Prototypes---------------------------------*/
0100
0101
0102 /**
0103 * Service entry
0104 * @return SUCCEED/FAIL
0105 */
0106 void "+svcnm+@" (TPSVCINFO *p_svc)
0107 {
0108 int ret = SUCCEED;
0109
0110 "+(useubf=="y"?@"
0111 UBFH *p_ub = (UBFH *)p_svc->data;
0112
0113 tplogprintubf(log_info, ""Got request"", p_ub);
0114
0115
0116 /* allocate some stuff for more data to put in */
0117 if (NULL==(p_ub = (UBFH *)tprealloc((char *)p_ub, 4096)))
0118 {
0119 ret=FAIL;
0120 goto out;
0121 }
0122
0123 if (FAIL==Badd(p_ub, T_STRING_2_FLD, ""Hello World from XATMI server"", 0L))
0124 {
0125 ret=FAIL;
0126 goto out;
0127 }
0128
0129 out:
0130
0131 tpreturn( ret==SUCCEED?TPSUCCESS:TPFAIL,
0132 0L,
0133 (char *)p_ub,
0134 0L,
0135 0L);
0136 ":@"
0137 tpreturn( ret==SUCCEED?TPSUCCESS:TPFAIL,
0138 0L,
0139 (char *)p_svc->data,
0140 0L,
0141 0L);
0142 ")+@"
0143
0144 }
0145
0146 /**
0147 * Initialize the application
0148 * @param argc argument count
0149 * @param argv argument values
0150 * @return SUCCEED/FAIL
0151 */
0152 int init(int argc, char** argv)
0153 {
0154 int ret = SUCCEED;
0155 "+(config==""?"":@"
0156 UBFH *p_ub = NULL;
0157 char config_tag[128];
0158 long rsplen;
0159 BFLDLEN sz;
0160 int occ;
0161 int i;
0162 char key[KEY_VAL_BUFFSZ]={0};
0163 char val[KEY_VAL_BUFFSZ]={0};
0164 char *cctag;
0165 ")+@"
0166 TP_LOG(log_info, ""Initialising..."");
0167
0168 if (SUCCEED!=tpinit(NULL))
0169 {
0170 TP_LOG(log_error, ""Failed to Initialise: %s"",
0171 tpstrerror(tperrno));
0172 ret = FAIL;
0173 goto out;
0174 }
0175
0176 "+(config==""?"":@"
0177 /* Download configuration */
0178
0179 if (NULL==(p_ub = (UBFH *)tpalloc(""UBF"", NULL, 1024)))
0180 {
0181 TP_LOG(log_error, ""Failed to alloc:%s"", tpstrerror(tperrno));
0182 ret=FAIL;
0183 goto out;
0184 }
0185
0186 cctag = getenv(""NDRX_CCTAG"");
0187 if (NULL!=cctag)
0188 {
0189 snprintf(config_tag, sizeof(config_tag), ""%s/%s"",
0190 PROGSECTION, cctag);
0191 }
0192 else
0193 {
0194 /* NO subsection configured */
0195 snprintf(config_tag, sizeof(config_tag), ""%s"", PROGSECTION);
0196 }
0197
0198 if ( (SUCCEED!=Bchg(p_ub, EX_CC_CMD, 0, ""g"", 0L))
0199 || (SUCCEED!=Bchg(p_ub, EX_CC_LOOKUPSECTION, 0, config_tag, 0L)))
0200 {
0201 TP_LOG(log_error, ""Failed to set EX_CC_CMD/EX_CC_LOOKUPSECTION: %s"",
0202 Bstrerror(Berror));
0203 ret = FAIL;
0204 goto out;
0205 }
0206
0207 if (FAIL==tpcall(CONFIG_SERVER, (char *)p_ub, 0L, (char **)&p_ub, &rsplen, TPNOTIME))
0208 {
0209 TP_LOG(log_error, ""Failed to call %s: %s"",
0210 CONFIG_SERVER,tpstrerror(tperrno));
0211 ret=FAIL;
0212 goto out;
0213 }
0214
0215
0216 tplogprintubf(log_info, ""Got configuration"", p_ub);
0217
0218 occ = Boccur(p_ub, EX_CC_KEY);
0219
0220 for (i=0; i<occ; i++)
0221 {
0222 sz = sizeof(key);
0223 if (SUCCEED!=CBget(p_ub, EX_CC_KEY, i, key, &sz, BFLD_STRING))
0224 {
0225 TP_LOG(log_error, ""Failed to get EX_CC_KEY[%d]: %s"", i,
0226 Bstrerror(Berror));
0227 ret=FAIL;
0228 goto out;
0229 }
0230
0231 sz = sizeof(val);
0232 if (SUCCEED!=CBget(p_ub, EX_CC_VALUE, i, val, &sz, BFLD_STRING))
0233 {
0234 TP_LOG(log_error, ""Failed to get EX_CC_VALUE[%d]: %s"", i,
0235 Bstrerror(Berror));
0236 ret=FAIL;
0237 goto out;
0238 }
0239
0240 TP_LOG(log_debug, ""Got key: [%s] = [%s]"",
0241 key, val);
0242
0243 if (0==strcmp(key, ""someparam1""))
0244 {
0245 TP_LOG(log_debug, ""Got param1: [%s]"", val);
0246 }
0247 else if (0==strcmp(key, ""someparam2""))
0248 {
0249 TP_LOG(log_debug, ""Got param2: [%s]"", val);
0250 }
0251 else
0252 {
0253 TP_LOG(log_debug, ""Unknown setting [%s] - ignoring..."",
0254 key
0255 );
0256 }
0257 }
0258 ")+@"
0259
0260 /* Advertise our service */
0261 if (SUCCEED!=tpadvertise("""+svcnm+@""", "+svcnm+@"))
0262 {
0263 TP_LOG(log_error, ""Failed to initialise "+svcnm+@"!"");
0264 ret=FAIL;
0265 goto out;
0266 }
0267
0268 out:
0269 "+(config==""?"":@"
0270 if (NULL!=p_ub)
0271 {
0272 tpfree((char *)p_ub);
0273 }")+@"
0274
0275 return ret;
0276 }
0277
0278 /**
0279 * Terminate the application
0280 */
0281 void uninit(void)
0282 {
0283 TP_LOG(log_info, ""Uninitialising..."");
0284 }
0285
0286 /**
0287 * Server program main entry
0288 * @param argc argument count
0289 * @param argv argument values
0290 * @return SUCCEED/FAIL
0291 */
0292 int main(int argc, char** argv)
0293 {
0294 /* Launch the Enduro/x thread */
0295 return ndrx_main_integra(argc, argv, init, uninit, 0);
0296 }
0297
0298 ";
0299 }
0300
0301 ////////////////////////////////////////////////////////////////////////
0302 //Server END
0303 ////////////////////////////////////////////////////////////////////////
0304
0305
0306 ////////////////////////////////////////////////////////////////////////
0307 //Build makefile
0308 ////////////////////////////////////////////////////////////////////////
0309
0310 makeFileVal = "";
0311 function buildMakefile()
0312 {
0313
0314 makeFileVal = @"
0315 SOURCES = "+srvname+@".c
0316 TARGET = "+srvname+@"
0317 OBJECTS = $(SOURCES:%.c=%.o)
0318 CFLAGS = -I../"+ubfname+@"
0319 LIBS = "+libs+@"
0320
0321 default: target
0322
0323 target: $(TARGET)
0324
0325 $(TARGET): $(OBJECTS)
0326 $(CC) -o $@ $(LDFLAGS) $^ $(LIBS)
0327
0328 %.o: %.c
0329 $(CC) -c -o $@ $< $(CFLAGS)
0330
0331 clean:
0332 -rm *.o
0333 -rm $(TARGET)
0334
0335 ";
0336 }
0337
0338 ////////////////////////////////////////////////////////////////////////
0339 //Build makefile, END
0340 ////////////////////////////////////////////////////////////////////////
0341
0342 }
0343
0344
0345 //Run the mater installer
0346 function install()
0347 {
0348 local root = getroottable();
0349
0350 //Create a provision object
0351 root["prov"] <- CClient();
0352
0353 if (!::prov.isDefaulted)
0354 {
0355 ::prov.runInteractive();
0356 }
0357
0358 if (::prov.validatAndPrintConfig())
0359 {
0360 ::prov.getOutputFiles();
0361 ::prov.buildServer();
0362
0363 if (!::prov.writeFile(::prov.goClientFile, ::prov.cClientVal))
0364 {
0365 return false;
0366 }
0367
0368 //Build makefile if needed...
0369 if ("y"==::prov.genmake)
0370 {
0371 ::prov.buildMakefile();
0372
0373 if (!::prov.writeFile(::prov.makeFile, ::prov.makeFileVal))
0374 {
0375 return false;
0376 }
0377 }
0378 }
0379 else
0380 {
0381 return false;
0382 }
0383
0384 return true;
0385 }
0386
0387 if (::install())
0388 {
0389 print("C server gen ok!\n");
0390
0391 return 0;
0392 }
0393 else
0394 {
0395 print("C server gen failed!\n");
0396 return -1;
0397 }