MonitorWnd.cpp
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:11k
源码类别:

P2P编程

开发平台:

Visual C++

  1. // MonitorWnd.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "testbt.h"
  5. #include "MonitorWnd.h"
  6. #include "download.h"
  7. #include "FileDB.h"
  8. #include "FileTorrent.h"
  9. #include "MainFrm.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. const CString CMonitorWnd::m_strSection = "Gui";
  16. const CString CMonitorWnd::m_strStateKey = "splitter";
  17. /////////////////////////////////////////////////////////////////////////////
  18. // CMonitorWnd
  19. CMonitorWnd::CMonitorWnd()
  20. {
  21. m_pFileDBItem = 0;
  22. m_pSetupRegister = 0;
  23. m_bDeleteing = false;
  24. m_lSplitPercent = 200; // (n%100)
  25. }
  26. CMonitorWnd::~CMonitorWnd()
  27. {
  28. }
  29. BEGIN_MESSAGE_MAP(CMonitorWnd, CWnd)
  30. //{{AFX_MSG_MAP(CMonitorWnd)
  31. ON_WM_CREATE()
  32. ON_WM_SIZE()
  33. ON_WM_DESTROY()
  34. //}}AFX_MSG_MAP
  35. ON_NOTIFY(TVN_SELCHANGED, 10568, OnNodeSelect)
  36. ON_MESSAGE(WM_SPLITTER_MOVED, OnSplitterMoved)
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CMonitorWnd message handlers
  40. void CMonitorWnd::LoadState()
  41. {
  42. m_lSplitPercent = AfxGetApp()->GetProfileInt(m_strSection, m_strStateKey, m_lSplitPercent);
  43. }
  44. void CMonitorWnd::SaveState()
  45. {
  46. AfxGetApp()->WriteProfileInt(m_strSection, m_strStateKey, m_lSplitPercent);
  47. }
  48. void CMonitorWnd::SetCateGoryBarWnd(CCategoryBarWnd* pCategoryBarWnd)
  49. {
  50. m_MonitorDetailWnd.SetCateGoryBarWnd(pCategoryBarWnd);
  51. }
  52. BOOL CMonitorWnd::CreateEx(CWnd *pParent, CRect &rc, long lParam, CSetupRegister* pSetupRegister)
  53. {
  54. m_pSetupRegister = pSetupRegister;
  55. return Create(0, 0, WS_VISIBLE|WS_CHILD|WS_BORDER, rc, pParent, 21321);
  56. }
  57. int CMonitorWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  58. {
  59. if (CWnd::OnCreate(lpCreateStruct) == -1)
  60. return -1;
  61. // Create the MonitorDetailWnd.
  62. m_MonitorDetailWnd.CreateEx(this, CRect(0, 0, 100, 100), 32645, 0, m_pSetupRegister);
  63. // Create the treeCategory.
  64. m_treeCategory.Create(WS_VISIBLE|WS_CHILD|WS_BORDER|TVS_HASBUTTONS | TVS_LINESATROOT | TVS_HASLINES|TVS_SHOWSELALWAYS,
  65. CRect(0, 0, 100, 100), this, 10568);
  66. //
  67. // Create BitmapList
  68. //
  69. // m_ctlImage.Create(IDB_IMAGELIST,16,0,RGB(255,0,255));
  70. // m_ctlImage.SetBkColor(GetSysColor(COLOR_WINDOW));
  71. HBITMAP hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
  72. MAKEINTRESOURCE(IDB_IMAGELIST),
  73. IMAGE_BITMAP,
  74. 0,0, // cx,cy
  75. LR_CREATEDIBSECTION); // | LR_LOADMAP3DCOLORS );
  76. CBitmap bm;
  77. bm.Attach(hbm);
  78. m_ctlImage.Create(16,15, ILC_COLOR8|ILC_MASK, 4, 4);
  79. m_ctlImage.SetBkColor(::GetSysColor(COLOR_WINDOW)); 
  80. m_ctlImage.Add(&bm, (COLORREF)RGB(255,0,255));
  81. m_treeCategory.SetImageList(&m_ctlImage);
  82. // add cursor.
  83. CTreeCursor curRoot = m_treeCategory.GetRootItem();
  84. m_curInfomation = curRoot.AddTail("详细信息", IID_INFOMATION);
  85. m_curSubFiles = curRoot.AddTail("包含文件", IID_SUBFILES);
  86. // m_curStatics = curRoot.AddTail("运行信息", IID_RUN_STATICS);
  87. m_curAllPeers = curRoot.AddTail("Peers", IID_ALL_PEERS);
  88. m_curGraph = curRoot.AddTail("图形", IID_RUN_GRAPH);
  89. m_curLog = curRoot.AddTail("日志", IID_LOG);
  90. // init
  91. m_MonitorDetailWnd.ShowNode(m_curInfomation, false);
  92. // Create splitter.
  93. CRect rect(0,0,0,0);
  94. m_wndHSplitterBar.Create(WS_CHILD|WS_BORDER|WS_DLGFRAME|WS_VISIBLE,rect,this, 112998, FALSE);
  95. m_wndHSplitterBar.SetPanes(&m_treeCategory, &m_MonitorDetailWnd);
  96. LoadState();
  97. return 0;
  98. }
  99. void CMonitorWnd::OnSize(UINT nType, int cx, int cy) 
  100. {
  101. long lTreeCX = min(cx, m_lSplitPercent); // cx*m_lSplitPercent/100;
  102. long lMonitorCX = max((cx - lTreeCX), 0); //cx*(100 - m_lSplitPercent)/100;
  103. m_treeCategory.MoveWindow(CRect(0, 0, lTreeCX, cy));
  104. m_MonitorDetailWnd.MoveWindow(CRect(lMonitorCX, 0, cx, cy));
  105. m_wndHSplitterBar.SetPanes(&m_treeCategory,&m_MonitorDetailWnd);
  106. CWnd::OnSize(nType, cx, cy);
  107. }
  108. void CMonitorWnd::OnSplitterMoved(long lParam, long wParam)
  109. {
  110. CRect rc;
  111. m_treeCategory.GetClientRect(rc);
  112. long lTreeCX = rc.Width();
  113. m_lSplitPercent = lTreeCX;
  114. }
  115. void CMonitorWnd::OnNodeSelect(NMHDR *pNotifyStruct,LRESULT *result)
  116. {
  117. if (m_bDeleteing)
  118. return;
  119. CTreeCursor curSelected  = m_treeCategory.GetSelectedItem();
  120. if (!curSelected) return;
  121. m_MonitorDetailWnd.ShowNode(curSelected, true);
  122. }
  123. void CMonitorWnd::OnDestroy() 
  124. {
  125. m_bDeleteing = true;
  126. while (m_curAllPeers.HasChildren())
  127. {
  128. CTreeCursor curNext;
  129. CTreeCursor curChild = m_curAllPeers.GetChild();
  130. while (curChild)
  131. {
  132. CAddrPort* pData = (CAddrPort*)curChild.GetData();
  133. assert(pData);
  134. delete pData;
  135. curNext = curChild.GetNext(TVGN_NEXT);
  136. curChild.Delete();
  137. curChild = curNext;
  138. }
  139. }
  140. m_bDeleteing = false;
  141. SaveState();
  142. CWnd::OnDestroy();
  143. }
  144. /////////////////////////////////////////////////////////////////////////////
  145. // CMonitorWnd function
  146. void CMonitorWnd::Refresh()
  147. {
  148. if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload)
  149. return;
  150. // refresh peers in tree.
  151. // RefreshPeers();
  152. // show the infomation for each category.
  153. CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
  154. if (!curSelected)
  155. curSelected = m_curInfomation;
  156. m_MonitorDetailWnd.ShowNode(curSelected, false);
  157. }
  158. void CMonitorWnd::OnContextQuery(CMenu& menubar)
  159. {
  160. menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_INFORMATION, "详细信息");
  161. menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_SUBFILES, "包含文件");
  162. menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_PEERS, "Peers");
  163. menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_RUNSTATICS, "图形");
  164. menubar.AppendMenu(MF_STRING, ID_MENUITEM_MONITOR_LOGS, "日志");
  165. menubar.EnableMenuItem(ID_MENUITEM_MONITOR_INFORMATION, MF_BYCOMMAND | MF_ENABLED);
  166. menubar.EnableMenuItem(ID_MENUITEM_MONITOR_SUBFILES, MF_BYCOMMAND | MF_ENABLED);
  167. menubar.EnableMenuItem(ID_MENUITEM_MONITOR_RUNSTATICS, MF_BYCOMMAND | MF_ENABLED);
  168. menubar.EnableMenuItem(ID_MENUITEM_MONITOR_PEERS, MF_BYCOMMAND | MF_ENABLED);
  169. menubar.EnableMenuItem(ID_MENUITEM_MONITOR_LOGS, MF_BYCOMMAND | MF_ENABLED);
  170. }
  171. void CMonitorWnd::OnShowNode(UINT uID)
  172. {
  173. long lShowNode = uID - ID_MENUITEM_MONITOR_INFORMATION;
  174. CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
  175. // if called by menu monitor command, show the required node.
  176. switch (lShowNode)
  177. {
  178. case 0:
  179. curSelected = m_curInfomation;
  180. break;
  181. case 1:
  182. curSelected = m_curSubFiles;
  183. break;
  184. case 2:
  185. curSelected = m_curAllPeers;
  186. break;
  187. case 3:
  188. curSelected = m_curGraph;
  189. break;
  190. case 4:
  191. curSelected = m_curLog;
  192. break;
  193. }
  194. m_bDeleteing = true;
  195. curSelected.Select();
  196. m_MonitorDetailWnd.ShowNode(curSelected, true);
  197. m_bDeleteing = false;
  198. }
  199. void CMonitorWnd::Monitor(CFileDBItem* pFileDBItem, long lShowNode)
  200. {
  201. m_pFileDBItem = pFileDBItem;
  202. m_MonitorDetailWnd.Monitor(pFileDBItem);
  203. // show peers in tree.
  204. // ShowPeers();
  205. // show the infomation for node category.
  206. m_bDeleteing = true;
  207. CTreeCursor curSelected = m_treeCategory.GetSelectedItem();
  208. if (!curSelected)
  209. curSelected = m_curInfomation;
  210. if (lShowNode != -1)
  211. {
  212. // if not visible monitorbar, show it.
  213. CMainFrame* pMainFrame = (CMainFrame*)AfxGetMainWnd();
  214. if (!pMainFrame)
  215. {
  216. assert(false);
  217. return;
  218. }
  219. pMainFrame->ShowControlBar(&pMainFrame->m_MonitorBar, true, false);
  220. // if called by menu monitor command, show the required node.
  221. switch (lShowNode)
  222. {
  223. case 0:
  224. curSelected = m_curInfomation;
  225. break;
  226. case 1:
  227. curSelected = m_curSubFiles;
  228. break;
  229. case 2:
  230. curSelected = m_curAllPeers;
  231. break;
  232. case 3:
  233. curSelected = m_curGraph;
  234. break;
  235. case 4:
  236. curSelected = m_curLog;
  237. break;
  238. }
  239. }
  240. curSelected.Select();
  241. // m_curInfomation.Expand();
  242. // m_curStatics.Expand();
  243. m_MonitorDetailWnd.ShowNode(curSelected, true);
  244. m_bDeleteing = false;
  245. }
  246. void CMonitorWnd::ShowPeers()
  247. {
  248. // clear
  249. m_bDeleteing = true;
  250. if (m_curAllPeers.HasChildren())
  251. {
  252. CTreeCursor curNext;
  253. CTreeCursor curChild = m_curAllPeers.GetChild();
  254. while (curChild)
  255. {
  256. CAddrPort* pData = (CAddrPort*)curChild.GetData();
  257. if (pData)
  258. delete pData;
  259. else
  260. assert(false);
  261. curNext = curChild.GetNext(TVGN_NEXT);
  262. curChild.Delete();
  263. curChild = curNext;
  264. }
  265. }
  266. m_bDeleteing = false;
  267. // show peers.
  268. if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload || !m_pFileDBItem->m_pDownload->IsDownloading()) return;
  269. vector<CConncetionData> v = m_pFileDBItem->m_pDownload->GetConnections();
  270. for (int i=0; i<v.size(); i++)
  271. {
  272. in_addr inaddress;
  273. inaddress.S_un.S_addr = v[i].m_lAddr;
  274. char szText[100] = {0};
  275. sprintf(szText, "%s:%d(%d)", 
  276. inet_ntoa(inaddress), 
  277. ntohs(v[i].m_sPort), 
  278. v[i].m_lDownRate); 
  279. CTreeCursor curItem = m_curAllPeers.AddTail(szText, IID_PEER);
  280. CAddrPort* pData = new CAddrPort(v[i].m_lAddr, v[i].m_sPort);
  281. curItem.SetData((long)pData);
  282. }
  283. m_curAllPeers.Expand();
  284. }
  285. void CMonitorWnd::RefreshPeers()
  286. {
  287. if (!m_pFileDBItem || !m_pFileDBItem->m_pDownload || !m_pFileDBItem->m_pDownload->IsDownloading())
  288. return;
  289. m_bDeleteing = true;
  290. vector<CConncetionData> vConnections = m_pFileDBItem->m_pDownload->GetConnections();
  291. if (m_curAllPeers.HasChildren())
  292. {
  293. CTreeCursor curNext;
  294. CTreeCursor curChild = m_curAllPeers.GetChild();
  295. while (curChild)
  296. {
  297. CAddrPort* pData = (CAddrPort*)curChild.GetData();
  298. if (!pData)
  299. assert(false);
  300. bool bFind = false;
  301. for (int i=0; i<vConnections.size(); i++)
  302. {
  303. if (vConnections[i].m_lAddr == pData->m_lAddr && 
  304. vConnections[i].m_sPort == pData->m_sPort)
  305. {
  306. vConnections.erase(vConnections.begin() + i);
  307. bFind = true;
  308. break;
  309. }
  310. }
  311. curNext = curChild.GetNext(TVGN_NEXT);
  312. if (!bFind)
  313. {
  314. delete pData;
  315. curChild.Delete();
  316. }
  317. curChild = curNext;
  318. }
  319. }
  320. for (int i=0; i<vConnections.size(); i++)
  321. {
  322. in_addr inaddress;
  323. inaddress.S_un.S_addr = vConnections[i].m_lAddr;
  324. char szText[100] = {0};
  325. sprintf(szText, "%s:%d(%d)", 
  326. inet_ntoa(inaddress), 
  327. ntohs(vConnections[i].m_sPort), 
  328. vConnections[i].m_lDownRate); 
  329. CTreeCursor curItem = m_curAllPeers.AddTail(szText, IID_PEER);
  330. CAddrPort* pData = new CAddrPort(vConnections[i].m_lAddr, vConnections[i].m_sPort);
  331. curItem.SetData((long)pData);
  332. }
  333. m_bDeleteing = false;
  334. }
  335. void CMonitorWnd::SetFocusCategory()
  336. {
  337. m_treeCategory.SetFocus();
  338. }
  339. void CMonitorWnd::SwitchMonitorCategory(bool bNext)
  340. {
  341. CTreeCursor curSelected  = m_treeCategory.GetSelectedItem();
  342. if (!curSelected) 
  343. {
  344. m_curInfomation.Select();
  345. return;
  346. }
  347. if (curSelected == m_curInfomation)
  348. {
  349. if (bNext)
  350. curSelected = m_curSubFiles;
  351. else
  352. curSelected = m_curLog;
  353. }
  354. else if (curSelected == m_curSubFiles) 
  355. {
  356. if (bNext)
  357. curSelected = m_curAllPeers;
  358. else
  359. curSelected = m_curInfomation;
  360. }
  361. else if (curSelected == m_curAllPeers) 
  362. {
  363. if (bNext)
  364. curSelected = m_curGraph;
  365. else
  366. curSelected = m_curSubFiles;
  367. }
  368. else if (curSelected == m_curGraph) 
  369. {
  370. if (bNext)
  371. curSelected = m_curLog;
  372. else
  373. curSelected = m_curAllPeers;
  374. }
  375. else if (curSelected == m_curLog) 
  376. {
  377. if (bNext)
  378. curSelected = m_curInfomation;
  379. else
  380. curSelected = m_curGraph;
  381. }
  382. curSelected.Select();
  383. }