You are on page 1of 12

static void Main()

{
}

Application.Run(new Form1());

private void Form1_Load(object sender, System.EventArgs e)


{
}
Graphics g;
Bitmap bmp;
private void button1_Click(object sender, System.EventArgs e)
{
bmp=new Bitmap(pictureBox1.Width,pictureBox1.Height);
g=Graphics.FromImage(bmp);
pictureBox1.Image=bmp;
//g.DrawLine(new Pen(new SolidBrush(Color.Red),2),0,130,400,130);
g.DrawLine(new Pen(new
SolidBrush(Color.Red),2),0,0,pictureBox1.Width,0);
g.DrawLine(new Pen(new
SolidBrush(Color.Red),2),0,0,0,pictureBox1.Height);
g.DrawLine(new Pen(new
SolidBrush(Color.Red),2),0,pictureBox1.Height,pictureBox1.Width,pictureBox1.Height);
g.DrawLine(new Pen(new
SolidBrush(Color.Red),2),pictureBox1.Width,0,pictureBox1.Width,pictureBox1.Height);

int var=20;
for(int i=0;(i*var+10)<=pictureBox1.Width-10;i++)
{
g.DrawLine(new Pen(new SolidBrush(Color.Red),2),(var*i+10),10,
(var*i+10),pictureBox1.Height-10);
}
for(int j=0;(j*var+10)<=pictureBox1.Height-10;j++)
{
g.DrawLine(new Pen(new SolidBrush(Color.Red),2),10,
(var*j+10),pictureBox1.Height-10,(var*j+10));
}
for(int i=0;i<ob.Length;i++)
{
g.DrawRectangle(new Pen(new
SolidBrush(Color.Blue),4),ob[i].x,ob[i].y,2,2);
g.DrawString(i.ToString(),new Font("Arial",7),new
SolidBrush(Color.Blue),ob[i].x+4,ob[i].y+4);
//g.DrawRectangles(new Pen(new SolidBrush(Color.Green),2),new
Rectangle(ob[i].x,ob[i].y,2,2));
}
}
private void pictureBox1_Click(object sender, System.EventArgs e)
{
}

