source: src/main/java/genius/core/parties/SessionsInfo.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 3.6 KB
Line 
1package genius.core.parties;
2
3import java.io.FileInputStream;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.Serializable;
7import java.nio.file.Files;
8import java.nio.file.Path;
9
10import genius.core.Global;
11import genius.core.misc.FileTools;
12import genius.core.persistent.PersistentDataType;
13import genius.core.protocol.MultilateralProtocol;
14import genius.core.repository.ParticipantRepItem;
15import genius.core.repository.PartyRepItem;
16import genius.core.repository.ProfileRepItem;
17
18/**
19 * immutable info for all sessions.
20 */
21public class SessionsInfo {
22
23 private Path storageDir;
24 private final MultilateralProtocol protocol;
25 private PersistentDataType persistentDataType;
26 private boolean isPrintEnabled;
27
28 public SessionsInfo(MultilateralProtocol protocol, PersistentDataType type, boolean isPrintEnabled)
29 throws IOException {
30 if (type == null)
31 throw new NullPointerException("type");
32 this.persistentDataType = type;
33 this.protocol = protocol;
34 this.storageDir = Files.createTempDirectory("GeniusData");
35 this.isPrintEnabled = isPrintEnabled;
36 }
37
38 /**
39 * Try to return the stored data for given [party,profile] pair. If there is
40 * no file we return null. If there is a deserialization error, we delete
41 * the file and print an exception.
42 *
43 * @param party
44 * the {@link PartyRepItem}
45 * @param profile
46 * the {@link ProfileRepItem}
47 * @return storage for given agent and profile.
48 */
49 public Serializable getStorage(ParticipantRepItem party, ProfileRepItem profile)
50 throws ClassNotFoundException, IOException {
51 Path path = getPath(party, profile);
52 if (!Files.exists(path)) {
53 return null;
54 }
55
56 // file exists. Try to deserialize and return it
57 try {
58 return Global.deserializeObject(new FileInputStream(path.toFile()));
59 } catch (ClassNotFoundException | IOException e) {
60 try {
61 Files.delete(path);
62 } catch (IOException e1) {
63 e1.printStackTrace();
64 }
65 throw e;
66 }
67
68 }
69
70 /**
71 * @param party
72 * the {@link PartyRepItem}
73 * @param profile
74 * the {@link ProfileRepItem}
75 * @return the path to the temp file where the data is saved for given
76 * agentID + profile
77 */
78 public Path getPath(ParticipantRepItem party, ProfileRepItem profile) {
79 return storageDir.resolve(party.getUniqueName() + "-" + profile.getFullName());
80 }
81
82 /**
83 * Closes the SessionsInfo: removes the tmp dir, deletes all saved files.
84 * This SessionsInfo can not be used after calling this.
85 */
86 public void close() {
87 if (storageDir != null) {
88 FileTools.deleteDir(storageDir.toFile());
89 storageDir = null;
90 }
91 }
92
93 /**
94 * saves provided storageMap to the storageDir. The storageMap is just
95 * removed from disk if it's empty.
96 *
97 * @param content
98 * the data to save. If null, the old data is removed but no
99 * 'null' object is saved.
100 * @param party
101 * the {@link PartyRepItem}
102 * @param profile
103 * the {@link ProfileRepItem}
104 * @throws IOException
105 */
106 public void saveStorage(Serializable content, ParticipantRepItem party, ProfileRepItem profile) throws IOException {
107
108 Path path = getPath(party, profile);
109 if (Files.exists(path)) {
110 Files.delete(path);
111 }
112 if (content != null) {
113 Global.serializeObject(new FileOutputStream(path.toFile()), content);
114 }
115 }
116
117 public MultilateralProtocol getProtocol() {
118 return protocol;
119 }
120
121 public PersistentDataType getPersistentDataType() {
122 return persistentDataType;
123 }
124
125 /**
126 * True if print ot stdout is enabled.
127 *
128 * @return
129 */
130 public boolean isPrintEnabled() {
131 return isPrintEnabled;
132 }
133}
Note: See TracBrowser for help on using the repository browser.