home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************
- * SERVER.CXX
- * (c) 1992-1994 STAR DIVISION
- *******************************************************************/
-
- #include <time.h>
- #include <svwin.h>
- #include <sv.hxx>
- #include "svdde.hxx"
-
- // --- class ServerApp ---------------------------------------------
-
- class ServerApp : public Application
- {
- public:
- void Main( int, char* [] );
- };
-
- // --- class ServerWindow ------------------------------------------
-
- class ServerWindow : public WorkWindow
- {
- Timer aTimer;
- String aDate;
- String aTime;
- String aMem;
- DdeData aData;
-
- void GetValues();
-
- public:
- ServerWindow();
-
- void StartTimer() { aTimer.Start(); }
- DdeTopic* pDateTopic;
- DdeTopic* pTimeTopic;
- DdeTopic* pMemTopic;
-
- DdeData* GetTime( ULONG );
- DdeData* GetDate( ULONG );
- DdeData* GetMemory( ULONG );
-
- void Tick( Timer* );
- void Paint( const Rectangle& );
- };
-
- // --- Instance of Class Application -------------------------------
-
- ServerApp aMyApp;
-
- // --- ServerApp::Main() -------------------------------------------
-
- void ServerApp::Main( int, char* [] )
- {
- ServerWindow aWin;
- aWin.SetText( String( "DDE-Server TIMES" ) );
-
- // initialize service TIME
- DdeService aTimes( String( "TIMES" ) );
- if ( aTimes.GetError() )
- {
- ErrorBox( NULL, WinBits( WB_OK ),
- String( "Service TIMES not activated !" ) ).Execute();
- return;
- }
-
- // initialize topics for TIMES
- DdeTopic aDate( String( "DATE" ) );
- DdeTopic aTime( String( "TIME" ) );
- aDate.ChangeGetHdl( LINK( &aWin, ServerWindow, GetDate ) );
- aTime.ChangeGetHdl( LINK( &aWin, ServerWindow, GetTime ) );
-
- // insert item NOW
- aDate.AddItem( DdeItem( "NOW" ) );
- aTime.AddItem( DdeItem( "NOW" ) );
-
- // add topics to list of services
- aTimes.AddTopic( aDate );
- aTimes.AddTopic( aTime );
-
- // initialize service MEMORY
- DdeService aMemory( String( "MEMORY" ) );
- if ( aMemory.GetError() )
- {
- ErrorBox( NULL, WinBits( WB_OK ),
- String( "Service MEMORY not activated !" ) ).Execute();
- return;
- }
-
- // initialize topics for MEMORY
- DdeTopic aMem( String( "MEMORY" ) );
- aMem.ChangeGetHdl( LINK( &aWin, ServerWindow, GetMemory ) );
-
- // add item FREEMEM
- aMem.AddItem( DdeItem( "FREEMEM" ) );
-
- // add topic to list of services
- aMemory.AddTopic( aMem );
-
- aWin.pDateTopic = &aDate;
- aWin.pTimeTopic = &aTime;
- aWin.pMemTopic = &aMem;
-
- aWin.Minimize();
- aWin.Show();
- aWin.StartTimer();
-
- Execute();
- }
-
- // --- ServerWindow::ServerWindow() --------------------------------
-
- ServerWindow::ServerWindow() :
- WorkWindow( NULL, WinBits( WB_APP | WB_STDWORK ) )
- {
- aTimer.ChangeTimeoutHdl( LINK( this, ServerWindow, Tick ) );
- aTimer.ChangeTimeout( 1000 );
- }
-
- // --- ServerWindow::GetDate() -----------------------------------
-
- DdeData* ServerWindow::GetDate( ULONG )
- {
- GetValues();
- String s( aDate );
- s += "\r\n";
- aData = DdeData( s );
- return &aData;
- }
-
- // --- ServerWindow::GetTime() -----------------------------------
-
- DdeData* ServerWindow::GetTime( ULONG )
- {
- GetValues();
- String s( aTime );
- s += "\r\n";
- aData = DdeData( s );
- return &aData;
- }
-
- // --- ServerWindow::GetMemory() ---------------------------------
-
- DdeData* ServerWindow::GetMemory( ULONG )
- {
- GetValues();
- String s( aMem );
- s += "\r\n";
- aData = DdeData( s );
- return &aData;
- }
-
- // --- ServerWindow::GetValues() ---------------------------------
-
- // Data starts with one blank at the beginning, otherwise EXCEL would
- // interpret it as numbers.
-
- void ServerWindow::GetValues()
- {
- char buf [20];
-
- // get actual date
- tm* t;
- time_t curtime = time( NULL );
- t = localtime( &curtime );
- wsprintf( buf, " %02d.%02d.%02d", t->tm_mday, t->tm_mon+1, t->tm_year );
- aDate = String( buf );
-
- // get actual time
- wsprintf( buf, " %02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec );
- aTime = String( buf );
-
- // get free memory
- wsprintf( buf, "%ld", GetFreeSpace( 0 ) );
- aMem = String( buf );
- }
-
- // --- ServerWindow::Tick() ----------------------------------------
-
- void ServerWindow::Tick( Timer* )
- {
- GetValues();
-
- // notify clients
- pDateTopic->NotifyClient( String( "NOW" ) );
- pTimeTopic->NotifyClient( String( "NOW" ) );
- pMemTopic->NotifyClient( String( "FREEMEM" ) );
-
- if ( !IsMinimized() )
- Invalidate();
- aTimer.Start();
- }
-
- // --- ServerWindow::Paint() -------------------------------------
-
- void ServerWindow::Paint( const Rectangle& )
- {
- String aText( aDate );
- aText += " ";
- aText += aTime;
- aText += ", Mem = ";
- aText += aMem;
-
- Size aTextSize = GetTextSize( aText );
- Size aWinSize = GetOutputSize();
-
- DrawText( Point( (aWinSize.Width() - aTextSize.Width() ) / 2,
- (aWinSize.Height() - aTextSize.Height() ) / 2 ),
- aText );
- }
-