-
Notifications
You must be signed in to change notification settings - Fork 0
/
Final_Code.java
360 lines (272 loc) · 10.5 KB
/
Final_Code.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Blockchain Implementation in Vehicular Networks
// Imported date class so as to get timeStamp
import java.util.Date;
// Imported to make use of random numbers
import java.util.*;
// This is to use SHA-256 algorithm
import java.security.MessageDigest;
// Making our Block
class Block
{
// This is the data of our block
private String data;
// This is hash of previous block
public String previousHash;
// This is hash of current block
public String hash;
// This indicates when a block last updated
private long timeStamp;
private double trueness;
// Initializing all our members of Block
public Block(String data, String previousHash, double trueness)
{
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.trueness=trueness;
// Hash of current block is calculated as the aggregate of all the members present in that block
this.hash = current_block_hash_calculate();
}
public double getTruthValue()
{
return trueness;
}
public void setTruthValue(double val)
{
trueness=(trueness+val)/2;
}
// Calculatuing hash of current block
public String current_block_hash_calculate()
{
// Hash of current block is calculated as the aggregate of all the members present in that block
String current_hash = Sha_256(previousHash + Long.toString(timeStamp) + data);
return current_hash;
}
//Applying Sha256 algorithm to input string and getting the hash of that block
public static String Sha_256(String input)
{
// Wraping it inside try-catch as it is comes under checked exception
try
{
// Taking the Instance of SHA-256
MessageDigest md = MessageDigest.getInstance("SHA-256");
// Get our input in bytes
byte[] hash_in_bytes = md.digest(input.getBytes("UTF-8"));
// Representing our hash in hexidecimal
StringBuilder hash_in_hex = new StringBuilder();
for (int i = 0; i < hash_in_bytes.length; i++)
{
// This make sure our input is in Hex form only
String hex = Integer.toHexString(0xff & hash_in_bytes[i]);
hash_in_hex.append(hex);
}
return hash_in_hex.toString();
}
// catch block
catch(Exception e)
{
throw new RuntimeException(e);
}
}
}
// Making our class
class Car
{
private int Xcoordinate;
private int Ycoordinate;
Car(int x, int y)
{
Xcoordinate=x;
Ycoordinate=y;
}
public int getXcoordinate()
{
return Xcoordinate;
}
public int getYcoordinate()
{
return Ycoordinate;
}
public void setXcoordinate(int x)
{
Xcoordinate=x;
}
public void setYcoordinate(int y)
{
Ycoordinate=y;
}
}
class RSU
{
private int Xcoordinate;
private int Ycoordinate;
private String message;
RSU(int x, int y, String msg)
{
Xcoordinate=x;
Ycoordinate=y;
message=msg;
}
public int getXcoordinate()
{
return Xcoordinate;
}
public int getYcoordinate()
{
return Ycoordinate;
}
}
// Driver class
public class Main {
// Driver method
public static void main(String[] args) {
Random rdm = new Random();
double val=Math.random();
int xcord=rdm.nextInt(101)+200;
int ycord=rdm.nextInt(101)+200;
double tv1=Math.random();
double tv2=Math.random();
double tv3=Math.random();
String message;
// Making our 1st block
// Since it is our first block so its previous hash should be 0
Block GenesisBlock = new Block("This is our Genesis Block", "0",tv1);
//System.out.println("Genesis Block Hash is : " + GenesisBlock.hash);
//System.out.println("Hash of block previous to Genesis Block is : " +null);
//System.out.println();
// Making our 2nd block
// Since it is our 2nd block so its previous hash should be hash of genesis block (Block 1)
Block Block_2 = new Block("This is the 2nd Block",GenesisBlock.hash,tv2);
//System.out.println("Block 2 Hash is : " + Block_2.hash);
//System.out.println("Hash of block previous to Block 2 (i,e Block 1) is : " +GenesisBlock.hash);
//System.out.println();
// Making our 3rd block
// Since it is our 3rd block so its previous hash should be hash of Block 2
Block Block_3 = new Block("This is the 3rd Block",Block_2.hash,tv3);
//System.out.println("Block 3 Hash is : " + Block_3.hash);
//System.out.println("Hash of block previous to Block 3 (i,e Block 2) is : " +Block_2.hash);
Car c1=new Car(xcord,ycord);
Car c2=new Car(xcord-50,ycord);
Car c3=new Car(xcord-110,ycord);
System.out.println();
System.out.println("-----------------------------Initial Coordinates of Cars-------------------------");
System.out.println();
System.out.println("Initial Coordinates of Car 1 are ("+c1.getXcoordinate()+","+c2.getYcoordinate()+")");
System.out.println("Initial Coordinates of Car 2 are ("+c2.getXcoordinate()+","+c2.getYcoordinate()+")");
System.out.println("Initial Coordinates of Car 3 are ("+c3.getXcoordinate()+","+c3.getYcoordinate()+")");
System.out.println();
System.out.println("-----------------------------HAPPENING OF EVENT----------------------------------");
System.out.println();
System.out.print("EVENT HAPPENED IS: ");
if(GenesisBlock.getTruthValue()>=0.5 && val>=0.5)
{
message="Accident happened at coordinate ("+xcord+","+ycord+") of Car 1";
System.out.println(message);
GenesisBlock.setTruthValue(1);
}
else if(GenesisBlock.getTruthValue()<0.5 && val>=0.5)
{
message="False Accident message send by Car 1";
System.out.println(message);
GenesisBlock.setTruthValue(-1);
return;
}
else if(GenesisBlock.getTruthValue()>=0.5 && val<0.5)
{
message="Traffic Jam happened at coordinate ("+xcord+","+ycord+") of Car 1 ";
System.out.println(message);
GenesisBlock.setTruthValue(1);
}
else
{
message="False Traffic Jam message send by Car 1";
System.out.println(message);
GenesisBlock.setTruthValue(-1);
return;
}
System.out.println();
RSU rsu=new RSU(xcord,ycord+50, message);
Car[]arrayofcar=new Car[4];
arrayofcar[1]=c1;
arrayofcar[2]=c2;
arrayofcar[3]=c3;
boolean visited[]=new boolean[4];
visited[1]=true;
System.out.println();
System.out.println("-----------------------------HAPPENING OF V2V Communication----------------------");
System.out.println();
V2Vcommunication(arrayofcar, visited, 1);
System.out.println();
System.out.println("-----------------------------HAPPENING OF V2R Communication----------------------");
System.out.println();
System.out.println("Coordinates of RSU are ("+rsu.getXcoordinate()+","+rsu.getYcoordinate()+")");
V2Rcommunication(arrayofcar,visited, rsu);
System.out.println();
System.out.println("-----------------------------Final Coordinates of Cars---------------------------");
System.out.println();
System.out.println("Final Coordinates of Car 1 are ("+c1.getXcoordinate()+","+c1.getYcoordinate()+") ###### UNCHANGED");
System.out.println("Final Coordinates of Car 2 are ("+c2.getXcoordinate()+","+c2.getYcoordinate()+") ###### CHANGED");
System.out.println("Final Coordinates of Car 3 are ("+c3.getXcoordinate()+","+c3.getYcoordinate()+") ###### CHANGED");
}
public static void V2Vcommunication(Car[]arrayofcar, boolean[]visited, int carnumber)
{
if(carnumber==3)
return;
int q=carnumber;
for(;q<arrayofcar.length-1;q++)
{
int val1=Math.abs(arrayofcar[q].getXcoordinate()-arrayofcar[q+1].getXcoordinate());
int val2=Math.abs(arrayofcar[q].getYcoordinate()-arrayofcar[q+1].getYcoordinate());
double reach=Math.sqrt((val1*val1)+(val2*val2));
if(visited[q+1]==false)
{
if(reach<=50)
{
visited[q+1]=true;
System.out.println("Incident information passed from Car "+q+" to Car "+(q+1));
int xcord=arrayofcar[q+1].getXcoordinate();
int ycord=arrayofcar[q+1].getYcoordinate();
V2Vcommunication(arrayofcar, visited, carnumber+1);
System.out.println("Car "+(q+1)+" moves to Right from coordinate ("+xcord+","+ycord+") to coordinate ("+(xcord+25)+","+(ycord-25)+")");
arrayofcar[q+1].setXcoordinate(xcord+25);
arrayofcar[q+1].setYcoordinate(ycord-25);
break;
}
else
{
System.out.println("Incident information cannot be passed from Car "+q+ " to Car "+(q+1)+" because Car "+(q+1)+" is not in a range of Car "+q);
System.out.println("No other Car is in reach of Car "+q);
return;
}
}
}
System.out.println("No other Car is in reach of Car "+(q+2));
}
public static void V2Rcommunication(Car[]arrayofcar, boolean[]visited, RSU rsu)
{
System.out.println("Incident information passed from Car 1 to RSU ");
for(int q=2;q<arrayofcar.length;q++)
{
int val1=Math.abs(rsu.getXcoordinate()-arrayofcar[q].getXcoordinate());
int val2=Math.abs(rsu.getYcoordinate()-arrayofcar[q].getYcoordinate());
double reach=Math.sqrt((val1*val1)+(val2*val2));
if(reach<=150)
{
if(visited[q]==true)
System.out.println("Car "+q+" is in reach of RSU but it already gets V2V communication from Car 1");
else
{
System.out.println("Incident information passed from RSU to Car "+q);
int xcord=arrayofcar[q].getXcoordinate();
int ycord=arrayofcar[q].getYcoordinate();
System.out.println("Car "+q+" moves to Left from coordinate ("+xcord+","+ycord+") to coordinate ("+(xcord+25)+","+(ycord+25)+")");
arrayofcar[q].setXcoordinate(xcord+25);
arrayofcar[q].setYcoordinate(ycord+25);
}
}
else
System.out.println("Incident information cannot be passed from RSU to Car "+q+" because Car "+q+" is not in a range of RSU");
}
System.out.println("No other Car is in reach of RSU");
}
}