MainFrm.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:14k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "NetMap.h"
- #include "NetMapDoc.h"
- #include "MainFrm.h"
- #include "snmp_pp.h"
- #include "Show.h"
- #include "Modify.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_SCAN, OnScan)
- ON_COMMAND(ID_GRAPH, OnGraph)
- ON_COMMAND(ID_SHOWINFORMATION, OnShowinformation)
- ON_COMMAND(ID_NODE_MODIFY, OnNodeModify)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
-
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- //cs.style=WS_MAXIMIZE;
- cs.x=4;
- cs.y=4;
- cs.cx=800;
- cs.cy=750;
- return TRUE;
- }
- BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
- {
- BOOL Result=v.CreateStatic(this,1,2,WS_CHILD | WS_VISIBLE);
- ASSERT(Result);
- h.CreateStatic(&v,2,1,WS_CHILD | WS_VISIBLE,v.IdFromRowCol(0,1));
- v.CreateView(0,0,RUNTIME_CLASS(CMapView),CSize(237,100),pContext);
- h.CreateView(0,0,RUNTIME_CLASS(CNetMapView),CSize(400,400),pContext);
- h.CreateView(1,0,RUNTIME_CLASS(CVlanView),CSize(400,400),pContext);
- return Result;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- //根据text属性查找特定的节点是否存在,如找到,返回符合条件节点的句柄,
- //否则返回NULL;
- HTREEITEM CMainFrame::FindNode(HTREEITEM hSubRoot,const CString &text)
- {
- if (hSubRoot==NULL)
- return NULL;
- //判断是否符合条件,如是,返回该节点的举柄;
- if (pListView->m_map.GetItemText(hSubRoot)==text)
- return hSubRoot;
- hSubRoot=pListView->m_map.GetChildItem(hSubRoot);
- do
- {
- HTREEITEM ht;
- //递归调用FindNode;
- if ((ht=FindNode(hSubRoot,text)) !=NULL)
- return ht;
- } while ((hSubRoot=pListView->m_map.GetNextSiblingItem(hSubRoot)) !=NULL);
- return NULL;
- }
- bool CMainFrame::Findport(CString &port,NodeData *p)
- {
- for (int i=0;i<(p->m_neighbor.GetSize());i++)
- {
- if (p->m_neighbor.GetAt(i).localport==port)
- return true;
- }
- return false;
- }
- //将CISCO格式的网络地址转换为正常格式的IP地址;
- CString CMainFrame::HtoIP(CString Hex)
- {
-
-
- char ch[4];
- CString ip;
- if (Hex.GetLength()<11)
- {
- ip="NULL";
- return ip;
- }
- CString Hex1;
- Hex.Replace(" ","");
- for (int i=0;i<4;i++)
- {
- Hex1=Hex.Mid(i*2,2);
- int data=0;
- if((Hex1[0]>='A') && (Hex1[0]<='F'))
- data=data+Hex1[0]-'A'+10;
- if((Hex1[0]>='0') && (Hex1[0]<='9'))
- data=data+Hex1[0]-'0';
- data=data*16;
- int data1=0;
- if((Hex1[1]>='A') && (Hex1[1]<='F'))
- data1=data1+Hex1[1]-'A'+10;
- if((Hex1[1]>='0') && (Hex1[1]<='9'))
- data1=data1+Hex1[1]-'0';
- data=data+data1;
- itoa(data,ch,10);
- ip=ip+ch+'.';
- }
- ip=ip.Left(ip.GetLength()-1);
- return ip;
- }
- //扫描获取一个节点中CDP信息的函数,
- void CMainFrame::ScanNode(CString &add,CString &community,HTREEITEM hparent)
- {
- char ch[3];
- neighbor n1;
- CString BaseOid;
- NodeData *p1;
- m_snmp.clear();
- //清除原来的数据;
- for (int i=4;i<10;i++)
- {
- BaseOid="1.3.6.1.4.1.9.9.23.1.2.1.1.";
- itoa(i,ch,10);
- BaseOid=BaseOid+ch;
- if (m_snmp.GetTree(add,community,BaseOid)==false)
- {
- MessageBox("获取数据失败,请检查网络和SNMP配置参数!");
- return;
- }
- }
- //(pNodeData *)(pListView->m_map.GetItemData(hparent))->scan=true;
- //将CDP条目表中第4~9六个列对象的实例值取回,并存放在一个MyStringArray
- //对象中。关于这些列对象的具体含义请参考本章前面的内容;
- p1=(NodeData *)(pListView->m_map.GetItemData(hparent));
- p1->scan=true;
- //设置已经扫描标志;
- int row=m_snmp.GetCount()/6;
- CString h;
- int j;
- for (j=0; j<row;j++)
- {
- h=m_snmp.GetValue(j);
- h.TrimLeft();
- h.TrimRight();
- h=h.Left(11);
- m_snmp.SetValue(j,HtoIP(h));
- }
- //将得到的IP地址格式转换为正常的格式;
- for (j=0; j<row;j++)
- {
- CString remote="NULL";
- CString Temp_oid=m_snmp.GetOid(j);
- Temp_oid=Temp_oid.Mid(BaseOid.GetLength()+1,8);
- Temp_oid=Temp_oid.Left(Temp_oid.Find('.'));
- Temp_oid.TrimLeft();
- Temp_oid.TrimRight();
- Temp_oid="1.3.6.1.2.1.2.2.1.2."+Temp_oid;
- m_snmp.GetOne(add,community,Temp_oid,remote);
- m_snmp.Add(Temp_oid,remote);
- }
- //获得本地端口名,既通过哪个本地端口获得的该条CDP信息;
- int pos=0;
- //MessageBox("scaned: "+p1->ip);
- for (j=0; j<row;j++)
- //对获得的每一份邻居节点信息进行处理。正常情况下,每个CDP条目就是
- //一台新发现的设备,都应该在设备树增加一个节点,并在新增的节点中
- //保存到本节点的连接信息。但符合下列情况的CDP条目不是新发现设备,
- //要做特殊处理:
- // 1:和父节点连接的CDP条目;
- // 2:和同一节点有多条连接;
- // 3:环路;
- {
- HTREEITEM h;
- if (p1->m_neighbor.GetSize()>0)
- if (Findport(m_snmp.GetValue(j+6*row),p1))
-
- continue;
-
- //和父节点连接的CDP条目,在扫描父节点时这些信息已经保存在本地
- //节点中,跳过;
- h=FindNode(pListView->m_map.GetRootItem(),m_snmp.GetValue(j));
- if (h!=NULL)
- {
- neighbor n2;
- NodeData * p2=(NodeData *)(pListView->m_map.GetItemData(h));
- n2.Id=hparent;
- n2.localport=m_snmp.GetValue(j+3*row);
- n2.remoteport=m_snmp.GetValue(j+6*row);
- p2->m_neighbor.Add(n2);
- continue;
- }
- //已经存在的节点,只增加连接信息,不增加新的节点;
- pNodeData=new NodeData;
- pNodeData->ip=m_snmp.GetValue(j);
- pNodeData->sysDescr=m_snmp.GetValue(j+row);
- pNodeData->scan=false;
- pNodeData->router=false;
- pNodeData->level=p1->level+1;
- pNodeData->pImage=NULL;
- pNodeData->position=p1->position+pos;
- pos+=1;
- //对任一子节点,层次数等于父节点层次数加1;横向位置以父节点
- //为基础,依次累加;
- CString rs=m_snmp.GetValue(j+5*row);
- rs=rs.Mid(9,2);
- if ((atoi(rs)%2)==1)
- {
- pNodeData->scan=true;
- pNodeData->router=true;
- }
- if (atoi(rs)>=8)
- pNodeData->scan=false;
- n1.Id=hparent;
- n1.localport=m_snmp.GetValue(j+3*row);
- n1.remoteport=m_snmp.GetValue(j+6*row);
- pNodeData->m_neighbor.Add(n1);
- h=pListView->m_map.InsertItem(m_snmp.GetValue(j),hparent);
- pListView->m_map.SetItemData(h,(DWORD)pNodeData);
- NodeArray.Add(h);
- }
- }
- void CMainFrame::OnScan()
- {
- if (NodeArray.GetSize()>0)
- NodeArray.RemoveAll();
- if (pListView->m_map.GetCount()>0)
- pListView->m_map.DeleteAllItems();
- //清除上次扫描数据;
- NodeData *p1;
- CNetMapDoc *pDoc=(CNetMapDoc *)this->GetActiveDocument();
- if (pDoc->ip.GetLength()==0||pDoc->community.GetLength()==0)
- {
- MessageBox("设备地址,Community信息错误!");
- return;
- }
- CString sysoid="1.3.6.1.2.1.1.1.0";
- CString reply;
- m_snmp.GetOne(pDoc->ip,pDoc->community,sysoid,reply);
- pNodeData=new NodeData;
- pNodeData->ip=pDoc->ip;
- pNodeData->sysDescr=reply;
- pNodeData->scan=false;
- pNodeData->pImage=NULL;
- pNodeData->level=0;
- pNodeData->position=0;
- HRoot=pListView->m_map.InsertItem(pDoc->ip);
- NodeArray.Add(HRoot);
- pListView->m_map.SetItemData(HRoot,(DWORD)pNodeData);
- HCurrent=HRoot;
- int NodeNumber;
- int i;
- while (1)
- {
- NodeNumber=NodeArray.GetSize();
- for(i=0;i<NodeNumber;i++)
- {
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[i]));
- if (p1->scan==true)
- continue;
- ScanNode(p1->ip,pDoc->community,NodeArray[i]);
- }
- if (NodeNumber==NodeArray.GetSize())
- break;
- }
-
- }
- void CMainFrame::OnGraph()
- {
- NodeData *p1;
- int NodeNumber=NodeArray.GetSize();
- if (NodeNumber==0) return;
- //没有图形数据,退出;
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[0]));
- if (p1->pImage!=NULL) return;
- //已经生成图形,退出;
- int x=80;
- int y=50;
- //CRect cr;
- CBitmap bmpLoad;
- for(int i=0;i<NodeNumber;i++)
- {
- p1=(NodeData *)(pListView->m_map.GetItemData(NodeArray[i]));
- CRect cr(x+100*p1->position,y+80*p1->level,65,35);
- p1->pImage=new CMyImage();
- p1->pImage->Create(NULL,WS_CHILD|SS_BITMAP|WS_VISIBLE|SS_NOTIFY,
- cr,pNetMapView,IDC_MYIMAGE+i);
- HBITMAP hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP2));
- if (p1->router==true)
- hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
- p1->pImage->SetBitmap(hBitmap);
- p1->pImage->MyPos.x=cr.left+33;
- p1->pImage->MyPos.y=cr.top+12;
- //保存生成连接线时的点坐标;
- p1->pImage->HSelf=NodeArray[i];
- //保存在树形控件中的举柄;
- }
- pNetMapView->paint=true;
- pNetMapView->RedrawWindow();
- }
- void CMainFrame::OnShowinformation()
- {
- NodeData * p1=(NodeData *)(pListView->m_map.GetItemData(HSelect));
- CShow dlg;
- dlg.m_descr=p1->sysDescr;
- CString local="";
- CString remote="";
- CString ip="";
- for (int i=0;i<p1->m_neighbor.GetSize();i++)
- {
- ip+=pListView->m_map.GetItemText(p1->m_neighbor[i].Id)+" ";
- local+=p1->m_neighbor[i].localport+" ";
- remote+=p1->m_neighbor[i].remoteport+" ";
- }
- dlg.m_localport=local;
- dlg.m_remotelocal=remote;
- dlg.m_ip=ip;
- dlg.DoModal();
- }
- void CMainFrame::OnNodeModify()
- {
- HTREEITEM h;
- NodeData *p1;
- NodeData *p2;
- CString Lip,Lport,Rip,Rport;
- CModify dlg;
- if(dlg.DoModal()==IDOK)
- {
- Lip=dlg.m_LocalIp;
- Lport=dlg.m_LocalPort;
- Rip=dlg.m_RemoteIp;
- Rport=dlg.m_RemotePort;
- if (dlg.add==true)
- {
- h=FindNode(pListView->m_map.GetRootItem(),Lip);
- if (h!=NULL)
- {
- MessageBox("要增加的节点已经存在!");
- return;
- }
- h=FindNode(pListView->m_map.GetRootItem(),Rip);
- if (h==NULL)
- {
- MessageBox("上一级节点不存在!");
- return;
- }
- p2=(NodeData *)(pListView->m_map.GetItemData(h));
- p1=new NodeData;
- p1->ip=Lip;
- p1->level=p2->level+1;
- p1->position=p2->position;
- p1->sysDescr="增加的节点,IP:"+Lip;
- if (dlg.m_router==1)
- p1->router=true;;
- p1->scan=true;
- neighbor n1;
- n1.Id=h;
- n1.localport=Lport;
- n1.remoteport=Rport;
- p1->m_neighbor.Add(n1);
- //设置邻居节点信息;
- HCurrent=pListView->m_map.InsertItem(Lip,h);
- NodeArray.Add(HCurrent);
- //在tree中增加节点,父节点为制定节点;
- h=pListView->m_map.GetPrevSiblingItem(HCurrent);
- if (h!=NULL)
- {
- p2=(NodeData *)(pListView->m_map.GetItemData(h));
- p1->position=p2->position+1;
- }
-
- //根据前面的兄弟节点的位置信息,获取新增加节点的位置信息;
- pListView->m_map.SetItemData(HCurrent,(DWORD)p1);
- //设置data属性;
- p1=(NodeData *)(pListView->m_map.GetItemData(pListView->m_map.GetRootItem()));
- p2=(NodeData *)pListView->m_map.GetItemData(HCurrent);
- if (p1->pImage!=NULL)
- {
- CRect cr(80+100*p2->position,50+80*p2->level,65,35);
- p2->pImage=new CMyImage();
- p2->pImage->Create(NULL,WS_CHILD|SS_BITMAP|WS_VISIBLE|SS_NOTIFY,
- cr,pNetMapView,IDC_MYIMAGE+NodeArray.GetSize()+1);
- HBITMAP hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP2));
- if (p2->router==true)
- hBitmap = ::LoadBitmap(AfxGetApp()->m_hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
- p2->pImage->SetBitmap(hBitmap);
- p2->pImage->MyPos.x=cr.left+33;
- p2->pImage->MyPos.y=cr.top+12;
- //保存生成连接线时的点坐标;
- p2->pImage->HSelf=HCurrent;
- }
- pNetMapView->RedrawWindow();
- pListView->RedrawWindow();
- }
- else
- {
- h=FindNode(pListView->m_map.GetRootItem(),Lip);
- if (h==NULL)
- {
- MessageBox("要合并的节点不存在!");
- return;
- }
- HCurrent=FindNode(pListView->m_map.GetRootItem(),Rip);
- if (h==NULL)
- {
- MessageBox("上一级节点不存在!");
- return;
- }
- if ((pListView->m_map.GetChildItem(h)) ||(pListView->m_map.GetChildItem(HCurrent)))
- {
- MessageBox("只有叶子节点才能被合并!");
- return;
- }
- p1=(NodeData *)(pListView->m_map.GetItemData(h));
- p2=(NodeData *)(pListView->m_map.GetItemData(HCurrent));
- if ((p1->router!=true) ||(p2->router!=true))
- {
- MessageBox("要合并的设备必须是路由设备!");
- return;
- }
- p2->m_neighbor.Append(p1->m_neighbor);
- //合并连结信息;
- for(int i=0;i<NodeArray.GetSize();i++)
- {
- if (NodeArray[i]==h)
- {
- NodeArray.RemoveAt(i);
- break;
- }
- }
- if (p1->pImage!=NULL) delete p1->pImage;
- delete p1;
- pListView->m_map.DeleteItem(h);
- pNetMapView->RedrawWindow();
- pListView->RedrawWindow();
- }
- }
- }