MonitorWnd.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:11k
源码类别:
P2P编程
开发平台:
Visual C++
- // MonitorWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "testbt.h"
- #include "MonitorWnd.h"
- #include "download.h"
- #include "FileDB.h"
- #include "FileTorrent.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- const CString CMonitorWnd::m_strSection = "Gui";
- const CString CMonitorWnd::m_strStateKey = "splitter";
- /////////////////////////////////////////////////////////////////////////////
- // CMonitorWnd
- CMonitorWnd::CMonitorWnd()
- {
- m_pFileDBItem = 0;
- m_pSetupRegister = 0;
- m_bDeleteing = false;
- m_lSplitPercent = 200; // (n%100)
- }
- CMonitorWnd::~CMonitorWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CMonitorWnd, CWnd)
- //{{AFX_MSG_MAP(CMonitorWnd)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_DESTROY()
- //}}AFX_MSG_MAP
- ON_NOTIFY(TVN_SELCHANGED, 10568, OnNodeSelect)
- ON_MESSAGE(WM_SPLITTER_MOVED, OnSplitterMoved)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMonitorWnd message handlers
- void CMonitorWnd::LoadState()
- {
- m_lSplitPercent = AfxGetApp()->GetProfileInt(m_strSection, m_strStateKey, m_lSplitPercent);
- }
- void CMonitorWnd::SaveState()
- {
- AfxGetApp()->WriteProfileInt(m_strSection, m_strStateKey, m_lSplitPercent);
- }
- void CMonitorWnd::SetCateGoryBarWnd(CCategoryBarWnd* pCategoryBarWnd)
- {
- m_MonitorDetailWnd.SetCateGoryBarWnd(pCategoryBarWnd);
- }
- BOOL CMonitorWnd::CreateEx(CWnd *pParent, CRect &rc, long lParam, CSetupRegister* pSetupRegister)
- {
- m_pSetupRegister = pSetupRegister;
- return Create(0, 0, WS_VISIBLE|WS_CHILD|WS_BORDER, rc, pParent, 21321);
- }
- int CMonitorWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- // Create the MonitorDetailWnd.
- m_MonitorDetailWnd.CreateEx(this, CRect(0, 0, 100, 100), 32645, 0, m_pSetupRegister);
- // Create the treeCategory.
- m_treeCategory.Create(WS_VISIBLE|WS_CHILD|WS_BORDER|TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES|TVS_SHOWSELALWAYS,
- CRect(0, 0, 100, 100), this, 10568);
- //
- // Create BitmapList
- //
- // m_ctlImage.Create(IDB_IMAGELIST,16,0,RGB(255,0,255));
- // m_ctlImage.SetBkColor(GetSysColor(COLOR_WINDOW));
- HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
- MAKEINTRESOURCE(IDB_IMAGELIST),
- IMAGE_BITMAP,
- 0,0, // cx,cy
- LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
- CBitmap bm;
- bm.Attach(hbm);
- m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 4, 4);
- m_ctlImage.SetBkColor(::GetSysColor(COLOR_WINDOW));
- m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
- m_treeCategory.SetImageList(&m_ctlImage);
- // add cursor.
- CTreeCursor curRoot = m_treeCategory.GetRootItem();
- m_curInfomation = curRoot.AddTail("详细信息", IID_INFOMATION);
- m_curSubFiles = curRoot.AddTail("包含文件", IID_SUBFILES);
- // m_curStatics = curRoot.AddTail("运行信息", IID_RUN_STATICS);
- m_curAllPeers = curRoot.AddTail("Peers", IID_ALL_PEERS);
- m_curGraph = curRoot.AddTail("图形", IID_RUN_GRAPH);
- m_curLog = curRoot.AddTail("日志", IID_LOG);
- // init
- m_MonitorDetailWnd.ShowNode(m_curInfomation, false);
- // Create splitter.
- CRect rect(0,0,0,0);
- m_wndHSplitterBar.Create(WS_CHILD|WS_BORDER|WS_DLGFRAME|WS_VISIBLE,rect,this, 112998, FALSE);
- m_wndHSplitterBar.SetPanes(&m_treeCategory, &m_MonitorDetailWnd);
- LoadState();
- return 0;
- }
- void CMonitorWnd::OnSize(UINT nType, int cx, int cy)
- {
- long lTreeCX = min(cx, m_lSplitPercent); // cx*m_lSplitPercent/100;
- long lMonitorCX = max((cx - lTreeCX), 0); //cx*(100 - m_lSplitPercent)/100;
- m_treeCategory.MoveWindow(CRect(0, 0, lTreeCX, cy));
- m_MonitorDetailWnd.MoveWindow(CRect(lMonitorCX, 0, cx, cy));
- m_wndHSplitterBar.SetPanes(&m_treeCategory,&m_MonitorDetailWnd);
- CWnd::OnSize(nType, cx, cy);
- }
- void CMonitorWnd::OnSplitterMoved(long lParam, long wParam)
- {
- CRect rc;
- m_treeCategory.GetClientRect(rc);
- long lTreeCX = rc.Width();
- m_lSplitPercent = lTreeCX;
- }
- void CMonitorWnd::OnNodeSelect(NMHDR *pNotifyStruct,LRESULT *result)
- {
- if (m_bDeleteing)
- return;
- CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
- if (!curSelected) return;
- m_MonitorDetailWnd.ShowNode(curSelected, true);
- }
- void CMonitorWnd::OnDestroy()
- {
- m_bDeleteing = true;
- while (m_curAllPeers.HasChildren())
- {
- CTreeCursor curNext;
- CTreeCursor curChild = m_curAllPeers.GetChild();
- while (curChild)
- {
- CAddrPort* pData = (CAddrPort*)curChild.GetData();
- assert(pData);
- delete pData;
- curNext = curChild.GetNext(TVGN_NEXT);
- curChild.Delete();
- curChild = curNext;
- }
- }
- m_bDeleteing = false;
- SaveState();
- CWnd::OnDestroy();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMonitorWnd function
- void CMonitorWnd::Refresh()
- {
- if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload)
- return;
- // refresh peers in tree.
- // RefreshPeers();
- // show the infomation for each category.
- CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
- if (!curSelected)
- curSelected = m_curInfomation;
- m_MonitorDetailWnd.ShowNode(curSelected, false);
- }
- void CMonitorWnd::OnContextQuery(CMenu& menubar)
- {
- menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_INFORMATION, "详细信息");
- menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_SUBFILES, "包含文件");
- menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_PEERS, "Peers");
- menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_RUNSTATICS, "图形");
- menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_LOGS, "日志");
- menubar.EnableMenuItem(ID_MENUITEM_MONITOR_INFORMATION, MF_BYCOMMAND | MF_ENABLED);
- menubar.EnableMenuItem(ID_MENUITEM_MONITOR_SUBFILES, MF_BYCOMMAND | MF_ENABLED);
- menubar.EnableMenuItem(ID_MENUITEM_MONITOR_RUNSTATICS, MF_BYCOMMAND | MF_ENABLED);
- menubar.EnableMenuItem(ID_MENUITEM_MONITOR_PEERS, MF_BYCOMMAND | MF_ENABLED);
- menubar.EnableMenuItem(ID_MENUITEM_MONITOR_LOGS, MF_BYCOMMAND | MF_ENABLED);
- }
- void CMonitorWnd::OnShowNode(UINT uID)
- {
- long lShowNode = uID - ID_MENUITEM_MONITOR_INFORMATION;
- CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
- // if called by menu monitor command, show the required node.
- switch (lShowNode)
- {
- case 0:
- curSelected = m_curInfomation;
- break;
- case 1:
- curSelected = m_curSubFiles;
- break;
- case 2:
- curSelected = m_curAllPeers;
- break;
- case 3:
- curSelected = m_curGraph;
- break;
- case 4:
- curSelected = m_curLog;
- break;
- }
- m_bDeleteing = true;
- curSelected.Select();
- m_MonitorDetailWnd.ShowNode(curSelected, true);
- m_bDeleteing = false;
- }
- void CMonitorWnd::Monitor(CFileDBItem* pFileDBItem, long lShowNode)
- {
- m_pFileDBItem = pFileDBItem;
- m_MonitorDetailWnd.Monitor(pFileDBItem);
- // show peers in tree.
- // ShowPeers();
- // show the infomation for node category.
- m_bDeleteing = true;
- CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
- if (!curSelected)
- curSelected = m_curInfomation;
- if (lShowNode != -1)
- {
- // if not visible monitorbar, show it.
- CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
- if (!pMainFrame)
- {
- assert(false);
- return;
- }
- pMainFrame->ShowControlBar(&pMainFrame->m_MonitorBar, true, false);
- // if called by menu monitor command, show the required node.
- switch (lShowNode)
- {
- case 0:
- curSelected = m_curInfomation;
- break;
- case 1:
- curSelected = m_curSubFiles;
- break;
- case 2:
- curSelected = m_curAllPeers;
- break;
- case 3:
- curSelected = m_curGraph;
- break;
- case 4:
- curSelected = m_curLog;
- break;
- }
- }
- curSelected.Select();
- // m_curInfomation.Expand();
- // m_curStatics.Expand();
- m_MonitorDetailWnd.ShowNode(curSelected, true);
- m_bDeleteing = false;
- }
- void CMonitorWnd::ShowPeers()
- {
- // clear
- m_bDeleteing = true;
- if (m_curAllPeers.HasChildren())
- {
- CTreeCursor curNext;
- CTreeCursor curChild = m_curAllPeers.GetChild();
- while (curChild)
- {
- CAddrPort* pData = (CAddrPort*)curChild.GetData();
- if (pData)
- delete pData;
- else
- assert(false);
- curNext = curChild.GetNext(TVGN_NEXT);
- curChild.Delete();
- curChild = curNext;
- }
- }
- m_bDeleteing = false;
- // show peers.
- if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload || !m_pFileDBItem->m_pDownload->IsDownloading()) return;
- vector<CConncetionData> v = m_pFileDBItem->m_pDownload->GetConnections();
- for (int i=0; i<v.size(); i++)
- {
- in_addr inaddress;
- inaddress.S_un.S_addr = v[i].m_lAddr;
- char szText[100] = {0};
- sprintf(szText, "%s:%d(%d)",
- inet_ntoa(inaddress),
- ntohs(v[i].m_sPort),
- v[i].m_lDownRate);
- CTreeCursor curItem = m_curAllPeers.AddTail(szText, IID_PEER);
- CAddrPort* pData = new CAddrPort(v[i].m_lAddr, v[i].m_sPort);
- curItem.SetData((long)pData);
- }
- m_curAllPeers.Expand();
- }
- void CMonitorWnd::RefreshPeers()
- {
- if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload || !m_pFileDBItem->m_pDownload->IsDownloading())
- return;
- m_bDeleteing = true;
- vector<CConncetionData> vConnections = m_pFileDBItem->m_pDownload->GetConnections();
- if (m_curAllPeers.HasChildren())
- {
- CTreeCursor curNext;
- CTreeCursor curChild = m_curAllPeers.GetChild();
- while (curChild)
- {
- CAddrPort* pData = (CAddrPort*)curChild.GetData();
- if (!pData)
- assert(false);
- bool bFind = false;
- for (int i=0; i<vConnections.size(); i++)
- {
- if (vConnections[i].m_lAddr == pData->m_lAddr &&
- vConnections[i].m_sPort == pData->m_sPort)
- {
- vConnections.erase(vConnections.begin() + i);
- bFind = true;
- break;
- }
- }
- curNext = curChild.GetNext(TVGN_NEXT);
- if (!bFind)
- {
- delete pData;
- curChild.Delete();
- }
- curChild = curNext;
- }
- }
- for (int i=0; i<vConnections.size(); i++)
- {
- in_addr inaddress;
- inaddress.S_un.S_addr = vConnections[i].m_lAddr;
- char szText[100] = {0};
- sprintf(szText, "%s:%d(%d)",
- inet_ntoa(inaddress),
- ntohs(vConnections[i].m_sPort),
- vConnections[i].m_lDownRate);
- CTreeCursor curItem = m_curAllPeers.AddTail(szText, IID_PEER);
- CAddrPort* pData = new CAddrPort(vConnections[i].m_lAddr, vConnections[i].m_sPort);
- curItem.SetData((long)pData);
- }
- m_bDeleteing = false;
- }
- void CMonitorWnd::SetFocusCategory()
- {
- m_treeCategory.SetFocus();
- }
- void CMonitorWnd::SwitchMonitorCategory(bool bNext)
- {
- CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
- if (!curSelected)
- {
- m_curInfomation.Select();
- return;
- }
- if (curSelected == m_curInfomation)
- {
- if (bNext)
- curSelected = m_curSubFiles;
- else
- curSelected = m_curLog;
- }
- else if (curSelected == m_curSubFiles)
- {
- if (bNext)
- curSelected = m_curAllPeers;
- else
- curSelected = m_curInfomation;
- }
- else if (curSelected == m_curAllPeers)
- {
- if (bNext)
- curSelected = m_curGraph;
- else
- curSelected = m_curSubFiles;
- }
- else if (curSelected == m_curGraph)
- {
- if (bNext)
- curSelected = m_curLog;
- else
- curSelected = m_curAllPeers;
- }
- else if (curSelected == m_curLog)
- {
- if (bNext)
- curSelected = m_curInfomation;
- else
- curSelected = m_curGraph;
- }
- curSelected.Select();
- }