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

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

Initial Release

File size: 4.7 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.
39 *
40 * @param settings the settings to use for this session
41 * @param repo the {@link RunningNegotiationsRepo} to use (mainly for
42 * testing)
43 * @param logger the {@link Reporter} to be used. Usually "Protocol" or
44 * "runserver". The logger is used both for logging protocol
45 * events and for logging events from this RunningNego
46 */
47 public RunningNego(NegoSettings settings, RunningNegotiationsRepo repo,
48 Reporter logger) {
49 this.log = logger;
50 protocol = settings.getProtocol(logger);
51 this.sessionid = "" + protocol.getRef().getURI() + (negoNr++);
52 this.repo = repo;
53 System.out.println("new session. logging to " + logdir);
54 protocol.addListener(evt -> handle(evt));
55 protocol.start(new PartyConnectionFactory(logger));
56 }
57
58 private void handle(ProtocolEvent evt) {
59 if (evt instanceof CurrentNegoState) {
60 NegoState state = ((CurrentNegoState) evt).getState();
61 writeLogFile(state);
62 if (state.isFinal(System.currentTimeMillis())) {
63 repo.remove(sessionid);
64 }
65 }
66 }
67
68 /**
69 * Write the (final) state to the log file.
70 *
71 * @param sessionState
72 * @throws IOException
73 */
74 protected void writeLogFile(NegoState sessionState) {
75 try {
76 PrintWriter out = new PrintWriter(
77 logdir.resolve(sessionid + ".json").toFile());
78 out.print(jackson.writeValueAsString(sessionState));
79 out.close();
80 } catch (IOException e) {
81 log.log(Level.SEVERE, "Failed to write log file for " + sessionid,
82 e);
83 }
84 }
85
86 /**
87 * Search from location of this class upwards till we find a directory
88 * called "domainsrepo".
89 *
90 * @return the root dir of the repo. This is the 'database' where all known
91 * profiles are stored. If not found , returns log dir in
92 * workingdirectory
93 * @throws URISyntaxException
94 */
95 protected static Path getLogDir() {
96 // If you run this normal from tomcat we are somewhere inside
97 // projectroot/WEB_INF/classes/....
98 // Search back upwards to projectroot.
99 Path dir;
100 try {
101 dir = Paths.get(RunningNego.class.getProtectionDomain()
102 .getCodeSource().getLocation().toURI()).getParent();
103 } catch (URISyntaxException e) {
104 throw new RuntimeException("Problem with path URI", e);
105 }
106 System.out.println("searching from " + dir);
107 while (dir.getNameCount() > 1) {
108 Path repo = dir.resolve(LOGDIRNAME);
109 if (repo.toFile().exists())
110 return repo;
111 System.out.println(
112 "Directory did not contain " + LOGDIRNAME + ":" + dir);
113 dir = dir.getParent();
114 }
115
116 // fallback to working dir
117 File file = new File(LOGDIRNAME);
118 if (!file.exists()) {
119 file.mkdir();
120 }
121 return file.toPath();
122 }
123
124 public NegoProtocol getProtocol() {
125 return protocol;
126 }
127
128 @Override
129 public String getID() {
130 return sessionid;
131 }
132
133 @Override
134 public int hashCode() {
135 final int prime = 31;
136 int result = 1;
137 result = prime * result
138 + ((protocol == null) ? 0 : protocol.hashCode());
139 result = prime * result
140 + ((sessionid == null) ? 0 : sessionid.hashCode());
141 return result;
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 if (this == obj)
147 return true;
148 if (obj == null)
149 return false;
150 if (getClass() != obj.getClass())
151 return false;
152 RunningNego other = (RunningNego) obj;
153 if (protocol == null) {
154 if (other.protocol != null)
155 return false;
156 } else if (!protocol.equals(other.protocol))
157 return false;
158 if (sessionid == null) {
159 if (other.sessionid != null)
160 return false;
161 } else if (!sessionid.equals(other.sessionid))
162 return false;
163 return true;
164 }
165
166}
Note: See TracBrowser for help on using the repository browser.