// NodeViewDoc.cpp : implementation of the CNodeViewDoc class
//
#include "stdafx.h"
#include "NodeView.h"
#include <iostream> using namespace std;
#include "Line.h"
#include "Math.h"
#include "NodeViewDoc.h"
#include <queue>
#include <list>
using namespace std;
typedef queue <int> INTQUEUE;
#define NONE 1000
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAXNUM 81920
#define MAXNODE 1092
char m_ptl[MAXNUM][5]; //各条日志协议
char m_sip[MAXNUM][20]; //各条日志来源IP
char m_dip[MAXNUM][20]; //各条日志目的IP
char m_pkg[MAXNUM][10]; //各条日志ip包长
double m_netmb[MAXNUM]; //各条日志网络流量(KB)
char node_ip1[MAXNODE][20], node_ip2[MAXNODE][20]; //各线路的两个IP
double node_netmb[MAXNODE]; //各节点、线路流量KB
extern char NodeName1[NODENUM][20];
extern char NodeName2[NODENUM][20];
extern char NodeLineByte[NODENUM][10]; //两个节点之间的流量(KB)
extern int NodeCount;
/////////////////////////////////////////////////////////////////////////////
// CNodeViewDoc
IMPLEMENT_DYNCREATE(CNodeViewDoc, CDocument)
BEGIN_MESSAGE_MAP(CNodeViewDoc, CDocument)
//{{AFX_MSG_MAP(CNodeViewDoc)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNodeViewDoc construction/destruction
CNodeViewDoc::CNodeViewDoc()
{
// TODO: add one-time construction code here
// Innitialize the random number generator
srand((int)time(NULL));
// Initialize the top matrix
int i, j;
for (i = 0; i < NodeCount; i++)
{
for (j = 0; j < NodeCount; j++)
{
if(i==j)
m_iMatrix[i][j] = 1;
else
m_iMatrix[i][j] = 0;
}
}
}
CNodeViewDoc::~CNodeViewDoc()
{
}
BOOL CNodeViewDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CNodeViewDoc serialization
void CNodeViewDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CNodeViewDoc diagnostics
#ifdef _DEBUG
void CNodeViewDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CNodeViewDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CNodeViewDoc commands
void CNodeViewDoc::ProduceNodes()
{
int i, j;
//txt文件中一行的两个节点之间画线,以下设置画线标志
for (i = 0; i < NodeCount; i=i+2) {
SaveTop(i, i+1);
}
// Save the nodes and lines
SaveViewDocument();
}
MyPoint* CNodeViewDoc::GetNodes(int nIndex)
{
// Return a pointer in the object array
return (MyPoint*)m_oaNodes[nIndex];
}
CLine* CNodeViewDoc::AddLine(MyPoint ptFrom, MyPoint ptTo)
{
// Create a new CLine object
CLine* pLine = new CLine(ptFrom, ptTo);
try
{
// Add the new line to the object array
m_oaLines.Add(pLine);
}
// Did we run into a memory exception?
catch (CMemoryException* perr)
{
// Display a message for the user, giving hime her the bad news
AfxMessageBox("Out of memory", MB_ICONSTOP | MB_OK);
// Did we create a line object?
if (pLine)
{
// Delete it
delete pLine;
pLine = NULL;
}
// Delete the exception object
perr ->Delete();
}
return pLine;
}
int CNodeViewDoc::GetLineCount()
{
// Return the array count
return m_oaLines.GetSize ();
}
CLine* CNodeViewDoc::GetLine(int nIndex)
{
// Return a pointer to the line object at the specified pooint in the object array
return (CLine*)m_oaLines[nIndex];
}
int CNodeViewDoc::NodeDistance(MyPoint ptFrom, MyPoint ptTo)
{
// Calculate the distance between nodes
int iDistance;
iDistance = sqrt((ptFrom.x - ptTo.x) * (ptFrom.x - ptTo.x) + (ptFrom.y - ptTo.y) * (ptFrom.y - ptTo.y));
return iDistance;
}
void CNodeViewDoc::SaveTop(int nIndex1, int nIndex2)
{
m_iMatrix[nIndex1][nIndex2] = 1;
m_iMatrix[nIndex2][nIndex1] = 1;
}
BOOL CNodeViewDoc::TopTest(int (*TopMatrix)[NODENUM])
{
int u, w;
int i;
int tag=0;
INTQUEUE Q;
bool visited[NODENUM];
int NeibourNodeSet[NODENUM];
for(int v = 0; v < NodeCount; v++)
{
visited[v]=FALSE;
}
for(int v = 0; v < NodeCount; v++)
{
if(!visited[v])
{
tag++;
visited[v] = TRUE;
Q.push(v);
while(!Q.empty())
{
u = Q.front();
Q.pop();
GetNeibournodeSet(u, NeibourNodeSet, TopMatrix);
for(i = 0; NeibourNodeSet[i] != NONE; i++)
{
w = NeibourNodeSet[i];
if(!visited[w])
{
visited[w] = TRUE;
Q.push(w);
}
}
}
}
}
if(tag > 1) return(FALSE);
else return(TRUE);
}
void CNodeViewDoc::GetNeibournodeSet(int NodeId, int *NeibourNodeSet, int (*TopMatrix)[NODENUM])
{
for(int i = 0; i < NodeCount; i++)
NeibourNodeSet[i] = NONE;
int k = 0;
for(int i = 0; i < NodeCount ; i++)
{
if(NodeId != i)
{
if(m_iMatrix[NodeId][i] != 0)
{
NeibourNodeSet[k] = i;
k++;
}
}
}
}
int CNodeViewDoc::GetNodeCount()
{
// Get the nodes's number in the array
return m_oaNodes.GetSize();
}
void CNodeViewDoc::SaveViewDocument()
{
int i, j;
MyPoint* ptPoint;
// Add the nodes to the array
ptPoint = pNodeCo;
for (i = 0; i < NodeCount; i++)
{
m_oaNodes.Add(ptPoint);
ptPoint++;
}
// Add the lines to the array
for (i = 0; i < NodeCount; i++)
for (j = i + 1; j < NodeCount; j++)
{
if(m_iMatrix[i][j] == 1)
{
AddLine(pNodeCo[i], pNodeCo[j]);
}
}
}
void CNodeViewDoc::OpenFile()
{
//将网络抓包器的流量文件转换为点到点流量文件
// 读取文件内容
CFileDialog *lpOpenFile;
CString FileData;
CString str;
CString m_GetFilePath;
lpOpenFile=new CFileDialog(TRUE,"","",OFN_FILEMUSTEXIST,
"Csv File(*.csv)|*.csv||");
int i;
char s[200];
for (i=0; i<MAXNUM; i++) {
memset(m_ptl[i],0,sizeof(m_ptl[i]));
memset(m_sip[i],0,sizeof(m_sip[i]));
memset(m_dip[i],0,sizeof(m_dip[i]));
memset(m_pkg[i],0,sizeof(m_pkg[i]));
m_netmb[i] = 0.00;
}
//获取网络访问日志
i = 0;
if(lpOpenFile->DoModal()==IDOK)
{
m_GetFilePath=lpOpenFile->GetPathName();
CStdioFile File;
File.Open(m_GetFilePath,CFile::modeRead);
File.ReadString(FileData); //忽略第一行标题
do
{
File.ReadString(FileData);
if(FileData.GetLength()<9)
break;
strcpy(s, FileData.GetBuffer());
char *p = strstr(s, ",");
*p = 0;
strcpy(m_ptl[i], s);
char *p1 = strstr(p+1, ",");
*p1 = 0;
char *ptr1 = strstr(p+1, ":");
if (ptr1!=NULL) *ptr1 = 0;
strcpy(m_sip[i], p+1);
char *p2 = strstr(p1+1, ",");
*p2 = 0;
char *ptr2 = strstr(p1+1, ":");
if (ptr2!=NULL) *ptr2 = 0;
strcpy(m_dip[i], p1+1);
char *p3 = strstr(p2+1, ",");
*p3 = 0;
strcpy(m_pkg[i], p2+1);
i++;
}while(1);
}
else {
delete lpOpenFile;
return;
}
delete lpOpenFile;
//文件读取结束
//统计各节点流量
for(int j=0; j<MAXNODE; j++)
node_netmb[j] = 0.00;
strcpy(node_ip1[0], m_sip[0]);
strcpy(node_ip2[0], m_dip[0]);
int node = 1, match; //当前线路统计数
for (int j=0; j<i; j++) { //轮循日志表
match = 0;
for (int k=0; k<node; k++) { //轮循node表
if ((strcmp(node_ip1[k],m_sip[j])==0
&& strcmp(node_ip2[k],m_dip[j])==0) ||
(strcmp(node_ip1[k],m_dip[j])==0
&& str