-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonWork.java
More file actions
executable file
·112 lines (90 loc) · 3.13 KB
/
JsonWork.java
File metadata and controls
executable file
·112 lines (90 loc) · 3.13 KB
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
import com.google.gson.Gson;
import com.sun.security.ntlm.Client;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.*;
import java.nio.charset.Charset;
import java.util.*;
import java.time.*;
/*êëàññ äëÿ óðîùåíèÿ ðàáîòû ïðè ãåíåðàöèè àäðåñà*/
class Country{
public String country;
public String[] cities;
public Country(String Country, String[] Cities) {
country = Country;
cities = Cities;
}
}
public class JsonWork {
/*ìåòîä ñîçäàåò ñëó÷àéíîãî êëèåíòà è âîçâðàùàåò îáúåêò ClientData*/
static ClientData GenerateData() {
Random rand = new Random();
ClientData client = new ClientData( GenerateIp(),
Math.abs(rand.nextLong()),
Duration.ofHours(Math.abs(rand.nextLong())%512),
GenerateModules(),
GenerateAddress());
return client;
}
/* ìåòîä ñîçäàåò ñëó÷àéíîãî êëèåíòà è âîçâðàùàåò ñòðîêó ôîðìàòà Json,
* îïèñûâàþùóþ îáúåêò ClientData*/
static String GenJsonClientData() {
Gson JsonClient = new Gson();
return JsonClient.toJson(GenerateData());
}
/*ìåòîä ãåíåðèðóåò àéïè*/
private static String GenerateIp() {
Random r = new Random();
return r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
}
/*Ìåòîä ãåíåðèðóåò ìàññèâ ìîäóëåé*/
private static ArrayList<Module> GenerateModules(){
Random rand = new Random();
/*ðàçìåð èìåíè ìîäóëÿ è êîëè÷åñòâà ìîäóëåé*/
int size = rand.nextInt(10) + 3;
ArrayList<Module> modules = new ArrayList<Module>();
for(int i = 0; i < size; i++) {
/*ñîçäàåì ñëó÷àéíóþ ñòðîêó - èìÿ ìîäóëÿ*/
byte[] array = new byte[size];
for(int j = 0; j < array.length; j++)
array[j] = (byte)('0' + rand.nextInt(74));
modules.add(new Module( new String(array, Charset.forName("UTF-8")),
Duration.ofHours(Math.abs(rand.nextLong())%64),
Math.abs(rand.nextLong())%100));
}
return modules;
}
private static Address GenerateAddress() {
Random rand = new Random();
Country[] countries = {
new Country("USA",new String[]{ "Franklin",
"Washington",
"Springfield",
"New York"}),
new Country("Russia",new String[]{ "Abakan",
"Borovsk",
"Chelyabinsk",
"Gus-Khrustalny",
"Moscow"}),
new Country("Germany",new String[]{ "Ahrensburg",
"Bleckede",
"Baumholder",
"Delitzsch",
"Munich"})};
/*âûáèðàåì ñëó÷àéíóþ ñòðàíó èç ìàññèâà*/
int country_index = rand.nextInt(countries.length);
/*âûáèðàåì ñëó÷àéíûé ãîðîä äàííîé ñòðàíû*/
int city_index = rand.nextInt(countries[country_index].cities.length);
/*ñîçäàåì àäðåñ*/
Address addr = new Address( countries[country_index].country,
countries[country_index].cities[city_index]);
return addr;
}
static String Serialize(ClientData client){
return new Gson().toJson(client);
}
static ClientData Deserialize(String json){
return new Gson().fromJson(json, ClientData.class);
}
}