home *** CD-ROM | disk | FTP | other *** search
/ PC Direkt 1995 March / PCD_395.iso / starview / pm2csci / german / server.cx_ / SERVER.CXX
Encoding:
C/C++ Source or Header  |  1994-01-21  |  5.2 KB  |  211 lines

  1. /*******************************************************************
  2. *  SERVER.CXX
  3. *  (c) 1992-1994 STAR DIVISION
  4. *******************************************************************/
  5.  
  6. #include <time.h>
  7. #include <svwin.h>
  8. #include <sv.hxx>
  9. #include "svdde.hxx"
  10.  
  11. // --- class ServerApp ---------------------------------------------
  12.  
  13. class ServerApp : public Application
  14. {
  15. public:
  16.     void Main( int, char* [] );
  17. };
  18.  
  19. // --- class ServerWindow ------------------------------------------
  20.  
  21. class ServerWindow : public WorkWindow
  22. {
  23.     Timer       aTimer;
  24.     String      aDate;
  25.     String      aTime;
  26.     String      aMem;
  27.     DdeData     aData;
  28.  
  29.     void        GetValues();
  30.  
  31. public:
  32.                 ServerWindow();
  33.  
  34.     void        StartTimer() { aTimer.Start(); }
  35.     DdeTopic*   pDateTopic;
  36.     DdeTopic*   pTimeTopic;
  37.     DdeTopic*   pMemTopic;
  38.  
  39.     DdeData*    GetTime( ULONG );
  40.     DdeData*    GetDate( ULONG );
  41.     DdeData*    GetMemory( ULONG );
  42.  
  43.     void        Tick( Timer* );
  44.     void        Paint( const Rectangle&  );
  45. };
  46.  
  47. // --- Instance of Class Application -------------------------------
  48.  
  49. ServerApp aMyApp;
  50.  
  51. // --- ServerApp::Main() -------------------------------------------
  52.  
  53. void ServerApp::Main( int, char* [] )
  54. {
  55.     ServerWindow aWin;
  56.     aWin.SetText( String( "DDE-Server TIMES" ) );
  57.  
  58.     // initialize service TIME
  59.     DdeService aTimes( String( "TIMES" ) );
  60.     if ( aTimes.GetError() )
  61.     {
  62.         ErrorBox( NULL, WinBits( WB_OK ),
  63.                   String( "Service TIMES not activated !" ) ).Execute();
  64.         return;
  65.     }
  66.  
  67.     // initialize topics for TIMES
  68.     DdeTopic aDate( String( "DATE" ) );
  69.     DdeTopic aTime( String( "TIME" ) );
  70.     aDate.ChangeGetHdl( LINK( &aWin, ServerWindow, GetDate ) );
  71.     aTime.ChangeGetHdl( LINK( &aWin, ServerWindow, GetTime ) );
  72.  
  73.     // insert item NOW
  74.     aDate.AddItem( DdeItem( "NOW" ) );
  75.     aTime.AddItem( DdeItem( "NOW" ) );
  76.  
  77.     // add topics to list of services
  78.     aTimes.AddTopic( aDate );
  79.     aTimes.AddTopic( aTime );
  80.  
  81.     // initialize service MEMORY
  82.     DdeService aMemory( String( "MEMORY" ) );
  83.     if ( aMemory.GetError() )
  84.     {
  85.         ErrorBox( NULL, WinBits( WB_OK ),
  86.                   String( "Service MEMORY not activated !" ) ).Execute();
  87.         return;
  88.     }
  89.  
  90.     // initialize topics for MEMORY
  91.     DdeTopic aMem( String( "MEMORY" ) );
  92.     aMem.ChangeGetHdl( LINK( &aWin, ServerWindow, GetMemory ) );
  93.  
  94.     // add item FREEMEM
  95.     aMem.AddItem( DdeItem( "FREEMEM" ) );
  96.  
  97.     // add topic to list of services
  98.     aMemory.AddTopic( aMem );
  99.  
  100.     aWin.pDateTopic = &aDate;
  101.     aWin.pTimeTopic = &aTime;
  102.     aWin.pMemTopic  = &aMem;
  103.  
  104.     aWin.Minimize();
  105.     aWin.Show();
  106.     aWin.StartTimer();
  107.  
  108.     Execute();
  109. }
  110.  
  111. // --- ServerWindow::ServerWindow() --------------------------------
  112.  
  113. ServerWindow::ServerWindow() :
  114.                 WorkWindow( NULL, WinBits( WB_APP | WB_STDWORK ) )
  115. {
  116.     aTimer.ChangeTimeoutHdl( LINK( this, ServerWindow, Tick ) );
  117.     aTimer.ChangeTimeout( 1000 );
  118. }
  119.  
  120. // --- ServerWindow::GetDate() -----------------------------------
  121.  
  122. DdeData* ServerWindow::GetDate( ULONG )
  123. {
  124.     GetValues();
  125.     String s( aDate );
  126.     s += "\r\n";
  127.     aData = DdeData( s );
  128.     return &aData;
  129. }
  130.  
  131. // --- ServerWindow::GetTime() -----------------------------------
  132.  
  133. DdeData* ServerWindow::GetTime( ULONG )
  134. {
  135.     GetValues();
  136.     String s( aTime );
  137.     s += "\r\n";
  138.     aData = DdeData( s );
  139.     return &aData;
  140. }
  141.  
  142. // --- ServerWindow::GetMemory() ---------------------------------
  143.  
  144. DdeData* ServerWindow::GetMemory( ULONG )
  145. {
  146.     GetValues();
  147.     String s( aMem );
  148.     s += "\r\n";
  149.     aData = DdeData( s );
  150.     return &aData;
  151. }
  152.  
  153. // --- ServerWindow::GetValues() ---------------------------------
  154.  
  155. // Data starts with one blank at the beginning, otherwise EXCEL would
  156. // interpret it as numbers.
  157.  
  158. void ServerWindow::GetValues()
  159. {
  160.     char buf [20];
  161.  
  162.     // get actual date
  163.     tm* t;
  164.     time_t curtime = time( NULL );
  165.     t = localtime( &curtime );
  166.     wsprintf( buf, " %02d.%02d.%02d", t->tm_mday, t->tm_mon+1, t->tm_year );
  167.     aDate = String( buf );
  168.  
  169.     // get actual time
  170.     wsprintf( buf, " %02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec );
  171.     aTime = String( buf );
  172.  
  173.     // get free memory
  174.     wsprintf( buf, "%ld", GetFreeSpace( 0 ) );
  175.     aMem = String( buf );
  176. }
  177.  
  178. // --- ServerWindow::Tick() ----------------------------------------
  179.  
  180. void ServerWindow::Tick( Timer* )
  181. {
  182.     GetValues();
  183.  
  184.     // notify clients
  185.     pDateTopic->NotifyClient( String( "NOW" ) );
  186.     pTimeTopic->NotifyClient( String( "NOW" ) );
  187.     pMemTopic->NotifyClient( String( "FREEMEM" ) );
  188.  
  189.     if ( !IsMinimized() )
  190.         Invalidate();
  191.     aTimer.Start();
  192. }
  193.  
  194. // --- ServerWindow::Paint() -------------------------------------
  195.  
  196. void ServerWindow::Paint( const Rectangle& )
  197. {
  198.     String aText( aDate );
  199.     aText += " ";
  200.     aText += aTime;
  201.     aText += ", Mem = ";
  202.     aText += aMem;
  203.  
  204.     Size    aTextSize = GetTextSize( aText );
  205.     Size    aWinSize  = GetOutputSize();
  206.  
  207.     DrawText( Point( (aWinSize.Width()  - aTextSize.Width() )  / 2,
  208.                      (aWinSize.Height() - aTextSize.Height() ) / 2 ),
  209.                      aText );
  210. }
  211.