⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 status.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/EnhPi3/Status.cpp,v $
 * $Date: 2003/05/13 18:41:59 $
 *
 Description:

\*____________________________________________________________________________*/
/* $SourceTop:$ */

#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <assert.h>
#include <stdio.h>

#include "resrc1.h"
#include "Common.h"

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
static HWND hWndRefresh = 0;
static HWND hWndStatus = 0;
static HWND hWndStart = 0;
static HWND hWndStop = 0;
static HWND hWndURL = 0;

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns 1 for true, 0 for false and -1 for failure.
\*____________________________________________________________________________*/
static int Internal_isServerRunning()
{
	int iRet;

	const char *pSemaphoreName = AD_lookupValue( __pConfig,
		"Startup", "SemaphoreName" );

	/*
	** see what the run mode of the server is 
	*/
	const char *pType = AD_lookupValue( __pConfig, "General", "RunMode" );
	if ( pType && strcmp( pType, "Desktop" ) )
		{
		/*
		** Service
		*/
		const char *pServiceName = AD_lookupValue( __pConfig, "General", 
			"ServiceName" );
		iRet = CTRL_isServiceRunning( pServiceName );
		}
	else
		{
		/*
		** Desktop
		*/

		/*
		** Get semaphore name
		*/
		iRet = CTRL_isPi3Running( pSemaphoreName );
		};

	return iRet;
}
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns 1 for true, 0 for false and -1 for failure.
\*____________________________________________________________________________*/
static int Internal_isServiceInstalled()
{
	const char *pServiceName = AD_lookupValue( __pConfig, "General", 
			"ServiceName" );
	return CTRL_isServiceInstalled( pServiceName );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns 1 for true and -1 for failure.
\*____________________________________________________________________________*/
static int Internal_installService()
{
	/*
	** Get command line of server
	*/
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	const char *pServiceName = AD_lookupValue( __pConfig, "General", 
			"ServiceName" );

	/*
	** Get commandline
	*/
	sprintf( szBuf, "%s\\bin\\Pi3Srv32.exe", 
		AD_lookupValue( __pConfig, "General", "InstallDir" ) );
	return CTRL_installService( pServiceName, szBuf );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns 1 for true and -1 for failure.
\*____________________________________________________________________________*/
static int Internal_unInstallService()
{
	const char *pServiceName = AD_lookupValue( __pConfig, "General", 
			"ServiceName" );
	return CTRL_unInstallService( pServiceName );
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Start server with appropriate messages. Starts the server as either
	a system service or an application. Assumes the server is not already
	running.

	Returns 1 on success, and -1 on failure.
\*____________________________________________________________________________*/
static int Internal_startServer()
{
	/*
	** Get command line of server
	*/
	enum { BUF_SIZE=1023 };
	char szBuf[BUF_SIZE+1];

	int iRet;

	/*
	** see what the run mode of the server is 
	*/
	const char *pType = AD_lookupValue( __pConfig, "General", "RunMode" );
	if ( pType && strcmp( pType, "Desktop" ) )
		{
		/*
		** Service
		*/
		
		/*
		** Firstly, make sure the service is installed
		*/
		int iInstalled = Internal_isServiceInstalled();
		if ( iInstalled==-1 )
			{ return -1; };
		if ( !iInstalled )
			{
			if ( Internal_installService()!=1 )
				{ return -1; };
			};

		const char *pServiceName = AD_lookupValue( __pConfig, "General", 
			"ServiceName" );
		iRet = CTRL_startService( pServiceName );
		}
	else
		{
		/*
		** Desktop
		*/

		/*
		** Get commandline and semaphore name
		*/
		sprintf( szBuf, "\"%s\" /START %s",
			AD_lookupValue( __pConfig, "General", "ExePath" ),
			AD_lookupValue( __pConfig, "Startup", "ConfigName" ) );
		iRet = CTRL_startPi3( szBuf,		
			AD_lookupValue( __pConfig, "Startup", "SemaphoreName" ) );
		};

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Returns 1 on success, and -1 on failure.
\*____________________________________________________________________________*/
static int Internal_stopServer()
{
	int iRet;

	/*
	** see what the run mode of the server is 
	*/
	const char *pType = AD_lookupValue( __pConfig, "General", "RunMode" );
	if ( pType && strcmp( pType, "Desktop" ) )
		{
		/*
		** Service
		*/
		const char *pServiceName = AD_lookupValue( __pConfig, "General", 
			"ServiceName" );
		iRet = CTRL_stopService( pServiceName );
		}
	else
		{
		/*
		** Desktop
		*/

		/*
		** Get semaphore name
		*/
		iRet = CTRL_stopPi3( AD_lookupValue( __pConfig,
				"Startup", "SemaphoreName" ) );
		};

	return iRet;
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Return 1 on success, 0 on error.
\*____________________________________________________________________________*/
int Internal_refreshStatus( HWND hDlg )
{
	int iRunning = Internal_isServerRunning();
	if ( iRunning==-1 )
		{
		MessageBox( hDlg, "Could not get server status, installation error",
			"Error", MB_OK | MB_ICONEXCLAMATION );

		EnableWindow( hWndStop, FALSE );
		EnableWindow( hWndStart, FALSE );
		EnableWindow( hWndURL, FALSE );
		SendMessage( hWndStatus, WM_SETTEXT, 0, (LPARAM)
			"Could not get server status." );

		return 0;
		};

	/*
	** Check whether this is a service or an application
	*/
	SendMessage( hWndURL, WM_SETTEXT, 0, (LPARAM)
		AD_lookupValue( __pConfig, "Identity", "URL" ) );

	if ( iRunning )
		{
		EnableWindow( hWndStop, TRUE );
		EnableWindow( hWndStart, FALSE );
		EnableWindow( hWndURL, TRUE );
		SendMessage( hWndStatus, WM_SETTEXT, 0, (LPARAM)
			"The server is running." );
		}
	else
		{
		EnableWindow( hWndStop, FALSE );
		EnableWindow( hWndStart, TRUE );
		EnableWindow( hWndURL, FALSE );
		SendMessage( hWndStatus, WM_SETTEXT, 0, (LPARAM)
			"The server is not running." );
		};

	return 1;
}
/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
	Test if changes need to be applied before starting.
\*____________________________________________________________________________*/
static void Internal_maybeApplyChanges( HWND hDlg )
{
	/*
	** Where is this control ID defined?
	*/
	if ( IsWindowEnabled( GetDlgItem( GetParent( hDlg ), 0x3021 ) ) )
		{
		if ( MessageBox( hDlg, "Changes have been made. Would \
you like to apply them now?", "Apply Changes",
			MB_YESNO | MB_ICONEXCLAMATION )==IDYES )
			{ PropSheet_Apply( GetParent( hDlg ) ); };
		};	
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static void Internal_centerPropertySheet( HWND hWnd )
{
    HWND hProp = ::GetParent( hWnd );
    if ( hProp )
        {
        /*
        ** Center window
        */
        RECT rDesktop;
        RECT rProp;

        ::GetWindowRect( ::GetDesktopWindow(), &rDesktop );
        ::GetWindowRect( hProp, &rProp );

        /*
        ** Calculate centered coordinates for property
        ** window
        */
        LONG lMiddleX = ( ( rDesktop.right - rDesktop.left ) /2 )
            + rDesktop.left;
        LONG lMiddleY = ( ( rDesktop.bottom - rDesktop.top ) /2 )
            + rDesktop.top;
        LONG lWidth = rProp.right - rProp.left;
        LONG lHeight = rProp.bottom - rProp.top;

        /*
        ** Move window
        */
        ::MoveWindow( hProp,
            lMiddleX - ( lWidth / 2 ),
            lMiddleY - ( lHeight / 2 ),
            lWidth,
            lHeight,
            TRUE );
        };
}

/*____________________________________________________________________________*\
 *
 Function:
 Synopsis:
 Description:
\*____________________________________________________________________________*/
static BOOL CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam,
	LPARAM lParam)
{
	switch (uMsg)
		{
		case WM_INITDIALOG:
			{
            /*
            ** Since this is the first page to appear, center the
            ** property sheet
            */
            Internal_centerPropertySheet( hDlg );

            /*
            ** Then do the usual initialization
            */
			hWndRefresh = GetDlgItem( hDlg, IDC_REFRESH );
			hWndStatus = GetDlgItem( hDlg, IDC_STATUS );
			hWndStart = GetDlgItem( hDlg, IDC_START );
			hWndStop = GetDlgItem( hDlg, IDC_STOP );
			hWndURL = GetDlgItem( hDlg, IDC_URL );

			assert( hWndStatus && hWndStart );
			assert( hWndStop && hWndURL  );
			}
			break;

		case WM_COMMAND:
			if ( GET_WM_COMMAND_CMD(wParam, lParam)==BN_CLICKED )
				{ 
				switch( GET_WM_COMMAND_ID(wParam, lParam) )
					{
					case IDC_REFRESH:
						Internal_refreshStatus( hDlg );
						break;

					case IDC_STOP:
						AD_hourglassOn();
						if ( Internal_stopServer()==1 )
							{
							Sleep( 1000 );
							Internal_refreshStatus( hDlg );
							};
						AD_hourglassOff();
						break;

					case IDC_START:
						Internal_maybeApplyChanges( hDlg );
						AD_hourglassOn();
						if ( Internal_startServer()==1 )
							{
							Sleep( 1000 );
							Internal_refreshStatus( hDlg );
                            AD_hourglassOff();
							}
						else
                            {
                            AD_hourglassOff();
                            MessageBox( hDlg, "Failed to start server", "Error",
                                MB_OK );
                            assert( 0 );
                            };
						break;

					case IDC_URL:
						Internal_maybeApplyChanges( hDlg );
#if 0
						{
						const char *pURL = AD_lookupValue( __pConfig, 
							"Identity", "URL" );
						if ( !pURL || !CTRL_openURL( hDlg, pURL ) )
							{
							MessageBox( hDlg, 
								"Could not open home page", "Open Home Page", 
								MB_OK ); 
							};
						}
#endif
						break;

					default:;
					};
				};
			break;

		case WM_NOTIFY:
			switch( ((NMHDR *)lParam)->code )
				{
				case PSN_SETACTIVE:
					Internal_refreshStatus( hDlg );
					break;

				case PSN_QUERYCANCEL:
					return AD_cancel( hDlg );
				
				case PSN_KILLACTIVE:
					SetWindowLong( hDlg, DWL_MSGRESULT, FALSE );
					return (TRUE);

				case PSN_APPLY:
					return AD_apply( hDlg );

				default:
					return (FALSE);
				};
			break;

		default:
			return (FALSE);
		};

	return (TRUE);
}

/*____________________________________________________________________________*\
 *
 Description:
	Definitions and global values
\*____________________________________________________________________________*/
DLGPROC fnStatus = (DLGPROC)DialogProc;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -