-
Notifications
You must be signed in to change notification settings - Fork 0
/
Psimulator.java
115 lines (92 loc) · 2.96 KB
/
Psimulator.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
/*
* created 2.2.2012
*/
package psimulator2;
import config.configTransformer.Saver;
import device.Device;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import logging.Loggable;
import logging.Logger;
import logging.LoggingCategory;
import logging.SystemListener;
import logging.networkEvents.EventServer;
import psimulator.dataLayer.DataLayerFacade;
import shared.Components.NetworkModel;
import shared.Serializer.AbstractNetworkSerializer;
import shared.Serializer.NetworkModelSerializerXML;
import shared.Serializer.SaveLoadException;
import utils.Alarm;
/**
* Instance of Psimulator.
* Pouzit navrhovy vzor Singleton.
*
* @author Stanislav Rehak <[email protected]>
* @author Tomas Pitrinec
*/
public class Psimulator implements Loggable {
public final List<Device> devices = new ArrayList<>();
public NetworkModel configModel;
public String lastConfigFile;
public Alarm budik;
public SystemListener systemListener;
public EventServer eventServer;
private String realInterface;
private Psimulator() {
budik = new Alarm();
}
/**
* Metoda na ulozeni
*
* @param configFileName
* @return
*/
public int saveSimulatorToConfigFile(String configFileName) {
int vratit = 0;
if (!configFileName.isEmpty()) {
lastConfigFile = configFileName;
}
//ukladani do ukladacich struktur:
Saver saver = new Saver(configModel);
saver.saveToModel();
AbstractNetworkSerializer serializer = new NetworkModelSerializerXML(); // vytvori se serializer
//ukladani do samotnyho souboru:
try {
serializer.saveNetworkModelToFile(configModel, new File(lastConfigFile)); // nacita se xmlko do ukladacich struktur
Logger.log(Psimulator.getNameOfProgram(), Logger.IMPORTANT, LoggingCategory.XML_LOAD_SAVE, "File succesfully saved into " + lastConfigFile);
} catch (SaveLoadException ex) {
Logger.log(this, Logger.DEBUG, LoggingCategory.XML_LOAD_SAVE, "Hozena vyjimka: ", ex);
Logger.log(Logger.WARNING, LoggingCategory.XML_LOAD_SAVE, "Cannot save network model to: " + configFileName);
vratit = 1;
}
return vratit;
}
@Override
public String getDescription() {
return "Class Psimulator";
}
public Device getDeviceByName(String name) {
for (Device d : devices) {
if (d.getName().equals(name)) {
return d;
}
}
return null;
}
// staticky veci:
public static String getNameOfProgram() {
return "psimulator2";
}
private static volatile Psimulator instance;
public static Psimulator getPsimulator() {
if (instance == null) {
synchronized (Psimulator.class) {
if (instance == null) {
instance = new Psimulator();
}
}
}
return instance;
}
}