/* Create a simple stand-alone web server */ int main(int argc, char **argv, char **envp) { Mpr *mpr; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprError("Cannot create runtime"); return -1; } if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) == 0) { mprError("Cannot create the HTTP services"); return -1; } mprStart(); if (maParseConfig("appweb.conf") < 0) { mprError("Cannot parse the config file %s", "appweb.conf"); return -1; } httpDefineAction("/action/myaction", myaction); if (httpStartEndpoints() < 0) { mprError("Cannot start the web server"); return -1; } mprServiceEvents(-1, 0); mprDestroy(); return 0; }
/* Create a simple stand-alone web server */ int main(int argc, char **argv, char **envp) { Mpr *mpr; MaAppweb *appweb; MaServer *server; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0) { mprError("Cannot create the web server runtime"); return -1; } mprStart(); appweb = maCreateAppweb(mpr); mprAddRoot(appweb); server = maCreateServer(appweb, 0); if (maParseConfig(server, "appweb.conf", 0) < 0) { mprError("Cannot parse the config file %s", "appweb.conf"); return -1; } httpDefineAction("/action/myaction", myaction); if (maStartServer(server) < 0) { mprError("Cannot start the web server"); return -1; } mprServiceEvents(-1, 0); maStopServer(server); mprRemoveRoot(appweb); mprDestroy(MPR_EXIT_DEFAULT); return 0; }
static int join(Ejs *ejs, EjsVar *workers, int timeout) { MprTime mark, remaining; int result, count, total; mark = mprGetTime(ejs); ejs->joining = 1; do { /* * Must process all pending messages */ total = 0; while ((count = mprServiceEvents(ejs->dispatcher, 0, MPR_SERVICE_EVENTS)) > 0) { total += count; } ejs->joining = !reapJoins(ejs, workers); if (total == 0 && ejs->joining) { mprWaitForCond(ejs->dispatcher->cond, timeout); } remaining = mprGetRemainingTime(ejs, mark, timeout); } while (ejs->joining && remaining > 0 && !ejs->exception); if (ejs->exception) { return 0; } result = (ejs->joining) ? MPR_ERR_TIMEOUT: 0; ejs->joining = 0; return result; }
MAIN(httpMain, int argc, char *argv[]) { MprTime start; double elapsed; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if (!parseArgs(argc, argv)) { showUsage(); return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); #if BLD_FEATURE_SSL if (!mprLoadSsl(1)) { mprError("Can't load SSL"); exit(1); } #endif if (mprStart() < 0) { mprError("Can't start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); app->http = httpCreate(); httpEaseLimits(app->http->clientLimits); processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprError("Request failed"); } return (app->success) ? 0 : 255; }
MAIN(benchMpr, int argc, char **argv, char **envp) { MprThread *thread; Mpr *mpr; char *argp; int err, nextArg; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); app->mutex = mprCreateLock(mpr); app->complete = mprCreateCond(); app->iterations = 5; err = 0; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--iterations") == 0 || strcmp(argp, "-i") == 0) { if (nextArg >= argc) { err++; } else { app->iterations = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--alloc") == 0 || strcmp(argp, "-a") == 0) { app->testAllocOnly++; } else { err++; } } if (err) { mprEprintf("usage: bench [-a] [-i iterations]\n"); mprEprintf("usage: %s [options]\n" " -a # Alloc test only\n" " --iterations count # Number of iterations to run the test\n", mprGetAppName(mpr)); exit(2); } mprStart(mpr); thread = mprCreateThread("bench", (MprThreadProc) doBenchmark, (void*) MPR, 0); mprStartThread(thread); while (!testComplete) { mprServiceEvents(250, 0); } mprPrintMem("Memory Report", 0); mprDestroy(); return 0; }
/* * Thread main for serviceEvents */ static void serviceEvents(void *data, MprThread *tp) { Mpr *mpr; mpr = mprGetMpr(tp); mpr->serviceThread = tp->osThread; mprServiceEvents(mpr->dispatcher, -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); mpr->serviceThread = 0; mpr->hasDedicatedService = 1; }
/* * Service requests for a web server. */ int maServiceWebServer(MaHttp *http) { if (maStartHttp(http) < 0) { mprError(http, "Can't start the web server"); return MPR_ERR_CANT_CREATE; } mprServiceEvents(mprGetDispatcher(http), -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); maStopHttp(http); return 0; }
MAIN(httpMain, int argc, char **argv, char **envp) { MprTime start; double elapsed; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if (parseArgs(argc, argv) < 0) { return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); if (mprStart() < 0) { mprError("Cannot start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); app->http = httpCreate(HTTP_CLIENT_SIDE); httpEaseLimits(app->http->clientLimits); #if BIT_STATIC && BIT_PACK_SSL extern MprModuleEntry mprSslInit; mprNop(mprSslInit); #endif processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprError("Request failed"); } mprDestroy(MPR_EXIT_DEFAULT); return (app->success) ? 0 : 255; }
MAIN(httpMain, int argc, char **argv, char **envp) { MprTime start; double elapsed; int success; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD) == 0) { return MPR_ERR_MEMORY; } if ((app = mprAllocObj(App, manageApp)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(app); mprAddStandardSignals(); initSettings(); if ((app->http = httpCreate(HTTP_CLIENT_SIDE)) == 0) { return MPR_ERR_MEMORY; } if (parseArgs(argc, argv) < 0) { return MPR_ERR_BAD_ARGS; } mprSetMaxWorkers(app->workers); if (mprStart() < 0) { mprLog("error http", 0, "Cannot start MPR for %s", mprGetAppTitle()); exit(2); } start = mprGetTime(); processing(); mprServiceEvents(-1, 0); if (app->benchmark) { elapsed = (double) (mprGetTime() - start); if (app->fetchCount == 0) { elapsed = 0; app->fetchCount = 1; } mprPrintf("\nRequest Count: %13d\n", app->fetchCount); mprPrintf("Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf("Time per request: %13.4f sec\n", elapsed / 1000.0 / app->fetchCount); mprPrintf("Requests per second: %13.4f\n", app->fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf("Load threads: %13d\n", app->loadThreads); mprPrintf("Worker threads: %13d\n", app->workers); } if (!app->success && app->verbose) { mprLog("error http", 0, "Request failed"); } success = app->success; mprDestroy(); return success ? 0 : 255; }
int maRunSimpleWebServer(cchar *ipAddr, int port, cchar *docRoot) { Mpr *mpr; MaHttp *http; MaServer *server; /* * Initialize and start the portable runtime services. */ if ((mpr = mprCreate(0, NULL, NULL)) == 0) { mprError(mpr, "Can't create the web server runtime"); return MPR_ERR_CANT_CREATE; } if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start the web server runtime"); return MPR_ERR_CANT_INITIALIZE; } /* * Create the HTTP object. */ if ((http = maCreateHttp(mpr)) == 0) { mprError(mpr, "Can't create the web server http services"); return MPR_ERR_CANT_INITIALIZE; } /* * Create and start the HTTP server. Give the server a name of "default" and define "." as * the default serverRoot, ie. the directory with the server configuration files. */ server = maCreateServer(http, ipAddr, ".", ipAddr, port); if (server == 0) { mprError(mpr, "Can't create the web server"); return MPR_ERR_CANT_CREATE; } maSetDocumentRoot(server->defaultHost, docRoot); if (maStartHttp(http) < 0) { mprError(mpr, "Can't start the web server"); return MPR_ERR_CANT_CREATE; } mprServiceEvents(mprGetDispatcher(mpr), -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); maStopHttp(http); mprFree(mpr); return 0; }
/* * function waitForMessage(timeout: Number = -1): Boolean */ static EjsVar *workerWaitForMessage(Ejs *ejs, EjsWorker *worker, int argc, EjsVar **argv) { MprTime mark, remaining; int timeout; timeout = (argc > 0) ? ejsGetInt(argv[0]): MAXINT; if (timeout < 0) { timeout = MAXINT; } mark = mprGetTime(ejs); do { if (mprServiceEvents(ejs->dispatcher, timeout, MPR_SERVICE_EVENTS | MPR_SERVICE_ONE_THING) > 0) { return (EjsVar*) ejs->trueValue; } remaining = mprGetRemainingTime(ejs, mark, timeout); } while (remaining > 0 && !mprIsExiting(ejs) && !ejs->exiting); return (EjsVar*) ejs->falseValue; }
/* Run a web server not based on a config file. */ int maRunSimpleWebServer(cchar *ip, int port, cchar *home, cchar *documents) { Mpr *mpr; MaServer *server; MaAppweb *appweb; int rc; /* Initialize and start the portable runtime services. */ rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprError("Can't create the web server runtime"); } else { if (mprStart(mpr) < 0) { mprError("Can't start the web server runtime"); } else { if ((appweb = maCreateAppweb(mpr)) == 0) { mprError("Can't create the web server http services"); } else { mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprError("Can't create the web server"); } else { if (maConfigureServer(server, 0, home, documents, ip, port) < 0) { mprError("Can't create the web server"); } else { if (maStartServer(server) < 0) { mprError("Can't start the web server"); } else { mprServiceEvents(-1, 0); rc = 0; } maStopServer(server); } } mprRemoveRoot(appweb); } } mprDestroy(MPR_EXIT_DEFAULT); } return rc; }
static int runServer(cchar *configFile, cchar *ip, int port, cchar *home, cchar *documents) { MaAppweb *appweb; MaServer *server; if (mprStart() < 0) { mprLog("error appweb", 0, "Cannot start the web server runtime"); return MPR_ERR_CANT_CREATE; } if ((appweb = maCreateAppweb()) == 0) { mprLog("error appweb", 0, "Cannot create appweb object"); return MPR_ERR_CANT_CREATE; } mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprLog("error appweb", 0, "Cannot create the web server"); mprRemoveRoot(appweb); return MPR_ERR_CANT_CREATE; } if (home) { if (maConfigureServer(server, 0, home, documents, ip, port, 0) < 0) { mprLog("error appweb", 0, "Cannot create the web server"); mprRemoveRoot(appweb); return MPR_ERR_BAD_STATE; } } else { if (maParseConfig(server, configFile, 0) < 0) { mprLog("error appweb", 0, "Cannot parse the config file %s", configFile); mprRemoveRoot(appweb); return MPR_ERR_CANT_READ; } } if (maStartServer(server) < 0) { mprLog("error appweb", 0, "Cannot start the web server"); mprRemoveRoot(appweb); return MPR_ERR_CANT_COMPLETE; } mprServiceEvents(-1, 0); maStopServer(server); mprRemoveRoot(appweb); return 0; }
/* Wait for a command to complete. Return 0 if the command completed, otherwise it will return MPR_ERR_TIMEOUT. */ PUBLIC int mprWaitForCmd(MprCmd *cmd, MprTicks timeout) { MprTicks expires, remaining, delay; int64 dispatcherMark; assert(cmd); if (timeout < 0) { timeout = MAXINT; } if (mprGetDebugMode()) { timeout = MAXINT; } if (cmd->stopped) { timeout = 0; } expires = mprGetTicks() + timeout; remaining = timeout; /* Add root to allow callers to use mprRunCmd without first managing the cmd */ mprAddRoot(cmd); dispatcherMark = mprGetEventMark(cmd->dispatcher); while (!cmd->complete && remaining > 0) { if (mprShouldAbortRequests()) { break; } delay = (cmd->eofCount >= cmd->requiredEof) ? 10 : remaining; if (!MPR->eventing) { mprServiceEvents(delay, MPR_SERVICE_NO_BLOCK); delay = 0; } mprWaitForEvent(cmd->dispatcher, delay, dispatcherMark); remaining = (expires - mprGetTicks()); dispatcherMark = mprGetEventMark(cmd->dispatcher); } mprRemoveRoot(cmd); if (cmd->pid) { return MPR_ERR_TIMEOUT; } return 0; }
EjsVar *ejsServiceEvents(Ejs *ejs, int count, int timeout, int flags) { MprTime mark, remaining; int rc; if (count < 0) { count = MAXINT; } if (timeout < 0) { timeout = MAXINT; } mark = mprGetTime(ejs); do { rc = mprServiceEvents(ejs->dispatcher, timeout, MPR_SERVICE_EVENTS | MPR_SERVICE_ONE_THING); if (rc > 0) { count -= rc; } remaining = mprGetRemainingTime(ejs, mark, timeout); } while (count > 0 && remaining > 0 && !mprIsExiting(ejs) && !ejs->exiting); return 0; }
/* Create a web server described by a config file. */ int maRunWebServer(cchar *configFile) { Mpr *mpr; MaAppweb *appweb; MaServer *server; int rc; rc = MPR_ERR_CANT_CREATE; if ((mpr = mprCreate(0, NULL, 0)) == 0) { mprError("Can't create the web server runtime"); } else { if (mprStart() < 0) { mprError("Can't start the web server runtime"); } else { if ((appweb = maCreateAppweb(mpr)) == 0) { mprError("Can't create appweb object"); } else { mprAddRoot(appweb); if ((server = maCreateServer(appweb, 0)) == 0) { mprError("Can't create the web server"); } else { if (maParseConfig(server, configFile, 0) < 0) { mprError("Can't parse the config file %s", configFile); } else { if (maStartServer(server) < 0) { mprError("Can't start the web server"); } else { mprServiceEvents(-1, 0); rc = 0; } maStopServer(server); } } mprRemoveRoot(appweb); } } } mprDestroy(MPR_EXIT_DEFAULT); return rc; }
static void eventLoop() { int timeout, till; /* * We will nap for 50 milliseconds to avoid busy waiting */ timeout = 50; while (!mprIsExiting()) { if (mprRunTimers() > 0) { till = 0; } else { till = mprGetIdleTime(); } /* * This will run tasks if maxThreads == 0 (single threaded). If * multithreaded, the thread pool will run tasks */ if (mprRunTasks() > 0) { /* Returns > 0 if more work to do */ till = 0; } /* * Do some work here */ /* * Now service any pending I/O */ mprServiceEvents(1, min(timeout, till)); } }
MAIN(benchMain, int argc, char *argv[]) { Mpr *mpr; char *argp; int err, i, nextArg; #if BLD_FEATURE_MULTITHREAD MprThread *tp; #endif mpr = mprCreate(argc, argv, 0); #if VXWORKS || WINCE /* * These platforms pass an arg string in via the argc value. Assumes 32-bit. */ mprMakeArgv(mpr, "http", (char*) argc, &argc, &argv); #endif err = 0; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--iterations") == 0 || strcmp(argp, "-i") == 0) { if (nextArg >= argc) { err++; } else { iterations = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--workers") == 0 || strcmp(argp, "-w") == 0) { if (nextArg >= argc) { err++; } else { i = atoi(argv[++nextArg]); if (i <= 0 || i > 100) { mprError(mpr, "%s: Bad number of worker threads (0-100)", mprGetAppName(mpr)); exit(2); } workers = i; } } else { err++; } } if (err) { mprPrintf(mpr, "usage: bench [-em] [-i iterations] [-t workers]\n"); mprRawLog(mpr, 0, "usage: %s [options]\n" " --iterations count # Number of iterations to run the test\n" " --workers count # Set maximum worker threads\n", mprGetAppName(mpr)); exit(2); } #if BLD_FEATURE_MULTITHREAD mutex = mprCreateLock(mpr); mprSetMaxWorkers(mpr, workers); #endif mprStart(mpr, 0); #if BLD_FEATURE_MULTITHREAD tp = mprCreateThread(mpr, "bench", (MprThreadProc) doBenchmark, (void*) mpr, MPR_NORMAL_PRIORITY, 0); mprStartThread(tp); while (!testComplete) { mprServiceEvents(mprGetDispatcher(mpr), 250, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); } #else doBenchmark(mpr, NULL); #endif mprPrintf(mpr, "\n\n"); // mprFree(mpr); return 0; }
MAIN(httpMain, int argc, char *argv[]) { MprTime start; double elapsed; /* * Explicit initialization of globals for re-entrancy on Vxworks */ activeLoadThreads = benchmark = continueOnErrors = fetchCount = iterations = isBinary = httpVersion = 0; success = loadThreads = nextArg = noout = nofollow = showHeaders = printable = workers = 0; retries = singleStep = timeout = verbose = 0; chunkSize = host = method = password = ranges = 0; username = 0; mpr = 0; headers = 0; formData = 0; mpr = mprCreate(argc, argv, NULL); initSettings(mpr); if (!parseArgs(mpr, argc, argv)) { showUsage(mpr); return MPR_ERR_BAD_ARGS; } #if BLD_FEATURE_MULTITHREAD mprSetMaxWorkers(mpr, workers); #endif #if BLD_FEATURE_SSL if (!mprLoadSsl(mpr, 1)) { mprError(mpr, "Can't load SSL"); exit(1); } #endif /* * Start the Timer, Socket and Worker services */ if (mprStart(mpr, 0) < 0) { mprError(mpr, "Can't start MPR for %s", mprGetAppTitle(mpr)); exit(2); } start = mprGetTime(mpr); processing(); /* * Wait for all the threads to complete (simple but effective). Keep servicing events as we wind down. */ while (activeLoadThreads > 0) { mprServiceEvents(mprGetDispatcher(mpr), 250, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); } if (benchmark) { elapsed = (double) (mprGetTime(mpr) - start); if (fetchCount == 0) { elapsed = 0; fetchCount = 1; } mprPrintf(mpr, "\nRequest Count: %13d\n", fetchCount); mprPrintf(mpr, "Time elapsed: %13.4f sec\n", elapsed / 1000.0); mprPrintf(mpr, "Time per request: %13.4f sec\n", elapsed / 1000.0 / fetchCount); mprPrintf(mpr, "Requests per second: %13.4f\n", fetchCount * 1.0 / (elapsed / 1000.0)); mprPrintf(mpr, "Load threads: %13d\n", loadThreads); mprPrintf(mpr, "Worker threads: %13d\n", workers); } if (!success && verbose) { mprError(mpr, "Request failed"); } return (success) ? 0 : 255; }
MAIN(http, int argc, char **argv, char **envp) { Mpr *mpr; cchar *argp; char *logSpec, *traceSpec; int argind; logSpec = 0; traceSpec = 0; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } if ((app = mprAllocObj(App, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) < 0) { exit(2); } app->mpr = mpr; app->home = mprGetCurrentPath(); argc = mpr->argc; argv = (char**) mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--show") || smatch(argp, "-s")) { app->show = 1; } else if (smatch(argp, "--trace") || smatch(argp, "-t")) { if (argind >= argc) { usageError(); } traceSpec = argv[++argind]; } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { if (!logSpec) { logSpec = sfmt("stderr:2"); } if (!traceSpec) { traceSpec = sfmt("stderr:2"); } } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s\n", ME_VERSION); exit(0); } else if (*argp == '-' && isdigit((uchar) argp[1])) { if (!logSpec) { logSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } if (!traceSpec) { traceSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } } else if (smatch(argp, "-?") || scontains(argp, "-help")) { usageError(); exit(3); } else if (*argp == '-') { mprLog("error http", 0, "Unknown switch \"%s\"", argp); usageError(); exit(4); } } if (logSpec) { mprStartLogging(logSpec, MPR_LOG_CMDLINE); } if (traceSpec) { httpStartTracing(traceSpec); } if (mprStart() < 0) { mprLog("error http", 0, "Cannot start MPR"); mprDestroy(); return MPR_ERR_CANT_INITIALIZE; } if (argc == argind && !app->configFile) { if (findConfig() < 0) { exit(5); } } if (createEndpoints(argc - argind, &argv[argind]) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (httpStartEndpoints() < 0) { mprLog("error http", 0, "Cannot start HTTP service, exiting."); exit(6); } if (app->show) { httpLogRoutes(0, 0); } mprServiceEvents(-1, 0); mprLog("info http", 1, "Stopping http-server ..."); mprDestroy(); return mprGetExitStatus(); }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *argp, *jail; char *logSpec; int argind, status, verbose; jail = 0; verbose = 0; logSpec = 0; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } mprSetAppName(BIT_PRODUCT, BIT_TITLE, BIT_VERSION); /* Allocate the top level application object. ManageApp is the GC manager function and is called by the GC to mark references in the app object. */ if ((app = mprAllocObj(AppwebApp, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); app->mpr = mpr; app->configFile = sclone("appweb.conf"); app->home = mprGetCurrentPath(); app->documents = app->home; argc = mpr->argc; argv = (char**) mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if BIT_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = mprGetAbsPath(argv[++argind]); if (chdir(app->home) < 0) { mprError("%s: Cannot change directory to %s", mprGetAppName(), app->home); exit(4); } } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s-%s\n", BIT_VERSION, BIT_BUILD_NUMBER); exit(0); } else { if (!smatch(argp, "?")) { mprError("Unknown switch \"%s\"", argp); } usageError(); exit(5); } } if (logSpec) { mprStartLogging(logSpec, 1); mprSetCmdlineLogging(1); } else if (verbose) { mprStartLogging(sfmt("stderr:%d", verbose + 1), 1); mprSetCmdlineLogging(1); } /* Start the multithreaded portable runtime (MPR) */ if (mprStart() < 0) { mprError("Cannot start MPR for %s", mprGetAppName()); mprDestroy(MPR_EXIT_DEFAULT); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (findAppwebConf() < 0) { exit(7); } if (jail && changeRoot(jail) < 0) { exit(8); } /* Open the sockets to listen on */ if (createEndpoints(argc - argind, &argv[argind]) < 0) { return MPR_ERR_CANT_INITIALIZE; } /* Start HTTP services */ if (maStartAppweb(app->appweb) < 0) { mprError("Cannot start HTTP service, exiting."); exit(9); } /* Service I/O events until instructed to exit */ while (!mprIsStopping()) { mprServiceEvents(-1, 0); } status = mprGetExitStatus(); mprLog(1, "Stopping Appweb ..."); maStopAppweb(app->appweb); mprDestroy(MPR_EXIT_DEFAULT); return status; }
MAIN(appweb, int argc, char **argv) { Mpr *mpr; MaHttp *http; cchar *ipAddrPort, *documentRoot, *argp, *logSpec; char *configFile, *ipAddr, *homeDir, *timeText, *ejsPrefix, *ejsPath, *changeRoot; int workers, outputVersion, argind, port; documentRoot = 0; changeRoot = ejsPrefix = ejsPath = 0; ipAddrPort = 0; ipAddr = 0; port = -1; logSpec = 0; server = 0; outputVersion = 0; workers = -1; configFile = BLD_FEATURE_CONFIG_FILE; homeDir = BLD_FEATURE_SERVER_ROOT; mpr = mprCreate(argc, argv, memoryFailure); argc = mpr->argc; argv = mpr->argv; #if BLD_FEATURE_ROMFS extern MprRomInode romFiles[]; mprSetRomFileSystem(mpr, romFiles); #endif if (osInit(mpr) < 0) { exit(2); } if (mprStart(mpr, 0) < 0) { mprUserError(mpr, "Can't start MPR for %s", mprGetAppName(mpr)); mprFree(mpr); return MPR_ERR_CANT_INITIALIZE; } for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (strcmp(argp, "--config") == 0) { if (argind >= argc) { return printUsage(mpr); } configFile = argv[++argind]; #if BLD_UNIX_LIKE } else if (strcmp(argp, "--chroot") == 0) { if (argind >= argc) { return printUsage(mpr); } changeRoot = mprGetAbsPath(mpr, argv[++argind]); #endif } else if (strcmp(argp, "--debug") == 0 || strcmp(argp, "-D") == 0 || strcmp(argp, "-d") == 0 || strcmp(argp, "--debugger") == 0) { mprSetDebugMode(mpr, 1); } else if (strcmp(argp, "--ejs") == 0) { if (argind >= argc) { return printUsage(mpr); } ejsPrefix = mprStrdup(mpr, argv[++argind]); if ((ejsPath = strchr(ejsPrefix, ':')) != 0) { *ejsPath++ = '\0'; } ejsPath = mprGetAbsPath(mpr, ejsPath); } else if (strcmp(argp, "--home") == 0) { if (argind >= argc) { return printUsage(mpr); } homeDir = mprGetAbsPath(mpr, argv[++argind]); if (chdir((char*) homeDir) < 0) { mprPrintfError(mpr, "%s: Can't change directory to %s\n", mprGetAppName(mpr), homeDir); exit(2); } } else if (strcmp(argp, "--log") == 0 || strcmp(argp, "-l") == 0) { if (argind >= argc) { return printUsage(mpr); } logSpec = argv[++argind]; maStartLogging(mpr, logSpec); } else if (strcmp(argp, "--name") == 0 || strcmp(argp, "-n") == 0) { if (argind >= argc) { return printUsage(mpr); } mprSetAppName(mpr, argv[++argind], 0, 0); } else if (strcmp(argp, "--threads") == 0) { if (argind >= argc) { return printUsage(mpr); } workers = atoi(argv[++argind]); } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { maStartLogging(mpr, "stdout:2"); } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { outputVersion++; } else { mprPrintfError(mpr, "Unknown switch \"%s\"\n", argp); printUsage(mpr); exit(2); } } if (argc > argind) { if (argc > (argind + 2)) { return printUsage(mpr); } ipAddrPort = argv[argind++]; if (argc > argind) { documentRoot = argv[argind++]; } else { documentRoot = "."; } } if (outputVersion) { mprPrintf(mpr, "%s %s-%s\n", mprGetAppTitle(mpr), BLD_VERSION, BLD_NUMBER); exit(0); } if (ipAddrPort) { mprParseIp(mpr, ipAddrPort, &ipAddr, &port, MA_SERVER_DEFAULT_PORT_NUM); } else { #if BLD_FEATURE_CONFIG_PARSE if (configFile == 0) { configFile = mprStrcat(mpr, -1, mprGetAppName(mpr), ".conf", NULL); } if (!mprPathExists(mpr, configFile, R_OK)) { mprPrintfError(mpr, "Can't open config file %s\n", configFile); exit(2); } #else return printUsage(mpr); #endif #if !BLD_FEATURE_ROMFS if (homeDir == 0) { homeDir = mprGetPathParent(mpr, configFile); if (chdir((char*) homeDir) < 0) { mprPrintfError(mpr, "%s: Can't change directory to %s\n", mprGetAppName(mpr), homeDir); exit(2); } } #endif } homeDir = mprGetCurrentPath(mpr); if (checkEnvironment(mpr, argv[0], homeDir) < 0) { exit(3); } #if BLD_UNIX_LIKE if (changeRoot) { homeDir = mprGetAbsPath(mpr, changeRoot); if (chdir(homeDir) < 0) { mprError(mpr, "%s: Can't change directory to %s", homeDir); exit(4); } if (chroot(homeDir) < 0) { if (errno == EPERM) { mprError(mpr, "%s: Must be super user to use the --chroot option", mprGetAppName(mpr)); } else { mprError(mpr, "%s: Can't change change root directory to %s, errno %d", mprGetAppName(mpr), homeDir, errno); } exit(5); } } #endif /* * Create the top level HTTP service and default HTTP server. Set the initial server root to "." */ http = maCreateHttp(mpr); if (http == 0) { mprUserError(mpr, "Can't create HTTP service for %s", mprGetAppName(mpr)); return MPR_ERR_CANT_INITIALIZE; } server = maCreateServer(http, "default", ".", 0, -1); if (server == 0) { mprUserError(mpr, "Can't create HTTP server for %s", mprGetAppName(mpr)); return MPR_ERR_CANT_INITIALIZE; } if (maConfigureServer(mpr, http, server, configFile, ipAddr, port, documentRoot) < 0) { /* mprUserError(mpr, "Can't configure the server, exiting."); */ exit(6); } if (mpr->ipAddr) { mprLog(mpr, 2, "Server IP address %s", mpr->ipAddr); } timeText = mprFormatLocalTime(mpr, mprGetTime(mpr)); mprLog(mpr, 1, "Started at %s", timeText); mprFree(timeText); #if BLD_FEATURE_EJS if (ejsPrefix) { createEjsAlias(mpr, http, server, ejsPrefix, ejsPath); } #endif #if BLD_FEATURE_MULTITHREAD if (workers >= 0) { mprSetMaxWorkers(http, workers); } #endif #if BLD_WIN_LIKE if (!ejsPrefix) { writePort(server->defaultHost); } #endif if (maStartHttp(http) < 0) { mprUserError(mpr, "Can't start HTTP service, exiting."); exit(7); } #if BLD_FEATURE_MULTITHREAD mprLog(mpr, 1, "HTTP services are ready with max %d worker threads", mprGetMaxWorkers(mpr)); #else mprLog(mpr, 1, "HTTP services are ready (single-threaded)"); #endif /* * Service I/O events until instructed to exit */ while (!mprIsExiting(mpr)) { mprServiceEvents(mpr->dispatcher, -1, MPR_SERVICE_EVENTS | MPR_SERVICE_IO); } /* * Signal a graceful shutdown */ mprLog(http, 1, "Exiting ..."); maStopHttp(http); mprLog(http, 1, "Exit complete"); #if VXWORKS if (mprStop(mpr)) { mprFree(mpr); } #endif return 0; }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *ipAddrPort, *argp, *jail; char *ip, *logSpec; int argind, port, status, verbose; ipAddrPort = 0; ip = 0; jail = 0; port = -1; verbose = 0; logSpec = 0; argv[0] = BLD_APPWEB_PATH; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } mprSetAppName(BLD_PRODUCT, BLD_NAME, BLD_VERSION); if ((app = mprAllocObj(App, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); #if BLD_FEATURE_ROMFS extern MprRomInode romFiles[]; mprSetRomFileSystem(romFiles); #endif app->mpr = mpr; app->workers = -1; app->configFile = BLD_CONFIG_FILE; app->home = BLD_SERVER_ROOT; app->documents = app->home; argc = mpr->argc; argv = mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if BLD_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = mprGetAbsPath(argv[++argind]); #if UNUSED && KEEP if (chdir(app->home) < 0) { mprError("%s: Can't change directory to %s", mprGetAppName(), app->home); exit(4); } #endif } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--threads")) { if (argind >= argc) { usageError(); } app->workers = atoi(argv[++argind]); } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { verbose++; } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s %s-%s\n", mprGetAppTitle(), BLD_VERSION, BLD_NUMBER); exit(0); } else { mprError("Unknown switch \"%s\"", argp); usageError(); exit(5); } } if (logSpec) { mprStartLogging(logSpec, 1); mprSetCmdlineLogging(1); } else if (verbose) { mprStartLogging(sfmt("stderr:%d", verbose + 1), 1); mprSetCmdlineLogging(1); } if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); mprDestroy(MPR_EXIT_DEFAULT); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (argc > argind) { if (argc > (argind + 2)) { usageError(); } ipAddrPort = argv[argind++]; if (argc > argind) { app->documents = sclone(argv[argind++]); } mprParseSocketAddress(ipAddrPort, &ip, &port, HTTP_DEFAULT_PORT); } else if (findConfigFile() < 0) { exit(7); } if (jail && changeRoot(jail) < 0) { exit(8); } if (initialize(ip, port) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (maStartAppweb(app->appweb) < 0) { mprUserError("Can't start HTTP service, exiting."); exit(9); } /* Service I/O events until instructed to exit */ while (!mprIsStopping()) { mprServiceEvents(-1, 0); } status = mprGetExitStatus(); mprLog(1, "Stopping Appweb ..."); maStopAppweb(app->appweb); mprDestroy(MPR_EXIT_DEFAULT); return status; }
MAIN(appweb, int argc, char **argv, char **envp) { Mpr *mpr; cchar *argp, *jail; char *logSpec, *traceSpec; int argind; jail = 0; logSpec = 0; traceSpec = 0; if ((mpr = mprCreate(argc, argv, MPR_USER_EVENTS_THREAD)) == NULL) { exit(1); } if ((app = mprAllocObj(AppwebApp, manageApp)) == NULL) { exit(2); } mprAddRoot(app); mprAddStandardSignals(); #if ME_ROM extern MprRomInode romFiles[]; mprSetRomFileSystem(romFiles); #endif if (httpCreate(HTTP_CLIENT_SIDE | HTTP_SERVER_SIDE) == 0) { exit(3); } app->mpr = mpr; app->workers = -1; app->home = sclone(ME_SERVER_ROOT); app->documents = app->home; argc = mpr->argc; argv = (char**) mpr->argv; for (argind = 1; argind < argc; argind++) { argp = argv[argind]; if (*argp != '-') { break; } if (smatch(argp, "--config") || smatch(argp, "--conf")) { if (argind >= argc) { usageError(); } app->configFile = sclone(argv[++argind]); #if ME_UNIX_LIKE } else if (smatch(argp, "--chroot")) { if (argind >= argc) { usageError(); } jail = mprGetAbsPath(argv[++argind]); #endif } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--exe")) { if (argind >= argc) { usageError(); } mpr->argv[0] = mprGetAbsPath(argv[++argind]); mprSetAppPath(mpr->argv[0]); mprSetModuleSearchPath(NULL); } else if (smatch(argp, "--home")) { if (argind >= argc) { usageError(); } app->home = sclone(argv[++argind]); if (chdir(app->home) < 0) { mprLog("error appweb", 0, "Cannot change directory to %s", app->home); exit(4); } } else if (smatch(argp, "--log") || smatch(argp, "-l")) { if (argind >= argc) { usageError(); } logSpec = argv[++argind]; } else if (smatch(argp, "--name") || smatch(argp, "-n")) { if (argind >= argc) { usageError(); } mprSetAppName(argv[++argind], 0, 0); } else if (smatch(argp, "--threads")) { if (argind >= argc) { usageError(); } app->workers = atoi(argv[++argind]); } else if (smatch(argp, "--show") || smatch(argp, "-s")) { app->show = 1; } else if (smatch(argp, "--trace") || smatch(argp, "-t")) { if (argind >= argc) { usageError(); } traceSpec = argv[++argind]; } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { if (!logSpec) { logSpec = sfmt("stderr:2"); } if (!traceSpec) { traceSpec = sfmt("stderr:2"); } } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s\n", ME_VERSION); exit(0); } else if (*argp == '-' && isdigit((uchar) argp[1])) { if (!logSpec) { logSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } if (!traceSpec) { traceSpec = sfmt("stderr:%d", (int) stoi(&argp[1])); } } else if (smatch(argp, "-?") || scontains(argp, "-help")) { usageError(); exit(5); } else if (*argp == '-') { mprLog("error appweb", 0, "Unknown switch \"%s\"", argp); usageError(); exit(5); } } app->home = mprGetAbsPath(app->home); if (logSpec) { mprStartLogging(logSpec, MPR_LOG_DETAILED | MPR_LOG_CONFIG | MPR_LOG_CMDLINE); } if (traceSpec) { httpStartTracing(traceSpec); } if (mprStart() < 0) { mprLog("error appweb", 0, "Cannot start MPR"); mprDestroy(); return MPR_ERR_CANT_INITIALIZE; } if (checkEnvironment(argv[0]) < 0) { exit(6); } if (argc == argind && !app->configFile) { if (findAppwebConf() < 0) { exit(7); } } if (jail && changeRoot(jail) < 0) { exit(8); } if (createEndpoints(argc - argind, &argv[argind]) < 0) { return MPR_ERR_CANT_INITIALIZE; } if (maStartAppweb(app->appweb) < 0) { mprLog("error appweb", 0, "Cannot start HTTP service, exiting."); exit(9); } if (app->show) { httpLogRoutes(0, 0); } mprServiceEvents(-1, 0); mprLog("info appweb", 1, "Stopping Appweb ..."); mprDestroy(); return mprGetExitStatus(); }
APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *command, int junk2) { char *argv[ME_MAX_ARGC], *argp; int argc, err, nextArg, manage, stop; argv[0] = ME_NAME "Monitor"; argc = mprParseArgs(command, &argv[1], ME_MAX_ARGC - 1) + 1; if (mprCreate(argc, argv, MPR_USER_EVENTS_THREAD | MPR_NO_WINDOW) == NULL) { exit(1); } if ((app = mprAllocObj(App, manageApp)) == NULL) { exit(2); } mprAddRoot(app); err = 0; stop = 0; manage = 0; app->appInst = inst; app->company = sclone(ME_COMPANY); app->serviceName = sclone(ME_NAME); mprSetLogHandler(logHandler); chdir(mprGetAppDir()); /* Parse command line arguments */ for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--manage") == 0) { manage++; } else if (strcmp(argp, "--stop") == 0) { stop++; } else { err++; } if (err) { mprError("appweb monitor", "Bad command line: %s\n" " Usage: %s [options]\n" " Switches:\n" " --manage # Launch browser to manage", " --stop # Stop a running monitor", command, mprGetAppName()); return -1; } } if (stop) { stopMonitor(); return 0; } if (findInstance()) { mprError("appweb monitor", "Application %s is already active.", mprGetAppTitle()); return MPR_ERR_BUSY; } app->hwnd = mprSetWindowsThread(0); mprSetWinMsgCallback(msgProc); if (app->taskBarIcon > 0) { ShowWindow(app->hwnd, SW_MINIMIZE); UpdateWindow(app->hwnd); } if (manage) { /* Launch the browser */ runBrowser("/index.html"); } else { if (openMonitorIcon() < 0) { mprError("appweb monitor", "Cannot open %s tray", mprGetAppName()); } else { mprServiceEvents(-1, 0); closeMonitorIcon(); } } mprDestroy(); return 0; }