1
1
package fi .helsinki .cs .tmc .spyware ;
2
2
3
3
import fi .helsinki .cs .tmc .spyware .eventsources .WindowStatechangesEventSource ;
4
- import fi .helsinki .cs .tmc .data .Exercise ;
5
4
import fi .helsinki .cs .tmc .events .TmcEvent ;
6
5
import fi .helsinki .cs .tmc .events .TmcEventBus ;
7
6
import fi .helsinki .cs .tmc .model .CourseDb ;
8
7
import fi .helsinki .cs .tmc .model .ServerAccess ;
9
- import fi .helsinki .cs .tmc .model .TmcProjectInfo ;
10
8
import fi .helsinki .cs .tmc .model .TmcSettings ;
11
9
import fi .helsinki .cs .tmc .spyware .eventsources .TextInsertEventSource ;
12
10
import fi .helsinki .cs .tmc .spyware .eventsources .ProjectActionCaptor ;
13
11
import fi .helsinki .cs .tmc .spyware .eventsources .ProjectActionEventSource ;
14
12
import fi .helsinki .cs .tmc .spyware .eventsources .SourceSnapshotEventSource ;
15
13
import fi .helsinki .cs .tmc .spyware .eventsources .TmcEventBusEventSource ;
16
- import fi .helsinki .cs .tmc .utilities .JsonMaker ;
17
14
import fi .helsinki .cs .tmc .utilities .TmcSwingUtilities ;
18
- import java .net .NetworkInterface ;
19
- import java .net .SocketException ;
20
- import java .net .UnknownHostException ;
21
- import java .nio .charset .Charset ;
22
- import java .util .ArrayList ;
23
- import java .util .Enumeration ;
24
- import java .util .List ;
25
- import java .util .logging .Level ;
15
+ import java .io .IOException ;
26
16
import java .util .logging .Logger ;
27
17
28
18
public class SpywareFacade implements SpywareSettings {
@@ -38,7 +28,6 @@ public static void start() {
38
28
instance = new SpywareFacade ();
39
29
TmcEventBus .getDefault ().post (new InvokedEvent ("spyware_loaded" ));
40
30
41
- updateHostInformation ();
42
31
}
43
32
44
33
public static void close () {
@@ -68,6 +57,7 @@ public void run() {
68
57
private TmcSettings settings ;
69
58
70
59
private EventSendBuffer sender ;
60
+ private EventReceiver taggingSender ;
71
61
72
62
private EventDeduplicater sourceSnapshotDedup ;
73
63
@@ -77,26 +67,51 @@ public void run() {
77
67
private TextInsertEventSource textInsertEventSource ;
78
68
private WindowStatechangesEventSource windowStatechangesEventSource ;
79
69
70
+ private static final class TaggingEventReceiver implements EventReceiver {
71
+
72
+ private final EventReceiver nextReceiver ;
73
+ private final int hostId ;
74
+
75
+ public TaggingEventReceiver (EventReceiver nextReceiver , int hostId ) {
76
+ this .nextReceiver = nextReceiver ;
77
+ this .hostId = hostId ;
78
+ }
79
+
80
+ @ Override
81
+ public void receiveEvent (LoggableEvent event ) {
82
+ event .setHostId (hostId );
83
+ nextReceiver .receiveEvent (event );
84
+ }
85
+
86
+ @ Override
87
+ public void close () throws IOException {
88
+ nextReceiver .close ();
89
+ }
90
+ }
91
+
92
+
80
93
public SpywareFacade () {
81
94
settings = TmcSettings .getDefault ();
82
95
83
96
sender = new EventSendBuffer (this , new ServerAccess (), CourseDb .getInstance (), new EventStore ());
84
97
sender .sendNow ();
85
98
86
- sourceSnapshotDedup = new EventDeduplicater (sender );
99
+ int hostId = new HostInformationGenerator ().updateHostInformation ();
100
+ taggingSender = new TaggingEventReceiver (sender , hostId );
101
+ sourceSnapshotDedup = new EventDeduplicater (taggingSender );
87
102
sourceSnapshotSource = new SourceSnapshotEventSource (this , sourceSnapshotDedup );
88
103
sourceSnapshotSource .startListeningToFileChanges ();
89
104
90
- projectActionSource = new ProjectActionEventSource (sender );
91
- tmcEventBusSource = new TmcEventBusEventSource (sender );
105
+ projectActionSource = new ProjectActionEventSource (taggingSender );
106
+ tmcEventBusSource = new TmcEventBusEventSource (taggingSender );
92
107
93
- windowStatechangesEventSource = new WindowStatechangesEventSource (sender );
108
+ windowStatechangesEventSource = new WindowStatechangesEventSource (taggingSender );
94
109
TmcSwingUtilities .ensureEdt (new Runnable () {
95
110
@ Override
96
111
public void run () {
97
112
ProjectActionCaptor .addListener (projectActionSource );
98
113
TmcEventBus .getDefault ().subscribeStrongly (tmcEventBusSource );
99
- textInsertEventSource = new TextInsertEventSource (sender );
114
+ textInsertEventSource = new TextInsertEventSource (taggingSender );
100
115
}
101
116
});
102
117
}
@@ -138,59 +153,4 @@ public InvokedEvent(String message) {
138
153
this .message = message ;
139
154
}
140
155
}
141
-
142
- private static void updateHostInformation () {
143
- JsonMaker data = getStaticHostInformation ();
144
- // Should be unique enough not to collapse among singe users machines.
145
- int hostId = data .toString ().hashCode ();
146
- LoggableEvent .setGlobalHostId (hostId );
147
-
148
- data .add ("hostId" , hostId );
149
-
150
- LoggableEvent event = new LoggableEvent ("host_information_update" , data .toString ().getBytes (Charset .forName ("UTF-8" )));
151
- TmcEventBus .getDefault ().post (event );
152
- }
153
-
154
- private static JsonMaker getStaticHostInformation () {
155
- JsonMaker builder = JsonMaker .create ();
156
-
157
- try {
158
- java .net .InetAddress localMachine = java .net .InetAddress .getLocalHost ();
159
- builder .add ("hostAddress" , localMachine .getHostAddress ());
160
- builder .add ("hostName" , localMachine .getHostName ());
161
- } catch (Exception ex ) {
162
- log .log (Level .WARNING , "Exception while getting host name information: {0}" , ex );
163
- }
164
-
165
- try {
166
- Enumeration <NetworkInterface > iterator = NetworkInterface .getNetworkInterfaces ();
167
- List <String > macs = new ArrayList <String >(2 );
168
- while (iterator .hasMoreElements ()) {
169
- NetworkInterface networkInterface = iterator .nextElement ();
170
- if (networkInterface .isUp () && !networkInterface .isLoopback ()) {
171
- byte [] mac = networkInterface .getHardwareAddress ();
172
- StringBuilder sb = new StringBuilder ();
173
- for (int i = 0 ; i < mac .length ; i ++) {
174
- sb .append (String .format ("%02X%s" , mac [i ], (i < mac .length - 1 ) ? ":" : "" ));
175
- }
176
- macs .add (sb .toString ());
177
- }
178
- }
179
- builder .add ("mac_addresses" , macs );
180
-
181
- } catch (Exception ex ) {
182
- log .log (Level .WARNING , "Exception while getting host mac information: {0}" , ex );
183
- }
184
-
185
- try {
186
- builder .add ("user.name" , System .getProperty ("user.name" ));
187
- builder .add ("java.runtime.version" , System .getProperty ("java.runtime.version" ));
188
- builder .add ("os.name" , System .getProperty ("os.name" ));
189
- builder .add ("os.version" , System .getProperty ("os.version" ));
190
- } catch (Exception e ) {
191
- log .log (Level .WARNING , "Exception while getting basic host information: {0}" , e );
192
- }
193
-
194
- return builder ;
195
- }
196
156
}
0 commit comments