Mobile[] ob;
double[,] Distance;
int MaxMobile=0;
private void button2_Click(object sender, System.EventArgs e)
{
MaxMobile=int.Parse(txtMobileQty.Text);
RouteDiscover.MAX=MaxMobile;
Distance=new double[MaxMobile,MaxMobile];
int i=0;
ob=new Mobile[MaxMobile];
Random rx=new Random();
Random ry=new Random();
//Random xr=new Random();
//Random yr=new Random();
for( i=0;i<ob.Length;i++)
{

ob[i]=new Mobile();
ob[i].x=rx.Next(400);
ob[i].y=ry.Next(400);
ob[i].x=rx.Next(400);
ob[i].startx=ob[i].x;
ob[i].starty=ob[i].y;
//ob[i].startx=ob[i].x;
//ob[i].y=ry.Next(400);

//MessageBox.Show("x "+ob[i].x.ToString()

+"y " +ob[i].y.ToString());


}
}
private void button3_Click(object sender, System.EventArgs e)
{
for(int i=0;i<MaxMobile;i++)
{
for(int j=0;j<MaxMobile;j++)
{
if(i!=j)
{
Distance[i,j]=Dist.CalcDist(ob[i].x,ob[i].y,ob[j].x,ob[j].y);
//MessageBox.Show("i="+i.ToString()+"
j="+j.ToString()+" "+Distance[i,j].ToString());
}
else
{
Distance[i,j]=0;
}
}
}

}
int []paths;
int Control=0;
void RePath()
{Control++;

int src,dst;
paths=null;
src=int.Parse(textBox1.Text);
dst=int.Parse(textBox2.Text);
try
{
RREQ req=new RREQ(0,src,dst,50,0);

RouteDiscover rd=new RouteDiscover();


paths=rd.Discover(req,Distance);
for(int i=0;i<paths.Length-1;i++)
{
//MessageBox.Show(paths[i].ToString());
g.DrawLine(new Pen(new
SolidBrush(Color.Ivory),05),ob[paths[i]].x,ob[paths[i]].y,ob[paths[i+1]].x,ob[paths[i+1]].y);
pictureBox1.Image=bmp;
}
}
catch(Exception ex)
{
}
}
private void button4_Click(object sender, System.EventArgs e)
{
int src,dst;
src=int.Parse(textBox1.Text);
dst=int.Parse(textBox2.Text);
try
{
RREQ req=new RREQ(0,src,dst,50,0);

RouteDiscover rd=new RouteDiscover();


paths=rd.Discover(req,Distance);
for(int i=0;i<paths.Length-1;i++)
{
//MessageBox.Show(paths[i].ToString());
g.DrawLine(new Pen(new
SolidBrush(Color.CornflowerBlue),05),ob[paths[i]].x,ob[paths[i]].y,ob[paths[i+1]].x,ob[paths[i+1]]
.y);
pictureBox1.Image=bmp;
}
}

catch(Exception ex)
{
}

private void txtMobileQty_TextChanged(object sender, System.EventArgs e)


{
}
private void button5_Click(object sender, System.EventArgs e)
{
/*for(int i=0;i<ob.Length;i++)
{

}*/
for(int i=0;i<ob.Length;i++)
{
Random rd=new Random(MaxMobile);
do
{
ob[0].dst_mov=rd.Next(MaxMobile);
} while(ob[0].dst_mov==0);//src
//for(int i=0;i<ob.Length;i++)
//
"+ob[i].dst_mov.ToString());

MessageBox.Show("0 will move towards

ob[i].movx=ob[ob[i].dst_mov].startx-ob[i].startx;
ob[i].movy=ob[ob[i].dst_mov].starty-ob[i].starty;
//MessageBox.Show(ob[0].movx.ToString()+ ","
+ob[0].movy.ToString());

//g.DrawLine(new Pen(new
SolidBrush(Color.Green),03),ob[0].x,ob[0].y,ob[ob[0].dst_mov].startx,ob[ob[0].dst_mov].starty);

//timer2.Enabled=true;

int count=0;

}
private void timer1_Tick(object sender, System.EventArgs e)
{TotData=int.Parse(txtData.Text);
/***************** Distance updation **********/
for(int i=0;i<MaxMobile;i++)
{
for(int j=0;j<MaxMobile;j++)
{
if(i!=j)
{

Distance[i,j]=Dist.CalcDist(ob[i].x,ob[i].y,ob[j].x,ob[j].y);
//MessageBox.Show("i="+i.ToString()+"
j="+j.ToString()+" "+Distance[i,j].ToString());
}
else
{
Distance[i,j]=0;
}
}
}
/*********************************************/
if(count==MaxMobile)
{//timer1.Enabled=false;
}
for(int i=0;i<ob.Length;i++)
{
ob[i].prevx=ob[i].x;
ob[i].prevy=ob[i].y;
if((ob[i].movx==0) && (ob[i].movy==0))
{
ob[i].IsMove=false;
count++;
}
//if(ob[i].IsMove==false)
//
return;
if(ob[i].IsMove==true)
{
if(ob[i].movx>0)
{
ob[i].x++;
ob[i].movx--;
}
else if(ob[i].movx<0)
{
ob[i].x--;
ob[i].movx=ob[i].movx+1;
}
if(ob[i].movy>0)
{
ob[i].y=ob[i].y+1;
ob[i].movy--;
}
else if(ob[i].movy<0)
{
ob[i].y=ob[i].y-1;
ob[i].movy++;
}
g.DrawLine(new Pen(new
SolidBrush(Color.Green),10),ob[i].x,ob[i].y,ob[i].prevx,ob[i].prevy);
pictureBox1.Image=bmp;
}
}
//MessageBox.Show("line");
if(DataInd==TotData)

timer1.Enabled=false;
if(Loss==0)
{
Loss=TotData/2;
}
if(Loss==TotData)
{ Loss=TotData/2;
}
txtLoss.Text=Loss.ToString();

}
else
Transmit();
DataInd++;
}
int Loss=0;
Datapack data=new Datapack();
int DistTh=150;
Double tmpDist=0;
void Transmit()
{

try
{
int i=0;
while(i<paths.Length-1)
{
//MessageBox.Show(paths[i].ToString());
tmpDist=Dist.CalcDist(ob[paths[i]].x,ob[paths[i]].y,ob[paths[i+1]].x,ob[paths[i+1]].y);
if(tmpDist<=int.Parse(txtTh.Text))
{
g.DrawLine(new Pen(new
SolidBrush(Color.RosyBrown),2),ob[paths[i]].x,ob[paths[i]].y,ob[paths[i+1]].x,ob[paths[i+1]].y);
pictureBox1.Image=bmp;
}
else
{

Loss++;
return;
/*
if(rdWith.Checked==true)
{// Generate the Path
bool tf1=false,tf2=false;
if(timer1.Enabled==true)
{
tf1=true;
timer1.Enabled=false;
}
if(timer2.Enabled==true)

}*/

tf2=true;
timer2.Enabled=false;

}
MessageBox.Show("new RREQ would be generated");
//RePath();
//DistTh=DistTh+50;
if(tf1==true)
{
timer1.Enabled=true;
}
if(tf2==true)
{
timer2.Enabled=true;
}
}
i++;

}
}
catch(Exception ex)
{
}

int TotData=0;
Datapack[] dp;
int DataInd=0;

private void button6_Click(object sender, System.EventArgs e)


{
timer1.Enabled=true;
}
private void timer2_Tick(object sender, System.EventArgs e)
{

}
private void label4_Click(object sender, System.EventArgs e)
{
}
private void button7_Click(object sender, System.EventArgs e)
{
bool tf1=false,tf2=false;

if(timer1.Enabled==true)
{tf1=true;
timer1.Enabled=false;
}
if(timer2.Enabled==true)
{
tf2=true;
timer2.Enabled=false;
}
SaveFileDialog sd=new SaveFileDialog();
sd.ShowDialog();
if(sd.FileName!=null)
pictureBox1.Image.Save(sd.FileName);
if(tf1==true)
{
timer1.Enabled=true;
}
if(tf2==true)
{
timer2.Enabled=true;
}
}
private void rdWith_CheckedChanged(object sender, System.EventArgs e)
{
}
private void button8_Click(object sender, System.EventArgs e)
{

int Thr=50;

}
private void timer3_Tick(object sender, System.EventArgs e)
{
}
#region result store

