TrafficDlg.cpp
上传用户:hbytqc8
上传日期:2014-07-31
资源大小:527k
文件大小:13k
- // TrafficDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Traffic.h"
- #include "TrafficDlg.h"
- #include "input.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTrafficDlg dialog
- CTrafficDlg::CTrafficDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTrafficDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTrafficDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTrafficDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTrafficDlg)
- DDX_Control(pDX, IDC_EDIT2, m_community);
- DDX_Control(pDX, IDC_IPADDRESS1, m_ipadd);
- DDX_Control(pDX, IDC_LIST2, m_list1);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTrafficDlg, CDialog)
- //{{AFX_MSG_MAP(CTrafficDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_BUTTON1, OnScan)
- ON_BN_CLICKED(IDC_BUTTON2, OnSure)
- ON_BN_CLICKED(IDC_BUTTON3, OnModify)
- ON_BN_CLICKED(IDC_BUTTON4, OnStart)
- ON_WM_TIMER()
- ON_BN_CLICKED(IDC_BUTTON5, OnEnd)
- ON_BN_CLICKED(IDC_BUTTON6, OnWorkData)
- ON_BN_CLICKED(IDC_BUTTON7, OnOpenFile)
- ON_BN_CLICKED(IDC_BUTTON8, OnToLeft)
- ON_BN_CLICKED(IDC_BUTTON9, OnToRight)
- ON_BN_CLICKED(IDC_CHECK1, OnChangeUse)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTrafficDlg message handlers
- BOOL CTrafficDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- m_list1.InsertColumn(0," 类型 ",LVCFMT_LEFT,80,0);
- m_list1.InsertColumn(1," 名称(DLCI)",LVCFMT_LEFT,130,1);
- m_list1.InsertColumn(2," 最大速率",LVCFMT_LEFT,70,2);
- m_list1.InsertColumn(3," 状态 ",LVCFMT_LEFT,70,3);
- m_list1.InsertColumn(4," 辅助信息 ",LVCFMT_LEFT,300,4);
- m_list1.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- CRect cr(25,12,518,150);
- CSize c(388,150);
- pDay=NULL;
- DayNumber=288;
- pDay=new CLineChart(c,DayNumber);
- pDay->Create(NULL,WS_CHILD|WS_VISIBLE|SS_NOTIFY|SS_BLACKFRAME, cr,this,50000);
- pDay->SetTitle("日流量图");
- pDay->SetLegend("流入","流出");
- CRect cr1(25,169,518,170);
- CSize c1(388,150);
- pWeek=NULL;
- pWeek=new CLineChart(c1,DayNumber);
- pWeek->Create(NULL,WS_CHILD|WS_VISIBLE|SS_NOTIFY|SS_BLACKFRAME, cr1,this,50001);
- pWeek->SetTitle("周流量图");
- pWeek->SetLegend("流入","流出");
- index="";
- SampleNumber=0;
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTrafficDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CTrafficDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CTrafficDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CTrafficDlg::OnDestroy()
- {
- CDialog::OnDestroy();
-
- if (pDay!=NULL) delete pDay;
- if (pWeek!=NULL) delete pWeek;
- }
- void CTrafficDlg::OnScan()
- {
- CString BaseOid;
- m_ipadd.GetWindowText(ip);
- m_community.GetWindowText(community);
- if ((community.GetLength()==0)||(ip.GetLength()==0))
- {
- MessageBox("地址或口令错误!");
- return;
- }
- m_snmp.clear();
- BaseOid="1.3.6.1.2.1.2.2.1.1";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.2.2.1.2";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.2.2.1.8";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.2.2.1.5";
- m_snmp.GetTree(ip,community,BaseOid);
- int row=m_snmp.GetCount()/4;
- for (int i=0;i<row;i++)
- {
- pLine=new Line;
- pLine->Type="普通接口";
- pLine->Index=m_snmp.GetValue(i);
- pLine->Name=m_snmp.GetValue(i+row);
- pLine->Speed=m_snmp.GetValue(i+3*row);
- if (m_snmp.GetValue(i+2*row)=="1")
- pLine->State="UP";
- else
- pLine->State="Down";
- pLine->Descr="";
- pLine->Ip=ip;
- LineArray.Add(pLine);
- }
- m_snmp.clear();
- BaseOid="1.3.6.1.2.1.4.20.1.2";
- m_snmp.GetTree(ip,community,BaseOid);
- CString temp;
- for (i=0;i<m_snmp.GetCount();i++)
- {
- for (int j=0;j<LineArray.GetSize();j++)
- if (m_snmp.GetValue(i)==LineArray[j]->Index)
- {
- temp=m_snmp.GetOid(i);
- temp=temp.Right(temp.GetLength()-21);
- LineArray[j]->Descr="IP:"+temp;
- break;
- }
-
- }
- for (int j=0;j<LineArray.GetSize();j++)
- {
- BaseOid="1.3.6.1.2.1.31.1.1.1.18."+LineArray[j]->Index;
- m_snmp.GetOne(ip,community,BaseOid,temp);
- LineArray[j]->Descr+=" 备注:"+temp;
- }
- //获取网络接口的信息并保存;
- m_snmp.clear();
- BaseOid="1.3.6.1.2.1.10.32.2.1.1";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.10.32.2.1.2";
- m_snmp.GetTree(ip,community,BaseOid);
- BaseOid="1.3.6.1.2.1.10.32.2.1.3";
- m_snmp.GetTree(ip,community,BaseOid);
- row=m_snmp.GetCount()/3;
- for (i=0;i<row;i++)
- {
- pLine=new Line;
-
- pLine->Type="帧中继PVC";
- pLine->Ip=ip;
- pLine->Speed="65535";
- pLine->Index=m_snmp.GetValue(i);
- pLine->Name=m_snmp.GetValue(i+row);
- if (m_snmp.GetValue(i+2*row)=="2")
- pLine->State="UP";
- else pLine->State="Down";
- BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.2."+pLine->Index+"."+pLine->Name;
- m_snmp.GetOne(ip,community,BaseOid,temp);
- if ("2"==temp)
- {
- CString rv;
- BaseOid="1.3.6.1.4.1.9.9.49.1.2.2.1.3."+pLine->Index+"."+pLine->Name;
- m_snmp.GetOne(ip,community,BaseOid,rv);
- BaseOid="1.3.6.1.2.1.31.1.1.1.18."+rv;
- m_snmp.GetOne(ip,community,BaseOid,rv);
- pLine->Descr=rv;
- }
- else
- {
- CString rv;
- BaseOid="1.3.6.1.4.1.9.9.49.1.3.1.1.3."+pLine->Index+"."+pLine->Name;
- m_snmp.GetOne(ip,community,BaseOid,rv);
- pLine->Descr=rv;
- }
- LineArray.Add(pLine);
-
- }
- m_list1.DeleteAllItems();
- for (i=0;i<LineArray.GetSize();i++)
- {
- m_list1.InsertItem(i,LineArray[i]->Type);
- m_list1.SetItemText(i,1,LineArray[i]->Name);
- m_list1.SetItemText(i,2,LineArray[i]->Speed);
- m_list1.SetItemText(i,3,LineArray[i]->State);
- m_list1.SetItemText(i,4,LineArray[i]->Descr);
- }
- }
- void CTrafficDlg::OnSure()
- {
- if (LineArray.GetSize()==0)
- {
- MessageBox("无可用数据!");
- return;
- }
- POSITION pos;
- pos=m_list1.GetFirstSelectedItemPosition();
- if (pos==NULL)
- {
- MessageBox("没有选择线路!");
- return;
- }
- int n=m_list1.GetNextSelectedItem(pos);
- if (LineArray[n]->Type=="帧中继PVC")
- {
- index=LineArray[n]->Index+"."+LineArray[n]->Name;
- oid="1.3.6.1.2.1.10.32.2.1";
- }
- else
- {
- index=LineArray[n]->Index;
- oid="1.3.6.1.2.1.2.2.1";
- }
- MaxSpeed=atoi(LineArray[n]->Speed);
- for (int i=0;i<LineArray.GetSize();i++)
- {
- if (LineArray[i]!=NULL) delete LineArray[i];
- }
- m_list1.DeleteAllItems();
- }
- void CTrafficDlg::OnModify()
- {
- char ch[10];
- itoa(MaxSpeed,ch,10);
- Cinput dlg;
- dlg.m_old=ch;
- if (dlg.DoModal()==IDOK)
- {
- if (dlg.m_new.GetLength()>0)
- MaxSpeed=atoi((LPCTSTR)dlg.m_new);
- MessageBox("速率更改成功!");
- }
- }
- void CTrafficDlg::OnStart()
- {
- if (index.GetLength()==0)
- {
- MessageBox("OID错误!");
- return;
- }
- pDay->Init();
- pWeek->Init();
- CTime t=CTime::GetCurrentTime();
- fDayName=t.Format("%m-%d-%H");
- fDayName=fDayName+"Day.txt";
- DataFile.Open(fDayName,CFile::modeCreate);
- DataFile.Close();
- fWeekName=t.Format("%m-%d-%H");
- fWeekName=fWeekName+"Week.txt";
- DataFile.Open(fWeekName,CFile::modeCreate);
- DataFile.Close();
- SetTimer(1,300000,NULL);
- m_snmp.GetThree(ip,community,oid,index,last_in,last_out,last_time);
- CButton* m_pB=(CButton*)GetDlgItem(IDC_BUTTON4);
- m_pB->EnableWindow(FALSE);
- }
- void CTrafficDlg::OnTimer(UINT nIDEvent)
- {
- unsigned long InBits,OutBits;
- unsigned long in_v,out_v,time_v,in_diff,out_diff,time_diff;
- CTime t=CTime::GetCurrentTime();
- CString time=t.Format("%H:%M");
- m_snmp.GetThree(ip,community,oid,index,in_v,out_v,time_v);
- in_diff=in_v-last_in;
- if (in_diff<0)
- in_diff+=4294967295;
- out_diff=out_v-last_out;
- if (out_diff<0)
- out_diff+=4294967295;
- time_diff=time_v-last_time;
- if (time_diff<0)
- time_diff+=4294967295;
- InBits=(in_diff*8)/(time_diff/100);
- OutBits=(out_diff*8)/(time_diff/100);
- int In_use=InBits*100/MaxSpeed;
- int Out_use=OutBits*100/MaxSpeed;
- pDay->Append(InBits,OutBits,time,In_use,Out_use);
- SampleNumber+=1;
- CString line,s;
- line="";
- if ((SampleNumber % 6)==0)
- {
- CString time1=t.Format("%a");
- pWeek->Append(pDay->GetXAverage(),pDay->GetYAverage(),time1,0,0);
- line+=time1;
- if(DataFile.Open(fWeekName,CFile::modeWrite))
- {
- s.Format("%d",pDay->GetXAverage());
- line=line+"@"+s;
- s.Format("%d",pDay->GetYAverage());
- line=line+"#"+s;
- s.Format("%d",0);
- line=line+"$"+s;
- s.Format("%d",0);
- line=line+"&"+s+"rn";
- DataFile.SeekToEnd();
- DataFile.WriteString(line);
- line="";
- DataFile.Close();
- }
- }
- last_in=in_v;
- last_out=out_v;
- last_time=time_v;
- line+=time;
- if(DataFile.Open(fDayName,CFile::modeWrite))
- {
- s.Format("%d",InBits);
- line=line+"@"+s;
- s.Format("%d",OutBits);
- line=line+"#"+s;
- s.Format("%d",In_use);
- line=line+"$"+s;
- s.Format("%d",Out_use);
- line=line+"&"+s+"rn";
- DataFile.SeekToEnd();
- DataFile.WriteString(line);
- DataFile.Close();
- }
- if (SampleNumber==DayNumber)
- {
- CTime t=CTime::GetCurrentTime();
- fDayName=t.Format("%m-%d-%H");
- fDayName=fDayName+".txt";
- DataFile.Open(fDayName,CFile::modeCreate);
- DataFile.Close();
- SampleNumber=0;
- }
-
- CDialog::OnTimer(nIDEvent);
- }
- void CTrafficDlg::OnEnd()
- {
- KillTimer(1);
- CButton* m_pB=(CButton*)GetDlgItem(IDC_BUTTON4);
- m_pB->EnableWindow(TRUE);
- }
- void CTrafficDlg::OnWorkData()
- {
- CButton* m_pB=(CButton*)GetDlgItem(IDC_BUTTON4);
- if (!(m_pB->IsWindowEnabled()))
- {
- MessageBox("先停止流量测试!");
- return;
- }
- pDay->Init();
- pWeek->Init();
- pDay->SetCross(true);
- }
- void CTrafficDlg::OnOpenFile()
- {
- bool show;
- unsigned long x,y;
- int number,Usex,Usey;
- CStdioFile dataFile;
- CString line,t;
- pDay->Init();
- CFileDialog openfile(TRUE,"*.txt",NULL,
- OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- "Text files(*.txt)|*.txt");
- if (openfile.DoModal()==IDOK)
- {
- if(dataFile.Open(openfile.GetPathName(),CFile::modeRead))
- {
- number=0;
- while (dataFile.ReadString(line))
- {
- int n0=line.Find('@');
- CString str=line.Mid(0,n0);
- t=str;
- int n1=line.Find('#');
- str=line.Mid(n0+1,n1-n0-1);
- x=atoi((LPCTSTR)str);
- n0=line.Find('$');
- str=line.Mid(n1+1,n0-n1-1);
- y=atoi((LPCTSTR)str);
- n1=line.Find('&');
- str=line.Mid(n0+1,n1-n0-1);
- Usex=atoi((LPCTSTR)str);
- str=line.Mid(n1+1,10);
- Usey=atoi((LPCTSTR)str);
- if ((number % 36)==0)
- show=true;
- else
- show=false;
- pDay->AddData(x,y,t,Usex,Usey,number,show);
- number+=1;
- }
-
- }
- dataFile.Close();
- }
- pDay->Invalidate();
- }
- void CTrafficDlg::OnToLeft()
- {
- pDay->ToLeft();
- }
- void CTrafficDlg::OnToRight()
- {
- pDay->ToRight();
- }
- void CTrafficDlg::OnChangeUse()
- {
- CButton* m_pCheck=(CButton*)GetDlgItem(IDC_CHECK1);
- if(m_pCheck->GetCheck())
- pDay->SetUse(true);
- else
- pDay->SetUse(false);
- }