source: src/main/java/geniusweb/runserver/RunningNego.java@ 5

Last change on this file since 5 was 5, checked in by bart, 5 years ago

Fix consistency check LinearAdditiveUtilitySpace

File size: 4.8 KB
Line 
1package geniusweb.runserver;
2
3import java.io.File;
4import java.io.IOException;
5import java.io.PrintWriter;
6import java.net.URISyntaxException;
7import java.nio.file.Path;
8import java.nio.file.Paths;
9import java.util.logging.Level;
10
11import com.fasterxml.jackson.databind.ObjectMapper;
12
13import geniusweb.events.ProtocolEvent;
14import geniusweb.protocol.CurrentNegoState;
15import geniusweb.protocol.NegoProtocol;
16import geniusweb.protocol.NegoSettings;
17import geniusweb.protocol.NegoState;
18import tudelft.utilities.logging.Reporter;
19import tudelft.utilities.repository.RepoElement;
20
21/**
22 * Contains a currently running {@link NegoProtocol}. Listens to protocol
23 * events. Kept so that we can keep track of the amount of running sessions and
24 * show progress states.
25 */
26public class RunningNego implements RepoElement<String> {
27 private static final String LOGDIRNAME = "log";
28 private static final Path logdir = getLogDir();
29 private static int negoNr = 1; // generate unique IDs.
30 private static final ObjectMapper jackson = new ObjectMapper();
31
32 private final String sessionid;
33 private final transient Reporter log;
34 private final transient NegoProtocol protocol;
35 private final transient RunningNegotiationsRepo repo;
36
37 /**
38 * Create new session wth unique ID. You should call {@link #start()} to
39 * actually start this.
40 *
41 * @param settings the settings to use for this session
42 * @param repo the {@link RunningNegotiationsRepo} to use (mainly for
43 * testing)
44 * @param logger the {@link Reporter} to be used. Usually "Protocol" or
45 * "runserver". The logger is used both for logging protocol
46 * events and for logging events from this RunningNego
47 */
48 public RunningNego(NegoSettings settings, RunningNegotiationsRepo repo,
49 Reporter logger) {
50 this.log = logger;
51 this.protocol = settings.getProtocol(logger);
52 this.sessionid = "" + protocol.getRef().getURI() + (negoNr++);
53 this.repo = repo;
54 }
55
56 public void start() {
57 System.out.println("new session. logging to " + logdir);
58 repo.put(this);
59 protocol.addListener(evt -> handle(evt));
60 protocol.start(new PartyConnectionFactory(log));
61 }
62
63 private void handle(ProtocolEvent evt) {
64 if (evt instanceof CurrentNegoState) {
65 NegoState state = ((CurrentNegoState) evt).getState();
66 writeLogFile(state);
67 if (state.isFinal(System.currentTimeMillis())) {
68 repo.remove(sessionid);
69 }
70 }
71 }
72
73 /**
74 * Write the (final) state to the log file.
75 *
76 * @param sessionState
77 * @throws IOException
78 */
79 protected void writeLogFile(NegoState sessionState) {
80 try {
81 PrintWriter out = new PrintWriter(
82 logdir.resolve(sessionid + ".json").toFile());
83 out.print(jackson.writeValueAsString(sessionState));
84 out.close();
85 } catch (IOException e) {
86 log.log(Level.SEVERE, "Failed to write log file for " + sessionid,
87 e);
88 }
89 }
90
91 /**
92 * Search from location of this class upwards till we find a directory
93 * called "domainsrepo".
94 *
95 * @return the root dir of the repo. This is the 'database' where all known
96 * profiles are stored. If not found , returns log dir in
97 * workingdirectory
98 * @throws URISyntaxException
99 */
100 protected static Path getLogDir() {
101 // If you run this normal from tomcat we are somewhere inside
102 // projectroot/WEB_INF/classes/....
103 // Search back upwards to projectroot.
104 Path dir;
105 try {
106 dir = Paths.get(RunningNego.class.getProtectionDomain()
107 .getCodeSource().getLocation().toURI()).getParent();
108 } catch (URISyntaxException e) {
109 throw new RuntimeException("Problem with path URI", e);
110 }
111 System.out.println("searching from " + dir);
112 while (dir.getNameCount() > 1) {
113 Path repo = dir.resolve(LOGDIRNAME);
114 if (repo.toFile().exists())
115 return repo;
116 System.out.println(
117 "Directory did not contain " + LOGDIRNAME + ":" + dir);
118 dir = dir.getParent();
119 }
120
121 // fallback to working dir
122 File file = new File(LOGDIRNAME);
123 if (!file.exists()) {
124 file.mkdir();
125 }
126 return file.toPath();
127 }
128
129 public NegoProtocol getProtocol() {
130 return protocol;
131 }
132
133 @Override
134 public String getID() {
135 return sessionid;
136 }
137
138 @Override
139 public int hashCode() {
140 final int prime = 31;
141 int result = 1;
142 result = prime * result
143 + ((protocol == null) ? 0 : protocol.hashCode());
144 result = prime * result
145 + ((sessionid == null) ? 0 : sessionid.hashCode());
146 return result;
147 }
148
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj)
152 return true;
153 if (obj == null)
154 return false;
155 if (getClass() != obj.getClass())
156 return false;
157 RunningNego other = (RunningNego) obj;
158 if (protocol == null) {
159 if (other.protocol != null)
160 return false;
161 } else if (!protocol.equals(other.protocol))
162 return false;
163 if (sessionid == null) {
164 if (other.sessionid != null)
165 return false;
166 } else if (!sessionid.equals(other.sessionid))
167 return false;
168 return true;
169 }
170
171}
Note: See TracBrowser for help on using the repository browser.