static void usageError(Mpr *mpr) { cchar *name; name = mprGetAppName(); mprEprintf("\n%s Usage:\n\n" " %s [options]\n" " %s [options] documents ip[:port] ...\n\n" " Without [documents ip:port], %s will read the appweb.conf configuration file.\n\n" " Options:\n" " --config configFile # Use named config file instead appweb.conf\n" " --chroot directory # Change root directory to run more securely (Unix)\n" " --debugger # Disable timeouts to make debugging easier\n" " --exe path # Set path to Appweb executable on Vxworks\n" " --home directory # Change to directory to run\n" " --log logFile:level # Log to file at verbosity level (0-5)\n" " --name uniqueName # Unique name for this instance\n" " --show # Show route table\n" " --trace traceFile:level # Trace to file at verbosity level (0-5)\n" " --verbose # Same as --log stderr:2\n" " --version # Output version information\n" " --DIGIT # Same as --log stderr:DIGIT\n\n", mprGetAppTitle(), name, name, name); exit(10); }
/* Default log output is just to the console */ static void logHandler(int flags, int level, cchar *msg) { if (flags & MPR_USER_MSG) { MessageBoxEx(NULL, msg, mprGetAppTitle(), MB_OK, 0); } mprWriteToOsLog(msg, 0, 0); }
/* * Initialize the applications's window */ static int initWindow(Mpr *mpr) { WNDCLASS wc; int rc; wc.style = CS_HREDRAW | CS_VREDRAW; wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = (HINSTANCE) appInst; wc.hIcon = NULL; wc.lpfnWndProc = (WNDPROC) msgProc; wc.lpszMenuName = wc.lpszClassName = mprGetAppName(mpr); rc = RegisterClass(&wc); if (rc == 0) { mprError(mpr, "Can't register windows class"); return -1; } appHwnd = CreateWindow(mprGetAppName(mpr), mprGetAppTitle(mpr), WS_OVERLAPPED, CW_USEDEFAULT, 0, 0, 0, NULL, NULL, 0, NULL); if (! appHwnd) { mprError(mpr, "Can't create window"); return -1; } if (taskBarIcon > 0) { ShowWindow(appHwnd, SW_MINIMIZE); UpdateWindow(appHwnd); } return 0; }
static long msgProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { MprThread *tp; char buf[ME_MAX_FNAME]; switch (msg) { case WM_DESTROY: case WM_QUIT: mprShutdown(0, -1, 0); break; case APPWEB_MONITOR_MESSAGE: return monitorEvent(hwnd, wp, lp); case WM_COMMAND: switch (LOWORD(wp)) { case MA_MENU_STATUS: break; case MA_MENU_DOC: runBrowser("http://embedthis.com/appweb/doc/index.html"); break; #if UNUSED case MA_MENU_MANAGE: runBrowser("/index.html"); break; #endif case MA_MENU_START: tp = mprCreateThread("startService", startService, 0, 0); mprStartThread(tp); break; case MA_MENU_STOP: tp = mprCreateThread("stopService", stopService, 0, 0); mprStartThread(tp); break; case MA_MENU_ABOUT: /* Single-threaded users beware. This blocks !! */ fmt(buf, sizeof(buf), "%s %s", ME_TITLE, ME_VERSION); MessageBoxEx(hwnd, buf, mprGetAppTitle(), MB_OK, 0); break; case MA_MENU_EXIT: PostMessage(hwnd, WM_QUIT, 0, 0L); break; default: return (long) DefWindowProc(hwnd, msg, wp, lp); } break; default: return (long) DefWindowProc(hwnd, msg, wp, lp); } return 0; }
static long msgProc(HWND hwnd, UINT msg, UINT wp, LPARAM lp) { MprThread *tp; char buf[MPR_MAX_FNAME]; switch (msg) { case WM_DESTROY: case WM_QUIT: mprTerminate(MPR_EXIT_GRACEFUL, -1); break; case APPWEB_MONITOR_MESSAGE: return monitorEvent(hwnd, wp, lp); case WM_COMMAND: switch (LOWORD(wp)) { case MA_MENU_STATUS: break; case MA_MENU_DOC: runBrowser("/doc/index.html"); break; case MA_MENU_MANAGE: runBrowser("/index.html"); break; case MA_MENU_START: tp = mprCreateThread("startService", startService, 0, 0); mprStartThread(tp); break; case MA_MENU_STOP: tp = mprCreateThread("stopService", stopService, 0, 0); mprStartThread(tp); break; case MA_MENU_ABOUT: /* Single-threaded users beware. This blocks !! */ mprSprintf(buf, sizeof(buf), "%s %s-%s", BIT_TITLE, BIT_VERSION, BIT_BUILD_NUMBER); MessageBoxEx(hwnd, buf, mprGetAppTitle(), MB_OK, 0); break; case MA_MENU_EXIT: PostMessage(hwnd, WM_QUIT, 0, 0L); break; default: return (long) DefWindowProc(hwnd, msg, wp, lp); } break; default: return (long) DefWindowProc(hwnd, msg, wp, lp); } return 0; }
static void stopMonitor() { HWND hwnd; hwnd = FindWindow(mprGetAppName(mpr), mprGetAppTitle(mpr)); if (hwnd) { PostMessage(hwnd, WM_QUIT, 0, 0L); } }
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(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; }
/* * Start error and information logging. Note: this is not per-request access logging */ int maStartLogging(MprCtx ctx, cchar *logSpec) { Mpr *mpr; MprFile *file; MprTime now; char *levelSpec, *spec, timeText[80]; int level; level = 0; mpr = mprGetMpr(ctx); if (logSpec == 0) { logSpec = "stdout:2"; } if (*logSpec && strcmp(logSpec, "none") != 0) { spec = mprStrdup(mpr, logSpec); if ((levelSpec = strrchr(spec, ':')) != 0 && isdigit((int) levelSpec[1])) { *levelSpec++ = '\0'; level = atoi(levelSpec); } if (strcmp(spec, "stdout") == 0) { file = mpr->fileService->console; } else { if ((file = mprOpen(mpr, spec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) { mprErrorPrintf(mpr, "Can't open log file %s\n", spec); return -1; } } mprSetLogLevel(mpr, level); mprSetLogHandler(mpr, logHandler, (void*) file); now = mprGetTime(mpr); mprCtime(mpr, timeText, sizeof(timeText), now); mprLog(mpr, MPR_CONFIG, "Configuration for %s", mprGetAppTitle(mpr)); mprLog(mpr, MPR_CONFIG, "--------------------------------------------"); mprLog(mpr, MPR_CONFIG, "Host: %s", mprGetHostName(mpr)); mprLog(mpr, MPR_CONFIG, "CPU: %s", BLD_HOST_CPU); mprLog(mpr, MPR_CONFIG, "OS: %s", BLD_HOST_OS); mprLog(mpr, MPR_CONFIG, "Distribution: %s %s", BLD_HOST_DIST, BLD_HOST_DIST_VER); mprLog(mpr, MPR_CONFIG, "OS: %s", BLD_HOST_OS); mprLog(mpr, MPR_CONFIG, "Version: %s.%d", BLD_VERSION, BLD_NUMBER); mprLog(mpr, MPR_CONFIG, "BuildType: %s", BLD_TYPE); mprLog(mpr, MPR_CONFIG, "Started at: %s", timeText); mprLog(mpr, MPR_CONFIG, "--------------------------------------------"); } return 0; }
/* See if an instance of this product is already running */ static int findInstance() { HWND hwnd; hwnd = FindWindow(mprGetAppName(), mprGetAppTitle()); if (hwnd) { if (IsIconic(hwnd)) { ShowWindow(hwnd, SW_RESTORE); } SetForegroundWindow(hwnd); return 1; } return 0; }
/* * Can be called multiple times */ static int openMonitorIcon() { NOTIFYICONDATA data; HICON iconHandle; HBITMAP go, stop; static int doOnce = 0; int rc; if (monitorMenu == NULL) { monitorMenu = LoadMenu(appInst, "monitorMenu"); if (! monitorMenu) { mprError(mpr, "Can't locate monitorMenu"); return MPR_ERR_CANT_OPEN; } } if (subMenu == NULL) { subMenu = GetSubMenu(monitorMenu, 0); go = LoadBitmap(appInst, MAKEINTRESOURCE(IDB_GO)); rc = GetLastError(); stop = LoadBitmap(appInst, MAKEINTRESOURCE(IDB_STOP)); SetMenuItemBitmaps(subMenu, MA_MENU_STATUS, MF_BYCOMMAND, stop, go); } iconHandle = (HICON) LoadImage(appInst, APPWEB_ICON, IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE); if (iconHandle == 0) { mprError(mpr, "Can't load icon %s", APPWEB_ICON); return MPR_ERR_CANT_INITIALIZE; } data.uID = APPWEB_MONITOR_ID; data.hWnd = appHwnd; data.hIcon = iconHandle; data.cbSize = sizeof(NOTIFYICONDATA); data.uCallbackMessage = APPWEB_MONITOR_MESSAGE; data.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; mprStrcpy(data.szTip, sizeof(data.szTip), mprGetAppTitle(mpr)); Shell_NotifyIcon(NIM_ADD, &data); if (iconHandle) { DestroyIcon(iconHandle); } return 0; }
/* * Start error and information logging. Note: this is not per-request access logging */ int maStartLogging(MprCtx ctx, cchar *logSpec) { Mpr *mpr; MprFile *file; char *levelSpec, *spec; int level; level = 0; mpr = mprGetMpr(ctx); if (logSpec == 0) { logSpec = "stdout:0"; } if (*logSpec && strcmp(logSpec, "none") != 0) { spec = mprStrdup(mpr, logSpec); if ((levelSpec = strrchr(spec, ':')) != 0 && isdigit((int) levelSpec[1])) { *levelSpec++ = '\0'; level = atoi(levelSpec); } if (strcmp(spec, "stdout") == 0) { file = mpr->fileSystem->stdOutput; } else { if ((file = mprOpen(mpr, spec, O_CREAT | O_WRONLY | O_TRUNC | O_TEXT, 0664)) == 0) { mprPrintfError(mpr, "Can't open log file %s\n", spec); return -1; } } mprSetLogLevel(mpr, level); mprSetLogHandler(mpr, logHandler, (void*) file); mprLog(mpr, MPR_CONFIG, "Configuration for %s", mprGetAppTitle(mpr)); mprLog(mpr, MPR_CONFIG, "---------------------------------------------"); mprLog(mpr, MPR_CONFIG, "Host: %s", mprGetHostName(mpr)); mprLog(mpr, MPR_CONFIG, "CPU: %s", BLD_CPU); mprLog(mpr, MPR_CONFIG, "OS: %s", BLD_OS); if (strcmp(BLD_DIST, "Unknown") != 0) { mprLog(mpr, MPR_CONFIG, "Distribution: %s %s", BLD_DIST, BLD_DIST_VER); } mprLog(mpr, MPR_CONFIG, "Version: %s-%s", BLD_VERSION, BLD_NUMBER); mprLog(mpr, MPR_CONFIG, "BuildType: %s", BLD_TYPE); mprLog(mpr, MPR_CONFIG, "---------------------------------------------"); } return 0; }
static void usageError() { cchar *name; name = mprGetAppName(); mprEprintf("\n%s Usage:\n\n" " %s [options]\n" " Options:\n" " --config configFile # Use named config file instead http.json\n" " --debugger # Disable timeouts to make debugging easier\n" " --log logFile:level # Log to file at verbosity level (0-5)\n" " --name uniqueName # Unique name for this instance\n" " --show # Show route table\n" " --trace traceFile:level # Trace to file at verbosity level (0-5)\n" " --verbose # Same as --log stderr:2\n" " --version # Output version information\n" " --DIGIT # Same as --log stderr:DIGIT\n\n", mprGetAppTitle(), name); exit(7); }
/* Create a default window if the application has not already created one. */ PUBLIC int mprInitWindow() { MprWaitService *ws; WNDCLASS wc; HWND hwnd; wchar *name, *title; int rc; ws = MPR->waitService; if (ws->hwnd) { return 0; } name = (wchar*) wide(mprGetAppName()); title = (wchar*) wide(mprGetAppTitle()); wc.style = CS_HREDRAW | CS_VREDRAW; wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = 0; wc.hIcon = NULL; wc.lpfnWndProc = (WNDPROC) msgProc; wc.lpszMenuName = wc.lpszClassName = name; rc = RegisterClass(&wc); if (rc == 0) { mprError("Cannot register windows class"); return MPR_ERR_CANT_INITIALIZE; } hwnd = CreateWindow(name, title, WS_OVERLAPPED, CW_USEDEFAULT, 0, 0, 0, NULL, NULL, 0, NULL); if (!hwnd) { mprError("Cannot create window"); return -1; } ws->hwnd = hwnd; ws->socketMessage = MPR_SOCKET_MESSAGE; return 0; }
static void usageError(Mpr *mpr) { cchar *name; name = mprGetAppName(); mprPrintfError("\n%s Usage:\n\n" " %s [options] [IPaddress][:port] [documents]\n\n" " Options:\n" " --config configFile # Use named config file instead appweb.conf\n" " --chroot directory # Change root directory to run more securely (Unix)\n" " --debugger # Disable timeouts to make debugging easier\n" " --exe path # Set path to Appweb executable on Vxworks\n" " --home directory # Change to directory to run\n" " --log logFile:level # Log to file file at verbosity level\n" " --name uniqueName # Unique name for this instance\n" " --threads maxThreads # Set maximum worker threads\n" " --verbose # Same as --log stderr:2\n" " --version # Output version information\n\n" " Without IPaddress, %s will read the appweb.conf configuration file.\n\n", mprGetAppTitle(), name, name, name, name); exit(10); }
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; }
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2) { char **argv, *argp, *serviceArgs, *homeDir; int argc, err, nextArg, installFlag; mpr = mprCreate(0, NULL, NULL); appInst = inst; serviceArgs = 0; err = 0; exiting = 0; installFlag = 0; homeDir = 0; heartBeatPeriod = HEART_BEAT_PERIOD; initService(); mprSetAppName(mpr, BLD_PRODUCT "Angel", BLD_NAME "Angel", BLD_VERSION); appName = mprGetAppName(mpr); appTitle = mprGetAppTitle(mpr); mprSetLogHandler(mpr, logHandler, NULL); /* * Create the window */ if (initWindow() < 0) { mprError(mpr, "Can't initialize application Window"); return FALSE; } /* * Parse command line arguments */ if (mprMakeArgv(mpr, "", args, &argc, &argv) < 0) { return FALSE; } for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-' || strcmp(argp, "--") == 0) { break; } if (strcmp(argp, "--args") == 0) { /* * Args to pass to service when it starts */ if (nextArg >= argc) { err++; } else { serviceArgs = argv[++nextArg]; } } else if (strcmp(argp, "--console") == 0) { /* * Allow the service to interact with the console */ createConsole++; } else if (strcmp(argp, "--daemon") == 0) { /* Ignored on windows */ } else if (strcmp(argp, "--heartBeat") == 0) { /* * Set the heart beat period */ if (nextArg >= argc) { err++; } else { heartBeatPeriod = atoi(argv[++nextArg]) * 1000; } } else if (strcmp(argp, "--home") == 0) { /* * Change to this directory before starting the service */ if (nextArg >= argc) { err++; } else { homeDir = argv[++nextArg]; } } else if (strcmp(argp, "--program") == 0) { if (nextArg >= argc) { err++; } else { serviceProgram = argv[++nextArg]; } } else if (strcmp(argp, "--install") == 0) { installFlag++; } else if (strcmp(argp, "--start") == 0) { /* * Start the angel */ if (startService() < 0) { return FALSE; } mprSleep(mpr, 2000); /* Time for service to really start */ } else if (strcmp(argp, "--stop") == 0) { /* * Stop the angel */ if (removeService(0) < 0) { return FALSE; } } else if (strcmp(argp, "--uninstall") == 0) { /* * Remove the angel */ if (removeService(1) < 0) { return FALSE; } } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { verbose++; } else { err++; } if (err) { mprUserError(mpr, "Bad command line: %s\n" " Usage: %s [options] [program args]\n" " Switches:\n" " --args # Args to pass to service\n" " --console # Display the service console\n" " --heartBeat interval # Heart beat interval period (secs)\n" " --home path # Home directory for service\n" " --install # Install the service\n" " --program # Service program to start\n" " --start # Start the service\n" " --stop # Stop the service\n" " --uninstall # Uninstall the service", args, appName); return -1; } } if (installFlag) { /* * Install the angel */ if (installService(homeDir, serviceArgs) < 0) { return FALSE; } } if (argc <= 1) { /* * This will block if we are a service and are being started by the * service control manager. While blocked, the svcMain will be called * which becomes the effective main program. */ startDispatcher(serviceMain); } return 0; }
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2) { char **argv, *argp; int argc, err, nextArg; mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddTerminator(terminating); err = 0; app->appInst = inst; app->heartBeatPeriod = HEART_BEAT_PERIOD; setAppDefaults(); mprSetAppName(BLD_PRODUCT "Manager", BLD_NAME "Manager", BLD_VERSION); app->appName = mprGetAppName(); app->appTitle = mprGetAppTitle(mpr); mprSetLogHandler(logHandler); mprSetWinMsgCallback((MprMsgCallback) msgProc); if ((argc = mprMakeArgv(args, &argv, MPR_ARGV_ARGS_ONLY)) < 0) { return FALSE; } for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-' || strcmp(argp, "--") == 0) { break; } if (strcmp(argp, "--args") == 0) { /* Args to pass to service when it starts */ if (nextArg >= argc) { err++; } else { app->serviceArgs = argv[++nextArg]; } } else if (strcmp(argp, "--console") == 0) { /* Allow the service to interact with the console */ app->createConsole++; } else if (strcmp(argp, "--daemon") == 0) { /* Ignored on windows */ } else if (strcmp(argp, "--heartBeat") == 0) { /* Set the heart beat period */ if (nextArg >= argc) { err++; } else { app->heartBeatPeriod = atoi(argv[++nextArg]) * 1000; } } else if (strcmp(argp, "--home") == 0) { /* Change to this directory before starting the service */ if (nextArg >= argc) { err++; } else { app->serviceHome = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--log") == 0) { /* Pass the log directive through to the service */ if (nextArg >= argc) { err++; } else { app->logSpec = sclone(argv[++nextArg]); mprStartLogging(app->logSpec, 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--name") == 0) { if (nextArg >= argc) { err++; } else { app->serviceName = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--program") == 0) { if (nextArg >= argc) { err++; } else { app->serviceProgram = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--verbose") == 0 || strcmp(argp, "-v") == 0) { mprSetLogLevel(1); } else { err++; } if (err) { mprUserError("Bad command line: %s\n" " Usage: %s [options] [program args]\n" " Switches:\n" " --args # Args to pass to service\n" " --console # Display the service console\n" " --heartBeat interval # Heart beat interval period (secs)\n" " --home path # Home directory for service\n" " --log logFile:level # Log directive for service\n" " --name name # Name of the service to manage\n" " --program path # Service program to start\n" " --verbose # Show command feedback\n" " Commands:\n" " disable # Disable the service\n" " enable # Enable the service\n" " start # Start the service\n" " stop # Start the service\n" " run # Run and watch over the service\n", args, app->appName); return -1; } } if (mprStart() < 0) { mprUserError("Can't start MPR for %s", mprGetAppName()); } else { mprStartEventsThread(); if (nextArg >= argc) { return process("run"); } else for (; nextArg < argc; nextArg++) { if (!process(argv[nextArg])) { return FALSE; } } } return TRUE; }
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; }
static long msgProc(HWND hwnd, uint msg, uint wp, long lp) { char buf[MPR_MAX_FNAME]; switch (msg) { case WM_DESTROY: case WM_QUIT: mprSignalExit(mpr); break; case APPWEB_MONITOR_MESSAGE: return monitorEvent(hwnd, wp, lp); case WM_COMMAND: switch (LOWORD(wp)) { case MA_MENU_STATUS: break; case MA_MENU_DOC: runBrowser("/doc/index.html"); break; case MA_MENU_MANAGE: runBrowser("/index.html"); break; case MA_MENU_START: startService(); break; case MA_MENU_STOP: stopService(); break; case MA_MENU_ABOUT: /* * Single-threaded users beware. This blocks !! */ mprSprintf(buf, sizeof(buf), "%s %s-%s", BLD_NAME, BLD_VERSION, BLD_NUMBER); MessageBoxEx(NULL, buf, mprGetAppTitle(mpr), MB_OK, 0); break; case MA_MENU_EXIT: /* * FUTURE * * h = CreateDialog(appInst, MAKEINTRESOURCE(IDD_EXIT), appHwnd, dialogProc); * ShowWindow(h, SW_SHOW); */ PostMessage(hwnd, WM_QUIT, 0, 0L); break; default: return DefWindowProc(hwnd, msg, wp, lp); } break; default: return DefWindowProc(hwnd, msg, wp, lp); } return 0; }
/* Default log output is just to the console */ static void logHandler(cchar *tags, int level, cchar *msg) { MessageBoxEx(NULL, msg, mprGetAppTitle(), MB_OK, 0); }
/* * Default log output is just to the console */ static void logHandler(MprCtx ctx, int flags, int level, cchar *msg) { MessageBoxEx(NULL, msg, mprGetAppTitle(ctx), MB_OK, 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(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; }
int APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *args, int junk2) { char **argv, *argp; int argc, err, nextArg, manage, stop; mpr = mprCreate(0, NULL, NULL); err = 0; manage = 0; appInst = inst; stop = 0; serviceName = BLD_COMPANY "-" BLD_PRODUCT; serviceTitle = BLD_NAME; serviceWindowName = BLD_PRODUCT "Angel"; serviceWindowTitle = BLD_NAME "Angel"; mprSetAppName(mpr, BLD_PRODUCT "Monitor", BLD_NAME " Monitor", BLD_VERSION); appName = mprGetAppName(mpr); appTitle = mprGetAppTitle(mpr); mprSetLogHandler(mpr, logHandler, NULL); chdir(mprGetAppDir(mpr)); /* * Parse command line arguments */ if (mprMakeArgv(mpr, "", args, &argc, &argv) < 0) { return FALSE; } 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) { mprUserError(mpr, "Bad command line: %s\n" " Usage: %s [options]\n" " Switches:\n" " --manage # Launch browser to manage", " --stop # Stop a running monitor", args, appName); return -1; } } if (stop) { stopMonitor(); return 0; } if (findInstance(mpr)) { mprUserError(mpr, "Application %s is already active.", mprGetAppTitle(mpr)); return MPR_ERR_BUSY; } /* * Create the window */ if (initWindow(mpr) < 0) { mprUserError(mpr, "Can't initialize application Window"); return MPR_ERR_CANT_INITIALIZE; } if (manage) { /* * Launch the browser */ runBrowser("/index.html"); } else { if (openMonitorIcon() < 0) { mprUserError(mpr, "Can't open %s tray", appName); } else { eventLoop(); closeMonitorIcon(); } } // mprFree(mpr); return 0; }
APIENTRY WinMain(HINSTANCE inst, HINSTANCE junk, char *command, int junk2) { char *argv[MPR_MAX_ARGC], *argp; int argc, err, nextArg, manage, stop; argc = mprParseArgs(command, &argv[1], MPR_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->serviceName = sclone(BIT_COMPANY "-" BIT_PRODUCT); app->serviceTitle = sclone(BIT_TITLE); app->serviceWindowName = sclone(BIT_PRODUCT "Angel"); app->serviceWindowTitle = sclone(BIT_TITLE "Angel"); mprSetAppName(BIT_PRODUCT "Monitor", BIT_TITLE " Monitor", BIT_VERSION); 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) { mprUserError("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()) { mprUserError("Application %s is already active.", mprGetAppTitle()); return MPR_ERR_BUSY; } if (mprInitWindow() < 0) { mprUserError("Can't initialize application Window"); return MPR_ERR_CANT_INITIALIZE; } app->appHwnd = mprGetHwnd(); mprSetWinMsgCallback(msgProc); if (app->taskBarIcon > 0) { ShowWindow(app->appHwnd, SW_MINIMIZE); UpdateWindow(app->appHwnd); } if (manage) { /* Launch the browser */ runBrowser("/index.html"); } else { if (openMonitorIcon() < 0) { mprUserError("Can't open %s tray", mprGetAppName()); } else { eventLoop(); closeMonitorIcon(); } } return 0; }