home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / NETTOOLS.ZIP / MainFrm.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-12-30  |  12.8 KB  |  550 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "afxmt.h"
  6. #include "nettools.h"
  7. #include "MainFrm.h"
  8. #include "PingDlg.h"
  9. #include "NetstatDlg.h"
  10. //#include "NbstatDlg.h"
  11. //#include "TracertDlg.h"
  12.  
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CMainFrame
  22.  
  23. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  24.  
  25. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  26.     //{{AFX_MSG_MAP(CMainFrame)
  27.     ON_WM_CREATE()
  28.     ON_COMMAND(ID_COMMANDS_PING, OnCommandsPing)
  29.     ON_COMMAND(ID_COMMANDS_NETSTAT, OnCommandsNetstat)
  30.     ON_COMMAND(ID_FILE_NEW, OnFileNew)
  31.     ON_UPDATE_COMMAND_UI(ID_COMMANDS_PING, OnUpdateCommandsPing)
  32.     ON_COMMAND(ID_COMMANDS_STOPPING, OnCommandsStopPing)
  33.     ON_UPDATE_COMMAND_UI(ID_COMMANDS_STOPPING, OnUpdateCommandsStopPing)
  34.     ON_UPDATE_COMMAND_UI(ID_COMMANDS_NETSTAT, OnUpdateCommandsNetstat)
  35.     ON_UPDATE_COMMAND_UI(ID_COMMANDS_STOPSTAT, OnUpdateCommandsStopstat)
  36.     ON_COMMAND(ID_COMMANDS_STOPSTAT, OnCommandsStopstat)
  37.     ON_WM_CLOSE()
  38.     //}}AFX_MSG_MAP
  39.     ON_MESSAGE(WM_USER_PING_DONE,OnPingDone)
  40.     ON_MESSAGE(WM_USER_PING_PRINT,OnPingPrint)
  41.     ON_MESSAGE(WM_USER_INET_DONE,OnInetDone)
  42.     ON_MESSAGE(WM_USER_INET_PRINT,OnInetPrint)
  43.     ON_MESSAGE(WM_USER_INET_TABS,OnInetTabs)
  44. END_MESSAGE_MAP()
  45.  
  46. static UINT indicators[] =
  47. {
  48.     ID_SEPARATOR,           // status line indicator
  49.     ID_INDICATOR_CAPS,
  50.     ID_INDICATOR_NUM,
  51.     ID_INDICATOR_SCRL,
  52. };
  53.  
  54.  
  55. CCriticalSection m_CritWinLb;
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CMainFrame construction/destruction
  58.  
  59. CMainFrame::CMainFrame()
  60. {
  61.     
  62.  
  63.     memset(m_PingThreadInfo.hostname,0,sizeof(m_PingThreadInfo.hostname));
  64.     
  65.  
  66.     m_PingThreadInfo.m_hEventStartPing =
  67.         CreateEvent(NULL, FALSE, FALSE, NULL);
  68.     
  69.  
  70.     m_PingThreadInfo.m_hEventPingDone =
  71.         CreateEvent(NULL, TRUE, TRUE, NULL);
  72.     
  73.     m_PingThreadInfo.m_hEventKillPing =
  74.         CreateEvent(NULL, FALSE, FALSE, NULL);
  75.     
  76.     m_PingThreadInfo.m_hEventPingDead = 
  77.         CreateEvent(NULL, FALSE, FALSE, NULL);
  78.     
  79.     
  80.  
  81.     m_pPingWorkerThread = NULL;
  82.     m_pPingWorkerThread = new CPingThread(&m_PingThreadInfo);
  83.     if (!m_pPingWorkerThread->Start())
  84.     {
  85.         AfxMessageBox("Cannot Start Ping");
  86.         delete m_pPingWorkerThread;
  87.         m_pPingWorkerThread = NULL;
  88.         return;
  89.     }
  90.     m_PingStop_ButtEnabled = FALSE;
  91.     m_Ping_ButtEnabled = TRUE;
  92.  
  93.  
  94.     //
  95.     // Inet Thread
  96.     //
  97.  
  98.     m_InetThreadInfo.m_hEventStartInet =
  99.         CreateEvent(NULL, FALSE, FALSE, NULL);
  100.     
  101.  
  102.     m_InetThreadInfo.m_hEventInetDone =
  103.         CreateEvent(NULL, TRUE, TRUE, NULL);
  104.     
  105.     m_InetThreadInfo.m_hEventKillInet =
  106.         CreateEvent(NULL, FALSE, FALSE, NULL);
  107.     
  108.     m_InetThreadInfo.m_hEventInetDead = 
  109.         CreateEvent(NULL, FALSE, FALSE, NULL);
  110.     
  111.     
  112.     m_pInetWorkerThread = NULL;
  113.     m_pInetWorkerThread = new CInetThread(&m_InetThreadInfo);
  114.     if (!m_pInetWorkerThread->Start())
  115.     {
  116.         AfxMessageBox("Cannot Start Inet");
  117.         delete m_pInetWorkerThread;
  118.         m_pInetWorkerThread = NULL;
  119.         return;
  120.     }
  121.     m_InetStop_ButtEnabled = FALSE;
  122.     m_Inet_ButtEnabled = TRUE;
  123.     //
  124.     // end
  125.     //
  126.     
  127. }
  128.  
  129. CMainFrame::~CMainFrame()
  130. {
  131.  
  132.     //DWORD dwInetExitCode;
  133.     if (m_pInetWorkerThread != NULL)
  134.         //&&
  135.         //GetExitCodeThread(m_pInetWorkerThread->m_hThread,
  136.         //&dwInetExitCode) &&
  137.         //dwInetExitCode == STILL_ACTIVE)
  138.     {
  139.         
  140.         m_pInetWorkerThread->CleanUp();  //stop icmp/tcp
  141.             
  142.         // Kill the worker thread by setting the "kill thread" event.
  143.         // See comment in OnKillWorkerThread for explanation of the sequence
  144.         // of the "kill thread" and "start recalc" events.
  145.         SetEvent(m_InetThreadInfo.m_hEventKillInet);
  146.         SetEvent(m_InetThreadInfo.m_hEventStartInet);
  147.         WaitForSingleObject(m_InetThreadInfo.m_hEventInetDead,
  148.                             INFINITE);
  149.         
  150.     }
  151.         // In this application, the document owns the worker thread.
  152.     // The document's destructor is responsible for killing the active worker
  153.     // thread.
  154.  
  155.     // It's a good idea to wait for the worker thread to notify via a
  156.     // "thread killed" event that it has killed itself. Otherwise, in the case
  157.     // where the app is terminating, is possible (even if unlikely) that it
  158.     // will detect a memory leak of the CWinThread object before the
  159.     // CWinThread object has had a chance to auto-delete itself.
  160.  
  161.     //DWORD dwPingExitCode;
  162.     if (m_pPingWorkerThread != NULL)
  163.         //&&
  164.         //GetExitCodeThread(m_pPingWorkerThread->m_hThread,
  165.         //&dwPingExitCode) &&
  166.         //dwPingExitCode == STILL_ACTIVE)
  167.     {
  168.         m_pPingWorkerThread->CleanUp();  //stop icmp/tcp
  169.     
  170.         // Kill the worker thread by setting the "kill thread" event.
  171.         // See comment in OnKillWorkerThread for explanation of the sequence
  172.         // of the "kill thread" and "start recalc" events.
  173.         SetEvent(m_PingThreadInfo.m_hEventKillPing);
  174.         SetEvent(m_PingThreadInfo.m_hEventStartPing);
  175.         WaitForSingleObject(m_PingThreadInfo.m_hEventPingDead,
  176.                             INFINITE);
  177.         
  178.     }
  179.     delete m_pPingWorkerThread;
  180.     delete m_pInetWorkerThread;
  181.     //
  182.     // terminate inet thread
  183.     //
  184.     
  185. }
  186.  
  187. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  188. {
  189.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  190.         return -1;
  191.     
  192.     if (!m_wndToolBar.Create(this) ||
  193.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  194.     {
  195.         TRACE0("Failed to create toolbar\n");
  196.         return -1;      // fail to create
  197.     }
  198.  
  199.     if (!m_wndStatusBar.Create(this) ||
  200.         !m_wndStatusBar.SetIndicators(indicators,
  201.           sizeof(indicators)/sizeof(UINT)))
  202.     {
  203.         TRACE0("Failed to create status bar\n");
  204.         return -1;      // fail to create
  205.     }
  206.  
  207.     // TODO: Remove this if you don't want tool tips or a resizeable toolbar
  208.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  209.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  210.  
  211.     // TODO: Delete these three lines if you don't want the toolbar to
  212.     //  be dockable
  213.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  214.     EnableDocking(CBRS_ALIGN_ANY);
  215.     DockControlBar(&m_wndToolBar);
  216.     m_wndListBox.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL
  217.                         | LBS_USETABSTOPS | WS_HSCROLL,
  218.                         CRect(0,0,0,0),
  219.                         this,
  220.                         LISTBOX_WINDOW);
  221.  
  222.  
  223.     INT Tabs[7]  = {32,100,100,100,100,100,100};
  224.     m_wndListBox.SetTabStops(7,Tabs);
  225.     
  226.     
  227.     return 0;
  228. }
  229.  
  230. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  231. {
  232.     // TODO: Modify the Window class or styles here by modifying
  233.     //  the CREATESTRUCT cs
  234.  
  235.     return CFrameWnd::PreCreateWindow(cs);
  236. }
  237.  
  238. /////////////////////////////////////////////////////////////////////////////
  239. // CMainFrame diagnostics
  240.  
  241. #ifdef _DEBUG
  242. void CMainFrame::AssertValid() const
  243. {
  244.     CFrameWnd::AssertValid();
  245. }
  246.  
  247. void CMainFrame::Dump(CDumpContext& dc) const
  248. {
  249.     CFrameWnd::Dump(dc);
  250. }
  251.  
  252. #endif //_DEBUG
  253.  
  254. /////////////////////////////////////////////////////////////////////////////
  255. // CMainFrame message handlers
  256.  
  257. void CMainFrame::RecalcLayout(BOOL bNotify) 
  258. {
  259.     
  260.     
  261.     CFrameWnd::RecalcLayout(bNotify);
  262.     CRect rc;
  263.     RepositionBars(0,
  264.                     0xFFFF,
  265.                     LISTBOX_WINDOW,
  266.                     CWnd::reposQuery,
  267.                     &rc);
  268.     if (IsWindow(m_wndListBox.GetSafeHwnd()))
  269.     {
  270.         m_wndListBox.MoveWindow(&rc,FALSE);
  271.     }
  272.  
  273. }
  274.  
  275. void CMainFrame::OnCommandsPing() 
  276. {
  277.  
  278.     if (m_pPingWorkerThread == NULL)
  279.     {
  280.         AfxMessageBox("Ping Not Started");
  281.         return;
  282.     }
  283.     
  284.     CPingDlg pingdlg(this);
  285.     
  286.  
  287.     pingdlg.m_ping_hostname = m_save_hostname;
  288.     m_PingThreadInfo.opt_rrdata.RemoveAll();
  289.  
  290.     pingdlg.m_lpPingParms = &m_PingThreadInfo;
  291.  
  292.     if (pingdlg.DoModal() == IDOK)
  293.     {
  294.         
  295.         m_save_hostname = pingdlg.m_ping_hostname;
  296.         
  297.         // The events are initially set or reset in the CreateEvent call;
  298.         // but they may be left in an improperly initialized state if
  299.         // a worker thread has been previously started and then prematurely
  300.         // killed.  Set/reset the events to the proper initial state.
  301.         // Set the "start recalc" event last, since it is the event the
  302.         // triggers the starting of the worker thread recalculation.
  303.         // OnPingDone() will be called by the frame when the 
  304.         //thread sends a WM_USER_PING_DONE message.
  305.         
  306.         m_Ping_ButtEnabled = FALSE; // turn off start
  307.         m_PingStop_ButtEnabled = TRUE; // turn on stop
  308.  
  309.         m_PingThreadInfo.m_hwndNotifyPingDone = 
  310.             AfxGetMainWnd()->m_hWnd;
  311.  
  312.         
  313.         ResetEvent(m_PingThreadInfo.m_hEventPingDone);
  314.         ResetEvent(m_PingThreadInfo.m_hEventKillPing);
  315.         ResetEvent(m_PingThreadInfo.m_hEventPingDead);
  316.         SetEvent(m_PingThreadInfo.m_hEventStartPing);
  317.         
  318.         
  319.     }
  320.     
  321. }
  322.  
  323. LRESULT CMainFrame::OnPingPrint(WPARAM wParam,LPARAM lParam) 
  324. {
  325.     CHAR *prtline;;
  326.     
  327.     
  328.     prtline = (CHAR*)lParam;
  329.     //CString prt;
  330.     //CPingThreadParmList *pPTPparm;
  331.     //CStringArray *pPTPparm;
  332.     //pPTPparm = (CPingThreadParmList*)lParam;
  333.     //pPTPparm = (CStringArray*)lParam;
  334.  
  335.     m_CritWinLb.Lock();
  336.     //for (int x = 0;x<pPTPparm->GetSize();x++)
  337.     //{
  338.     //    m_wndListBox.InsertString(-1,pPTPparm->GetAt(x));
  339.     //}
  340.     m_wndListBox.InsertString(-1,prtline);
  341.     m_CritWinLb.Unlock();
  342.  
  343.     //pPTPparm->RemoveAll();
  344.     return 0;
  345. }
  346.  
  347.  
  348.  
  349. LRESULT CMainFrame::OnPingDone(WPARAM wParam,LPARAM lParam) 
  350. {
  351.     m_CritWinLb.Lock();
  352.  
  353.     m_wndListBox.InsertString(-1,"- Ping Done");
  354.         
  355.     m_CritWinLb.Unlock();
  356.  
  357.     m_Ping_ButtEnabled = TRUE;
  358.     m_PingStop_ButtEnabled = FALSE;
  359.     return 0;
  360. }
  361.  
  362. LRESULT CMainFrame::OnInetDone(WPARAM wParam,LPARAM lParam) 
  363. {
  364.     m_CritWinLb.Lock();
  365.  
  366.     m_wndListBox.InsertString(-1,"- Inet Done");
  367.         
  368.     m_CritWinLb.Unlock();
  369.  
  370.     m_Inet_ButtEnabled = TRUE;
  371.     m_InetStop_ButtEnabled = FALSE;
  372.     return 0;
  373. }
  374.  
  375. LRESULT CMainFrame::OnInetTabs(WPARAM wParam,LPARAM lParam) 
  376. {
  377.     LPINT tabs;
  378.     tabs = (LPINT)lParam;
  379.  
  380.     m_CritWinLb.Lock();
  381.  
  382.     m_wndListBox.SetTabStops(7,tabs);
  383.         
  384.     m_CritWinLb.Unlock();
  385.  
  386.     
  387.     return 0;
  388. }
  389.  
  390. LRESULT CMainFrame::OnInetPrint(WPARAM wParam,LPARAM lParam) 
  391. {
  392.     CHAR *prtline;;
  393.     
  394.     
  395.     prtline = (CHAR*)lParam;
  396.     
  397.     
  398.     m_CritWinLb.Lock();
  399.  
  400.     m_wndListBox.InsertString(-1,prtline);
  401.         
  402.     m_CritWinLb.Unlock();
  403.  
  404.  
  405.     return 0;
  406. }
  407.  
  408.  
  409. void CMainFrame::OnCommandsNetstat() 
  410. {
  411.     if (m_pInetWorkerThread == NULL)
  412.     {
  413.         AfxMessageBox("Inet Not Started");
  414.         return;
  415.     }
  416.  
  417.     CNetstatDlg netstatdlg(this);
  418.     netstatdlg.m_lpInetParms = &m_InetThreadInfo;
  419.     if (netstatdlg.DoModal() == IDOK)
  420.     {
  421.         
  422.         // The events are initially set or reset in the CreateEvent call;
  423.         // but they may be left in an improperly initialized state if
  424.         // a worker thread has been previously started and then prematurely
  425.         // killed.  Set/reset the events to the proper initial state.
  426.         // Set the "start recalc" event last, since it is the event the
  427.         // triggers the starting of the worker thread recalculation.
  428.         // OnPingDone() will be called by the frame when the 
  429.         //thread sends a WM_USER_PING_DONE message.
  430.         
  431.         m_Inet_ButtEnabled = FALSE; // turn off start
  432.         m_InetStop_ButtEnabled = TRUE; // turn on stop
  433.  
  434.         m_InetThreadInfo.m_hwndNotifyInetDone = 
  435.             AfxGetMainWnd()->m_hWnd;
  436.  
  437.         //SetEvent(m_InetThreadInfo.m_hEventInetDone);
  438.         ResetEvent(m_InetThreadInfo.m_hEventInetDone);
  439.         ResetEvent(m_InetThreadInfo.m_hEventKillInet);
  440.         ResetEvent(m_InetThreadInfo.m_hEventInetDead);
  441.         SetEvent(m_InetThreadInfo.m_hEventStartInet);
  442.         
  443.         
  444.     }
  445.     
  446.     
  447.     
  448. }
  449.  
  450.  
  451. void CMainFrame::OnFileNew() 
  452. {
  453.     m_CritWinLb.Lock();
  454.     m_wndListBox.ResetContent();
  455.     m_CritWinLb.Unlock();
  456.     
  457.     
  458. }
  459.  
  460.  
  461.  
  462. void CMainFrame::OnUpdateCommandsPing(CCmdUI* pCmdUI) 
  463. {
  464.     // TODO: Add your command update UI handler code here
  465.     pCmdUI->Enable(m_Ping_ButtEnabled);
  466.     
  467. }
  468.  
  469. void CMainFrame::OnCommandsStopPing() 
  470. {
  471.     if (m_pPingWorkerThread == NULL)
  472.     {
  473.         AfxMessageBox("Ping Not Started");
  474.         return;
  475.     }
  476.     m_pPingWorkerThread->Kill();
  477.  
  478.     // TODO: Add your command handler code here
  479.     
  480. }
  481.  
  482. void CMainFrame::OnUpdateCommandsStopPing(CCmdUI* pCmdUI) 
  483. {
  484.     // TODO: Add your command update UI handler code here
  485.     pCmdUI->Enable(m_PingStop_ButtEnabled);
  486. }
  487.  
  488. void CMainFrame::OnUpdateCommandsNetstat(CCmdUI* pCmdUI) 
  489. {
  490.     // TODO: Add your command update UI handler code here
  491.     pCmdUI->Enable(m_Inet_ButtEnabled);
  492. }
  493.  
  494. void CMainFrame::OnUpdateCommandsStopstat(CCmdUI* pCmdUI) 
  495. {
  496.     // TODO: Add your command update UI handler code here
  497.     pCmdUI->Enable(m_InetStop_ButtEnabled);
  498. }
  499.  
  500. void CMainFrame::OnCommandsStopstat() 
  501. {
  502.     // TODO: Add your command handler code here
  503.     if (m_pInetWorkerThread == NULL)
  504.     {
  505.         AfxMessageBox("Inet Not Started");
  506.         return;
  507.     }
  508.     m_pInetWorkerThread->Kill();
  509. }
  510.  
  511.  
  512. void CMainFrame::OnClose() 
  513. {
  514.     // TODO: Add your message handler code here and/or call default
  515.     DWORD wrc;
  516.     CHAR line[128];
  517.  
  518.     if (m_pInetWorkerThread != NULL)
  519.     {
  520.         m_pInetWorkerThread->Kill();
  521.         wrc = WaitForSingleObject(m_InetThreadInfo.m_hEventInetDone,
  522.                             0);
  523.  
  524.         if (wrc != WAIT_OBJECT_0)
  525.         {
  526.             strcpy(line,"Inet Still Running! Being Terminated");
  527.             OnInetPrint(0,(LONG)line);
  528.             return;
  529.         }
  530.     }
  531.  
  532.     if (m_pPingWorkerThread != NULL)
  533.     {
  534.         m_pPingWorkerThread->Kill();
  535.         wrc = WaitForSingleObject(m_PingThreadInfo.m_hEventPingDone,
  536.                             0);
  537.  
  538.         if (wrc != WAIT_OBJECT_0)
  539.         {
  540.             strcpy(line,"Ping Still Running! Being Terminated");
  541.             OnPingPrint(0,(LONG)line);
  542.             return;
  543.         }
  544.     }
  545.  
  546.     
  547.     
  548.     CFrameWnd::OnClose();
  549. }
  550.