int InsertAodv(int mobiles, int data, int loss)


{
string connectionString = @"Data Source=KUSHBOO-PC\SQLEXPRESS; Initial Catalog=
aodv; Integrated Security= true";
//string connectionString = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=AODV.mdf;Integrated Security= true";
//Data
Source=.\SQLEXPRESS;AttachDbFilename=E:\M_TECH\SWATI\simulator\simulator\AODV.mdf;Integ
rated Security=True;User Instance=True
//string connectionString = "server=\'(local)\'; trusted_connection=true;
database=\'aodv\'";
System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(connectionString);

string queryString = "INSERT INTO [results] ([mobiles], [data], [loss])


VALUES (@mobiles, @data, @loss)" + "";
System.Data.IDbCommand dbCommand = new
System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDataParameter dbParam_mobiles = new
System.Data.SqlClient.SqlParameter();
dbParam_mobiles.ParameterName = "@mobiles";
dbParam_mobiles.Value = mobiles;
dbParam_mobiles.DbType = System.Data.DbType.Int32;
dbCommand.Parameters.Add(dbParam_mobiles);
System.Data.IDataParameter dbParam_data = new
System.Data.SqlClient.SqlParameter();
dbParam_data.ParameterName = "@data";
dbParam_data.Value = data;
dbParam_data.DbType = System.Data.DbType.Int32;
dbCommand.Parameters.Add(dbParam_data);
System.Data.IDataParameter dbParam_loss = new
System.Data.SqlClient.SqlParameter();
dbParam_loss.ParameterName = "@loss";
dbParam_loss.Value = loss;
dbParam_loss.DbType = System.Data.DbType.Int32;
dbCommand.Parameters.Add(dbParam_loss);
int rowsAffected = 0;
dbConnection.Open();
try
{
rowsAffected = dbCommand.ExecuteNonQuery();
}
finally
{
dbConnection.Close();
}

return rowsAffected;

#endregion
private void button8_Click_1(object sender, System.EventArgs e)
{
int
i=InsertAodv(int.Parse(txtMobileQty.Text),int.Parse(txtData.Text),int.Parse(txtLoss.Text));
if(i>0)
{
MessageBox.Show("Data Inserted");
}
}
Results [] res=new Results[100];
int ri=0;
void ResultsFetch()
{
string connectionString = @"Data Source=KUSHBOO-PC\SQLEXPRESS; Initial Catalog=
aodv; Integrated Security= true";

System.Data.IDbConnection dbConnection = new


System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "SELECT * FROM results order by data";
System.Data.IDbCommand dbCommand = new
System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
dbConnection.Open();
System.Data.IDataReader dataReader =
dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
try
{
while(dataReader.Read())
{
res[ri]=new Results();
res[ri].Data=int.Parse(dataReader[1].ToString());
res[ri].Loss=int.Parse(dataReader[2].ToString());
res[ri].Mobile=int.Parse(dataReader[0].ToString());
ri++;

}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}

private void button9_Click(object sender, System.EventArgs e)


{
bmp=new Bitmap(pictureBox1.Width,pictureBox1.Height);
g=Graphics.FromImage(bmp);
pictureBox1.Image=bmp;
g.DrawLine(new Pen(new
SolidBrush(Color.White),2),10,10,10,pictureBox1.Height-10);
g.DrawLine(new Pen(new
SolidBrush(Color.White),2),10,pictureBox1.Height-10,pictureBox1.Width-10,pictureBox1.Height10);
ResultsFetch();
try
{
for(int i=0;i<ri-1;i++)
{
g.DrawLine(new Pen(new
SolidBrush(Color.Yellow),2),res[i].Data*2,400-((res[i].Data-res[i].Loss)*5),res[i+1].Data*2,400(res[i+1].Data-res[i+1].Loss)*5);
}
}
catch(Exception ex)
{
}

g.DrawString ("Data V/s Output",new Font("Arial",14),new


SolidBrush(Color.Pink),40,40);
pictureBox1.Image=bmp;
}
private void button10_Click(object sender, System.EventArgs e)
{
RePath();
}
int DelRes()
{
string connectionString = @"Data Source=KUSHBOO-PC\SQLEXPRESS; Initial Catalog=
aodv; Integrated Security= true";
System.Data.IDbConnection dbConnection = new
System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "DELETE FROM [results]";
System.Data.IDbCommand dbCommand = new
System.Data.SqlClient.SqlCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
int rowsAffected = 0;
dbConnection.Open();
try
{
rowsAffected = dbCommand.ExecuteNonQuery();
}
finally
{
dbConnection.Close();
}

return rowsAffected;

private void button11_Click(object sender, System.EventArgs e)


{
int i=DelRes();
if(i>0)
{
MessageBox.Show("Results Deleted");
}
}
private void txtLoss_TextChanged(object sender, EventArgs e)
{
}

You might also like