115 Commits
old ... v1.0.0

Author SHA1 Message Date
a360dc708e remove testing log file to clean up repository 2025-12-08 00:08:11 +00:00
3250d5a433 Merge pull request #39 from davidalves04/dev
dev merge
2025-12-07 23:58:33 +00:00
65cb0b52f6 Merge branch 'main' into dev 2025-12-07 23:55:33 +00:00
87a8a1bcb6 Merge pull request #38 from davidalves04/cleanup
Cleanup
2025-12-07 23:23:04 +00:00
bce15fe90f Merge branch 'dev' into cleanup 2025-12-07 23:21:44 +00:00
542ce9c8c0 Merge pull request #37 from davidalves04/javadoc
Javadoc
2025-12-07 23:18:41 +00:00
926245986f finish 1st javadoc round 2025-12-07 22:39:21 +00:00
3a3756f701 Translate graph labels and titles to PT 2025-12-07 20:12:43 +00:00
83c3d65e38 more javadoc - dashboard, des, logging 2025-12-07 19:57:40 +00:00
a8ce95e08c Refactor and enhance documentation across multiple classes (Analysis through DashboardStatistics) 2025-12-07 19:33:40 +00:00
Gaa56
b624cfe11e Documentation 2025-12-07 15:51:50 +00:00
a2f9e725de feat: Implement batch performance analysis dialog and routing policies
- Added BatchAnalysisDialog for running multiple simulations and generating reports.
- Implemented LeastCongestedRouteSelector for dynamic routing based on congestion levels.
- Created RandomRouteSelector for baseline random routing strategy.
- Developed ShortestPathRouteSelector to select routes based on the shortest path.
- Defined RouteSelector interface to standardize routing policy implementations.
- Introduced RoutingPolicy enum to manage available routing strategies.
2025-12-07 00:35:06 +00:00
92ff883d4c fixed dash formatting 2025-12-06 00:59:09 +00:00
ea33d61a9e removed tests 2025-12-05 02:42:31 +00:00
240563419b removed empty impl test files 2025-12-05 02:38:11 +00:00
90db380f61 Dash editor and DES impl 2025-12-05 02:29:33 +00:00
David Alves
8fe4e564d3 Update Diagrama de arquitetura - SD.jpg 2025-12-04 22:27:01 +00:00
David Alves
e389c0711e Updated architecture diagram image 2025-12-04 22:25:55 +00:00
d28a77b6a4 small fixes + debug 2025-11-29 00:07:53 +00:00
1b6ad03057 Merge pull request #35 from davidalves04/cleanup
Refactor Simulation Core & Enhance Dashboard UI
2025-11-27 20:20:34 +00:00
David Alves
24fe1c1d67 Update Diagrama de arquitetura - SD.drawio 2025-11-27 19:54:14 +00:00
David Alves
766eabbbe4 Update .gitignore 2025-11-27 15:49:03 +00:00
David Alves
d7b1de1fe3 Update Diagrama de arquitetura - SD.drawio 2025-11-27 15:47:30 +00:00
David Alves
96b3a66b96 Diagram update
@0x1eo @Gaa56 o que acham do diagrama? Mudariam/acrescentariam algo? As legendas, parecem-vos bem?
2025-11-27 15:46:02 +00:00
David Alves
29848b04a6 Update architecture diagram 2025-11-23 23:16:08 +00:00
173d9e54ce test: Reduce traffic light coordination test monitoring duration from 60s to 10s 2025-11-23 23:06:08 +00:00
5202032471 feat: Dynamically set simulation log file path using OS temporary directory and remove isSimulationRunning method. 2025-11-23 23:03:07 +00:00
46d148c9d5 Allow manual trigger for publish-release job 2025-11-23 22:23:13 +00:00
0d85d010bf Sync CI with main branch 2025-11-23 22:14:10 +00:00
043ba7d185 Add workflow_dispatch trigger to Maven CI 2025-11-23 22:12:35 +00:00
25f2876c34 Refactor GitHub Actions workflow for Maven build 2025-11-23 22:10:40 +00:00
7cbecc4fab Update Maven workflow for JDK setup and packaging 2025-11-23 22:00:49 +00:00
906e958729 feat: Introduce Launcher class as the application entry point and update pom.xml to use it. 2025-11-23 21:53:52 +00:00
72db59415f Add Windows build job to Maven workflow 2025-11-23 21:53:33 +00:00
19709f0d7a feat: update main class to sd.dashboard.DashboardUI in pom.xml configurations. 2025-11-23 21:29:38 +00:00
13fa2f877d refactor: improve traffic light queue processing, add graceful intersection shutdown, and remove obsolete event and serialization classes. 2025-11-23 21:23:33 +00:00
96c5680f41 moved start to dashboard + fixed holding queue - looped sleep might be fine in this case + better customization via CSS file 2025-11-22 23:52:51 +00:00
60b4f0c2b6 Add 'cleanup' branch to Maven CI workflow 2025-11-22 22:53:54 +00:00
d74517a27b starting the codebase cleanup for final delivery- single process prototype removal 2025-11-22 22:52:01 +00:00
ce7f642246 slight sim change and engine code fomat 2025-11-22 21:45:16 +00:00
8f97aab836 Merge pull request #34 from davidalves04/dev
testing
2025-11-22 21:43:33 +00:00
David Alves
86c0c4b5b3 Add configurable travel times by vehicle type
@0x1eo can u check this pls
2025-11-22 16:18:02 +00:00
6fdcf376b2 i might kms 2025-11-22 00:13:19 +00:00
81f842e2bb Change CI branch from 'main' to 'dev' 2025-11-19 20:54:47 +00:00
David Alves
ecb70fa6a2 Merge pull request #33 from davidalves04/17-create-dashboardserver-process
Dashboard Server Implementation
2025-11-19 19:16:50 +00:00
06f079ce5b fix intersections starting independently with no coordination 2025-11-18 14:29:11 +00:00
72893f87ae added dashboard server and built an example implementation for the message protocol 2025-11-14 02:01:51 +00:00
6b94d727e2 shutdown and teardown fixes + incoming connection handler 2025-11-11 17:28:44 +00:00
84cba39597 bullshit fixes 2025-11-06 20:31:59 +00:00
5dc1b40c88 Merge pull request #32 from davidalves04/14-create-trafficlightthread-class
14 create trafficlightthread class
2025-11-06 13:53:12 +00:00
3117bdf332 Merge branch 'dev' into 14-create-trafficlightthread-class 2025-11-06 13:53:01 +00:00
1140c3ca48 Merge pull request #30 from davidalves04/13-create-exit-node-process
13 create exit node process
2025-11-06 13:49:21 +00:00
Gaa56
484cba1eee Update TrafficLightThread 2025-11-05 13:21:10 +00:00
Gaa56
0e5526c3f6 Merge pull request #31 from davidalves04/dev
Dev
2025-11-05 12:37:48 +00:00
David Alves
cf88db4297 Add traffic light coordination and tests
Sorry to add this on this branch ahah
2025-11-05 12:09:32 +00:00
David Alves
0960a7a141 Add ExitNodeProcess and unit tests 2025-11-05 11:54:34 +00:00
David Alves
3b4f968a59 Merge pull request #29 from davidalves04/12-implement-coordinatorgenerator-process
Coordinator Process Implementation
2025-11-03 00:02:56 +00:00
0c256ad6f5 Fix Intersection Destination - Doubled Advance 2025-11-02 23:56:54 +00:00
340e436063 Merge branch 'dev' into 12-implement-coordinatorgenerator-process 2025-11-02 23:21:36 +00:00
1684a6713e Implementation of the Coordinator Process 2025-11-02 23:17:15 +00:00
22a7081ade Merge pull request #28 from davidalves04/10-create-network-communication-classes
Fix Serialization
2025-11-02 22:39:38 +00:00
3b699556db Merge branch 'dev' into 10-create-network-communication-classes 2025-11-02 22:39:26 +00:00
Gaa56
d078808486 Update SocketConnection 2025-10-30 19:25:27 +00:00
Gaa56
98581b562d Merge pull request #27 from davidalves04/9-design-message-protocol-specification
#10 Req
2025-10-30 18:44:54 +00:00
Gaa56
4710c96450 Create TrafficLightThread Class 2025-10-30 18:06:02 +00:00
f9644bd18c Merge pull request #26 from davidalves04/dev
#12 Req.
2025-10-30 16:09:04 +00:00
David Alves
c6b710ac52 Merge pull request #25 from davidalves04/11-convert-intersection-to-standalone-process
11 convert intersection to standalone process
2025-10-30 16:00:05 +00:00
David Alves
dc4f567e1f Move vehicle route advancement to intersection arrival 2025-10-30 15:57:58 +00:00
David Alves
db5e01021a Refactor IntersectionProcess and add unit tests 2025-10-30 10:41:17 +00:00
David Alves
dab0651dbd Corrected directions 2025-10-29 22:36:58 +00:00
David Alves
4772add574 Merge pull request #24 from davidalves04/dev
Dev
2025-10-27 23:04:15 +00:00
David Alves
ae27115791 Merge pull request #23 from davidalves04/11-convert-intersection-to-standalone-process
Create IntersectionProcess main class
2025-10-27 22:58:55 +00:00
David Alves
684fb408ef Create IntersectionProcess main class 2025-10-27 22:53:37 +00:00
David Alves
d057adeab3 Revert "Enunciado uploaded"
This reverts commit be4e7f66d6.
2025-10-27 22:52:19 +00:00
David Alves
be4e7f66d6 Enunciado uploaded 2025-10-27 18:03:17 +00:00
David Alves
108d2e544c Enunciado uploaded 2025-10-27 18:02:24 +00:00
fd26063f6e Merge pull request #22 from davidalves04/10-create-network-communication-classes
Create network communication classes
2025-10-27 12:29:22 +00:00
Gaa56
d8b59cc502 Deleted MessageSerializer 2025-10-27 09:18:33 +00:00
Gaa56
06c34a198a Removed MessageSerializer 2025-10-27 09:15:33 +00:00
Gaa56
1524188b29 Add connection retry logic 2025-10-26 17:00:34 +00:00
Gaa56
bc1a8da160 Create MessageSerializer utility 2025-10-25 18:00:58 +01:00
Gaa56
96903e4b7c SocketConnection 2025-10-25 17:43:25 +01:00
Gaa56
6c5eab0e72 Create SocketConnection wrapper class 2025-10-25 17:41:55 +01:00
23f7a74798 Add dependency build to CI job 2025-10-24 20:20:15 +01:00
d7dec0d73e Merge pull request #21 from davidalves04/9-design-message-protocol-specification
Mmessage protocol specification
2025-10-24 20:12:18 +01:00
David Alves
534a880e3e Remove unused supports method from MessageSerializer 2025-10-24 12:02:03 +01:00
David Alves
ba3233eae1 Java serialization removed 2025-10-23 22:44:25 +01:00
David Alves
d20040835c README 2025-10-23 20:28:43 +01:00
David Alves
2399b4b472 Delete main/docs directory 2025-10-23 20:22:53 +01:00
David Alves
974debf7db Design serialization format
JSON
2025-10-23 20:08:26 +01:00
8e95bc4c01 Testing job 2025-10-23 00:46:52 +01:00
33ed84b0c2 Enhance Maven workflow with release publishing
Added a publish-release job to create a GitHub release with the built JAR file when a tag is pushed.
2025-10-23 00:36:13 +01:00
9093b13c5d Rollback
Oops
2025-10-23 00:27:37 +01:00
12b7aabe87 Enhance CI workflow with security and dependency checks
Added security scan and dependency review jobs to the workflow.
2025-10-23 00:21:36 +01:00
c30aa25de0 Update Maven workflow to use JDK 17 and improve steps 2025-10-23 00:14:34 +01:00
3689f7a207 Set working directory for dependency graph update
Specify working directory for dependency graph update
2025-10-23 00:08:16 +01:00
bb18c1119e Update Maven build file path to main/pom.xml 2025-10-23 00:02:55 +01:00
f0dbdb551d Add GitHub Actions workflow for Java CI with Maven
This workflow builds a Java project using Maven, caches dependencies, and updates the dependency graph for improved Dependabot alerts.
2025-10-23 00:01:55 +01:00
f519c9aba7 Merge pull request #20 from davidalves04/8-single-process-prototype
Step 2: Single-Process Prototype
2025-10-22 23:51:19 +01:00
1216089e80 Step 2 - Finishing touches 2025-10-22 23:37:27 +01:00
211ea25ca5 Step 2 - Finishing touches 2025-10-22 23:36:41 +01:00
Gaa56
3fe467a2a3 Create MessageProtocol interface 2025-10-22 19:19:28 +01:00
David Alves
af9b091e76 Define message types 2025-10-22 18:43:49 +01:00
David Alves
fc46b9b83b Update SimulationConfig.java
Modification to open properties file.
2025-10-22 15:44:51 +01:00
Leandro Afonso
a7c17ca9b9 proto-doc 2025-10-21 23:00:40 +01:00
Leandro Afonso
1c033880e7 basic core function 2025-10-21 20:26:57 +01:00
David Alves
30fc2d6554 Merge pull request #1 from davidalves04/leo
Feat: Implement core discrete-event simulation logic and external configuration
2025-10-21 11:32:16 +01:00
Leandro Afonso
d41973d27f added bike//heavy prob & cross time 2025-10-21 11:19:40 +01:00
Leandro Afonso
ce226f261a added intersect, vehicle and light logic + random poisson dist 2025-10-21 11:11:56 +01:00
874fd53a21 Diagrama de Arquitetura 2025-10-20 12:35:15 +01:00
Leandro Afonso
08b254b8de added config based traffic 2025-10-20 12:09:05 +01:00
David Alves
19bf313c81 Actualizar Diagrama de arquitetura - SD.drawio 2025-10-20 12:00:44 +01:00
David Alves
cfb24b21bf Diagrama de arquitetura - SD.drawio 2025-10-20 11:50:37 +01:00
David Alves
b9991ba6ba Adicionado Diagrama de arquitetura - SD.drawio 2025-10-20 11:49:32 +01:00
Leandro Afonso
651dc754b8 personal branch 2025-10-14 02:20:24 +01:00
111 changed files with 11397 additions and 3881 deletions

View File

@@ -0,0 +1,110 @@
<mxfile host="Electron" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/29.0.3 Chrome/140.0.7339.249 Electron/38.7.0 Safari/537.36" version="29.0.3">
<diagram name="Arquitetura SD" id="QKeTeUWuUs8JeLsq44d-">
<mxGraphModel dx="1426" dy="841" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1654" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="0K4eb2koB2xQ8duQ1-_a-1" value="&lt;b&gt;CoordinatorProcess&lt;/b&gt;&lt;br&gt;(Cliente Socket)&lt;hr&gt;• VehicleGenerator&lt;br&gt;• Modelo Poisson (λ=0.5)&lt;br&gt;• Liga a Cr1-Cr5" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="560" y="40" width="240" height="100" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-2" value="&lt;b&gt;Cr1&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8001&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8001)&lt;br&gt;• Thread Semáforo Sul&lt;br&gt;• Thread Semáforo Este&lt;br&gt;• Thread Semáforo Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="280" y="200" width="180" height="160" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-3" value="&lt;b&gt;Cr2&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8002&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8002)&lt;br&gt;• Thread Semáforo Sul&lt;br&gt;• Thread Semáforo Este&lt;br&gt;• Thread Semáforo Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="590" y="190" width="180" height="160" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-5" value="&lt;b&gt;Cr4&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8004&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8004)&lt;br&gt;• Thread Semáforo Sul&lt;br&gt;• Thread Semáforo Este&lt;br&gt;• Thread Semáforo Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="440" y="530" width="180" height="160" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-6" value="&lt;b&gt;Cr5&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8005&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8005)&lt;br&gt;• Thread Semáforo Sul&lt;br&gt;• Thread Semáforo Este&lt;br&gt;• Thread Semáforo Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="910" y="430" width="180" height="160" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-7" value="&lt;b&gt;ExitNode (S)&lt;/b&gt;&lt;br&gt;Porta: 9001&lt;br&gt;Servidor Socket&lt;hr&gt;• Recebe veículos finais&lt;br&gt;• Calcula estatísticas:&lt;br&gt; - Tempo no sistema&lt;br&gt; - Tempo de espera&lt;br&gt; - Métricas por tipo&lt;br&gt;• Envia para Dashboard" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="727" y="810" width="200" height="170" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-8" value="&lt;b&gt;DashboardServer&lt;/b&gt;&lt;br&gt;Porta: 9000&lt;br&gt;Servidor Socket&lt;hr&gt;• Thread Pool (10 threads)&lt;br&gt;• ConcurrentHashMap&lt;br&gt;• Agrega estatísticas&lt;br&gt;• Display a cada 5s:&lt;br&gt; - Throughput&lt;br&gt; - Tempos médios&lt;br&gt; - Tamanhos de filas" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="1210" y="585" width="200" height="160" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-9" value="VEHICLE_SPAWN&lt;br&gt;(Vehicle)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#0000FF;strokeWidth=2;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-1" target="0K4eb2koB2xQ8duQ1-_a-2" edge="1">
<mxGeometry x="-0.2105" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-10" value="VEHICLE_SPAWN&lt;br&gt;(Vehicle)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#0000FF;strokeWidth=2;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-1" target="0K4eb2koB2xQ8duQ1-_a-3" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-11" value="VEHICLE_SPAWN&lt;br&gt;(Vehicle)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#0000FF;strokeWidth=2;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-1" target="0K4eb2koB2xQ8duQ1-_a-4" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-12" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-2" target="0K4eb2koB2xQ8duQ1-_a-3" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-13" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-3" target="0K4eb2koB2xQ8duQ1-_a-4" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-14" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-2" target="0K4eb2koB2xQ8duQ1-_a-5" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-15" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-3" target="0K4eb2koB2xQ8duQ1-_a-5" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-16" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-5" target="0K4eb2koB2xQ8duQ1-_a-6" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-17" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=none;startFill=0;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-4" target="0K4eb2koB2xQ8duQ1-_a-6" edge="1">
<mxGeometry x="0.3659" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-18" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#FF6600;strokeWidth=2;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-5" target="0K4eb2koB2xQ8duQ1-_a-7" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-19" value="VEHICLE_TRANSFER" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#FF6600;strokeWidth=2;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-6" target="0K4eb2koB2xQ8duQ1-_a-7" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-20" value="STATS_UPDATE&lt;br&gt;(periódico 5s)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-2" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-21" value="STATS_UPDATE&lt;br&gt;(periódico 5s)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-4" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-22" value="STATS_UPDATE&lt;br&gt;(periódico 5s)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-4" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-23" value="STATS_UPDATE&lt;br&gt;(periódico 5s)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-5" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-24" value="STATS_UPDATE&lt;br&gt;(periódico 5s)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-6" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-25" value="STATS_UPDATE&lt;br&gt;(periódico 5s)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-26" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-27" value="&lt;b&gt;MessageProtocol&lt;/b&gt;&lt;hr&gt;interface:&lt;br&gt;• getType()&lt;br&gt;• getPayload()&lt;br&gt;• getSourceNode()&lt;br&gt;• getDestinationNode()" style="rounded=0;whiteSpace=wrap;html=1;align=left;verticalAlign=top;fillColor=#fff2cc;strokeColor=#d6b656;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="30" y="30" width="180" height="120" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-28" value="&lt;b&gt;Tipos de Mensagens&lt;/b&gt;&lt;hr&gt;• VEHICLE_TRANSFER&lt;br&gt;• VEHICLE_SPAWN&lt;br&gt;• STATS_UPDATE&lt;br&gt;• TRAFFIC_LIGHT_SYNC&lt;br&gt;• HEARTBEAT" style="rounded=0;whiteSpace=wrap;html=1;align=left;verticalAlign=top;fillColor=#fff2cc;strokeColor=#d6b656;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="20" y="170" width="200" height="120" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-29" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-3" target="0K4eb2koB2xQ8duQ1-_a-4" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="480" y="280" as="sourcePoint" />
<mxPoint x="990" y="440" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-4" value="&lt;b&gt;Cr3&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8003&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8003)&lt;br&gt;• Thread Semáforo Sul&lt;br&gt;• Thread Semáforo Este&lt;br&gt;• Thread Semáforo Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="910" y="200" width="180" height="160" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-30" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="1" source="0K4eb2koB2xQ8duQ1-_a-7" target="0K4eb2koB2xQ8duQ1-_a-26" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="500" y="710" as="sourcePoint" />
<mxPoint x="1090" y="520" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-26" value="&lt;b&gt;LEGENDA&lt;/b&gt;&lt;hr&gt;━━━► Comunicação síncrona&lt;br&gt;╌╌╌► Comunicação periódica&lt;br&gt;&lt;br&gt;&lt;b&gt;Cores:&lt;/b&gt;&lt;br&gt;🔵 Azul = Geração&lt;br&gt;🟢 Verde = Transferência&lt;br&gt;🟠 Laranja = Finalização&lt;br&gt;🟣 Roxo = Monitorização&lt;br&gt;&lt;br&gt;&lt;b&gt;Serialização:&lt;/b&gt; JSON (Gson)&lt;br&gt;&lt;b&gt;Protocolo:&lt;/b&gt; TCP/IP" style="rounded=0;whiteSpace=wrap;html=1;align=left;verticalAlign=top;fillColor=#f5f5f5;strokeColor=#666666;fontColor=#333333;spacing=10;" parent="1" vertex="1">
<mxGeometry x="1210" y="825" width="200" height="220" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

104
.github/workflows/maven.yml vendored Normal file
View File

@@ -0,0 +1,104 @@
name: Java CI with Maven
on:
workflow_dispatch:
push:
branches: [ "dev", "cleanup" ]
tags:
- 'v*.*.*'
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package
working-directory: main
- name: Upload built JAR
uses: actions/upload-artifact@v4
with:
name: package
path: main/target/*.jar
- name: Generate dependency graph
run: mvn -B -f main/pom.xml com.github.ferstl:depgraph-maven-plugin:4.0.1:graph
- name: Upload dependency graph artifact
uses: actions/upload-artifact@v4
with:
name: dependency-graph
path: main/target/**
build-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven (Skip Tests)
run: mvn -B package -DskipTests
working-directory: main
- name: Create JPackage App Image
shell: pwsh
run: |
New-Item -ItemType Directory -Force -Path "dist"
jpackage --name "DTSS" `
--input main/target `
--main-jar main-1.0-SNAPSHOT.jar `
--dest dist `
--type app-image `
--win-console
- name: Inject java.exe
shell: pwsh
run: |
$javaPath = (Get-Command java).Source
Copy-Item -Path $javaPath -Destination "dist/DTSS/runtime/bin/"
- name: Zip Windows Release
shell: pwsh
run: |
Compress-Archive -Path "dist/DTSS" -DestinationPath "dist/DTSS-Windows.zip"
- name: Upload Windows Artifact
uses: actions/upload-artifact@v4
with:
name: windows-package
path: dist/DTSS-Windows.zip
publish-release:
runs-on: ubuntu-latest
needs: [build, build-windows]
if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch'
permissions:
contents: write
steps:
- name: Download Linux JAR
uses: actions/download-artifact@v4
with:
name: package
path: main/target/
- name: Download Windows Zip
uses: actions/download-artifact@v4
with:
name: windows-package
path: windows-dist/
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ startsWith(github.ref, 'refs/tags/') && github.ref_name || 'snapshot-build' }}
name: ${{ startsWith(github.ref, 'refs/tags/') && github.ref_name || 'Manual Snapshot Build' }}
draft: false
prerelease: true
make_latest: false
files: |
main/target/*.jar
windows-dist/*.zip

59
.gitignore vendored Normal file
View File

@@ -0,0 +1,59 @@
# Compiled class files
*.class
# Log files
*.log
*.trace
logs
*.md
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# VS Code settings
.vscode/
# Eclipse files
*.pydevproject
.project
.classpath
.cproject
.settings/
bin/
tmp/
# IntelliJ IDEA files
*.iml
.idea/
out/
# Mac system files
.DS_Store
# Windows system files
Thumbs.db
# Maven
target/
# Gradle
.gradle/
build/
# Other
*.swp
*.pdf
# JAR built pom file
dependency-reduced-pom.xml
# Python env
venv/

View File

@@ -0,0 +1,171 @@
<mxfile host="Electron" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/29.0.3 Chrome/140.0.7339.249 Electron/38.7.0 Safari/537.36" version="29.0.3">
<diagram name="Arquitetura SD" id="QKeTeUWuUs8JeLsq44d-">
<mxGraphModel dx="1426" dy="841" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1169" pageHeight="827" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="0K4eb2koB2xQ8duQ1-_a-27" value="&lt;b&gt;MessageProtocol&lt;/b&gt;&lt;hr&gt;interface:&lt;br&gt;• getType()&lt;br&gt;• getPayload()&lt;br&gt;• getSourceNode()&lt;br&gt;• getDestinationNode()" style="rounded=0;whiteSpace=wrap;html=1;align=left;verticalAlign=top;fillColor=#fff2cc;strokeColor=#d6b656;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="30" y="30" width="180" height="120" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-28" value="&lt;b&gt;Tipos de Mensagens&lt;/b&gt;&lt;hr&gt;• VEHICLE_TRANSFER&lt;br&gt;• VEHICLE_SPAWN&lt;br&gt;• STATS_UPDATE&lt;br&gt;• TRAFFIC_LIGHT_SYNC&lt;br&gt;• HEARTBEAT" style="rounded=0;whiteSpace=wrap;html=1;align=left;verticalAlign=top;fillColor=#fff2cc;strokeColor=#d6b656;spacing=10;fontColor=#000000;" parent="1" vertex="1">
<mxGeometry x="20" y="170" width="200" height="120" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-26" value="&lt;b&gt;LEGENDA&lt;/b&gt;&lt;hr&gt;━━━► Comunicação síncrona&lt;br&gt;╌╌╌► Comunicação periódica&lt;br&gt;&lt;br&gt;&lt;b&gt;Cores:&lt;/b&gt;&lt;br&gt;🔵 Azul =&amp;nbsp;&lt;span style=&quot;background-color: transparent;&quot;&gt;Criação do veículo&lt;/span&gt;&lt;div&gt;🟢 Verde = Transferência do veículo&lt;br&gt;🟠 Laranja = Chegada ao destino&lt;br&gt;🟣 Roxo =&amp;nbsp;&lt;span style=&quot;background-color: transparent;&quot;&gt;Envio das estatísticas&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;b&gt;Serialização:&lt;/b&gt; JSON (Gson)&lt;br&gt;&lt;b&gt;Protocolo:&lt;/b&gt; TCP/IP&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;align=left;verticalAlign=top;fillColor=#f5f5f5;strokeColor=#666666;fontColor=#333333;spacing=10;" parent="1" vertex="1">
<mxGeometry x="10" y="320" width="220" height="220" as="geometry" />
</mxCell>
<mxCell id="L62mICw2ZrYi1D68OOFe-13" value="" style="group" parent="1" vertex="1" connectable="0">
<mxGeometry x="280" y="40" width="850" height="730" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-20" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;exitX=0.616;exitY=-0.011;exitDx=0;exitDy=0;entryX=0.661;entryY=-0.002;entryDx=0;entryDy=0;entryPerimeter=0;exitPerimeter=0;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-2" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="794" y="530" as="targetPoint" />
<Array as="points">
<mxPoint x="99" y="122" />
<mxPoint x="793" y="122" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-1" value="&lt;b&gt;CoordinatorProcess&lt;/b&gt;&lt;br&gt;(Cliente Socket)&lt;hr&gt;• VehicleGenerator&lt;br&gt;• Modelo Poisson&lt;br&gt;• Liga a Cr1-Cr5" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry x="205.6637168141593" width="176.28318584070794" height="101.38888888888889" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-2" value="&lt;b&gt;Cr1&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8001&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8001)&lt;br&gt;• Thread Semáforo - Sul&lt;br&gt;• Thread Semáforo - Este&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry y="162.22" width="160" height="162.22" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-3" value="&lt;b&gt;Cr2&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8002&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8002)&lt;br&gt;• Thread Semáforo - Sul&lt;br&gt;• Thread Semáforo - Este&lt;br&gt;• Thread Semáforo - Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry x="227.7" y="162.22" width="162.3" height="162.22" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-5" value="&lt;b&gt;Cr4&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8004&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8004)&lt;br&gt;• Thread Semáforo - Este&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry y="486.67" width="160" height="133.33" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-6" value="&lt;b&gt;Cr5&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8005&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8005)&lt;br&gt;• Thread Semáforo - Este&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry x="220.35" y="486.67" width="169.65" height="162.22" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-7" value="&lt;b&gt;ExitNode (S)&lt;/b&gt;&lt;br&gt;Porta: 9001&lt;br&gt;Servidor Socket&lt;hr&gt;• Recebe veículos finais&lt;br&gt;• Calcula estatísticas:&lt;br&gt; - Tempo no sistema&lt;br&gt; - Tempo de espera&lt;br&gt; - Métricas por tipo&lt;br&gt;• Envia para o Dashboard" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffe6cc;strokeColor=#d79b00;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry x="464.07" y="476.53" width="154.6" height="172.36" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-8" value="&lt;b&gt;DashboardServer&lt;/b&gt;&lt;br&gt;Porta: 9000&lt;br&gt;Servidor Socket&lt;hr&gt;• Thread Pool (10 threads)&lt;br&gt;• ConcurrentHashMap&lt;br&gt;• Agrega estatísticas&lt;br&gt;• Display a cada 5s:&lt;br&gt; - Throughput&lt;br&gt; - Tempos médios&lt;br&gt; - Tamanhos de filas" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#e1d5e7;strokeColor=#9673a6;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry x="683.1" y="540" width="166.9" height="180" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-9" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#0000FF;strokeWidth=2;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-1" target="0K4eb2koB2xQ8duQ1-_a-2" edge="1">
<mxGeometry x="-0.2105" relative="1" as="geometry">
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-10" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#0000FF;strokeWidth=2;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-1" target="0K4eb2koB2xQ8duQ1-_a-3" edge="1">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="309" y="120" />
<mxPoint x="309" y="120" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-11" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#0000FF;strokeWidth=2;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-1" target="0K4eb2koB2xQ8duQ1-_a-4" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-12" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-2" target="0K4eb2koB2xQ8duQ1-_a-3" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-13" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=classic;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-3" target="0K4eb2koB2xQ8duQ1-_a-4" edge="1">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-14" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=none;startFill=0;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-2" target="0K4eb2koB2xQ8duQ1-_a-5" edge="1">
<mxGeometry x="0.125" y="100" relative="1" as="geometry">
<Array as="points">
<mxPoint x="66.10619469026548" y="446.11111111111114" />
<mxPoint x="66.10619469026548" y="446.11111111111114" />
</Array>
<mxPoint y="-1" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-16" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=classic;startArrow=none;startFill=0;exitX=1.005;exitY=0.63;exitDx=0;exitDy=0;exitPerimeter=0;align=center;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-5" edge="1">
<mxGeometry x="-0.0178" y="-49" relative="1" as="geometry">
<mxPoint x="139.55752212389382" y="588.0555555555555" as="sourcePoint" />
<mxPoint x="220" y="571" as="targetPoint" />
<Array as="points">
<mxPoint x="220" y="571" />
</Array>
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-19" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#b46504;strokeWidth=2;fillColor=#fad7ac;" parent="L62mICw2ZrYi1D68OOFe-13" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="390" y="580" as="sourcePoint" />
<mxPoint x="462.74" y="580.22" as="targetPoint" />
<Array as="points" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-22" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;exitX=0.981;exitY=0.08;exitDx=0;exitDy=0;exitPerimeter=0;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-4" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry x="0.1427" y="-60" relative="1" as="geometry">
<Array as="points">
<mxPoint x="593" y="175" />
<mxPoint x="593" y="140" />
<mxPoint x="764" y="140" />
</Array>
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-4" value="&lt;b&gt;Cr3&lt;/b&gt; (IntersectionProcess)&lt;br&gt;Porta: 8003&lt;br&gt;Servidor + Cliente&lt;hr&gt;• ServerSocket (8003)&lt;br&gt;• Thread Semáforo - Sul&lt;br&gt;• Thread Semáforo - Oeste&lt;br&gt;• Fila Eventos (DES)&lt;br&gt;• ReentrantLock" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;align=left;verticalAlign=top;spacing=10;fontColor=#000000;" parent="L62mICw2ZrYi1D68OOFe-13" vertex="1">
<mxGeometry x="462.74" y="162.22" width="167.26" height="162.22" as="geometry" />
</mxCell>
<mxCell id="0K4eb2koB2xQ8duQ1-_a-30" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;entryX=0.186;entryY=0.998;entryDx=0;entryDy=0;entryPerimeter=0;" parent="L62mICw2ZrYi1D68OOFe-13" target="0K4eb2koB2xQ8duQ1-_a-8" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="103" y="620" as="sourcePoint" />
<mxPoint x="710" y="730" as="targetPoint" />
<Array as="points">
<mxPoint x="103" y="730" />
<mxPoint x="714" y="730" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="L62mICw2ZrYi1D68OOFe-2" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#00AA00;strokeWidth=2;endArrow=none;startArrow=classic;startFill=1;endFill=0;" parent="L62mICw2ZrYi1D68OOFe-13" edge="1">
<mxGeometry x="-0.2214" y="26" relative="1" as="geometry">
<mxPoint x="293.8053097345133" y="486.6666666666666" as="sourcePoint" />
<mxPoint x="293.8053097345133" y="324.44444444444446" as="targetPoint" />
<Array as="points" />
<mxPoint x="-17" y="6" as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="L62mICw2ZrYi1D68OOFe-3" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#b46504;strokeWidth=2;endArrow=classic;startArrow=none;startFill=0;fillColor=#fad7ac;entryX=0.5;entryY=0;entryDx=0;entryDy=0;" parent="L62mICw2ZrYi1D68OOFe-13" target="0K4eb2koB2xQ8duQ1-_a-7" edge="1">
<mxGeometry x="0.3659" relative="1" as="geometry">
<mxPoint as="offset" />
<mxPoint x="541" y="324" as="sourcePoint" />
<mxPoint x="528.8495575221239" y="435.9722222222221" as="targetPoint" />
<Array as="points">
<mxPoint x="541" y="360" />
<mxPoint x="541" y="360" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="L62mICw2ZrYi1D68OOFe-10" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;exitX=0.621;exitY=-0.003;exitDx=0;exitDy=0;exitPerimeter=0;" parent="L62mICw2ZrYi1D68OOFe-13" source="0K4eb2koB2xQ8duQ1-_a-3" edge="1">
<mxGeometry x="0.1427" y="-60" relative="1" as="geometry">
<mxPoint x="330.53097345132744" y="141.94444444444443" as="sourcePoint" />
<mxPoint x="780" y="540" as="targetPoint" />
<Array as="points">
<mxPoint x="329" y="130" />
<mxPoint x="780" y="130" />
</Array>
<mxPoint as="offset" />
</mxGeometry>
</mxCell>
<mxCell id="L62mICw2ZrYi1D68OOFe-11" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="L62mICw2ZrYi1D68OOFe-13" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="326" y="654" as="sourcePoint" />
<mxPoint x="683.1" y="700" as="targetPoint" />
<Array as="points">
<mxPoint x="326.1" y="700" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="L62mICw2ZrYi1D68OOFe-12" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;strokeColor=#9933FF;strokeWidth=2;dashed=1;" parent="L62mICw2ZrYi1D68OOFe-13" edge="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="556" y="650" as="sourcePoint" />
<mxPoint x="683.0973451327434" y="663.0833333333331" as="targetPoint" />
<Array as="points">
<mxPoint x="556" y="663" />
</Array>
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

BIN
Enunciado.pdf Normal file

Binary file not shown.

View File

@@ -0,0 +1,172 @@
<?xml version='1.0' encoding='utf-8'?>
<mxfile host="app.diagrams.net" agent="Gemini" version="28.2.7">
<diagram name="Arquitetura-Sistema-Trafego" id="L-jWkP8vD7q_2fM6N-yC">
<mxGraphModel dx="1434" dy="746" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="1654" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="E1-process" value="Processo Gerador E1" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
<mxGeometry x="140" y="100" width="140" height="60" as="geometry" />
</mxCell>
<mxCell id="E2-process" value="Processo Gerador E2" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
<mxGeometry x="430" y="100" width="140" height="60" as="geometry" />
</mxCell>
<mxCell id="E3-process" value="Processo Gerador E3" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#dae8fc;strokeColor=#6c8ebf;" vertex="1" parent="1">
<mxGeometry x="720" y="100" width="140" height="60" as="geometry" />
</mxCell>
<mxCell id="Cr1-process" value="Processo Cruzamento (Cr1)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;verticalAlign=top;spacingTop=5;" vertex="1" parent="1">
<mxGeometry x="140" y="240" width="140" height="100" as="geometry" />
</mxCell>
<mxCell id="Cr1-thread1" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr1-process">
<mxGeometry x="20" y="30" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr1-thread2" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr1-process">
<mxGeometry x="20" y="65" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr2-process" value="Processo Cruzamento (Cr2)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;verticalAlign=top;spacingTop=5;" vertex="1" parent="1">
<mxGeometry x="430" y="240" width="140" height="140" as="geometry" />
</mxCell>
<mxCell id="Cr2-thread1" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr2-process">
<mxGeometry x="20" y="30" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr2-thread2" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr2-process">
<mxGeometry x="20" y="65" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr2-thread3" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr2-process">
<mxGeometry x="20" y="100" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr3-process" value="Processo Cruzamento (Cr3)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;verticalAlign=top;spacingTop=5;" vertex="1" parent="1">
<mxGeometry x="720" y="240" width="140" height="140" as="geometry" />
</mxCell>
<mxCell id="Cr3-thread1" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr3-process">
<mxGeometry x="20" y="30" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr3-thread2" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr3-process">
<mxGeometry x="20" y="65" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr3-thread3" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr3-process">
<mxGeometry x="20" y="100" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr4-process" value="Processo Cruzamento (Cr4)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;verticalAlign=top;spacingTop=5;" vertex="1" parent="1">
<mxGeometry x="140" y="460" width="140" height="100" as="geometry" />
</mxCell>
<mxCell id="Cr4-thread1" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr4-process">
<mxGeometry x="20" y="30" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr4-thread-peao" value="Thread Semáforo (Peões)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr4-process">
<mxGeometry x="20" y="65" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr5-process" value="Processo Cruzamento (Cr5)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#fff2cc;strokeColor=#d6b656;verticalAlign=top;spacingTop=5;" vertex="1" parent="1">
<mxGeometry x="430" y="460" width="140" height="100" as="geometry" />
</mxCell>
<mxCell id="Cr5-thread1" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr5-process">
<mxGeometry x="20" y="30" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="Cr5-thread2" value="Thread Semáforo" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#d5e8d4;strokeColor=#82b366;" vertex="1" parent="Cr5-process">
<mxGeometry x="20" y="65" width="100" height="30" as="geometry" />
</mxCell>
<mxCell id="S-process" value="Processo Saída (S)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#f8cecc;strokeColor=#b85450;" vertex="1" parent="1">
<mxGeometry x="720" y="460" width="140" height="60" as="geometry" />
</mxCell>
<mxCell id="dashboard-server" value="Servidor Dashboard" style="shape=cylinder3;whiteSpace=wrap;html=1;boundedLbl=1;backgroundOutline=1;size=15;fillColor=#e1d5e7;strokeColor=#9673a6;" vertex="1" parent="1">
<mxGeometry x="430" y="640" width="140" height="80" as="geometry" />
</mxCell>
<mxCell id="arrow-E1-Cr1" value="Fluxo Veículos&lt;br&gt;(Sockets/Middleware)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;fontSize=10;align=left;verticalAlign=bottom;" edge="1" parent="1" source="E1-process" target="Cr1-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-E2-Cr2" value="Fluxo Veículos&lt;br&gt;(Sockets/Middleware)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;fontSize=10;align=left;verticalAlign=bottom;" edge="1" parent="1" source="E2-process" target="Cr2-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-E3-Cr3" value="Fluxo Veículos&lt;br&gt;(Sockets/Middleware)" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;fontSize=10;align=left;verticalAlign=bottom;" edge="1" parent="1" source="E3-process" target="Cr3-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-Cr1-Cr4" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr1-process" target="Cr4-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-Cr2-Cr5" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr2-process" target="Cr5-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-Cr4-Cr5" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr4-process" target="Cr5-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-Cr5-S" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr5-process" target="S-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-Cr3-S" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr3-process" target="S-process">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="arrow-Cr1-Cr2" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr1-process" target="Cr2-process">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="280" y="290" />
<mxPoint x="430" y="290" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="arrow-Cr2-Cr1" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr2-process" target="Cr1-process">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="430" y="310" />
<mxPoint x="280" y="310" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="arrow-Cr2-Cr3" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr2-process" target="Cr3-process">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="570" y="290" />
<mxPoint x="720" y="290" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="arrow-Cr3-Cr2" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;" edge="1" parent="1" source="Cr3-process" target="Cr2-process">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="720" y="310" />
<mxPoint x="570" y="310" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="stats-Cr1-Dash" value="Envio de Estatísticas" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;dashed=1;strokeColor=#9673a6;fontSize=10;verticalAlign=bottom;" edge="1" parent="1" source="Cr1-process" target="dashboard-server">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="210" y="680" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="stats-Cr2-Dash" value="Envio de Estatísticas" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;dashed=1;strokeColor=#9673a6;fontSize=10;verticalAlign=bottom;" edge="1" parent="1" source="Cr2-process" target="dashboard-server">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="stats-Cr3-Dash" value="Envio de Estatísticas" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;dashed=1;strokeColor=#9673a6;fontSize=10;verticalAlign=bottom;" edge="1" parent="1" source="Cr3-process" target="dashboard-server">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="790" y="680" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="stats-Cr4-Dash" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;dashed=1;strokeColor=#9673a6;" edge="1" parent="1" source="Cr4-process" target="dashboard-server">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="210" y="680" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="stats-Cr5-Dash" value="" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;dashed=1;strokeColor=#9673a6;" edge="1" parent="1" source="Cr5-process" target="dashboard-server">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="stats-S-Dash" value="Estatísticas Globais" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=classic;endFill=1;dashed=1;strokeColor=#9673a6;fontSize=10;verticalAlign=bottom;" edge="1" parent="1" source="S-process" target="dashboard-server">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="790" y="680" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="legend" value="Legenda simplificada (removida tabela)" style="rounded=1;whiteSpace=wrap;html=1;fillColor=#ffffff;strokeColor=#999999;" vertex="1" parent="1">
</mxCell>
<mxCell id="title" value="Diagrama de Arquitetura - Simulador de Tráfego Distribuído" style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;fontSize=18;fontStyle=1" vertex="1" parent="1">
<mxGeometry x="290" y="40" width="420" height="30" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

View File

@@ -0,0 +1,6 @@
Execução,VeículosGerados,VeículosCompletados,TaxaConclusão,TempoMédioSistema,TempoMédioEspera,TempoMínimoSistema,TempoMáximoSistema
1,1784,877,49.16,64.58,61.43,32.29,129.16
2,1782,363,20.37,53.77,51.01,26.88,107.53
3,1786,883,49.44,53.09,50.08,26.54,106.17
4,1845,179,9.70,63.92,60.27,31.96,127.84
5,1872,953,50.91,65.41,62.16,32.70,130.81
1 Execução VeículosGerados VeículosCompletados TaxaConclusão TempoMédioSistema TempoMédioEspera TempoMínimoSistema TempoMáximoSistema
2 1 1784 877 49.16 64.58 61.43 32.29 129.16
3 2 1782 363 20.37 53.77 51.01 26.88 107.53
4 3 1786 883 49.44 53.09 50.08 26.54 106.17
5 4 1845 179 9.70 63.92 60.27 31.96 127.84
6 5 1872 953 50.91 65.41 62.16 32.70 130.81

View File

@@ -0,0 +1,215 @@
================================================================================
ANÁLISE ESTATÍSTICA MULTI-EXECUÇÃO
================================================================================
Configuração: simulation-high.properties
Número de Execuções: 5
Data da Análise: 2025-12-07 00:11:13
--------------------------------------------------------------------------------
MÉTRICAS GLOBAIS
--------------------------------------------------------------------------------
Veículos Gerados:
Média: 1813.80 Desvio Padrão: 41.93
Mediana: 1786.00 IC 95%: [1754.13, 1873.47]
Mín: 1782.00 Máx: 1872.00
Veículos Completados:
Média: 651.00 Desvio Padrão: 354.20
Mediana: 877.00 IC 95%: [146.96, 1155.04]
Mín: 179.00 Máx: 953.00
Taxa de Conclusão (%):
Média: 35.92 Desvio Padrão: 19.44
Mediana: 49.16 IC 95%: [8.25, 63.58]
Mín: 9.70 Máx: 50.91
Tempo Médio no Sistema (segundos):
Média: 60.15 Desvio Padrão: 6.17
Mediana: 63.92 IC 95%: [51.38, 68.93]
Mín: 53.09 Máx: 65.41
Tempo Médio de Espera (segundos):
Média: 56.99 Desvio Padrão: 5.93
Mediana: 60.27 IC 95%: [48.55, 65.43]
Mín: 50.08 Máx: 62.16
--------------------------------------------------------------------------------
ANÁLISE POR TIPO DE VEÍCULO
--------------------------------------------------------------------------------
--- BIKE ---
Contagem de Veículos:
Média: 135.40 Desvio Padrão: 77.66
Mediana: 167.00 IC 95%: [24.89, 245.91]
Mín: 37.00 Máx: 211.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 55.15 Desvio Padrão: 12.01
Mediana: 54.23 IC 95%: [38.07, 72.24]
Mín: 43.41 Máx: 74.99
--- LIGHT ---
Contagem de Veículos:
Média: 395.00 Desvio Padrão: 207.62
Mediana: 540.00 IC 95%: [99.55, 690.45]
Mín: 107.00 Máx: 548.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 59.79 Desvio Padrão: 7.28
Mediana: 61.58 IC 95%: [49.43, 70.15]
Mín: 50.81 Máx: 69.26
--- HEAVY ---
Contagem de Veículos:
Média: 120.60 Desvio Padrão: 72.95
Mediana: 142.00 IC 95%: [16.79, 224.41]
Mín: 35.00 Máx: 202.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 49.20 Desvio Padrão: 8.62
Mediana: 50.31 IC 95%: [36.94, 61.46]
Mín: 35.51 Máx: 58.20
--------------------------------------------------------------------------------
ANÁLISE POR INTERSEÇÃO
--------------------------------------------------------------------------------
--- Cr1 ---
Tamanho Máximo da Fila:
Média: 3.20 Desvio Padrão: 5.54
Mediana: 1.00 IC 95%: [-4.68, 11.08]
Mín: 0.00 Máx: 13.00
Tamanho Médio da Fila:
Média: 3.20 Desvio Padrão: 5.54
Mediana: 1.00 IC 95%: [-4.68, 11.08]
Mín: 0.00 Máx: 13.00
Veículos Processados:
Média: 378.40 Desvio Padrão: 252.94
Mediana: 512.00 IC 95%: [18.46, 738.34]
Mín: 58.00 Máx: 600.00
--- Cr2 ---
Tamanho Máximo da Fila:
Média: 0.60 Desvio Padrão: 1.34
Mediana: 0.00 IC 95%: [-1.31, 2.51]
Mín: 0.00 Máx: 3.00
Tamanho Médio da Fila:
Média: 0.60 Desvio Padrão: 1.34
Mediana: 0.00 IC 95%: [-1.31, 2.51]
Mín: 0.00 Máx: 3.00
Veículos Processados:
Média: 390.40 Desvio Padrão: 223.14
Mediana: 409.00 IC 95%: [72.87, 707.93]
Mín: 59.00 Máx: 599.00
--- Cr3 ---
Tamanho Máximo da Fila:
Média: 6.20 Desvio Padrão: 8.67
Mediana: 0.00 IC 95%: [-6.14, 18.54]
Mín: 0.00 Máx: 18.00
Tamanho Médio da Fila:
Média: 6.20 Desvio Padrão: 8.67
Mediana: 0.00 IC 95%: [-6.14, 18.54]
Mín: 0.00 Máx: 18.00
Veículos Processados:
Média: 339.00 Desvio Padrão: 239.34
Mediana: 416.00 IC 95%: [-1.59, 679.59]
Mín: 57.00 Máx: 622.00
--- Cr4 ---
Tamanho Máximo da Fila:
Média: 0.60 Desvio Padrão: 0.89
Mediana: 0.00 IC 95%: [-0.67, 1.87]
Mín: 0.00 Máx: 2.00
Tamanho Médio da Fila:
Média: 0.60 Desvio Padrão: 0.89
Mediana: 0.00 IC 95%: [-0.67, 1.87]
Mín: 0.00 Máx: 2.00
Veículos Processados:
Média: 123.40 Desvio Padrão: 116.13
Mediana: 109.00 IC 95%: [-41.85, 288.65]
Mín: 21.00 Máx: 316.00
--- Cr5 ---
Tamanho Máximo da Fila:
Média: 2.40 Desvio Padrão: 1.14
Mediana: 2.00 IC 95%: [0.78, 4.02]
Mín: 1.00 Máx: 4.00
Tamanho Médio da Fila:
Média: 2.40 Desvio Padrão: 1.14
Mediana: 2.00 IC 95%: [0.78, 4.02]
Mín: 1.00 Máx: 4.00
Veículos Processados:
Média: 200.80 Desvio Padrão: 114.19
Mediana: 261.00 IC 95%: [38.31, 363.29]
Mín: 70.00 Máx: 305.00
--- ExitNode ---
Tamanho Máximo da Fila: Sem dados
Tamanho Médio da Fila: Sem dados
Veículos Processados:
Média: 651.00 Desvio Padrão: 354.20
Mediana: 877.00 IC 95%: [146.96, 1155.04]
Mín: 179.00 Máx: 953.00
--------------------------------------------------------------------------------
RESUMOS INDIVIDUAIS DAS EXECUÇÕES
--------------------------------------------------------------------------------
Execução #1 [simulation-high.properties]:
Gerados: 1784, Completados: 877 (49.2%)
Tempo Médio no Sistema: 64.58s
Tempo Médio de Espera: 61.43s
Execução #2 [simulation-high.properties]:
Gerados: 1782, Completados: 363 (20.4%)
Tempo Médio no Sistema: 53.77s
Tempo Médio de Espera: 51.01s
Execução #3 [simulation-high.properties]:
Gerados: 1786, Completados: 883 (49.4%)
Tempo Médio no Sistema: 53.09s
Tempo Médio de Espera: 50.08s
Execução #4 [simulation-high.properties]:
Gerados: 1845, Completados: 179 (9.7%)
Tempo Médio no Sistema: 63.92s
Tempo Médio de Espera: 60.27s
Execução #5 [simulation-high.properties]:
Gerados: 1872, Completados: 953 (50.9%)
Tempo Médio no Sistema: 65.41s
Tempo Médio de Espera: 62.16s
================================================================================
FIM DO RELATÓRIO
================================================================================

View File

@@ -0,0 +1,6 @@
Execução,VeículosGerados,VeículosCompletados,TaxaConclusão,TempoMédioSistema,TempoMédioEspera,TempoMínimoSistema,TempoMáximoSistema
1,371,187,50.40,42.28,38.65,21.14,84.57
2,361,263,72.85,29.15,25.29,14.57,58.30
3,368,197,53.53,38.02,33.95,19.01,76.04
4,350,239,68.29,32.38,28.36,16.19,64.75
5,373,212,56.84,23.36,19.96,11.68,46.73
1 Execução VeículosGerados VeículosCompletados TaxaConclusão TempoMédioSistema TempoMédioEspera TempoMínimoSistema TempoMáximoSistema
2 1 371 187 50.40 42.28 38.65 21.14 84.57
3 2 361 263 72.85 29.15 25.29 14.57 58.30
4 3 368 197 53.53 38.02 33.95 19.01 76.04
5 4 350 239 68.29 32.38 28.36 16.19 64.75
6 5 373 212 56.84 23.36 19.96 11.68 46.73

View File

@@ -0,0 +1,209 @@
================================================================================
ANÁLISE ESTATÍSTICA MULTI-EXECUÇÃO
================================================================================
Configuração: simulation-low.properties
Número de Execuções: 5
Data da Análise: 2025-12-07 00:09:57
--------------------------------------------------------------------------------
MÉTRICAS GLOBAIS
--------------------------------------------------------------------------------
Veículos Gerados:
Média: 364.60 Desvio Padrão: 9.34
Mediana: 368.00 IC 95%: [351.30, 377.90]
Mín: 350.00 Máx: 373.00
Veículos Completados:
Média: 219.60 Desvio Padrão: 31.19
Mediana: 212.00 IC 95%: [175.22, 263.98]
Mín: 187.00 Máx: 263.00
Taxa de Conclusão (%):
Média: 60.38 Desvio Padrão: 9.71
Mediana: 56.84 IC 95%: [46.57, 74.20]
Mín: 50.40 Máx: 72.85
Tempo Médio no Sistema (segundos):
Média: 33.04 Desvio Padrão: 7.41
Mediana: 32.38 IC 95%: [22.50, 43.58]
Mín: 23.36 Máx: 42.28
Tempo Médio de Espera (segundos):
Média: 29.24 Desvio Padrão: 7.30
Mediana: 28.36 IC 95%: [18.85, 39.63]
Mín: 19.96 Máx: 38.65
--------------------------------------------------------------------------------
ANÁLISE POR TIPO DE VEÍCULO
--------------------------------------------------------------------------------
--- BIKE ---
Contagem de Veículos:
Média: 41.00 Desvio Padrão: 6.96
Mediana: 43.00 IC 95%: [31.09, 50.91]
Mín: 33.00 Máx: 50.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 25.91 Desvio Padrão: 3.91
Mediana: 26.98 IC 95%: [20.35, 31.47]
Mín: 19.60 Máx: 30.06
--- LIGHT ---
Contagem de Veículos:
Média: 134.00 Desvio Padrão: 24.07
Mediana: 130.00 IC 95%: [99.74, 168.26]
Mín: 104.00 Máx: 167.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 29.34 Desvio Padrão: 6.83
Mediana: 27.89 IC 95%: [19.62, 39.06]
Mín: 20.73 Máx: 36.42
--- HEAVY ---
Contagem de Veículos:
Média: 44.60 Desvio Padrão: 3.44
Mediana: 46.00 IC 95%: [39.71, 49.49]
Mín: 40.00 Máx: 48.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 32.11 Desvio Padrão: 15.90
Mediana: 30.74 IC 95%: [9.48, 54.74]
Mín: 18.09 Máx: 58.73
--------------------------------------------------------------------------------
ANÁLISE POR INTERSEÇÃO
--------------------------------------------------------------------------------
--- Cr1 ---
Tamanho Máximo da Fila:
Média: 0.60 Desvio Padrão: 1.34
Mediana: 0.00 IC 95%: [-1.31, 2.51]
Mín: 0.00 Máx: 3.00
Tamanho Médio da Fila:
Média: 0.60 Desvio Padrão: 1.34
Mediana: 0.00 IC 95%: [-1.31, 2.51]
Mín: 0.00 Máx: 3.00
Veículos Processados:
Média: 63.80 Desvio Padrão: 17.25
Mediana: 57.00 IC 95%: [39.25, 88.35]
Mín: 48.00 Máx: 91.00
--- Cr2 ---
Tamanho Máximo da Fila:
Média: 0.80 Desvio Padrão: 1.79
Mediana: 0.00 IC 95%: [-1.75, 3.35]
Mín: 0.00 Máx: 4.00
Tamanho Médio da Fila:
Média: 0.80 Desvio Padrão: 1.79
Mediana: 0.00 IC 95%: [-1.75, 3.35]
Mín: 0.00 Máx: 4.00
Veículos Processados:
Média: 56.20 Desvio Padrão: 18.51
Mediana: 50.00 IC 95%: [29.86, 82.54]
Mín: 35.00 Máx: 78.00
--- Cr3 ---
Tamanho Máximo da Fila:
Média: 1.00 Desvio Padrão: 1.41
Mediana: 0.00 IC 95%: [-1.01, 3.01]
Mín: 0.00 Máx: 3.00
Tamanho Médio da Fila:
Média: 1.00 Desvio Padrão: 1.41
Mediana: 0.00 IC 95%: [-1.01, 3.01]
Mín: 0.00 Máx: 3.00
Veículos Processados:
Média: 63.20 Desvio Padrão: 23.97
Mediana: 56.00 IC 95%: [29.09, 97.31]
Mín: 41.00 Máx: 104.00
--- Cr4 ---
Tamanho Máximo da Fila:
Média: 1.80 Desvio Padrão: 2.49
Mediana: 0.00 IC 95%: [-1.74, 5.34]
Mín: 0.00 Máx: 5.00
Tamanho Médio da Fila:
Média: 1.80 Desvio Padrão: 2.49
Mediana: 0.00 IC 95%: [-1.74, 5.34]
Mín: 0.00 Máx: 5.00
Veículos Processados:
Média: 51.00 Desvio Padrão: 16.05
Mediana: 53.00 IC 95%: [28.16, 73.84]
Mín: 31.00 Máx: 70.00
--- Cr5 ---
Tamanho Máximo da Fila: Sem dados
Tamanho Médio da Fila: Sem dados
Veículos Processados:
Média: 86.60 Desvio Padrão: 34.20
Mediana: 65.00 IC 95%: [37.94, 135.26]
Mín: 62.00 Máx: 139.00
--- ExitNode ---
Tamanho Máximo da Fila: Sem dados
Tamanho Médio da Fila: Sem dados
Veículos Processados:
Média: 219.60 Desvio Padrão: 31.19
Mediana: 212.00 IC 95%: [175.22, 263.98]
Mín: 187.00 Máx: 263.00
--------------------------------------------------------------------------------
RESUMOS INDIVIDUAIS DAS EXECUÇÕES
--------------------------------------------------------------------------------
Execução #1 [simulation-low.properties]:
Gerados: 371, Completados: 187 (50.4%)
Tempo Médio no Sistema: 42.28s
Tempo Médio de Espera: 38.65s
Execução #2 [simulation-low.properties]:
Gerados: 361, Completados: 263 (72.9%)
Tempo Médio no Sistema: 29.15s
Tempo Médio de Espera: 25.29s
Execução #3 [simulation-low.properties]:
Gerados: 368, Completados: 197 (53.5%)
Tempo Médio no Sistema: 38.02s
Tempo Médio de Espera: 33.95s
Execução #4 [simulation-low.properties]:
Gerados: 350, Completados: 239 (68.3%)
Tempo Médio no Sistema: 32.38s
Tempo Médio de Espera: 28.36s
Execução #5 [simulation-low.properties]:
Gerados: 373, Completados: 212 (56.8%)
Tempo Médio no Sistema: 23.36s
Tempo Médio de Espera: 19.96s
================================================================================
FIM DO RELATÓRIO
================================================================================

View File

@@ -0,0 +1,6 @@
Execução,VeículosGerados,VeículosCompletados,TaxaConclusão,TempoMédioSistema,TempoMédioEspera,TempoMínimoSistema,TempoMáximoSistema
1,950,416,43.79,49.34,45.70,24.67,98.68
2,886,480,54.18,35.08,31.69,17.54,70.16
3,954,535,56.08,43.76,40.30,21.88,87.51
4,948,354,37.34,41.68,37.96,20.84,83.37
5,898,312,34.74,52.56,49.26,26.28,105.13
1 Execução VeículosGerados VeículosCompletados TaxaConclusão TempoMédioSistema TempoMédioEspera TempoMínimoSistema TempoMáximoSistema
2 1 950 416 43.79 49.34 45.70 24.67 98.68
3 2 886 480 54.18 35.08 31.69 17.54 70.16
4 3 954 535 56.08 43.76 40.30 21.88 87.51
5 4 948 354 37.34 41.68 37.96 20.84 83.37
6 5 898 312 34.74 52.56 49.26 26.28 105.13

View File

@@ -0,0 +1,203 @@
================================================================================
ANÁLISE ESTATÍSTICA MULTI-EXECUÇÃO
================================================================================
Configuração: simulation-medium.properties
Número de Execuções: 5
Data da Análise: 2025-12-07 00:10:34
--------------------------------------------------------------------------------
MÉTRICAS GLOBAIS
--------------------------------------------------------------------------------
Veículos Gerados:
Média: 927.20 Desvio Padrão: 32.48
Mediana: 948.00 IC 95%: [880.97, 973.43]
Mín: 886.00 Máx: 954.00
Veículos Completados:
Média: 419.40 Desvio Padrão: 90.64
Mediana: 416.00 IC 95%: [290.42, 548.38]
Mín: 312.00 Máx: 535.00
Taxa de Conclusão (%):
Média: 45.23 Desvio Padrão: 9.64
Mediana: 43.79 IC 95%: [31.50, 58.95]
Mín: 34.74 Máx: 56.08
Tempo Médio no Sistema (segundos):
Média: 44.48 Desvio Padrão: 6.81
Mediana: 43.76 IC 95%: [34.79, 54.18]
Mín: 35.08 Máx: 52.56
Tempo Médio de Espera (segundos):
Média: 40.98 Desvio Padrão: 6.83
Mediana: 40.30 IC 95%: [31.26, 50.71]
Mín: 31.69 Máx: 49.26
--------------------------------------------------------------------------------
ANÁLISE POR TIPO DE VEÍCULO
--------------------------------------------------------------------------------
--- BIKE ---
Contagem de Veículos:
Média: 75.80 Desvio Padrão: 15.96
Mediana: 71.00 IC 95%: [53.09, 98.51]
Mín: 56.00 Máx: 95.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 42.34 Desvio Padrão: 10.81
Mediana: 39.70 IC 95%: [26.96, 57.72]
Mín: 31.96 Máx: 55.19
--- LIGHT ---
Contagem de Veículos:
Média: 263.20 Desvio Padrão: 58.29
Mediana: 265.00 IC 95%: [180.25, 346.15]
Mín: 204.00 Máx: 344.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 39.13 Desvio Padrão: 6.35
Mediana: 38.08 IC 95%: [30.09, 48.17]
Mín: 30.47 Máx: 47.99
--- HEAVY ---
Contagem de Veículos:
Média: 80.40 Desvio Padrão: 19.11
Mediana: 80.00 IC 95%: [53.20, 107.60]
Mín: 52.00 Máx: 102.00
Tempo Médio no Sistema (segundos): Sem dados
Tempo Médio de Espera (segundos):
Média: 48.02 Desvio Padrão: 30.99
Mediana: 34.44 IC 95%: [3.92, 92.11]
Mín: 32.46 Máx: 103.40
--------------------------------------------------------------------------------
ANÁLISE POR INTERSEÇÃO
--------------------------------------------------------------------------------
--- Cr1 ---
Tamanho Máximo da Fila:
Média: 5.60 Desvio Padrão: 11.44
Mediana: 0.00 IC 95%: [-10.67, 21.87]
Mín: 0.00 Máx: 26.00
Tamanho Médio da Fila:
Média: 5.60 Desvio Padrão: 11.44
Mediana: 0.00 IC 95%: [-10.67, 21.87]
Mín: 0.00 Máx: 26.00
Veículos Processados:
Média: 156.00 Desvio Padrão: 122.81
Mediana: 98.00 IC 95%: [-18.76, 330.76]
Mín: 35.00 Máx: 306.00
--- Cr2 ---
Tamanho Máximo da Fila: Sem dados
Tamanho Médio da Fila: Sem dados
Veículos Processados:
Média: 172.00 Desvio Padrão: 121.88
Mediana: 116.00 IC 95%: [-1.44, 345.44]
Mín: 66.00 Máx: 322.00
--- Cr3 ---
Tamanho Máximo da Fila:
Média: 0.60 Desvio Padrão: 1.34
Mediana: 0.00 IC 95%: [-1.31, 2.51]
Mín: 0.00 Máx: 3.00
Tamanho Médio da Fila:
Média: 0.60 Desvio Padrão: 1.34
Mediana: 0.00 IC 95%: [-1.31, 2.51]
Mín: 0.00 Máx: 3.00
Veículos Processados:
Média: 168.40 Desvio Padrão: 133.38
Mediana: 121.00 IC 95%: [-21.40, 358.20]
Mín: 48.00 Máx: 326.00
--- Cr4 ---
Tamanho Máximo da Fila: Sem dados
Tamanho Médio da Fila: Sem dados
Veículos Processados:
Média: 71.80 Desvio Padrão: 20.39
Mediana: 77.00 IC 95%: [42.79, 100.81]
Mín: 38.00 Máx: 92.00
--- Cr5 ---
Tamanho Máximo da Fila:
Média: 3.60 Desvio Padrão: 3.85
Mediana: 2.00 IC 95%: [-1.87, 9.07]
Mín: 0.00 Máx: 10.00
Tamanho Médio da Fila:
Média: 3.60 Desvio Padrão: 3.85
Mediana: 2.00 IC 95%: [-1.87, 9.07]
Mín: 0.00 Máx: 10.00
Veículos Processados:
Média: 150.60 Desvio Padrão: 43.37
Mediana: 126.00 IC 95%: [88.88, 212.32]
Mín: 116.00 Máx: 209.00
--- ExitNode ---
Tamanho Máximo da Fila: Sem dados
Tamanho Médio da Fila: Sem dados
Veículos Processados:
Média: 419.40 Desvio Padrão: 90.64
Mediana: 416.00 IC 95%: [290.42, 548.38]
Mín: 312.00 Máx: 535.00
--------------------------------------------------------------------------------
RESUMOS INDIVIDUAIS DAS EXECUÇÕES
--------------------------------------------------------------------------------
Execução #1 [simulation-medium.properties]:
Gerados: 950, Completados: 416 (43.8%)
Tempo Médio no Sistema: 49.34s
Tempo Médio de Espera: 45.70s
Execução #2 [simulation-medium.properties]:
Gerados: 886, Completados: 480 (54.2%)
Tempo Médio no Sistema: 35.08s
Tempo Médio de Espera: 31.69s
Execução #3 [simulation-medium.properties]:
Gerados: 954, Completados: 535 (56.1%)
Tempo Médio no Sistema: 43.76s
Tempo Médio de Espera: 40.30s
Execução #4 [simulation-medium.properties]:
Gerados: 948, Completados: 354 (37.3%)
Tempo Médio no Sistema: 41.68s
Tempo Médio de Espera: 37.96s
Execução #5 [simulation-medium.properties]:
Gerados: 898, Completados: 312 (34.7%)
Tempo Médio no Sistema: 52.56s
Tempo Médio de Espera: 49.26s
================================================================================
FIM DO RELATÓRIO
================================================================================

169
main/graphing.py Normal file
View File

@@ -0,0 +1,169 @@
import pandas as pd
import matplotlib.pyplot as plt
import glob
import os
# Find CSV files using glob
def load_latest_csv(pattern):
"""Load the most recent CSV file matching the pattern"""
files = glob.glob(pattern)
if not files:
print(f"Warning: No files found matching '{pattern}'")
return None
# Sort by modification time, get the latest
latest_file = max(files, key=os.path.getmtime)
print(f"Loading: {latest_file}")
return pd.read_csv(latest_file)
# Carregar dados
print("Looking for analysis files...")
low = load_latest_csv('analysis/LOW_LOAD_*.csv')
medium = load_latest_csv('analysis/MEDIUM_LOAD_*.csv')
high = load_latest_csv('analysis/HIGH_LOAD_*.csv')
# Check if we have all data
if low is None or medium is None or high is None:
print("\nError: Missing analysis files!")
print("Please run the batch analysis first:")
exit(1)
# Print available columns for debugging
print("\nAvailable columns in LOW_LOAD CSV:")
print(low.columns.tolist())
# Create output directory for graphs
os.makedirs('graphs', exist_ok=True)
# 1. Gráfico: Dwelling Time vs Load
plt.figure(figsize=(10, 6))
dwelling_times = [
low['TempoMédioSistema'].mean(),
medium['TempoMédioSistema'].mean(),
high['TempoMédioSistema'].mean()
]
plt.bar(['Baixa', 'Média', 'Alta'], dwelling_times, color=['green', 'orange', 'red'])
plt.ylabel('Tempo Médio no Sistema (s)')
plt.title('Desempenho do Sistema vs Carga')
plt.xlabel('Cenário de Carga')
plt.grid(axis='y', alpha=0.3)
for i, v in enumerate(dwelling_times):
plt.text(i, v + 1, f'{v:.2f}s', ha='center', va='bottom')
plt.savefig('graphs/dwelling_time_comparison.png', dpi=300, bbox_inches='tight')
print("\nGraph saved: graphs/dwelling_time_comparison.png")
plt.close()
# 2. Gráfico: Completion Rate vs Load
plt.figure(figsize=(10, 6))
completion_rates = [
low['TaxaConclusão'].mean(),
medium['TaxaConclusão'].mean(),
high['TaxaConclusão'].mean()
]
plt.bar(['Baixa', 'Média', 'Alta'], completion_rates, color=['green', 'orange', 'red'])
plt.ylabel('Taxa de Conclusão (%)')
plt.title('Taxa de Conclusão de Veículos vs Carga')
plt.xlabel('Cenário de Carga')
plt.grid(axis='y', alpha=0.3)
plt.ylim(0, 100)
for i, v in enumerate(completion_rates):
plt.text(i, v + 2, f'{v:.1f}%', ha='center', va='bottom')
plt.savefig('graphs/completion_rate_comparison.png', dpi=300, bbox_inches='tight')
print("Graph saved: graphs/completion_rate_comparison.png")
plt.close()
# 3. Gráfico: Waiting Time vs Load
plt.figure(figsize=(10, 6))
waiting_times = [
low['TempoMédioEspera'].mean(),
medium['TempoMédioEspera'].mean(),
high['TempoMédioEspera'].mean()
]
plt.bar(['Baixa', 'Média', 'Alta'], waiting_times, color=['green', 'orange', 'red'])
plt.ylabel('Tempo Médio de Espera (s)')
plt.title('Tempo Médio de Espera vs Carga')
plt.xlabel('Cenário de Carga')
plt.grid(axis='y', alpha=0.3)
for i, v in enumerate(waiting_times):
plt.text(i, v + 1, f'{v:.2f}s', ha='center', va='bottom')
plt.savefig('graphs/waiting_time_comparison.png', dpi=300, bbox_inches='tight')
print("Graph saved: graphs/waiting_time_comparison.png")
plt.close()
# 4. Gráfico: Summary Statistics
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(14, 10))
loads = ['Baixa', 'Média', 'Alta']
# Vehicles generated
ax1.bar(loads, [low['VeículosGerados'].mean(), medium['VeículosGerados'].mean(), high['VeículosGerados'].mean()], color=['green', 'orange', 'red'])
ax1.set_title('Veículos Gerados')
ax1.set_ylabel('Quantidade')
ax1.grid(axis='y', alpha=0.3)
# Vehicles completed
ax2.bar(loads, [low['VeículosCompletados'].mean(), medium['VeículosCompletados'].mean(), high['VeículosCompletados'].mean()], color=['green', 'orange', 'red'])
ax2.set_title('Veículos Concluídos')
ax2.set_ylabel('Quantidade')
ax2.grid(axis='y', alpha=0.3)
# Min/Max dwelling time
x = range(3)
width = 0.35
ax3.bar([i - width/2 for i in x], [low['TempoMínimoSistema'].mean(), medium['TempoMínimoSistema'].mean(), high['TempoMínimoSistema'].mean()], width, label='Mín', color='lightblue')
ax3.bar([i + width/2 for i in x], [low['TempoMáximoSistema'].mean(), medium['TempoMáximoSistema'].mean(), high['TempoMáximoSistema'].mean()], width, label='Máx', color='darkblue')
ax3.set_title('Tempo no Sistema Mín/Máx')
ax3.set_ylabel('Tempo (s)')
ax3.set_xticks(x)
ax3.set_xticklabels(loads)
ax3.legend()
ax3.grid(axis='y', alpha=0.3)
# Performance summary
metrics = ['Tempo no\nSistema', 'Tempo de\nEspera', 'Taxa de\nConclusão']
low_vals = [low['TempoMédioSistema'].mean(), low['TempoMédioEspera'].mean(), low['TaxaConclusão'].mean()]
med_vals = [medium['TempoMédioSistema'].mean(), medium['TempoMédioEspera'].mean(), medium['TaxaConclusão'].mean()]
high_vals = [high['TempoMédioSistema'].mean(), high['TempoMédioEspera'].mean(), high['TaxaConclusão'].mean()]
x = range(len(metrics))
width = 0.25
ax4.bar([i - width for i in x], low_vals, width, label='Baixa', color='green')
ax4.bar(x, med_vals, width, label='Média', color='orange')
ax4.bar([i + width for i in x], high_vals, width, label='Alta', color='red')
ax4.set_title('Resumo de Desempenho')
ax4.set_xticks(x)
ax4.set_xticklabels(metrics)
ax4.legend()
ax4.grid(axis='y', alpha=0.3)
plt.tight_layout()
plt.savefig('graphs/summary_statistics.png', dpi=300, bbox_inches='tight')
print("Graph saved: graphs/summary_statistics.png")
plt.close()
# Print summary statistics
print("\n" + "="*60)
print("SUMMARY STATISTICS")
print("="*60)
print(f"\nLOW LOAD:")
print(f" Avg Dwelling Time: {low['TempoMédioSistema'].mean():.2f}s")
print(f" Avg Waiting Time: {low['TempoMédioEspera'].mean():.2f}s")
print(f" Completion Rate: {low['TaxaConclusão'].mean():.1f}%")
print(f" Vehicles Generated: {low['VeículosGerados'].mean():.0f}")
print(f" Vehicles Completed: {low['VeículosCompletados'].mean():.0f}")
print(f"\nMEDIUM LOAD:")
print(f" Avg Dwelling Time: {medium['TempoMédioSistema'].mean():.2f}s")
print(f" Avg Waiting Time: {medium['TempoMédioEspera'].mean():.2f}s")
print(f" Completion Rate: {medium['TaxaConclusão'].mean():.1f}%")
print(f" Vehicles Generated: {medium['VeículosGerados'].mean():.0f}")
print(f" Vehicles Completed: {medium['VeículosCompletados'].mean():.0f}")
print(f"\nHIGH LOAD:")
print(f" Avg Dwelling Time: {high['TempoMédioSistema'].mean():.2f}s")
print(f" Avg Waiting Time: {high['TempoMédioEspera'].mean():.2f}s")
print(f" Completion Rate: {high['TaxaConclusão'].mean():.1f}%")
print(f" Vehicles Generated: {high['VeículosGerados'].mean():.0f}")
print(f" Vehicles Completed: {high['VeículosCompletados'].mean():.0f}")
print("\n" + "="*60)
print("All graphs saved in 'graphs/' directory!")
print("="*60)

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

View File

@@ -11,6 +11,79 @@
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JUnit 5 for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<!-- Gson for JSON serialization -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<!-- JavaFX for UI -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Exec Plugin for running examples -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<mainClass>sd.dashboard.Launcher</mainClass>
</configuration>
</plugin>
<!-- JavaFX Maven Plugin -->
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>sd.dashboard.Launcher</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>sd.dashboard.Launcher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,147 +0,0 @@
package client;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import client.structs.NetworkManager;
import client.handlers.UnicastHandler;
import client.handlers.MulticastHandler;
import client.handlers.BroadcastHandler;
import client.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* Client application that manages network communications and thread handling.
* Created by: 0x1eo
* Last modified: 2024-12-12
*/
public class Client implements AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(Client.class.getName());
public static final String SERVER_ADDRESS = "localhost";
public static final String BROADCAST_ADDRESS = "255.255.255.255";
public static final int SERVER_PORT = 7500;
public static int CLIENT_PORT = 7501; // Made non-final to allow dynamic assignment
public static final int MULTICAST_PORT = 7502;
public static final int BUFFER_SIZE = 1024;
private final ExecutorService executorService;
private final NetworkManager networkManager;
private static Client instance;
private Client() {
this.executorService = Executors.newFixedThreadPool(3);
this.networkManager = NetworkManager.getInstance();
}
public static synchronized Client getInstance() {
if (instance == null) {
instance = new Client();
}
return instance;
}
/**
* Initializes and starts the client application.
*/
public void start() {
LOGGER.info("Initializing client application...");
try {
networkManager.initializePrimaryConnection();
setupShutdownHook();
initializeAuthenticatedState();
startInputLoop();
LOGGER.info("Client initialization completed successfully");
} catch (Exception e) {
LOGGER.severe("Failed to initialize client: " + e.getMessage());
close();
}
}
/**
* Initializes authenticated state and starts network handlers.
*/
public void initializeAuthenticatedState() {
try {
NetworkManager networkManager = NetworkManager.getInstance();
networkManager.initializeAuthenticatedConnections();
if (networkManager.isAuthenticated()) {
startNetworkHandlers();
LOGGER.info("Authenticated state initialized successfully on port " + CLIENT_PORT);
} else {
LOGGER.severe("Authentication failed");
close();
}
} catch (NetworkManager.NetworkInitializationException e) {
LOGGER.log(Level.SEVERE, "Failed to initialize authenticated state", e);
close();
}
}
private void startNetworkHandlers() {
executorService.execute(new UnicastHandler());
executorService.execute(new MulticastHandler());
executorService.execute(new BroadcastHandler());
}
private void setupShutdownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOGGER.info("Shutdown hook triggered");
close();
}));
}
@Override
public void close() {
LOGGER.info("Initiating client shutdown sequence...");
shutdownExecutors();
networkManager.close();
LOGGER.info("Client shutdown completed");
System.exit(0);
}
private void shutdownExecutors() {
try {
UnicastHandler.getExecutorService().shutdown();
executorService.shutdown();
// Wait for termination
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) {
LOGGER.warning("Executor service shutdown interrupted: " + e.getMessage());
Thread.currentThread().interrupt();
} catch (Exception e) {
LOGGER.severe("Error during executor service shutdown: " + e.getMessage());
}
}
private void startInputLoop() {
Thread inputThread = new Thread(() -> {
try (Scanner scanner = new Scanner(System.in)) {
while (!Thread.currentThread().isInterrupted()) {
String input = scanner.nextLine();
String response = InputCommandRouter.processInput(ConnType.UNICAST, input);
if (response != null) {
networkManager.getUnicastOut().println(response);
}
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error in input loop", e);
}
});
inputThread.setName("InputProcessor");
inputThread.start();
}
public static void main(String[] args) {
Client client = Client.getInstance();
client.start();
}
}

View File

@@ -1,164 +0,0 @@
package client.handlers;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import client.Client;
import client.structs.NetworkManager;
import client.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* Handles broadcast communication across the network.
* Receives and processes broadcast packets, excluding packets from the local host.
*
* @author 0x1eo
* @since 2024-12-13
*/
public class BroadcastHandler implements Runnable, AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(BroadcastHandler.class.getName());
private static final int SOCKET_TIMEOUT_MS = 5000; // 5 seconds
private static final long TIMEOUT_LOG_INTERVAL = 300000; // Log timeouts every 5 minutes
private static final long HEARTBEAT_INTERVAL = 60000; // 1 minute
private final NetworkManager networkManager;
private volatile boolean isRunning;
private DatagramSocket socket;
private long lastTimeoutLog;
private long lastHeartbeat;
public BroadcastHandler() {
this.networkManager = NetworkManager.getInstance();
this.isRunning = true;
this.lastTimeoutLog = System.currentTimeMillis();
this.lastHeartbeat = System.currentTimeMillis();
}
@Override
public void run() {
try {
initializeSocket();
LOGGER.info("Broadcast handler started successfully on port " + socket.getLocalPort());
processBroadcastMessages();
} catch (IOException e) {
if (isRunning) {
LOGGER.log(Level.SEVERE, "Fatal error in broadcast handler", e);
}
} finally {
close();
}
}
private void initializeSocket() throws IOException {
this.socket = networkManager.getBroadcastSocket();
if (socket == null) {
throw new IOException("Failed to initialize broadcast socket");
}
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
LOGGER.fine("Broadcast socket timeout set to " + SOCKET_TIMEOUT_MS + "ms");
}
private void processBroadcastMessages() throws IOException {
byte[] buffer = new byte[Client.BUFFER_SIZE];
InetAddress localhost = InetAddress.getLocalHost();
while (isRunning) {
checkHeartbeat();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
receiveAndProcessPacket(packet, localhost);
} catch (IOException e) {
handleReceiveException(e);
}
}
}
private void handleReceiveException(IOException e) {
if (!isRunning) return;
if (e instanceof java.net.SocketTimeoutException) {
long currentTime = System.currentTimeMillis();
if (currentTime - lastTimeoutLog > TIMEOUT_LOG_INTERVAL) {
LOGGER.fine("No broadcast messages received in the last " +
(SOCKET_TIMEOUT_MS / 1000) + " seconds");
lastTimeoutLog = currentTime;
}
} else {
LOGGER.log(Level.WARNING, "Error receiving broadcast packet", e);
}
}
private void checkHeartbeat() {
long currentTime = System.currentTimeMillis();
if (currentTime - lastHeartbeat > HEARTBEAT_INTERVAL) {
if (socket != null && !socket.isClosed()) {
LOGGER.fine("Broadcast connection alive - listening for messages");
}
lastHeartbeat = currentTime;
}
}
private void receiveAndProcessPacket(DatagramPacket packet, InetAddress localhost) throws IOException {
socket.receive(packet);
if (packet.getAddress().equals(localhost)) {
return; // Skip localhost packets
}
String input = extractMessage(packet);
String output = processMessage(input);
if (output != null) {
sendResponse(output, packet.getAddress(), packet.getPort());
}
}
private String extractMessage(DatagramPacket packet) {
return new String(
packet.getData(),
0,
packet.getLength(),
StandardCharsets.UTF_8
).trim();
}
private String processMessage(String input) {
try {
return InputCommandRouter.processInput(ConnType.BROADCAST, input);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error processing message", e);
return null;
}
}
private void sendResponse(String message, InetAddress address, int port) {
try {
byte[] responseData = message.getBytes(StandardCharsets.UTF_8);
DatagramPacket response = new DatagramPacket(
responseData,
responseData.length,
address,
port
);
socket.send(response);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to send broadcast response", e);
}
}
@Override
public void close() {
isRunning = false;
if (socket != null && !socket.isClosed()) {
socket.close();
LOGGER.info("Broadcast handler closed");
}
}
}

View File

@@ -1,163 +0,0 @@
package client.handlers;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import client.Client;
import client.structs.NetworkManager;
import client.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* Handles multicast communication between network nodes.
* Receives and processes multicast packets, excluding packets from the local host.
*
* @author 0x1eo
* @since 2024-12-13
*/
public class MulticastHandler implements Runnable, AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(MulticastHandler.class.getName());
private static final int SOCKET_TIMEOUT_MS = 5000; // 5 seconds
private static final long TIMEOUT_LOG_INTERVAL = 300000; // Log timeouts every 5 minutes
private static final long HEARTBEAT_INTERVAL = 60000; // 1 minute
private final NetworkManager networkManager;
private volatile boolean isRunning;
private DatagramSocket socket;
private long lastTimeoutLog;
private long lastHeartbeat;
public MulticastHandler() {
this.networkManager = NetworkManager.getInstance();
this.isRunning = true;
this.lastTimeoutLog = System.currentTimeMillis();
this.lastHeartbeat = System.currentTimeMillis();
}
@Override
public void run() {
try {
initializeSocket();
LOGGER.info("Multicast handler started successfully on port " + socket.getLocalPort());
processMulticastMessages();
} catch (IOException e) {
if (isRunning) {
LOGGER.log(Level.SEVERE, "Fatal error in multicast handler", e);
}
} finally {
close();
}
}
private void initializeSocket() throws IOException {
this.socket = networkManager.getMulticastSocket();
if (socket == null) {
throw new IOException("Failed to initialize multicast socket");
}
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
LOGGER.fine("Multicast socket timeout set to " + SOCKET_TIMEOUT_MS + "ms");
}
private void processMulticastMessages() throws IOException {
byte[] buffer = new byte[Client.BUFFER_SIZE];
InetAddress localhost = InetAddress.getLocalHost();
while (isRunning) {
checkHeartbeat();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try {
receiveAndProcessPacket(packet, localhost);
} catch (IOException e) {
handleReceiveException(e);
}
}
}
private void handleReceiveException(IOException e) {
if (!isRunning) return;
if (e instanceof java.net.SocketTimeoutException) {
long currentTime = System.currentTimeMillis();
if (currentTime - lastTimeoutLog > TIMEOUT_LOG_INTERVAL) {
LOGGER.fine("No multicast messages received in the last " +
(SOCKET_TIMEOUT_MS / 1000) + " seconds");
lastTimeoutLog = currentTime;
}
} else {
LOGGER.log(Level.WARNING, "Error receiving multicast packet", e);
}
}
private void checkHeartbeat() {
long currentTime = System.currentTimeMillis();
if (currentTime - lastHeartbeat > HEARTBEAT_INTERVAL) {
if (socket != null && !socket.isClosed()) {
LOGGER.fine("Multicast connection alive - listening for messages");
}
lastHeartbeat = currentTime;
}
}
private void receiveAndProcessPacket(DatagramPacket packet, InetAddress localhost) throws IOException {
socket.receive(packet);
if (packet.getAddress().equals(localhost)) {
return; // Skip localhost packets
}
String input = extractMessage(packet);
String output = processMessage(input);
if (output != null) {
sendResponse(output, packet.getAddress(), packet.getPort());
}
}
private String extractMessage(DatagramPacket packet) {
return new String(
packet.getData(),
0,
packet.getLength(),
StandardCharsets.UTF_8
).trim();
}
private String processMessage(String input) {
try {
return InputCommandRouter.processInput(ConnType.MULTICAST, input);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error processing message", e);
return null;
}
}
private void sendResponse(String message, InetAddress address, int port) {
try {
byte[] responseData = message.getBytes(StandardCharsets.UTF_8);
DatagramPacket response = new DatagramPacket(
responseData,
responseData.length,
address,
port
);
socket.send(response);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to send multicast response", e);
}
}
@Override
public void close() {
isRunning = false;
if (socket != null && !socket.isClosed()) {
socket.close();
LOGGER.info("Multicast handler closed");
}
}
}

View File

@@ -1,143 +0,0 @@
package client.handlers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import client.structs.NetworkManager;
import client.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* Handles unicast (point-to-point) connections between clients.
* Manages incoming connections and processes their messages in separate threads.
*
* @author 0x1eo
* @since 2024-12-12
*/
public class UnicastHandler implements Runnable, AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(UnicastHandler.class.getName());
private static final int MAX_THREADS = 50;
private static final int SOCKET_TIMEOUT_MS = 30000; // 30 seconds
private static final int SHUTDOWN_TIMEOUT_SECONDS = 5;
private static ExecutorService connectionPool = null;
private final NetworkManager networkManager;
private volatile boolean isRunning;
/**
* Creates a new UnicastHandler with a fixed thread pool.
*/
public UnicastHandler() {
connectionPool = Executors.newFixedThreadPool(MAX_THREADS);
this.networkManager = NetworkManager.getInstance();
this.isRunning = true;
}
@Override
public void run() {
ServerSocket serverSocket = networkManager.getServerSocket();
if (serverSocket == null) {
LOGGER.severe("Server socket is null. Cannot start UnicastHandler.");
return;
}
while (isRunning) {
try {
Socket clientSocket = serverSocket.accept();
configureSocket(clientSocket);
connectionPool.execute(new ClientHandler(clientSocket));
} catch (IOException e) {
if (isRunning) {
LOGGER.log(Level.SEVERE, "Error accepting connection", e);
}
}
}
}
private void configureSocket(Socket socket) throws IOException {
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
socket.setKeepAlive(true);
}
@Override
public void close() {
isRunning = false;
shutdownConnectionPool();
}
private void shutdownConnectionPool() {
connectionPool.shutdown();
try {
if (!connectionPool.awaitTermination(SHUTDOWN_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
connectionPool.shutdownNow();
}
} catch (InterruptedException e) {
connectionPool.shutdownNow();
Thread.currentThread().interrupt();
}
}
/**
* Gets the executor service managing client connections.
*
* @return the executor service
*/
public static ExecutorService getExecutorService() {
return connectionPool;
}
/**
* Handles individual client connections in separate threads.
*/
private static class ClientHandler implements Runnable {
private final Socket clientSocket;
private final String clientInfo;
public ClientHandler(Socket socket) {
this.clientSocket = socket;
this.clientInfo = String.format("%s:%d",
socket.getInetAddress().getHostAddress(),
socket.getPort());
}
@Override
public void run() {
LOGGER.info(() -> "New connection established with " + clientInfo);
try (Socket socket = clientSocket;
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
handleClientCommunication(in, out);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error handling client " + clientInfo, e);
} finally {
LOGGER.info(() -> "Connection closed with " + clientInfo);
}
}
private void handleClientCommunication(BufferedReader in, PrintWriter out) throws IOException {
String input;
while ((input = in.readLine()) != null) {
try {
String response = InputCommandRouter.processInput(ConnType.UNICAST, input);
if (response != null) {
out.println(response);
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error processing message from " + clientInfo, e);
}
}
}
}
}

View File

@@ -1,222 +0,0 @@
package client.structs;
import java.io.*;
import java.net.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import client.Client;
/**
* Network connection manager for the emergency communication client.
* Implements thread-safe singleton pattern and manages all network resources.
*
* Features:
* - Connection retry mechanism
* - Multiple communication channels (Unicast, Multicast, Broadcast)
* - Automatic resource cleanup
* - Connection state monitoring
*
* @author 0x1eo
* @since 2024-12-13
*/
public class NetworkManager implements AutoCloseable {
private static final Logger LOGGER = Logger.getLogger(NetworkManager.class.getName());
private static volatile NetworkManager instance;
private static final Object LOCK = new Object();
private static final int MAX_CONNECTION_RETRIES = 3;
private static final int RETRY_DELAY_MS = 2000;
private static final int SOCKET_TIMEOUT_MS = 5000;
private final AtomicBoolean isAuthenticated = new AtomicBoolean(false);
private volatile String username;
private final SocketContainer sockets;
private static final int PORT_RANGE_START = 7501;
private static final int PORT_RANGE_END = 7600;
private static final int MAX_PORT_ATTEMPTS = 10;
private NetworkManager() {
this.sockets = new SocketContainer();
}
public static NetworkManager getInstance() {
NetworkManager result = instance;
if (result == null) {
synchronized (LOCK) {
result = instance;
if (result == null) {
instance = result = new NetworkManager();
}
}
}
return result;
}
/**
* Initializes primary connection with retry mechanism.
*
* @throws NetworkInitializationException if connection fails after retries
*/
public void initializePrimaryConnection() {
for (int attempt = 1; attempt <= MAX_CONNECTION_RETRIES; attempt++) {
try {
establishPrimaryConnection();
LOGGER.info("Primary connection initialized successfully on attempt " + attempt);
return;
} catch (IOException e) {
handleConnectionRetry(attempt, e);
}
}
throw new NetworkInitializationException("Failed to initialize after " + MAX_CONNECTION_RETRIES + " attempts", null);
}
private void establishPrimaryConnection() throws IOException {
Socket unicastSocket = new Socket(Client.SERVER_ADDRESS, Client.SERVER_PORT);
unicastSocket.setSoTimeout(SOCKET_TIMEOUT_MS);
sockets.unicastSocket = unicastSocket;
sockets.unicastIn = new BufferedReader(
new InputStreamReader(unicastSocket.getInputStream())
);
sockets.unicastOut = new PrintWriter(
unicastSocket.getOutputStream(),
true
);
}
private void handleConnectionRetry(int attempt, IOException e) {
LOGGER.log(Level.WARNING,
String.format("Connection attempt %d/%d failed", attempt, MAX_CONNECTION_RETRIES),
e
);
if (attempt < MAX_CONNECTION_RETRIES) {
try {
Thread.sleep(RETRY_DELAY_MS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new NetworkInitializationException("Connection retry interrupted", ie);
}
}
}
/**
* Initializes authenticated connections for multicast and broadcast.
*
* @throws NetworkInitializationException if initialization fails
*/
public void initializeAuthenticatedConnections() {
for (int port = PORT_RANGE_START; port <= PORT_RANGE_END; port++) {
try {
setupAuthenticatedSockets(port);
isAuthenticated.set(true);
LOGGER.info(String.format(
"Authenticated connections initialized for user %s on port %d",
username, port
));
return;
} catch (IOException e) {
if (port >= PORT_RANGE_END) {
isAuthenticated.set(false);
throw new NetworkInitializationException(
"Failed to find available ports in range " +
PORT_RANGE_START + "-" + PORT_RANGE_END,
e
);
}
LOGGER.warning(String.format(
"Port %d in use, trying next port", port
));
}
}
}
private void setupAuthenticatedSockets(int basePort) throws IOException {
// Server socket uses the base port
sockets.serverSocket = new ServerSocket(basePort);
// Multicast socket uses the multicast port
sockets.multicastSocket = new MulticastSocket(Client.MULTICAST_PORT);
// Broadcast socket uses base port + 1 to avoid conflict
sockets.broadcastSocket = createBroadcastSocket(basePort + 1);
// Update the client port in the Client class
Client.CLIENT_PORT = basePort;
}
private DatagramSocket createBroadcastSocket(int port) throws IOException {
DatagramSocket socket = new DatagramSocket(
port,
InetAddress.getByName(Client.BROADCAST_ADDRESS)
);
socket.setBroadcast(true);
return socket;
}
@Override
public void close() {
sockets.closeAll();
isAuthenticated.set(false);
LOGGER.info("Network manager closed successfully");
}
// Thread-safe getters
public boolean isAuthenticated() { return isAuthenticated.get(); }
public String getUsername() { return username; }
public Socket getUnicastSocket() { return sockets.unicastSocket; }
public BufferedReader getUnicastIn() { return sockets.unicastIn; }
public PrintWriter getUnicastOut() { return sockets.unicastOut; }
public ServerSocket getServerSocket() { return sockets.serverSocket; }
public MulticastSocket getMulticastSocket() { return sockets.multicastSocket; }
public DatagramSocket getBroadcastSocket() { return sockets.broadcastSocket; }
// Thread-safe setter
public void setUsername(String username) {
this.username = username;
LOGGER.info("Username set to: " + username);
}
/**
* Thread-safe container for network resources.
*/
private static class SocketContainer {
private volatile Socket unicastSocket;
private volatile BufferedReader unicastIn;
private volatile PrintWriter unicastOut;
private volatile ServerSocket serverSocket;
private volatile MulticastSocket multicastSocket;
private volatile DatagramSocket broadcastSocket;
private void closeAll() {
closeQuietly(broadcastSocket, "BroadcastSocket");
closeQuietly(multicastSocket, "MulticastSocket");
closeQuietly(serverSocket, "ServerSocket");
closeQuietly(unicastSocket, "UnicastSocket");
closeQuietly(unicastOut, "UnicastWriter");
closeQuietly(unicastIn, "UnicastReader");
}
private void closeQuietly(AutoCloseable resource, String resourceName) {
if (resource != null) {
try {
resource.close();
LOGGER.fine(resourceName + " closed successfully");
} catch (Exception e) {
LOGGER.warning(resourceName + " close failed: " + e.getMessage());
}
}
}
}
/**
* Custom exception for network initialization failures.
*/
public static class NetworkInitializationException extends RuntimeException {
public NetworkInitializationException(String message, Throwable cause) {
super(message, cause);
}
}
}

View File

@@ -1,18 +0,0 @@
package client.structs;
public class TerminalMessageHandler {
public static void displayMessage(String from, String to, String content, String date) {
System.out.printf("[%s] %s -> %s: %s%n", date, from, to, content);
}
public static void displayRequest(String from, String to, String content, String date, String accepter) {
String accepterStatus = accepter.isEmpty() ? "PENDING" : "ACCEPTED by " + accepter;
System.out.printf("[%s] REQUEST from %s to %s: %s (%s)%n", date, from, to, content, accepterStatus);
}
public static boolean promptRequestAcceptance(String from, String to, String content) {
System.out.printf("Accept request from %s to %s: %s%n", from, to, content);
System.out.print("Accept? (y/n): ");
return System.console().readLine().trim().toLowerCase().startsWith("y");
}
}

View File

@@ -1,162 +0,0 @@
package client.utils;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import client.structs.NetworkManager;
import client.structs.TerminalMessageHandler;
/**
* Handles chat message operations including creation, receiving, and processing of messages and requests.
*
* @author 0x1eo
* @since 2024-12-12
*/
public class ChatMessageHandler {
private static final Logger LOGGER = Logger.getLogger(ChatMessageHandler.class.getName());
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm");
private static final NetworkManager networkManager = NetworkManager.getInstance();
private ChatMessageHandler() {} // Prevent instantiation
/**
* Message types supported by the chat system.
*/
public enum MessageType {
MESSAGE("message"),
REQUEST("request"),
JOIN_GROUP("joinGroup");
private final String command;
MessageType(String command) {
this.command = command;
}
public String getCommand() {
return command;
}
}
/**
* Creates a basic message structure with common fields.
*/
private static JSONObject createBaseMessage(MessageType type, String destination, String content) {
JSONObject json = new JSONObject();
json.put("command", type.getCommand());
json.put("from", networkManager.getUsername());
json.put("to", destination);
json.put("content", content);
json.put("date", LocalDateTime.now().format(DATE_FORMATTER));
return json;
}
public static JSONObject createMessage(String destination, String content) {
return createBaseMessage(MessageType.MESSAGE, destination, content);
}
public static JSONObject createRequest(String destination, String content) {
JSONObject json = createBaseMessage(MessageType.REQUEST, destination, content);
json.put("accepter", "");
return json;
}
/**
* Validates if a JSON object contains all required fields.
*/
private static boolean validateMessageFields(JSONObject json, String... requiredFields) {
for (String field : requiredFields) {
if (!json.has(field)) {
LOGGER.warning("Missing required field: " + field);
return false;
}
}
return true;
}
public static void receiveMessage(JSONObject json) {
if (!validateMessageFields(json, "from", "to", "content", "date")) {
return;
}
TerminalMessageHandler.displayMessage(
json.getString("from"),
json.getString("to"),
json.getString("content"),
json.getString("date")
);
}
public static void receiveRequest(JSONObject json) {
if (!validateMessageFields(json, "from", "to", "content", "date", "accepter")) {
return;
}
TerminalMessageHandler.displayRequest(
json.getString("from"),
json.getString("to"),
json.getString("content"),
json.getString("date"),
json.getString("accepter")
);
}
public static String handleAnswerRequest(JSONObject json) {
if (!validateMessageFields(json, "from", "to", "content")) {
return null;
}
boolean accepted = TerminalMessageHandler.promptRequestAcceptance(
json.getString("from"),
json.getString("to"),
json.getString("content")
);
return new JSONObject().put("response", accepted ? "YES" : "NO").toString();
}
public static void processEvents(JSONObject json) {
if (!json.has("events")) {
LOGGER.warning("No events field in JSON");
return;
}
JSONArray events = json.getJSONArray("events");
events.forEach(event -> {
JSONObject eventObj = (JSONObject) event;
if (!eventObj.has("command")) {
LOGGER.warning("Event missing command field");
return;
}
String command = eventObj.getString("command");
try {
switch (MessageType.valueOf(command.toUpperCase())) {
case MESSAGE -> receiveMessage(eventObj);
case REQUEST -> receiveRequest(eventObj);
default -> LOGGER.warning("Unknown command: " + command);
}
} catch (IllegalArgumentException e) {
LOGGER.warning("Invalid command type: " + command);
}
});
}
public static void announceJoinGroup(String ip) {
try {
JSONObject json = new JSONObject()
.put("command", MessageType.JOIN_GROUP.getCommand())
.put("group", ip)
.put("username", networkManager.getUsername());
networkManager.getUnicastOut().println(json);
} catch (JSONException e) {
LOGGER.severe("Failed to announce group join: " + e.getMessage());
}
}
}

View File

@@ -1,157 +0,0 @@
package client.utils;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import shared.enums.ConnType;
import client.structs.NetworkManager;
import shared.enums.Hierarchy;
/**
* Client-side command router that processes and formats commands according to server protocol.
*
* @author 0x1eo
* @since 2024-12-13 16:41:03
*/
public class InputCommandRouter {
private static final Logger LOGGER = Logger.getLogger(InputCommandRouter.class.getName());
private static final NetworkManager networkManager = NetworkManager.getInstance();
/**
* Processes input and formats it according to server protocol.
*
* @param connType The type of connection (UNICAST, MULTICAST, BROADCAST)
* @param input The user's input string
* @return Formatted JSON string or null if no response needed
*/
public static String processInput(ConnType connType, String input) {
if (input == null || input.trim().isEmpty()) {
return null;
}
String trimmedInput = input.trim();
// Handle client-side commands
if (trimmedInput.startsWith("{command:")) {
return handleCommand(connType, trimmedInput);
}
// Handle messages and requests
return handleMessage(connType, trimmedInput);
}
private static String handleCommand(ConnType connType, String command) {
if (connType != ConnType.UNICAST) {
return null;
}
// Split by : but keep quotes intact
String[] parts = command.substring(9, command.length() - 1).split(":");
String cmd = parts[0].toLowerCase();
switch (cmd) {
case "help":
displayHelp();
return null;
case "register":
if (parts.length != 4) {
System.out.println("Invalid register format. Use: {command:register:username:name:password}");
return null;
}
JSONObject registerJson = new JSONObject()
.put("command", "register")
.put("username", parts[1])
.put("name", parts[2])
.put("password", parts[3])
.put("role", Hierarchy.LOW.name());
// Debug log
LOGGER.info("Sending registration JSON: " + registerJson.toString());
return registerJson.toString();
default:
LOGGER.warning("Unknown command: " + command);
System.out.println("Unknown command. Type {command:help} for available commands.");
return null;
}
}
private static void displayHelp() {
System.out.println("\nAvailable Commands:");
System.out.println("------------------");
System.out.println("{command:register:username:name:password} - Register with the server");
System.out.println("Example: {command:register:leo:Leandro:0808wq21}");
System.out.println("{command:help} - Display this help message");
System.out.println("\nMessage Formats:");
System.out.println("---------------");
System.out.println("Regular message: text");
System.out.println("Direct message: @username: message");
System.out.println("Group message: #groupname: message");
System.out.println("Request: !request @username: content");
System.out.println();
}
private static String createRegistrationJson() {
JSONObject json = new JSONObject()
.put("command", "register")
.put("username", networkManager.getUsername())
.put("name", networkManager.getUsername()) // Using username as name for now
.put("password", "default") // Should be handled properly in production
.put("role", Hierarchy.LOW.name()); // Using LOW as the default hierarchy level
return json.toString();
}
private static String createLoginJson() {
JSONObject json = new JSONObject()
.put("command", "login")
.put("username", networkManager.getUsername())
.put("password", "default"); // Should be handled properly in production
return json.toString();
}
private static String handleMessage(ConnType connType, String input) {
try {
JSONObject messageJson;
if (input.startsWith("!request")) {
// Handle request
String[] parts = input.substring(9).split(":", 2);
if (parts.length != 2 || !parts[0].startsWith("@")) {
System.out.println("Invalid request format. Use: !request @username: content");
return null;
}
String destination = parts[0].substring(1).trim();
messageJson = ChatMessageHandler.createRequest(destination, parts[1].trim());
} else if (input.startsWith("@")) {
// Handle direct message
String[] parts = input.substring(1).split(":", 2);
if (parts.length != 2) {
System.out.println("Invalid direct message format. Use: @username: message");
return null;
}
messageJson = ChatMessageHandler.createMessage(parts[0].trim(), parts[1].trim());
} else if (input.startsWith("#")) {
// Handle group message
String[] parts = input.substring(1).split(":", 2);
if (parts.length != 2) {
System.out.println("Invalid group message format. Use: #groupname: message");
return null;
}
String groupName = parts[0].trim();
ChatMessageHandler.announceJoinGroup(groupName); // Join group first
messageJson = ChatMessageHandler.createMessage(groupName, parts[1].trim());
} else {
// Handle broadcast message
messageJson = ChatMessageHandler.createMessage("", input);
}
return messageJson.toString();
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Failed to process message: " + e.getMessage());
return null;
}
}
}

View File

@@ -0,0 +1,532 @@
package sd;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import sd.config.SimulationConfig;
import sd.coordinator.SocketClient;
import sd.dashboard.StatsUpdatePayload;
import sd.des.DESEventType;
import sd.des.EventQueue;
import sd.des.SimulationClock;
import sd.des.SimulationEvent;
import sd.logging.EventLogger;
import sd.logging.EventType;
import sd.logging.VehicleTracer;
import sd.model.Message;
import sd.model.MessageType;
import sd.model.Vehicle;
import sd.model.VehicleType;
import sd.protocol.MessageProtocol;
import sd.protocol.SocketConnection;
/**
* Ponto terminal da malha de simulação (Sink Node).
* <p>
* Este processo atua como o sumidouro da rede de filas. A sua função primária é
* a <b>coleta de telemetria final</b>. Diferente das interseções, não encaminha veículos;
* em vez disso, retira-os do sistema, calcula as métricas de latência "end-to-end"
* (tempo no sistema, tempo de espera acumulado) e reporta ao Dashboard.
* <p>
* <b>Arquitetura de Concorrência:</b>
* Utiliza um {@link ServerSocket} multithreaded para aceitar conexões simultâneas de
* qualquer interseção de fronteira (Cr1, Cr5, etc.) que envie veículos para fora da malha.
*/
public class ExitNodeProcess {
// --- Configuration and Networking ---
private final SimulationConfig config;
private ServerSocket serverSocket;
/** Pool de threads elástica para tratamento de conexões de entrada. */
private final ExecutorService connectionHandlerPool;
// DES components
private final SimulationClock clock;
private final EventQueue eventQueue;
private final EventLogger eventLogger;
private Thread eventProcessorThread;
/** Flag de controlo (volatile para visibilidade entre threads de I/O e lógica). */
private volatile boolean running;
/** Instante de início da simulação (milissegundos) sincronizado com o Coordenador. */
private long simulationStartMillis;
/** Contador atómico (via synchronized) de throughput total. */
private int totalVehiclesReceived;
/** Tempo acumulado no sistema (System Time) de todos os veículos. */
private double totalSystemTime;
/** Tempo acumulado em espera (Waiting Time) de todos os veículos. */
private double totalWaitingTime;
/** Tempo acumulado em travessia (Service Time) de todos os veículos. */
private double totalCrossingTime;
/** Agregação por categoria de veículo. */
private final Map<VehicleType, Integer> vehicleTypeCount;
/** Latência acumulada por categoria. */
private final Map<VehicleType, Double> vehicleTypeWaitTime;
/** Cliente TCP persistente para push de métricas ao Dashboard. */
private SocketClient dashboardClient;
/**
* Bootstrap do processo ExitNode.
* Carrega configuração, inicializa subsistemas e entra no loop de serviço.
* * @param args Argumentos de CLI (caminho do config).
*/
public static void main(String[] args) {
System.out.println("=".repeat(60));
System.out.println("EXIT NODE PROCESS");
System.out.println("=".repeat(60));
try {
EventLogger.getInstance().log(EventType.PROCESS_STARTED, "ExitNode", "Exit node process started");
String configFile = args.length > 0 ? args[0] : "src/main/resources/simulation.properties";
System.out.println("Loading configuration from: " + configFile);
SimulationConfig config = new SimulationConfig(configFile);
ExitNodeProcess exitNode = new ExitNodeProcess(config);
System.out.println("\n" + "=".repeat(60));
exitNode.initialize();
System.out.println("\n" + "=".repeat(60));
exitNode.start();
} catch (IOException e) {
System.err.println("Failed to start exit node: " + e.getMessage());
EventLogger.getInstance().logError("ExitNode", "Failed to start", e);
System.exit(1);
} catch (Exception e) {
System.err.println("Exit node error: " + e.getMessage());
EventLogger.getInstance().logError("ExitNode", "Exit node error", e);
System.exit(1);
} finally {
EventLogger.getInstance().log(EventType.PROCESS_STOPPED, "ExitNode", "Exit node process stopped");
}
}
/**
* Instancia o nó de saída.
* Prepara os acumuladores estatísticos e a infraestrutura de logging distribuído.
* * @param config A configuração global da simulação.
*/
public ExitNodeProcess(SimulationConfig config) {
this.config = config;
this.connectionHandlerPool = Executors.newCachedThreadPool();
this.running = false;
this.totalVehiclesReceived = 0;
this.totalSystemTime = 0.0;
this.totalWaitingTime = 0.0;
this.totalCrossingTime = 0.0;
this.vehicleTypeCount = new HashMap<>();
this.vehicleTypeWaitTime = new HashMap<>();
// Inicializa os counters para cada tipo de veículo
for (VehicleType type : VehicleType.values()) {
vehicleTypeCount.put(type, 0);
vehicleTypeWaitTime.put(type, 0.0);
}
// Initialize DES components
this.clock = new SimulationClock();
this.eventQueue = new EventQueue(true); // Track history
this.eventLogger = EventLogger.getInstance();
eventLogger.log(EventType.PROCESS_STARTED, "ExitNode",
"Exit node initialized with DES architecture");
System.out.println("Exit node initialized (DES Mode)");
System.out.println(" - Exit port: " + config.getExitPort());
System.out.println(" - Dashboard: " + config.getDashboardHost() + ":" + config.getDashboardPort());
}
/**
* Estabelece o canal de controlo (Control Plane) com o Dashboard.
* Essencial para a visualização em tempo real das métricas de saída.
*/
public void initialize() {
System.out.println("Connecting to dashboard...");
try {
String host = config.getDashboardHost();
int port = config.getDashboardPort();
dashboardClient = new SocketClient("Dashboard", host, port);
dashboardClient.connect();
System.out.println("Successfully connected to dashboard");
} catch (IOException e) {
System.err.println("WARNING: Failed to connect to dashboard: " + e.getMessage());
System.err.println("Exit node will continue without dashboard connection");
}
}
/**
* Inicia a thread de processamento de eventos DES.
* Embora o ExitNode seja primariamente reativo (Network-driven), o motor DES
* é mantido para consistência de relógio e agendamento de fim de simulação.
*/
private void startEventProcessor() {
eventProcessorThread = new Thread(() -> {
eventLogger.log(EventType.SIMULATION_STARTED, "ExitNode",
"Event processor thread started");
// Keep running while process is active
while (running) {
SimulationEvent event = eventQueue.poll();
if (event == null) {
// No events currently, wait before checking again
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
continue;
}
// Advance clock to event time
clock.advanceTo(event.getTimestamp());
// Process the event
processEvent(event);
}
eventLogger.log(EventType.SIMULATION_STOPPED, "ExitNode",
String.format("Event processor thread terminated at time %.2f", clock.getCurrentTime()));
}, "EventProcessor-ExitNode");
eventProcessorThread.start();
}
/**
* Dispatcher de eventos discretos.
* Trata eventos de fim de simulação. Chegadas de veículos são tratadas via Socket.
*/
private void processEvent(SimulationEvent event) {
try {
switch (event.getType()) {
case VEHICLE_EXIT:
// Vehicle exits are handled via network messages in real-time
// This event type can be used for scheduled vehicle processing
break;
case SIMULATION_END:
handleSimulationEndEvent(event);
break;
default:
System.err.println("[ExitNode] Unknown event type: " + event.getType());
}
} catch (Exception e) {
System.err.println("[ExitNode] Error processing event " + event.getType() +
" at time " + event.getTimestamp() + ": " + e.getMessage());
e.printStackTrace();
}
}
/**
* Executa a lógica de encerramento desencadeada pelo evento DES.
*/
private void handleSimulationEndEvent(SimulationEvent event) {
eventLogger.log(EventType.SIMULATION_STOPPED, "ExitNode",
String.format("Simulation ended at time %.2f", event.getTimestamp()));
running = false;
// Print final statistics
printFinalStatistics();
}
/**
* Exporta o histórico completo de eventos para auditoria.
* Requisito funcional para verificação de trace.
*/
public void exportEventHistory(String outputPath) {
String history = eventQueue.exportEventHistory();
try (java.io.PrintWriter writer = new java.io.PrintWriter(outputPath)) {
writer.println(history);
System.out.println("[ExitNode] Event history exported to: " + outputPath);
} catch (java.io.FileNotFoundException e) {
System.err.println("[ExitNode] Failed to export event history: " + e.getMessage());
}
}
/**
* Agenda o fim determinístico da simulação.
* * @param endTime Tempo virtual de paragem.
*/
public void scheduleSimulationEnd(double endTime) {
SimulationEvent endEvent = new SimulationEvent(
endTime,
DESEventType.SIMULATION_END,
null);
eventQueue.schedule(endEvent);
System.out.println("[ExitNode] Simulation end scheduled at time " + endTime);
}
/**
* Inicia o servidor TCP em modo de bloqueio (Blocking I/O).
* @throws IOException Se ocorrer erro no bind da porta.
*/
public void start() throws IOException {
start(true); // Default to DES mode
}
/**
* Inicia o processo com opção de ativar o rastreio DES.
* * @param useDES Se verdadeiro, ativa a thread do processador de eventos.
*/
public void start(boolean useDES) throws IOException {
int port = config.getExitPort();
serverSocket = new ServerSocket(port);
running = true;
simulationStartMillis = System.currentTimeMillis();
System.out.println("Exit node started on port " + port);
if (useDES) {
System.out.println("Running in DES mode (event history tracking enabled)");
}
System.out.println("Waiting for vehicles...\\n");
// Loop de aceitação principal
while (running) {
try {
Socket clientSocket = serverSocket.accept();
// Delega o processamento da conexão para o Thread Pool
connectionHandlerPool.submit(() -> handleIncomingConnection(clientSocket));
} catch (IOException e) {
if (running) {
System.err.println("Error accepting connection: " + e.getMessage());
}
}
}
}
/**
* Worker method para tratar uma conexão persistente vinda de uma interseção.
* <p>
* Mantém o socket aberto e consome mensagens num loop até que a conexão seja fechada
* pelo remetente. Responsável pela desserialização polimórfica (JSON/Gson).
* * @param clientSocket O socket conectado.
*/
private void handleIncomingConnection(Socket clientSocket) {
String clientAddress = clientSocket.getInetAddress().getHostAddress();
System.out.println("New connection accepted from " + clientAddress);
try (SocketConnection connection = new SocketConnection(clientSocket)) {
while (running && connection.isConnected()) {
try {
System.out.println("[Exit] Waiting for message from " + clientAddress);
MessageProtocol message = connection.receiveMessage();
System.out.println("[Exit] Received message type: " + message.getType() +
" from " + message.getSourceNode());
if (message.getType() == MessageType.SIMULATION_START) {
// Sincronização de relógio com o Coordenador
simulationStartMillis = ((Number) message.getPayload()).longValue();
System.out.println("[Exit] Simulation start time synchronized");
} else if (message.getType() == MessageType.VEHICLE_TRANSFER) {
Object payload = message.getPayload();
System.out.println("[Exit] Payload type: " + payload.getClass().getName());
// Tratamento de artefatos de desserialização do Gson (LinkedTreeMap -> POJO)
Vehicle vehicle;
if (payload instanceof com.google.gson.internal.LinkedTreeMap ||
payload instanceof java.util.LinkedHashMap) {
String json = new com.google.gson.Gson().toJson(payload);
vehicle = new com.google.gson.Gson().fromJson(json, Vehicle.class);
} else {
vehicle = (Vehicle) payload;
}
processExitingVehicle(vehicle);
}
} catch (ClassNotFoundException e) {
System.err.println("[Exit] Unknown message type: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("[Exit] Error processing message: " + e.getMessage());
e.printStackTrace();
}
}
System.out.println("[Exit] Connection closed from " + clientAddress);
} catch (IOException e) {
if (running) {
System.err.println("[Exit] Connection error from " + clientAddress + ": " + e.getMessage());
e.printStackTrace();
}
}
}
/**
* Processa atomicamente a saída de um veículo.
* <p>
* <b>Secção Crítica:</b> Método {@code synchronized} para garantir que a atualização
* das estatísticas globais (totalSystemTime, contadores) é atómica, prevenindo
* Race Conditions quando múltiplos veículos chegam simultaneamente de interseções diferentes.
* * @param vehicle O veículo que completou a rota.
*/
private synchronized void processExitingVehicle(Vehicle vehicle) {
totalVehiclesReceived++;
// Cálculo de métricas finais baseadas no tempo virtual de simulação acumulado no veículo
double waitTime = vehicle.getTotalWaitingTime();
double crossingTime = vehicle.getTotalCrossingTime();
double systemTime = waitTime + crossingTime;
totalSystemTime += systemTime;
totalWaitingTime += waitTime;
totalCrossingTime += crossingTime;
VehicleType type = vehicle.getType();
vehicleTypeCount.put(type, vehicleTypeCount.get(type) + 1);
vehicleTypeWaitTime.put(type, vehicleTypeWaitTime.get(type) + waitTime);
System.out.printf("[Exit] Vehicle %s completed (type=%s, system_time=%.2fs, wait=%.2fs, crossing=%.2fs)%n",
vehicle.getId(), vehicle.getType(), systemTime, waitTime, crossingTime);
// Logging estruturado
EventLogger.getInstance().logVehicle(EventType.VEHICLE_EXITED, "ExitNode", vehicle.getId(),
String.format("Completed - System: %.2fs, Wait: %.2fs, Crossing: %.2fs", systemTime, waitTime,
crossingTime));
// Finaliza o trace individual do veículo
VehicleTracer.getInstance().logExit(vehicle, systemTime);
// Push imediato para o Dashboard para visualização em tempo real
sendStatsToDashboard();
}
/**
* Constrói e transmite o DTO de atualização de estatísticas.
*/
private void sendStatsToDashboard() {
if (dashboardClient == null || !dashboardClient.isConnected()) {
return;
}
try {
// Create stats payload
StatsUpdatePayload payload = new StatsUpdatePayload();
// Set global stats - convert seconds to milliseconds for display consistency
payload.setTotalVehiclesCompleted(totalVehiclesReceived);
payload.setTotalSystemTime((long) (totalSystemTime * 1000.0));
payload.setTotalWaitingTime((long) (totalWaitingTime * 1000.0));
// Hack: Usar campos de interseção para mostrar throughput no dashboard
payload.setIntersectionArrivals(totalVehiclesReceived);
payload.setIntersectionDepartures(totalVehiclesReceived);
payload.setIntersectionQueueSize(0);
// Detailed breakdown
Map<VehicleType, Integer> typeCounts = new HashMap<>();
Map<VehicleType, Long> typeWaitTimes = new HashMap<>();
for (VehicleType type : VehicleType.values()) {
typeCounts.put(type, vehicleTypeCount.get(type));
typeWaitTimes.put(type, (long) (vehicleTypeWaitTime.get(type) * 1000.0));
}
payload.setVehicleTypeCounts(typeCounts);
payload.setVehicleTypeWaitTimes(typeWaitTimes);
Message message = new Message(
MessageType.STATS_UPDATE,
"ExitNode",
"Dashboard",
payload);
dashboardClient.send(message);
double avgWait = totalVehiclesReceived > 0 ? totalWaitingTime / totalVehiclesReceived : 0.0;
System.out.printf("[Exit] Sent stats to dashboard (total=%d, avg_wait=%.2fs)%n",
totalVehiclesReceived, avgWait);
} catch (Exception e) {
System.err.println("[Exit] Failed to send stats to dashboard: " + e.getMessage());
}
}
/**
* Encerramento gracioso do processo.
* Fecha sockets, termina a pool de threads e liberta recursos.
*/
public void shutdown() {
System.out.println("\n[Exit] Shutting down...");
running = false;
printFinalStatistics();
sendStatsToDashboard();
try {
if (serverSocket != null && !serverSocket.isClosed()) {
serverSocket.close();
}
} catch (IOException e) {
System.err.println("Error closing server socket: " + e.getMessage());
}
connectionHandlerPool.shutdown();
try {
if (!connectionHandlerPool.awaitTermination(5, TimeUnit.SECONDS)) {
connectionHandlerPool.shutdownNow();
}
} catch (InterruptedException e) {
connectionHandlerPool.shutdownNow();
}
if (dashboardClient != null) {
dashboardClient.close();
}
System.out.println("[Exit] Shutdown complete.");
System.out.println("=".repeat(60));
}
/**
* Imprime o relatório final no stdout.
*/
private void printFinalStatistics() {
System.out.println("\n=== EXIT NODE STATISTICS ===");
System.out.printf("Total Vehicles Completed: %d%n", totalVehiclesReceived);
if (totalVehiclesReceived > 0) {
System.out.printf("%nAVERAGE METRICS:%n");
System.out.printf(" System Time: %.2f seconds%n", totalSystemTime / totalVehiclesReceived);
System.out.printf(" Waiting Time: %.2f seconds%n", totalWaitingTime / totalVehiclesReceived);
System.out.printf(" Crossing Time: %.2f seconds%n", totalCrossingTime / totalVehiclesReceived);
}
System.out.println("\nVEHICLE TYPE DISTRIBUTION:");
for (VehicleType type : VehicleType.values()) {
int count = vehicleTypeCount.get(type);
if (count > 0) {
double percentage = (count * 100.0) / totalVehiclesReceived;
double avgWait = vehicleTypeWaitTime.get(type) / count;
System.out.printf(" %s: %d (%.1f%%), Avg Wait: %.2fs%n",
type, count, percentage, avgWait);
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,285 @@
package sd.analysis;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import sd.model.VehicleType;
/**
* Responsável pela agregação e análise estatística de múltiplas execuções da simulação.
* <p>
* Esta classe coleta resultados individuais ({@link SimulationRunResult}) e calcula
* métricas consolidadas, incluindo média, desvio padrão, mediana e intervalos de
* confiança de 95%. O objetivo é fornecer uma visão robusta do comportamento do
* sistema, mitigando a variância estocástica de execuções isoladas.
*/
public class MultiRunAnalyzer {
/** Lista acumulada de resultados de execuções individuais. */
private final List<SimulationRunResult> results;
/** Identificador do ficheiro de configuração utilizado nas execuções. */
private final String configurationFile;
/**
* Inicializa o analisador para um conjunto específico de configurações.
*
* @param configurationFile O caminho ou nome do ficheiro de configuração base.
*/
public MultiRunAnalyzer(String configurationFile) {
this.configurationFile = configurationFile;
this.results = new ArrayList<>();
}
/**
* Adiciona o resultado de uma execução de simulação concluída ao conjunto de dados.
*
* @param result O objeto contendo as métricas da execução individual.
*/
public void addResult(SimulationRunResult result) {
results.add(result);
}
/**
* Retorna o número total de execuções armazenadas até o momento.
*
* @return O tamanho da lista de resultados.
*/
public int getRunCount() {
return results.size();
}
/**
* Gera um relatório estatístico abrangente formatado em texto.
* <p>
* O relatório inclui:
* <ul>
* <li>Métricas globais (throughput, tempos de espera, tempos no sistema).</li>
* <li>Análise segmentada por tipo de veículo ({@link VehicleType}).</li>
* <li>Análise de gargalos por interseção (tamanhos de fila).</li>
* <li>Resumos brutos das execuções individuais.</li>
* </ul>
*
* @return Uma String contendo o relatório completo formatado.
*/
public String generateReport() {
if (results.isEmpty()) {
return "No simulation results to analyze.";
}
StringBuilder report = new StringBuilder();
// Header
report.append("=".repeat(80)).append("\n");
report.append("ANÁLISE ESTATÍSTICA MULTI-EXECUÇÃO\n");
report.append("=".repeat(80)).append("\n");
report.append("Configuração: ").append(configurationFile).append("\n");
report.append("Número de Execuções: ").append(results.size()).append("\n");
report.append("Data da Análise: ").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())).append("\n");
report.append("\n");
// Global metrics
report.append("-".repeat(80)).append("\n");
report.append("MÉTRICAS GLOBAIS\n");
report.append("-".repeat(80)).append("\n\n");
report.append(analyzeMetric("Veículos Gerados",
extractValues(r -> (double) r.getTotalVehiclesGenerated())));
report.append("\n");
report.append(analyzeMetric("Veículos Completados",
extractValues(r -> (double) r.getTotalVehiclesCompleted())));
report.append("\n");
report.append(analyzeMetric("Taxa de Conclusão (%)",
extractValues(r -> r.getTotalVehiclesGenerated() > 0
? 100.0 * r.getTotalVehiclesCompleted() / r.getTotalVehiclesGenerated()
: 0.0)));
report.append("\n");
report.append(analyzeMetric("Tempo Médio no Sistema (segundos)",
extractValues(r -> r.getAverageSystemTime())));
report.append("\n");
report.append(analyzeMetric("Tempo Médio de Espera (segundos)",
extractValues(r -> r.getAverageWaitingTime())));
report.append("\n");
// Per-vehicle-type analysis
report.append("\n");
report.append("-".repeat(80)).append("\n");
report.append("ANÁLISE POR TIPO DE VEÍCULO\n");
report.append("-".repeat(80)).append("\n\n");
for (VehicleType type : VehicleType.values()) {
report.append("--- ").append(type).append(" ---\n");
report.append(analyzeMetric(" Contagem de Veículos",
extractValues(r -> (double) r.getVehicleCountByType().getOrDefault(type, 0))));
report.append("\n");
report.append(analyzeMetric(" Tempo Médio no Sistema (segundos)",
extractValues(r -> r.getAvgSystemTimeByType().getOrDefault(type, 0.0))));
report.append("\n");
report.append(analyzeMetric(" Tempo Médio de Espera (segundos)",
extractValues(r -> r.getAvgWaitTimeByType().getOrDefault(type, 0.0))));
report.append("\n\n");
}
// Per-intersection analysis
report.append("-".repeat(80)).append("\n");
report.append("ANÁLISE POR INTERSEÇÃO\n");
report.append("-".repeat(80)).append("\n\n");
Set<String> allIntersections = new TreeSet<>();
for (SimulationRunResult result : results) {
allIntersections.addAll(result.getMaxQueueSizeByIntersection().keySet());
}
for (String intersection : allIntersections) {
report.append("--- ").append(intersection).append(" ---\n");
report.append(analyzeMetric(" Tamanho Máximo da Fila",
extractValues(r -> (double) r.getMaxQueueSizeByIntersection().getOrDefault(intersection, 0))));
report.append("\n");
report.append(analyzeMetric(" Tamanho Médio da Fila",
extractValues(r -> r.getAvgQueueSizeByIntersection().getOrDefault(intersection, 0.0))));
report.append("\n");
report.append(analyzeMetric(" Veículos Processados",
extractValues(r -> (double) r.getVehiclesProcessedByIntersection().getOrDefault(intersection, 0))));
report.append("\n\n");
}
// Individual run summaries
report.append("-".repeat(80)).append("\n");
report.append("RESUMOS INDIVIDUAIS DAS EXECUÇÕES\n");
report.append("-".repeat(80)).append("\n\n");
for (SimulationRunResult result : results) {
report.append(result.toString()).append("\n\n");
}
report.append("=".repeat(80)).append("\n");
report.append("FIM DO RELATÓRIO\n");
report.append("=".repeat(80)).append("\n");
return report.toString();
}
/**
* Analisa uma métrica específica e retorna as estatísticas formatadas.
* <p>
* Calcula média, desvio padrão, mediana, intervalo de confiança (95%) e extremos (min/max).
*
* @param metricName O nome descritivo da métrica (ex: "Tempo de Espera").
* @param values A lista de valores numéricos brutos extraídos das execuções.
* @return Uma string formatada com os dados estatísticos.
*/
private String analyzeMetric(String metricName, List<Double> values) {
if (values.isEmpty() || values.stream().allMatch(v -> v == 0.0)) {
return metricName + ": Sem dados\n";
}
double mean = StatisticalAnalysis.mean(values);
double stdDev = StatisticalAnalysis.standardDeviation(values);
double[] ci = StatisticalAnalysis.confidenceInterval95(values);
double min = StatisticalAnalysis.min(values);
double max = StatisticalAnalysis.max(values);
double median = StatisticalAnalysis.median(values);
return String.format(
"%s:\n" +
" Média: %10.2f Desvio Padrão: %10.2f\n" +
" Mediana: %10.2f IC 95%%: [%.2f, %.2f]\n" +
" Mín: %10.2f Máx: %10.2f\n",
metricName, mean, stdDev, median, ci[0], ci[1], min, max
);
}
/**
* Extrai valores numéricos dos resultados de simulação usando uma função mapeadora.
* <p>
* Utilizado internamente para transformar a lista de objetos complexos {@link SimulationRunResult}
* em listas simples de Doubles para processamento estatístico.
*
* @param extractor Função lambda que define qual campo extrair de cada resultado.
* @return Lista de valores double correspondentes.
*/
private List<Double> extractValues(java.util.function.Function<SimulationRunResult, Double> extractor) {
List<Double> values = new ArrayList<>();
for (SimulationRunResult result : results) {
values.add(extractor.apply(result));
}
return values;
}
/**
* Persiste o relatório gerado num ficheiro de texto.
*
* @param filename O caminho do ficheiro de destino.
* @throws IOException Se ocorrer um erro de escrita no disco.
*/
public void saveReport(String filename) throws IOException {
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)))) {
writer.print(generateReport());
}
}
/**
* Gera um resumo em formato CSV para fácil importação em ferramentas de planilha.
* <p>
* Este método atua como um wrapper para {@link #saveCSVSummary(String)}.
*
* @param filename O caminho do ficheiro CSV de destino.
* @throws IOException Se ocorrer um erro de escrita no disco.
*/
public void saveCSV(String filename) throws IOException {
saveCSVSummary(filename);
}
/**
* Gera e grava o sumário CSV detalhado com métricas chave por execução.
* <p>
* Colunas incluídas: Execução, VeículosGerados, VeículosCompletados, TaxaConclusão,
* TempoMédioSistema, TempoMédioEspera, TempoMínimoSistema, TempoMáximoSistema.
*
* @param filename O caminho do ficheiro CSV de destino.
* @throws IOException Se ocorrer um erro de escrita no disco.
*/
public void saveCSVSummary(String filename) throws IOException {
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)))) {
// Header
writer.println("Execução,VeículosGerados,VeículosCompletados,TaxaConclusão," +
"TempoMédioSistema,TempoMédioEspera,TempoMínimoSistema,TempoMáximoSistema");
// Data rows
for (SimulationRunResult result : results) {
double completionRate = result.getTotalVehiclesGenerated() > 0
? 100.0 * result.getTotalVehiclesCompleted() / result.getTotalVehiclesGenerated()
: 0.0;
writer.printf("%d,%d,%d,%.2f,%.2f,%.2f,%.2f,%.2f\n",
result.getRunNumber(),
result.getTotalVehiclesGenerated(),
result.getTotalVehiclesCompleted(),
completionRate,
result.getAverageSystemTime(),
result.getAverageWaitingTime(),
result.getMinSystemTime(),
result.getMaxSystemTime()
);
}
}
}
}

View File

@@ -0,0 +1,206 @@
package sd.analysis;
import java.util.HashMap;
import java.util.Map;
import sd.model.VehicleType;
/**
* Encapsula os dados telemétricos e estatísticos resultantes de uma única execução da simulação.
* <p>
* Esta classe atua como um registo estruturado de métricas de desempenho, armazenando
* dados de latência (tempos de sistema/espera), vazão (throughput) e ocupação de recursos
* (tamanhos de fila). Os dados aqui contidos servem como base para a análise
* estatística agregada realizada pelo {@link MultiRunAnalyzer}.
*/
public class SimulationRunResult {
private final int runNumber;
private final String configurationFile;
private final long startTimeMillis;
private final long endTimeMillis;
// Global metrics
/** Total de veículos instanciados pelos geradores durante a execução. */
private int totalVehiclesGenerated;
/** Total de veículos que completaram o percurso e saíram do sistema com sucesso. */
private int totalVehiclesCompleted;
/** Média global do tempo total (em segundos) desde a geração até a saída. */
private double averageSystemTime; // seconds
/** Menor tempo de sistema registado (em segundos). */
private double minSystemTime; // seconds
/** Maior tempo de sistema registado (em segundos). */
private double maxSystemTime; // seconds
/** Média global do tempo (em segundos) que os veículos passaram parados em filas. */
private double averageWaitingTime; // seconds
// Per-type metrics
private final Map<VehicleType, Integer> vehicleCountByType;
private final Map<VehicleType, Double> avgSystemTimeByType;
private final Map<VehicleType, Double> avgWaitTimeByType;
// Per-intersection metrics
private final Map<String, Integer> maxQueueSizeByIntersection;
private final Map<String, Double> avgQueueSizeByIntersection;
private final Map<String, Integer> vehiclesProcessedByIntersection;
/**
* Inicializa um novo contentor de resultados para uma execução específica.
*
* @param runNumber O identificador sequencial desta execução.
* @param configurationFile O ficheiro de configuração utilizado.
*/
public SimulationRunResult(int runNumber, String configurationFile) {
this.runNumber = runNumber;
this.configurationFile = configurationFile;
this.startTimeMillis = System.currentTimeMillis();
this.endTimeMillis = 0;
this.vehicleCountByType = new HashMap<>();
this.avgSystemTimeByType = new HashMap<>();
this.avgWaitTimeByType = new HashMap<>();
this.maxQueueSizeByIntersection = new HashMap<>();
this.avgQueueSizeByIntersection = new HashMap<>();
this.vehiclesProcessedByIntersection = new HashMap<>();
}
/**
* Sinaliza o fim da recolha de dados para esta execução.
* (Placeholder para lógica de finalização de timestamps).
*/
public void markCompleted() {
// This will be called when the run finishes
}
// Getters
public int getRunNumber() { return runNumber; }
public String getConfigurationFile() { return configurationFile; }
public long getStartTimeMillis() { return startTimeMillis; }
public long getEndTimeMillis() { return endTimeMillis; }
/**
* Calcula a duração total da execução em milissegundos.
* @return Delta entre fim e início.
*/
public long getDurationMillis() { return endTimeMillis - startTimeMillis; }
public int getTotalVehiclesGenerated() { return totalVehiclesGenerated; }
public int getTotalVehiclesCompleted() { return totalVehiclesCompleted; }
public double getAverageSystemTime() { return averageSystemTime; }
public double getMinSystemTime() { return minSystemTime; }
public double getMaxSystemTime() { return maxSystemTime; }
public double getAverageWaitingTime() { return averageWaitingTime; }
/**
* Retorna o mapeamento de contagem de veículos por tipo.
* @return Uma cópia defensiva do mapa (snapshot).
*/
public Map<VehicleType, Integer> getVehicleCountByType() {
return new HashMap<>(vehicleCountByType);
}
/**
* Retorna o tempo médio no sistema segmentado por tipo de veículo.
* @return Uma cópia defensiva do mapa (snapshot).
*/
public Map<VehicleType, Double> getAvgSystemTimeByType() {
return new HashMap<>(avgSystemTimeByType);
}
/**
* Retorna o tempo médio de espera segmentado por tipo de veículo.
* @return Uma cópia defensiva do mapa (snapshot).
*/
public Map<VehicleType, Double> getAvgWaitTimeByType() {
return new HashMap<>(avgWaitTimeByType);
}
/**
* Retorna o tamanho máximo de fila registado por interseção (gargalos).
* @return Uma cópia defensiva do mapa (snapshot).
*/
public Map<String, Integer> getMaxQueueSizeByIntersection() {
return new HashMap<>(maxQueueSizeByIntersection);
}
/**
* Retorna o tamanho médio das filas por interseção.
* @return Uma cópia defensiva do mapa (snapshot).
*/
public Map<String, Double> getAvgQueueSizeByIntersection() {
return new HashMap<>(avgQueueSizeByIntersection);
}
/**
* Retorna o total de veículos processados (throughput) por interseção.
* @return Uma cópia defensiva do mapa (snapshot).
*/
public Map<String, Integer> getVehiclesProcessedByIntersection() {
return new HashMap<>(vehiclesProcessedByIntersection);
}
// Setters
public void setTotalVehiclesGenerated(int count) {
this.totalVehiclesGenerated = count;
}
public void setTotalVehiclesCompleted(int count) {
this.totalVehiclesCompleted = count;
}
public void setAverageSystemTime(double time) {
this.averageSystemTime = time;
}
public void setMinSystemTime(double time) {
this.minSystemTime = time;
}
public void setMaxSystemTime(double time) {
this.maxSystemTime = time;
}
public void setAverageWaitingTime(double time) {
this.averageWaitingTime = time;
}
public void setVehicleCountByType(VehicleType type, int count) {
vehicleCountByType.put(type, count);
}
public void setAvgSystemTimeByType(VehicleType type, double time) {
avgSystemTimeByType.put(type, time);
}
public void setAvgWaitTimeByType(VehicleType type, double time) {
avgWaitTimeByType.put(type, time);
}
public void setMaxQueueSize(String intersection, int size) {
maxQueueSizeByIntersection.put(intersection, size);
}
public void setAvgQueueSize(String intersection, double size) {
avgQueueSizeByIntersection.put(intersection, size);
}
public void setVehiclesProcessed(String intersection, int count) {
vehiclesProcessedByIntersection.put(intersection, count);
}
/**
* Gera uma representação textual resumida das métricas principais da execução.
* Útil para logs rápidos e debugging.
*/
@Override
public String toString() {
return String.format(
"Execução #%d [%s]:\n" +
" Gerados: %d, Completados: %d (%.1f%%)\n" +
" Tempo Médio no Sistema: %.2fs\n" +
" Tempo Médio de Espera: %.2fs",
runNumber,
configurationFile,
totalVehiclesGenerated,
totalVehiclesCompleted,
totalVehiclesGenerated > 0 ? 100.0 * totalVehiclesCompleted / totalVehiclesGenerated : 0.0,
averageSystemTime,
averageWaitingTime
);
}
}

View File

@@ -0,0 +1,193 @@
package sd.analysis;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Utilitário estático para processamento matemático e análise estatística dos dados da simulação.
* <p>
* Esta classe fornece algoritmos para cálculo de medidas de tendência central (média, mediana),
* dispersão (desvio padrão amostral) e inferência estatística (Intervalos de Confiança).
* É utilizada para normalizar e validar os resultados estocásticos obtidos através de
* múltiplas execuções do sistema.
*/
public class StatisticalAnalysis {
/**
* Calcula a média aritmética de um conjunto de valores.
* * @param values Lista de valores numéricos (double).
* @return A soma dos valores dividida pelo tamanho da amostra, ou 0.0 se a lista for nula/vazia.
*/
public static double mean(List<Double> values) {
if (values == null || values.isEmpty()) {
return 0.0;
}
double sum = 0.0;
for (double value : values) {
sum += value;
}
return sum / values.size();
}
/**
* Calcula o desvio padrão amostral (sample standard deviation).
* <p>
* Utiliza o denominador {@code n - 1} (Correção de Bessel) para fornecer um
* estimador não viesado da variância populacional, adequado para as amostras
* de simulação.
* * @param values Lista de observações.
* @return O desvio padrão calculado, ou 0.0 se o tamanho da amostra for < 2.
*/
public static double standardDeviation(List<Double> values) {
if (values == null || values.size() < 2) {
return 0.0;
}
double mean = mean(values);
double sumSquaredDiff = 0.0;
for (double value : values) {
double diff = value - mean;
sumSquaredDiff += diff * diff;
}
// Sample standard deviation (n-1 denominator)
return Math.sqrt(sumSquaredDiff / (values.size() - 1));
}
/**
* Calcula o Intervalo de Confiança (IC) de 95% para a média.
* <p>
* Utiliza a distribuição t de Student para maior precisão em amostras pequenas (n < 30),
* onde a aproximação pela distribuição Normal (Z) seria inadequada. O intervalo define
* a faixa onde a verdadeira média populacional reside com 95% de probabilidade.
* * @param values Lista de observações.
* @return Um array de double onde índice 0 é o limite inferior e índice 1 é o limite superior.
*/
public static double[] confidenceInterval95(List<Double> values) {
if (values == null || values.size() < 2) {
double m = mean(values);
return new double[]{m, m};
}
double mean = mean(values);
double stdDev = standardDeviation(values);
int n = values.size();
// Critical value from t-distribution (approximation for common sample sizes)
double tCritical = getTCriticalValue(n);
// Standard error of the mean
double standardError = stdDev / Math.sqrt(n);
// Margin of error
double marginOfError = tCritical * standardError;
return new double[]{
mean - marginOfError, // Lower bound
mean + marginOfError // Upper bound
};
}
/**
* Retorna o valor crítico t (t-score) para um IC de 95% (bicaudal).
* <p>
* Baseia-se nos graus de liberdade (gl = n - 1). Para amostras grandes (gl >= 30),
* aproxima-se do valor Z de 1.96.
* * @param sampleSize O tamanho da amostra (n).
* @return O fator multiplicativo t apropriado.
*/
private static double getTCriticalValue(int sampleSize) {
int df = sampleSize - 1; // degrees of freedom
// t-critical values for 95% confidence (two-tailed)
if (df >= 30) return 1.96; // z-score for large samples
if (df >= 20) return 2.086;
if (df >= 15) return 2.131;
if (df >= 10) return 2.228;
if (df >= 5) return 2.571;
if (df >= 3) return 3.182;
if (df >= 2) return 4.303;
return 12.706; // df = 1
}
/**
* Identifica o valor mínimo absoluto na amostra.
* * @param values Lista de valores.
* @return O menor valor encontrado.
*/
public static double min(List<Double> values) {
if (values == null || values.isEmpty()) {
return 0.0;
}
return Collections.min(values);
}
/**
* Identifica o valor máximo absoluto na amostra.
* * @param values Lista de valores.
* @return O maior valor encontrado.
*/
public static double max(List<Double> values) {
if (values == null || values.isEmpty()) {
return 0.0;
}
return Collections.max(values);
}
/**
* Calcula a mediana da amostra.
* <p>
* <b>Nota de Desempenho:</b> Este método ordena uma cópia da lista, resultando em
* complexidade O(n log n).
* * @param values Lista de valores.
* @return O valor central (ou média dos dois centrais) da distribuição ordenada.
*/
public static double median(List<Double> values) {
if (values == null || values.isEmpty()) {
return 0.0;
}
List<Double> sorted = new ArrayList<>(values);
Collections.sort(sorted);
int size = sorted.size();
if (size % 2 == 0) {
return (sorted.get(size / 2 - 1) + sorted.get(size / 2)) / 2.0;
} else {
return sorted.get(size / 2);
}
}
/**
* Formata um sumário estatístico completo para uma métrica específica.
* <p>
* Útil para logging e geração de relatórios textuais.
* * @param metricName Nome da métrica a ser exibida.
* @param values Os dados brutos associados à métrica.
* @return String formatada contendo Média, Desvio Padrão, IC95%, Min, Max e N.
*/
public static String formatSummary(String metricName, List<Double> values) {
if (values == null || values.isEmpty()) {
return metricName + ": No data";
}
double mean = mean(values);
double stdDev = standardDeviation(values);
double[] ci = confidenceInterval95(values);
double min = min(values);
double max = max(values);
return String.format(
"%s:\n" +
" Mean: %.2f\n" +
" Std Dev: %.2f\n" +
" 95%% CI: [%.2f, %.2f]\n" +
" Min: %.2f\n" +
" Max: %.2f\n" +
" Samples: %d",
metricName, mean, stdDev, ci[0], ci[1], min, max, values.size()
);
}
}

View File

@@ -0,0 +1,436 @@
package sd.config;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import com.google.gson.Gson;
/**
* Responsável pelo carregamento, validação e acesso centralizado às configurações da simulação.
* <p>
* Esta classe atua como uma fachada (Facade) para os parâmetros do sistema, abstraindo a origem
* dos dados (ficheiros {@code .properties} ou JSON). Implementa uma estratégia robusta de
* carregamento de recursos, suportando tanto caminhos absolutos do sistema de ficheiros quanto
* recursos embutidos no <i>classpath</i>.
* <p>
* Além de propriedades chave-valor simples, gerencia a desserialização da topologia da rede
* através da classe interna {@link NetworkConfig}.
*/
public class SimulationConfig {
/** Armazenamento em memória das propriedades chave-valor carregadas. */
private final Properties properties;
/** Estrutura hierárquica da configuração da rede carregada via JSON. */
private NetworkConfig networkConfig;
/**
* Objeto de transferência de dados (DTO) que representa a configuração global da rede.
* Mapeado a partir do ficheiro {@code network_config.json}.
*/
public static class NetworkConfig {
private List<IntersectionConfig> intersections;
public List<IntersectionConfig> getIntersections() {
return intersections;
}
}
/**
* DTO que representa a configuração de uma única interseção na topologia.
*/
public static class IntersectionConfig {
private String id;
private List<String> lights;
private Map<String, String> routes;
/** @return O identificador único da interseção (ex: "Cr1"). */
public String getId() {
return id;
}
/** @return Lista de identificadores dos semáforos associados a esta interseção. */
public List<String> getLights() {
return lights;
}
/** @return Mapa de roteamento definindo destinos alcançáveis e seus próximos saltos. */
public Map<String, String> getRoutes() {
return routes;
}
}
/**
* Inicializa o gestor de configuração carregando propriedades do caminho especificado.
* * <p>Implementa uma estratégia de carregamento em cascata (fallback) para garantir robustez
* em diferentes ambientes de execução (IDE, JAR, Docker):
* <ol>
* <li><b>Sistema de Ficheiros Direto:</b> Tenta carregar do caminho absoluto ou relativo.</li>
* <li><b>Classpath (Contexto):</b> Tenta carregar via {@code Thread.currentThread().getContextClassLoader()},
* normalizando prefixos como "src/main/resources" ou "classpath:".</li>
* <li><b>Classpath (Classe):</b> Tenta carregar via {@code SimulationConfig.class.getResourceAsStream},
* útil para recursos na raiz do JAR.</li>
* </ol>
*
* @param filePath O caminho ou nome do recurso do ficheiro {@code .properties}.
* @throws IOException Se o ficheiro não puder ser localizado em nenhuma das estratégias,
* com uma mensagem detalhada das tentativas falhadas.
*/
public SimulationConfig(String filePath) throws IOException {
properties = new Properties();
// List to track all attempted paths for better error reporting
List<String> attemptedPaths = new ArrayList<>();
IOException fileSystemException = null;
// Strategy 1: Try to load directly from file system
try (InputStream input = new FileInputStream(filePath)) {
properties.load(input);
loadNetworkConfig();
return; // Successfully loaded from file system
} catch (IOException e) {
fileSystemException = e;
attemptedPaths.add("File system: " + filePath);
}
// Strategy 2: Try to load from classpath with path normalization
String resourcePath = filePath;
// Remove common src/main/resources prefixes
resourcePath = resourcePath.replace("src/main/resources/", "").replace("src\\main\\resources\\", "");
// Remove classpath: prefix if provided
if (resourcePath.startsWith("classpath:")) {
resourcePath = resourcePath.substring("classpath:".length());
if (resourcePath.startsWith("/")) {
resourcePath = resourcePath.substring(1);
}
}
// Try loading from classpath using thread context class loader
InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath);
attemptedPaths.add("Classpath (context): " + resourcePath);
if (resourceStream == null) {
// Strategy 3: Try with leading slash
String slashPath = "/" + resourcePath;
resourceStream = SimulationConfig.class.getResourceAsStream(slashPath);
attemptedPaths.add("Classpath (class): " + slashPath);
}
if (resourceStream != null) {
try (InputStream input = resourceStream) {
properties.load(input);
loadNetworkConfig();
return; // Successfully loaded from classpath
} catch (IOException e) {
// Failed to read from classpath resource
throw new IOException(
String.format("Failed to read properties from classpath resource '%s': %s",
resourcePath, e.getMessage()),
e);
}
}
// All strategies failed - provide comprehensive error message
StringBuilder errorMsg = new StringBuilder();
errorMsg.append("Configuration file '").append(filePath).append("' could not be found.\n");
errorMsg.append("Attempted locations:\n");
for (String path : attemptedPaths) {
errorMsg.append(" - ").append(path).append("\n");
}
if (fileSystemException != null) {
errorMsg.append("\nOriginal error: ").append(fileSystemException.getMessage());
}
throw new IOException(errorMsg.toString(), fileSystemException);
}
/**
* Carrega a configuração da topologia de rede a partir do ficheiro "network_config.json".
* <p>
* Utiliza a biblioteca Gson para desserialização. Em caso de falha, emite um aviso para o
* {@code System.err} mas não aborta a execução, permitindo o uso de defaults ou redes vazias.
*/
private void loadNetworkConfig() {
try (InputStream is = getClass().getClassLoader().getResourceAsStream("network_config.json")) {
if (is == null) {
System.err.println("Warning: network_config.json not found in classpath. Using defaults/empty.");
return;
}
try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
Gson gson = new Gson();
this.networkConfig = gson.fromJson(reader, NetworkConfig.class);
}
} catch (IOException e) {
System.err.println("Failed to load network_config.json: " + e.getMessage());
e.printStackTrace();
}
}
/**
* Retorna a configuração estruturada da rede.
* @return Objeto {@link NetworkConfig} ou null se o carregamento falhou.
*/
public NetworkConfig getNetworkConfig() {
return networkConfig;
}
// --- Network configurations ---
/**
* Obtém o endereço de host (nome DNS ou IP) para uma interseção específica.
* * @param intersectionId O ID da interseção (ex: "Cr1").
* @return O host configurado ou "localhost" por omissão.
*/
public String getIntersectionHost(String intersectionId) {
return properties.getProperty("intersection." + intersectionId + ".host", "localhost");
}
/**
* Obtém a porta de escuta TCP para uma interseção específica.
* * @param intersectionId O ID da interseção (ex: "Cr1").
* @return O número da porta. Retorna 0 se não configurado.
*/
public int getIntersectionPort(String intersectionId) {
return Integer.parseInt(properties.getProperty("intersection." + intersectionId + ".port", "0"));
}
/**
* Obtém o endereço de host do servidor de Dashboard (monitorização).
* @return O host do dashboard (padrão: "localhost").
*/
public String getDashboardHost() {
return properties.getProperty("dashboard.host", "localhost");
}
/**
* Obtém a porta de conexão do servidor de Dashboard.
* @return A porta do dashboard (padrão: 9000).
*/
public int getDashboardPort() {
return Integer.parseInt(properties.getProperty("dashboard.port", "9000"));
}
/**
* Obtém o endereço de host do nó de saída (Exit Node), para onde os veículos são encaminhados ao sair da malha.
* @return O host do nó de saída (padrão: "localhost").
*/
public String getExitHost() {
return properties.getProperty("exit.host", "localhost");
}
/**
* Obtém a porta de conexão do nó de saída.
* @return A porta do nó de saída (padrão: 9001).
*/
public int getExitPort() {
return Integer.parseInt(properties.getProperty("exit.port", "9001"));
}
// --- Simulation configurations ---
/**
* Define a duração total da execução da simulação em segundos virtuais.
* @return A duração em segundos (padrão: 3600).
*/
public double getSimulationDuration() {
return Double.parseDouble(properties.getProperty("simulation.duration", "3600"));
}
/**
* Obtém o fator de escala temporal para visualização/execução.
* <ul>
* <li>0.0: Execução instantânea (DES puro, velocidade máxima).</li>
* <li>1.0: Tempo real (1 segundo simulado = 1 segundo real).</li>
* <li>0.01: Acelerado 100x.</li>
* </ul>
* @return O fator de escala.
*/
public double getTimeScale() {
return Double.parseDouble(properties.getProperty("simulation.time.scale", "0"));
}
/**
* Obtém o tempo de "drenagem" (drain time) em segundos virtuais.
* <p>
* Este é o período adicional executado após o fim da geração de veículos para permitir
* que os veículos restantes no sistema completem os seus percursos.
* @return O tempo de drenagem (padrão: 60.0s).
*/
public double getDrainTime() {
return Double.parseDouble(properties.getProperty("simulation.drain.time", "60.0"));
}
/**
* Determina o modelo estocástico utilizado para a chegada de veículos.
* @return "POISSON" (distribuição exponencial) ou "FIXED" (intervalo determinístico).
*/
public String getArrivalModel() {
return properties.getProperty("simulation.arrival.model", "POISSON");
}
/**
* Obtém a taxa média de chegada (lambda) para o modelo Poisson.
* @return Veículos por segundo (padrão: 0.5).
*/
public double getArrivalRate() {
return Double.parseDouble(properties.getProperty("simulation.arrival.rate", "0.5"));
}
/**
* Obtém o intervalo fixo entre chegadas para o modelo determinístico.
* @return O intervalo em segundos (padrão: 2.0).
*/
public double getFixedArrivalInterval() {
return Double.parseDouble(properties.getProperty("simulation.arrival.fixed.interval", "2.0"));
}
/**
* Obtém a política de roteamento utilizada pelos veículos para navegar na malha.
* @return A política: "RANDOM", "SHORTEST_PATH" ou "LEAST_CONGESTED".
*/
public String getRoutingPolicy() {
return properties.getProperty("simulation.routing.policy", "RANDOM");
}
// --- Traffic light configurations ---
/**
* Obtém a duração do estado VERDE para um semáforo específico.
* * @param intersectionId ID da interseção.
* @param direction Direção do fluxo (ex: "North").
* @return Duração em segundos (padrão: 30.0).
*/
public double getTrafficLightGreenTime(String intersectionId, String direction) {
String key = "trafficlight." + intersectionId + "." + direction + ".green";
return Double.parseDouble(properties.getProperty(key, "30.0"));
}
/**
* Obtém a duração do estado VERMELHO para um semáforo específico.
* * @param intersectionId ID da interseção.
* @param direction Direção do fluxo.
* @return Duração em segundos (padrão: 30.0).
*/
public double getTrafficLightRedTime(String intersectionId, String direction) {
String key = "trafficlight." + intersectionId + "." + direction + ".red";
return Double.parseDouble(properties.getProperty(key, "30.0"));
}
// --- Vehicle configurations ---
/**
* Probabilidade (0.0 a 1.0) de geração de um veículo do tipo LIGEIRO (LIGHT).
* @return Probabilidade (padrão: 0.7).
*/
public double getLightVehicleProbability() {
return Double.parseDouble(properties.getProperty("vehicle.probability.light", "0.7"));
}
/**
* Tempo médio necessário para um veículo LIGEIRO atravessar uma interseção.
* @return Tempo em segundos (padrão: 2.0).
*/
public double getLightVehicleCrossingTime() {
return Double.parseDouble(properties.getProperty("vehicle.crossing.time.light", "2.0"));
}
/**
* Probabilidade (0.0 a 1.0) de geração de um veículo do tipo BICICLETA (BIKE).
* @return Probabilidade (padrão: 0.0).
*/
public double getBikeVehicleProbability() {
return Double.parseDouble(properties.getProperty("vehicle.probability.bike", "0.0"));
}
/**
* Tempo médio necessário para uma BICICLETA atravessar uma interseção.
* @return Tempo em segundos (padrão: 1.5).
*/
public double getBikeVehicleCrossingTime() {
return Double.parseDouble(properties.getProperty("vehicle.crossing.time.bike", "1.5"));
}
/**
* Probabilidade (0.0 a 1.0) de geração de um veículo PESADO (HEAVY).
* @return Probabilidade (padrão: 0.0).
*/
public double getHeavyVehicleProbability() {
return Double.parseDouble(properties.getProperty("vehicle.probability.heavy", "0.0"));
}
/**
* Tempo médio necessário para um veículo PESADO atravessar uma interseção.
* @return Tempo em segundos (padrão: 4.0).
*/
public double getHeavyVehicleCrossingTime() {
return Double.parseDouble(properties.getProperty("vehicle.crossing.time.heavy", "4.0"));
}
/**
* Define o tempo base de viagem entre interseções para veículos padrão.
* @return Tempo em segundos (padrão: 8.0).
*/
public double getBaseTravelTime() {
return Double.parseDouble(properties.getProperty("vehicle.travel.time.base", "8.0"));
}
/**
* Multiplicador de tempo de viagem para bicicletas.
* <p>Tempo efetivo = Base * Multiplicador.
* @return Fator multiplicativo (padrão: 0.5).
*/
public double getBikeTravelTimeMultiplier() {
return Double.parseDouble(properties.getProperty("vehicle.travel.time.bike.multiplier", "0.5"));
}
/**
* Multiplicador de tempo de viagem para veículos pesados.
* <p>Tempo efetivo = Base * Multiplicador.
* @return Fator multiplicativo (padrão: 4.0).
*/
public double getHeavyTravelTimeMultiplier() {
return Double.parseDouble(properties.getProperty("vehicle.travel.time.heavy.multiplier", "4.0"));
}
// --- Statistics ---
/**
* Intervalo de tempo (em segundos virtuais) para agregação e envio de estatísticas periódicas.
* @return Intervalo de atualização (padrão: 1.0).
*/
public double getStatisticsUpdateInterval() {
return Double.parseDouble(properties.getProperty("statistics.update.interval", "1.0"));
}
// --- Generic getters ---
/**
* Recupera uma propriedade genérica como String, com valor padrão de segurança.
* * @param key A chave da propriedade.
* @param defaultValue O valor a retornar caso a chave não exista.
* @return O valor da propriedade ou o default.
*/
public String getProperty(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
/**
* Recupera uma propriedade genérica como String.
* * @param key A chave da propriedade.
* @return O valor da propriedade ou null se não encontrada.
*/
public String getProperty(String key) {
return properties.getProperty(key);
}
}

View File

@@ -0,0 +1,578 @@
package sd.coordinator;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import sd.config.SimulationConfig;
import sd.dashboard.DashboardStatistics;
import sd.dashboard.StatsUpdatePayload;
import sd.des.DESEventType;
import sd.des.EventQueue;
import sd.des.SimulationClock;
import sd.des.SimulationEvent;
import sd.logging.EventLogger;
import sd.model.Message;
import sd.model.MessageType;
import sd.model.Vehicle;
import sd.routing.LeastCongestedRouteSelector;
import sd.routing.RandomRouteSelector;
import sd.routing.RouteSelector;
import sd.routing.RoutingPolicy;
import sd.routing.ShortestPathRouteSelector;
import sd.serialization.SerializationException;
import sd.util.VehicleGenerator;
/**
* Coordenador central da arquitetura de simulação distribuída.
* <p>
* Este processo atua como o "cérebro" da simulação, sendo responsável por:
* <ol>
* <li><b>Orquestração DES:</b> Gerir o relógio global ({@link SimulationClock}) e a fila de eventos prioritária.</li>
* <li><b>Geração de Carga:</b> Injetar veículos na malha viária seguindo distribuições estocásticas (Poisson) ou determinísticas.</li>
* <li><b>Encaminhamento Dinâmico:</b> Decidir as rotas dos veículos com base na política ativa (Random, Shortest Path, Least Congested).</li>
* <li><b>Sincronização:</b> Garantir que todos os nós (Interseções e Dashboard) operem em uníssono.</li>
* </ol>
*/
public class CoordinatorProcess {
private final SimulationConfig config;
private final VehicleGenerator vehicleGenerator;
/** Mapa de clientes TCP persistentes para cada interseção (Worker Nodes). */
private final Map<String, SocketClient> intersectionClients;
private SocketClient dashboardClient;
// Componentes DES (Discrete Event Simulation)
private final SimulationClock clock;
private final EventQueue eventQueue;
private final EventLogger eventLogger;
// Estado da simulação
private int vehicleCounter;
private boolean running;
private double timeScale;
private RouteSelector currentRouteSelector;
/** Referência para estatísticas do dashboard para polling de mudanças de política. */
private DashboardStatistics dashboardStatistics;
/**
* Monitorização local (aproximada) dos tamanhos de fila nas interseções.
* <p>
* Utilizado exclusivamente pela política {@link LeastCongestedRouteSelector}.
* O coordenador incrementa este contador ao enviar um veículo para uma interseção.
* Nota: Esta é uma visão "borda" (edge) e pode não refletir a saída em tempo real
* dos veículos, mas serve como heurística suficiente para balanceamento de carga.
*/
private final Map<String, Integer> intersectionQueueSizes;
/**
* Ponto de entrada do processo Coordenador.
* Carrega configurações, estabelece conexões TCP e inicia o loop de eventos.
*/
public static void main(String[] args) {
System.out.println("=".repeat(60));
System.out.println("COORDINATOR PROCESS - DISTRIBUTED TRAFFIC SIMULATION");
System.out.println("=".repeat(60));
try {
// 1. Load configuration
String configFile = args.length > 0 ? args[0] : "src/main/resources/simulation.properties";
System.out.println("Loading configuration from: " + configFile);
SimulationConfig config = new SimulationConfig(configFile);
CoordinatorProcess coordinator = new CoordinatorProcess(config);
// 2. Connect to intersection processes
System.out.println("\n" + "=".repeat(60));
coordinator.initialize();
// 3. Run the sim
System.out.println("\n" + "=".repeat(60));
coordinator.run();
} catch (IOException e) {
System.err.println("Failed to load configuration: " + e.getMessage());
System.exit(1);
} catch (Exception e) {
System.err.println("Coordinator error: " + e.getMessage());
System.exit(1);
}
}
/**
* Inicializa o coordenador com a configuração fornecida.
* Configura o motor DES, logging e o seletor de rotas inicial.
*
* @param config Objeto de configuração carregado.
*/
public CoordinatorProcess(SimulationConfig config) {
this.config = config;
// Inicializa o RouteSelector baseado na política configurada
this.currentRouteSelector = createRouteSelector(config.getRoutingPolicy());
this.vehicleGenerator = new VehicleGenerator(config, currentRouteSelector);
this.intersectionClients = new HashMap<>();
this.vehicleCounter = 0;
this.running = false;
this.timeScale = config.getTimeScale();
this.intersectionQueueSizes = new HashMap<>();
this.clock = new SimulationClock();
this.eventQueue = new EventQueue(true);
this.eventLogger = EventLogger.getInstance();
eventLogger.log(sd.logging.EventType.PROCESS_STARTED, "Coordinator",
"Coordinator process initialized with DES architecture");
System.out.println("Coordinator initialized with configuration:");
System.out.println(" - Simulation duration: " + config.getSimulationDuration() + "s");
System.out.println(" - Arrival model: " + config.getArrivalModel());
System.out.println(" - Arrival rate: " + config.getArrivalRate() + " vehicles/s");
System.out.println(" - Routing policy: " + config.getRoutingPolicy());
System.out.println(" - DES Mode: ENABLED (Event-driven, no time-stepping)");
}
/**
* Fábrica de {@link RouteSelector} baseada no nome da política.
* * @param policyName Nome da política (RANDOM, SHORTEST_PATH, LEAST_CONGESTED).
* @return Uma instância da estratégia de roteamento.
*/
private RouteSelector createRouteSelector(String policyName) {
try {
RoutingPolicy policy = RoutingPolicy.valueOf(policyName.toUpperCase());
switch (policy) {
case RANDOM:
System.out.println(" - Using RANDOM routing (baseline with probabilities)");
return new RandomRouteSelector();
case SHORTEST_PATH:
System.out.println(" - Using SHORTEST_PATH routing (minimize intersections)");
return new ShortestPathRouteSelector();
case LEAST_CONGESTED:
System.out.println(" - Using LEAST_CONGESTED routing (dynamic, avoids queues)");
return new LeastCongestedRouteSelector();
default:
System.err.println(" ! Unknown routing policy: " + policyName + ", defaulting to RANDOM");
return new RandomRouteSelector();
}
} catch (IllegalArgumentException e) {
System.err.println(" ! Invalid routing policy: " + policyName + ", defaulting to RANDOM");
return new RandomRouteSelector();
}
}
/**
* Estabelece conexões TCP com o Dashboard e todas as Interseções (Worker Nodes).
* Essencial para o envio de comandos de controle e injeção de veículos.
*/
public void initialize() {
// Connect to dashboard first
connectToDashboard();
System.out.println("Connecting to intersection processes...");
String[] intersectionIds = { "Cr1", "Cr2", "Cr3", "Cr4", "Cr5" };
for (String intersectionId : intersectionIds) {
try {
String host = config.getIntersectionHost(intersectionId);
int port = config.getIntersectionPort(intersectionId);
SocketClient client = new SocketClient(intersectionId, host, port);
client.connect();
intersectionClients.put(intersectionId, client);
} catch (IOException e) {
System.err.println("Failed to connect to " + intersectionId + ": " + e.getMessage());
}
}
System.out.println("Successfully connected to " + intersectionClients.size() + " intersection(s)");
if (intersectionClients.isEmpty()) {
System.err.println("WARNING: No intersections connected. Simulation cannot proceed.");
}
}
/**
* Loop principal da simulação (DES Engine).
* <p>
* Executa a sequência:
* 1. Retira o próximo evento da fila prioritária.
* 2. Avança o relógio virtual para o timestamp do evento.
* 3. Aplica escala temporal (Time Scale) para visualização, se necessário.
* 4. Processa o evento.
*/
public void run() {
double duration = config.getSimulationDuration();
double drainTime = config.getDrainTime();
double totalDuration = duration + drainTime;
running = true;
System.out.println("Starting DES-based vehicle generation simulation...");
System.out.println("Duration: " + duration + "s (+ " + drainTime + "s drain)");
System.out.println();
// Log simulation start
eventLogger.log(sd.logging.EventType.SIMULATION_STARTED, "Coordinator",
String.format("Starting simulation - Duration: %.1fs", duration));
// Send simulation start time to all processes for synchronization
sendSimulationStartTime();
// Schedule first vehicle generation event
double firstArrivalTime = vehicleGenerator.getNextArrivalTime(clock.getCurrentTime());
eventQueue.schedule(new SimulationEvent(
firstArrivalTime,
DESEventType.VEHICLE_GENERATION,
null,
"Coordinator"));
// Schedule simulation end event
eventQueue.schedule(new SimulationEvent(
totalDuration,
DESEventType.SIMULATION_END,
null,
"Coordinator"));
System.out.printf("Initial event scheduled at t=%.3fs\n", firstArrivalTime);
System.out.println("Entering DES event loop...\n");
// Main DES loop - process events in chronological order
double lastTime = 0.0;
while (running && !eventQueue.isEmpty()) {
SimulationEvent event = eventQueue.poll();
// Apply time scaling for visualization
if (timeScale > 0) {
double simTimeDelta = event.getTimestamp() - lastTime;
long realDelayMs = (long) (simTimeDelta * timeScale * 1000);
if (realDelayMs > 0) {
try {
Thread.sleep(realDelayMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
lastTime = event.getTimestamp();
}
// Advance simulation time to event time
clock.advanceTo(event.getTimestamp());
// Process the event
processEvent(event, duration);
}
System.out.println();
System.out.printf("Simulation complete at t=%.2fs\n", clock.getCurrentTime());
System.out.println("Total vehicles generated: " + vehicleCounter);
System.out.println("Total events processed: " + eventQueue.getProcessedCount());
// Log simulation end
eventLogger.log(sd.logging.EventType.SIMULATION_STOPPED, "Coordinator",
String.format("Simulation ended - Vehicles: %d, Events: %d",
vehicleCounter, eventQueue.getProcessedCount()));
// Export event history (spec requirement: view complete event list)
exportEventHistory();
shutdown();
}
/**
* Trata o processamento de um evento DES retirado da fila.
* * @param event O evento a ser processado.
* @param generationDuration Duração da fase de geração ativa (antes do 'drain time').
*/
private void processEvent(SimulationEvent event, double generationDuration) {
double currentTime = clock.getCurrentTime();
switch (event.getType()) {
case VEHICLE_GENERATION:
// Only generate if we're still in the generation phase
if (currentTime < generationDuration) {
// Check for routing policy changes from dashboard
checkForPolicyChanges();
generateAndSendVehicle();
// Schedule next vehicle generation (Recursive scheduling)
double nextArrivalTime = vehicleGenerator.getNextArrivalTime(currentTime);
eventQueue.schedule(new SimulationEvent(
nextArrivalTime,
DESEventType.VEHICLE_GENERATION,
null,
"Coordinator"));
} else if (currentTime == generationDuration) {
System.out.printf("\n[t=%.2f] Generation phase complete. Entering DRAIN MODE...\n",
currentTime);
}
break;
case SIMULATION_END:
System.out.printf("[t=%.2f] Simulation end event reached\n", currentTime);
running = false;
break;
default:
System.err.println("WARNING: Unknown event type: " + event.getType());
}
}
/**
* Exporta o log completo de eventos DES para auditoria e debug.
* Caminho: {@code logs/coordinator-event-history.txt}.
*/
private void exportEventHistory() {
try (java.io.PrintWriter writer = new java.io.PrintWriter(
new java.io.FileWriter("logs/coordinator-event-history.txt"))) {
String history = eventQueue.exportEventHistory();
writer.println(history);
System.out.println("\nEvent history exported to: logs/coordinator-event-history.txt");
} catch (IOException e) {
System.err.println("Failed to export event history: " + e.getMessage());
}
}
/**
* Gera um novo veículo e envia-o via TCP para a interseção de entrada apropriada.
* Também atualiza o rastreio local de filas para balanceamento de carga.
*/
private void generateAndSendVehicle() {
double currentTime = clock.getCurrentTime();
// Usa os tamanhos de fila rastreados localmente para política LEAST_CONGESTED
// Isto permite roteamento dinâmico baseado no estado atual da rede
Vehicle vehicle = vehicleGenerator.generateVehicle("V" + (++vehicleCounter), currentTime, intersectionQueueSizes);
System.out.printf("[t=%.2f] Vehicle %s generated (type=%s, route=%s)%n",
currentTime, vehicle.getId(), vehicle.getType(), vehicle.getRoute());
// Log to event logger
eventLogger.log(sd.logging.EventType.VEHICLE_GENERATED, "Coordinator",
String.format("[%s] Type: %s, Route: %s", vehicle.getId(), vehicle.getType(), vehicle.getRoute()));
// Update local queue size tracking (increment first intersection's queue)
String firstIntersection = vehicle.getRoute().get(0);
intersectionQueueSizes.put(firstIntersection,
intersectionQueueSizes.getOrDefault(firstIntersection, 0) + 1);
// Send generation count to dashboard
sendGenerationStatsToDashboard();
if (vehicle.getRoute().isEmpty()) {
System.err.println("ERROR: Vehicle " + vehicle.getId() + " has empty route!");
return;
}
String entryIntersection = vehicle.getRoute().get(0);
sendVehicleToIntersection(vehicle, entryIntersection);
}
/**
* Serializa e transmite o objeto Veículo para o nó (interseção) de destino.
*/
private void sendVehicleToIntersection(Vehicle vehicle, String intersectionId) {
SocketClient client = intersectionClients.get(intersectionId);
if (client == null || !client.isConnected()) {
System.err.println("ERROR: No connection to " + intersectionId + " for vehicle " + vehicle.getId());
return;
}
try {
Message message = new Message(
MessageType.VEHICLE_SPAWN,
"COORDINATOR",
intersectionId,
vehicle);
client.send(message);
System.out.printf("->Sent to %s%n", intersectionId);
} catch (SerializationException | IOException e) {
System.err.println("ERROR: Failed to send vehicle " + vehicle.getId() + " to " + intersectionId);
System.err.println("Reason: " + e.getMessage());
}
}
/**
* Encerra graciosamente a simulação, enviando sinais de SHUTDOWN para todos os nós.
*/
public void shutdown() {
System.out.println();
System.out.println("=".repeat(60));
System.out.println("Shutting down coordinator...");
for (Map.Entry<String, SocketClient> entry : intersectionClients.entrySet()) {
String intersectionId = entry.getKey();
SocketClient client = entry.getValue();
try {
if (client.isConnected()) {
Message personalizedShutdown = new Message(
MessageType.SHUTDOWN,
"COORDINATOR",
intersectionId,
"Simulation complete");
client.send(personalizedShutdown);
System.out.println("Sent shutdown message to " + intersectionId);
}
} catch (SerializationException | IOException e) {
System.err.println("Error sending shutdown to " + intersectionId + ": " + e.getMessage());
} finally {
client.close();
}
}
System.out.println("Coordinator shutdown complete");
System.out.println("=".repeat(60));
}
public void stop() {
System.out.println("\nStop signal received...");
running = false;
}
/**
* Altera dinamicamente a política de roteamento durante a simulação (Hot-swap).
* Thread-safe.
* * @param policyName nome da nova política (RANDOM, SHORTEST_PATH, LEAST_CONGESTED)
*/
public synchronized void changeRoutingPolicy(String policyName) {
System.out.println("\n" + "=".repeat(60));
System.out.println("ROUTING POLICY CHANGE REQUEST");
System.out.println("=".repeat(60));
System.out.println("Current policy: " + getCurrentPolicyName());
System.out.println("Requested policy: " + policyName);
RouteSelector newSelector = createRouteSelector(policyName);
this.currentRouteSelector = newSelector;
this.vehicleGenerator.setRouteSelector(newSelector);
System.out.println("Routing policy successfully changed to: " + policyName);
System.out.println(" - New vehicles will use the updated policy");
System.out.println("=".repeat(60) + "\n");
eventLogger.log(sd.logging.EventType.CONFIG_CHANGED, "Coordinator",
"Routing policy changed to: " + policyName);
}
/**
* Retorna o nome da política de roteamento atual.
*/
private String getCurrentPolicyName() {
if (currentRouteSelector instanceof RandomRouteSelector) {
return "RANDOM";
} else if (currentRouteSelector instanceof ShortestPathRouteSelector) {
return "SHORTEST_PATH";
} else if (currentRouteSelector instanceof LeastCongestedRouteSelector) {
return "LEAST_CONGESTED";
}
return "UNKNOWN";
}
/**
* Verifica se há solicitação de mudança de política proveniente do dashboard
* e aplica a alteração se houver.
*/
private void checkForPolicyChanges() {
if (dashboardStatistics != null) {
String requestedPolicy = dashboardStatistics.getAndClearRequestedRoutingPolicy();
if (requestedPolicy != null && !requestedPolicy.isEmpty()) {
changeRoutingPolicy(requestedPolicy);
}
}
}
/**
* Injeta a referência para as estatísticas do dashboard.
* Permite que o coordenador consuma intenções de mudança de política do utilizador.
*/
public void setDashboardStatistics(DashboardStatistics stats) {
this.dashboardStatistics = stats;
}
private void connectToDashboard() {
try {
String host = config.getDashboardHost();
int port = config.getDashboardPort();
System.out.println("Connecting to dashboard at " + host + ":" + port);
dashboardClient = new SocketClient("Dashboard", host, port);
dashboardClient.connect();
System.out.println("Successfully connected to dashboard\n");
} catch (IOException e) {
System.err.println("WARNING: Failed to connect to dashboard: " + e.getMessage());
System.err.println("Coordinator will continue without dashboard connection\n");
}
}
private void sendGenerationStatsToDashboard() {
if (dashboardClient == null || !dashboardClient.isConnected()) {
return;
}
try {
// Create stats payload with vehicle generation count
StatsUpdatePayload payload = new StatsUpdatePayload();
payload.setTotalVehiclesGenerated(vehicleCounter);
Message message = new Message(
MessageType.STATS_UPDATE,
"COORDINATOR",
"Dashboard",
payload);
dashboardClient.send(message);
} catch (Exception e) { // This is fine - can add IOException if need be
// Don't crash if dashboard update fails
System.err.println("Failed to send stats to dashboard: " + e.getMessage());
}
}
/**
* Sincronização Global: Envia o timestamp de início (System.currentTimeMillis)
* para todos os componentes distribuídos, garantindo uma base de tempo comum
* para métricas de latência.
*/
private void sendSimulationStartTime() {
long startTimeMillis = System.currentTimeMillis();
// Send to all intersections
for (Map.Entry<String, SocketClient> entry : intersectionClients.entrySet()) {
try {
Message message = new Message(
MessageType.SIMULATION_START,
"COORDINATOR",
entry.getKey(),
startTimeMillis);
entry.getValue().send(message);
} catch (Exception e) { // Same thing here
System.err.println("Failed to send start time to " + entry.getKey() + ": " + e.getMessage());
}
}
// Send to dashboard
if (dashboardClient != null && dashboardClient.isConnected()) {
try {
Message message = new Message(
MessageType.SIMULATION_START,
"COORDINATOR",
"Dashboard",
startTimeMillis);
dashboardClient.send(message);
} catch (Exception e) { // And here
// Don't crash
}
}
}
}

View File

@@ -0,0 +1,140 @@
package sd.coordinator;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import sd.model.Message;
import sd.serialization.MessageSerializer;
import sd.serialization.SerializationException;
import sd.serialization.SerializerFactory;
/**
* Abstração de cliente TCP para comunicação outbound (de saída) com nós da rede.
* <p>
* Esta classe encapsula a gestão do socket raw, oferecendo uma interface de alto nível
* para envio de objetos {@link Message}. Implementa o protocolo de camada de aplicação
* proprietário, garantindo a serialização correta e o enquadramento (framing) dos dados
* na stream TCP.
* <p>
* É utilizada pelo Coordenador para controlar Interseções e enviar telemetria para o Dashboard.
*/
public class SocketClient {
private final String intersectionId;
private final String host;
private final int port;
private Socket socket;
private OutputStream outputStream;
private MessageSerializer serializer;
/**
* Instancia um novo cliente socket configurado para um destino específico.
*
* @param intersectionId Identificador lógico do nó de destino (ex: "Cr1", "Dashboard").
* @param host Endereço IP ou hostname do destino.
* @param port Porta TCP de escuta do destino.
*/
public SocketClient(String intersectionId, String host, int port) {
this.intersectionId = intersectionId;
this.host = host;
this.port = port;
this.serializer = SerializerFactory.createDefault();
}
/**
* Estabelece a conexão TCP (Handshake SYN/ACK) com o host remoto.
* * @throws IOException Se o host for inalcançável ou a conexão for recusada.
*/
public void connect() throws IOException {
try {
socket = new Socket(host, port);
outputStream = socket.getOutputStream();
System.out.println("Connected to " + intersectionId + " at " + host + ":" + port);
} catch (IOException e) {
System.err.println("Failed to connect to " + intersectionId + " at " + host + ":" + port);
throw e;
}
}
/**
* Serializa e transmite uma mensagem através do socket conectado.
* <p>
* <b>Protocolo de Envio (Length-Prefix Framing):</b>
* <ol>
* <li>Serializa o objeto {@link Message} para um array de bytes.</li>
* <li>Calcula o tamanho (N) do array.</li>
* <li>Escreve um cabeçalho de 4 bytes contendo N (Big-Endian).</li>
* <li>Escreve os N bytes do payload (corpo da mensagem).</li>
* <li>Realiza flush no stream para forçar o envio imediato do pacote TCP.</li>
* </ol>
* Este mecanismo garante que o recetor saiba exatamente quantos bytes ler,
* prevenindo problemas de fragmentação ou aglutinação de pacotes TCP.
*
* @param message O objeto de domínio a ser enviado.
* @throws SerializationException Se o objeto não puder ser convertido para bytes.
* @throws IOException Se houver falha na escrita do socket (ex: conexão resetada).
*/
public void send(Message message) throws SerializationException, IOException {
if (socket == null || socket.isClosed()) {
throw new IOException("Socket is not connected to " + intersectionId);
}
try {
byte[] data = serializer.serialize(message);
int length = data.length;
// Write 4-byte length header (Big Endian)
outputStream.write((length >> 24) & 0xFF);
outputStream.write((length >> 16) & 0xFF);
outputStream.write((length >> 8) & 0xFF);
outputStream.write(length & 0xFF);
// Write payload
outputStream.write(data);
outputStream.flush();
} catch (SerializationException | IOException e) {
System.err.println("Error sending message to " + intersectionId + ": " + e.getMessage());
throw e;
}
}
/**
* Realiza o encerramento gracioso (graceful shutdown) da conexão.
* Liberta os recursos do sistema operativo (descritores de arquivo).
* <p>
* Operação idempotente: pode ser chamada múltiplas vezes sem erro.
*/
public void close() {
try {
if (outputStream != null) {
outputStream.close();
}
if (socket != null && !socket.isClosed()) {
socket.close();
System.out.println("Closed connection to " + intersectionId);
}
} catch (IOException e) {
System.err.println("Error closing connection to " + intersectionId + ": " + e.getMessage());
}
}
/**
* Verifica o estado atual da conexão.
* * @return true se o socket estiver instanciado, conectado e aberto; false caso contrário.
*/
public boolean isConnected() {
return socket != null && socket.isConnected() && !socket.isClosed();
}
public String getIntersectionId() {
return intersectionId;
}
@Override
public String toString() {
return String.format("SocketClient[intersection=%s, host=%s, port=%d, connected=%s]",
intersectionId, host, port, isConnected());
}
}

View File

@@ -0,0 +1,576 @@
package sd.dashboard;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import sd.analysis.MultiRunAnalyzer;
import sd.analysis.SimulationRunResult;
import sd.model.VehicleType;
/**
* Diálogo para configuração e execução de análise de desempenho em lote (Batch Processing).
* <p>
* Esta classe fornece uma interface gráfica para automatizar múltiplas execuções da simulação
* sob diferentes cenários de carga. É responsável por:
* <ol>
* <li>Orquestrar o ciclo de vida dos processos de simulação (start/stop/wait).</li>
* <li>Coletar métricas estatísticas de cada execução.</li>
* <li>Agregar resultados usando o {@link MultiRunAnalyzer}.</li>
* <li>Gerar relatórios consolidados para análise de variância e intervalos de confiança.</li>
* </ol>
* A execução ocorre numa thread separada (background) para manter a responsividade da UI.
*/
public class BatchAnalysisDialog {
private Stage dialog;
private ProgressBar progressBar;
private Label statusLabel;
private Label progressLabel;
private TextArea logArea;
private Button startButton;
private Button closeButton;
// Flags de controlo de concorrência
private volatile boolean isRunning = false;
private volatile boolean shouldStop = false;
/** Referência partilhada para capturar estatísticas em tempo real do Dashboard. */
private DashboardStatistics sharedStatistics;
/**
* Exibe o diálogo de análise em lote.
* * @param owner A janela pai (Stage) para modalidade.
* @param statistics Objeto partilhado de estatísticas para coleta de dados.
*/
public static void show(Stage owner, DashboardStatistics statistics) {
BatchAnalysisDialog dialog = new BatchAnalysisDialog();
dialog.sharedStatistics = statistics;
dialog.createAndShow(owner);
}
/**
* Constrói e inicializa a interface gráfica do diálogo.
*/
private void createAndShow(Stage owner) {
dialog = new Stage();
dialog.initOwner(owner);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.setTitle("Batch Performance Analysis");
VBox root = new VBox(20);
root.setPadding(new Insets(20));
root.setAlignment(Pos.TOP_CENTER);
// Estilo Dark Mode conforme guidelines visuais
root.setStyle("-fx-background-color: #2b2b2b;");
// Header
Label title = new Label("Batch Performance Evaluation");
title.setStyle("-fx-font-size: 18px; -fx-font-weight: bold; -fx-text-fill: white;");
Label subtitle = new Label("Executar múltiplas simulações para gerar análise estatística consolidada");
subtitle.setStyle("-fx-font-size: 12px; -fx-text-fill: #cccccc;");
subtitle.setWrapText(true);
// Painéis de Componentes
VBox configPanel = createConfigPanel();
VBox progressPanel = createProgressPanel();
VBox logPanel = createLogPanel();
HBox buttonBox = createButtonBox();
root.getChildren().addAll(title, subtitle, configPanel, progressPanel, logPanel, buttonBox);
Scene scene = new Scene(root, 700, 600);
dialog.setScene(scene);
// Tratamento de fecho da janela: interromper thread de worker se ativa
dialog.setOnCloseRequest(e -> {
if (isRunning) {
e.consume(); // Previne fecho imediato
shouldStop = true;
log("A parar após conclusão da execução atual...");
}
});
dialog.show();
}
private VBox createConfigPanel() {
VBox panel = new VBox(15);
panel.setPadding(new Insets(15));
panel.setStyle("-fx-background-color: rgba(255, 255, 255, 0.05); -fx-background-radius: 5;");
Label header = new Label("Configuração");
header.setStyle("-fx-font-size: 14px; -fx-font-weight: bold; -fx-text-fill: white;");
// Runs per scenario
HBox runsBox = new HBox(10);
runsBox.setAlignment(Pos.CENTER_LEFT);
Label runsLabel = new Label("Execuções por cenário:");
runsLabel.setStyle("-fx-text-fill: white; -fx-min-width: 150px;");
Spinner<Integer> runsSpinner = new Spinner<>(1, 20, 5, 1);
runsSpinner.setEditable(true);
runsSpinner.setPrefWidth(80);
runsSpinner.setId("runsSpinner");
runsBox.getChildren().addAll(runsLabel, runsSpinner);
// Scenario selection
Label scenarioHeader = new Label("Selecionar Cenários:");
scenarioHeader.setStyle("-fx-text-fill: white; -fx-font-size: 12px; -fx-font-weight: bold;");
CheckBox lowCheck = new CheckBox("Carga Baixa (λ=0.2 v/s)");
lowCheck.setSelected(true);
lowCheck.setId("lowCheck");
lowCheck.setStyle("-fx-text-fill: white;");
CheckBox mediumCheck = new CheckBox("Carga Média (λ=0.5 v/s)");
mediumCheck.setSelected(true);
mediumCheck.setId("mediumCheck");
mediumCheck.setStyle("-fx-text-fill: white;");
CheckBox highCheck = new CheckBox("Carga Alta (λ=1.0 v/s)");
highCheck.setSelected(true);
highCheck.setId("highCheck");
highCheck.setStyle("-fx-text-fill: white;");
// Run duration
HBox durationBox = new HBox(10);
durationBox.setAlignment(Pos.CENTER_LEFT);
Label durationLabel = new Label("Duração (segundos):");
durationLabel.setStyle("-fx-text-fill: white; -fx-min-width: 150px;");
Spinner<Integer> durationSpinner = new Spinner<>(30, 3600, 120, 30);
durationSpinner.setEditable(true);
durationSpinner.setPrefWidth(80);
durationSpinner.setId("durationSpinner");
Label durationInfo = new Label("(tempo simulado - duração real depende do time.scale)");
durationInfo.setStyle("-fx-text-fill: #999999; -fx-font-size: 10px;");
durationBox.getChildren().addAll(durationLabel, durationSpinner, durationInfo);
panel.getChildren().addAll(header, runsBox, scenarioHeader, lowCheck, mediumCheck, highCheck, durationBox);
return panel;
}
private VBox createProgressPanel() {
VBox panel = new VBox(10);
panel.setPadding(new Insets(15));
panel.setStyle("-fx-background-color: rgba(255, 255, 255, 0.05); -fx-background-radius: 5;");
statusLabel = new Label("Pronto para iniciar");
statusLabel.setStyle("-fx-text-fill: white; -fx-font-weight: bold;");
progressBar = new ProgressBar(0);
progressBar.setPrefWidth(Double.MAX_VALUE);
progressBar.setPrefHeight(25);
progressLabel = new Label("0 / 0 execuções concluídas");
progressLabel.setStyle("-fx-text-fill: #cccccc; -fx-font-size: 11px;");
panel.getChildren().addAll(statusLabel, progressBar, progressLabel);
return panel;
}
private VBox createLogPanel() {
VBox panel = new VBox(5);
Label logHeader = new Label("Log de Atividade:");
logHeader.setStyle("-fx-text-fill: white; -fx-font-size: 12px; -fx-font-weight: bold;");
logArea = new TextArea();
logArea.setEditable(false);
logArea.setPrefRowCount(10);
logArea.setWrapText(true);
// Estilo de terminal para o log
logArea.setStyle("-fx-control-inner-background: #1e1e1e; -fx-text-fill: #00ff00; -fx-font-family: 'Courier New';");
VBox.setVgrow(logArea, Priority.ALWAYS);
panel.getChildren().addAll(logHeader, logArea);
return panel;
}
private HBox createButtonBox() {
HBox box = new HBox(15);
box.setAlignment(Pos.CENTER);
box.setPadding(new Insets(10, 0, 0, 0));
startButton = new Button("INICIAR BATCH");
startButton.setStyle("-fx-background-color: #28a745; -fx-text-fill: white; -fx-font-weight: bold; -fx-padding: 10 20;");
startButton.setOnAction(e -> startBatchAnalysis());
Button stopButton = new Button("PARAR");
stopButton.setStyle("-fx-background-color: #dc3545; -fx-text-fill: white; -fx-font-weight: bold; -fx-padding: 10 20;");
stopButton.setOnAction(e -> {
shouldStop = true;
log("Paragem solicitada...");
});
closeButton = new Button("FECHAR");
closeButton.setStyle("-fx-background-color: #6c757d; -fx-text-fill: white; -fx-font-weight: bold; -fx-padding: 10 20;");
closeButton.setOnAction(e -> dialog.close());
box.getChildren().addAll(startButton, stopButton, closeButton);
return box;
}
/**
* Valida configurações e inicia a thread de execução em batch.
*/
private void startBatchAnalysis() {
if (isRunning) return;
// Get configuration
Spinner<Integer> runsSpinner = (Spinner<Integer>) dialog.getScene().lookup("#runsSpinner");
Spinner<Integer> durationSpinner = (Spinner<Integer>) dialog.getScene().lookup("#durationSpinner");
CheckBox lowCheck = (CheckBox) dialog.getScene().lookup("#lowCheck");
CheckBox mediumCheck = (CheckBox) dialog.getScene().lookup("#mediumCheck");
CheckBox highCheck = (CheckBox) dialog.getScene().lookup("#highCheck");
int runsPerScenario = runsSpinner.getValue();
int duration = durationSpinner.getValue();
// Validate selection
if (!lowCheck.isSelected() && !mediumCheck.isSelected() && !highCheck.isSelected()) {
log("ERRO: Selecione pelo menos um cenário!");
return;
}
// Disable controls para evitar alterações durante execução
startButton.setDisable(true);
runsSpinner.setDisable(true);
durationSpinner.setDisable(true);
lowCheck.setDisable(true);
mediumCheck.setDisable(true);
highCheck.setDisable(true);
isRunning = true;
shouldStop = false;
// Executar em thread daemon para não bloquear a UI JavaFX
Thread analysisThread = new Thread(() -> {
try {
runBatchAnalysis(lowCheck.isSelected(), mediumCheck.isSelected(),
highCheck.isSelected(), runsPerScenario, duration);
} finally {
// Restaurar estado da UI no final
Platform.runLater(() -> {
startButton.setDisable(false);
runsSpinner.setDisable(false);
durationSpinner.setDisable(false);
lowCheck.setDisable(false);
mediumCheck.setDisable(false);
highCheck.setDisable(false);
isRunning = false;
});
}
});
analysisThread.setDaemon(true);
analysisThread.start();
}
/**
* Lógica principal de orquestração do batch.
* Itera sobre cenários e execuções, chamando a simulação e o analisador.
*/
private void runBatchAnalysis(boolean low, boolean medium, boolean high, int runsPerScenario, int durationSeconds) {
log("===========================================================");
log("INICIANDO ANÁLISE DE DESEMPENHO EM LOTE");
log("===========================================================");
log("Configuração:");
log(" • Execuções por cenário: " + runsPerScenario);
log(" • Duração por execução: " + durationSeconds + " segundos");
log(" • Cenários: " + (low ? "LOW " : "") + (medium ? "MEDIUM " : "") + (high ? "HIGH" : ""));
log("");
String[] scenarios = new String[]{
low ? "simulation-low.properties" : null,
medium ? "simulation-medium.properties" : null,
high ? "simulation-high.properties" : null
};
String[] scenarioNames = {"LOW LOAD", "MEDIUM LOAD", "HIGH LOAD"};
int totalRuns = 0;
for (String scenario : scenarios) {
if (scenario != null) totalRuns += runsPerScenario;
}
int currentRun = 0;
for (int i = 0; i < scenarios.length; i++) {
if (scenarios[i] == null) continue;
if (shouldStop) {
log("Batch analysis interrompida pelo utilizador");
updateStatus("Parado", currentRun, totalRuns);
return;
}
String configFile = scenarios[i];
String scenarioName = scenarioNames[i];
log("");
log("---------------------------------------------------------");
log("CENÁRIO: " + scenarioName + " (" + configFile + ")");
log("---------------------------------------------------------");
MultiRunAnalyzer analyzer = new MultiRunAnalyzer(configFile);
for (int run = 1; run <= runsPerScenario; run++) {
if (shouldStop) {
log("Batch analysis interrompida pelo utilizador");
updateStatus("Parado", currentRun, totalRuns);
savePartialReport(analyzer, scenarioName);
return;
}
currentRun++;
log("");
log("Execução " + run + "/" + runsPerScenario + " a iniciar...");
updateStatus("A correr " + scenarioName + " - Execução " + run + "/" + runsPerScenario,
currentRun - 1, totalRuns);
// Executa uma simulação completa e bloqueia até terminar
SimulationRunResult result = runSingleSimulation(configFile, run, durationSeconds);
if (result != null) {
analyzer.addResult(result);
log("Execução " + run + " completa - Gerados: " + result.getTotalVehiclesGenerated() +
" | Completados: " + result.getTotalVehiclesCompleted() +
" | Tempo Médio: " + String.format("%.2f", result.getAverageSystemTime()) + "s");
} else {
log("Execução " + run + " falhou!");
}
updateProgress(currentRun, totalRuns);
}
// Gera e guarda o relatório final deste cenário
saveScenarioReport(analyzer, scenarioName);
}
log("");
log("============================================================");
log("BATCH ANALYSIS COMPLETA!");
log("===========================================================");
log("Relatórios guardados em: analysis/");
log("");
updateStatus("Concluído!", totalRuns, totalRuns);
updateProgress(1.0);
}
/**
* Instancia os processos de simulação, monitoriza o estado e recolhe resultados.
*/
private SimulationRunResult runSingleSimulation(String configFile, int runNumber, int durationSeconds) {
SimulationProcessManager processManager = new SimulationProcessManager();
SimulationRunResult result = new SimulationRunResult(runNumber, configFile);
try {
// Start simulation
processManager.setConfigFile(configFile);
processManager.startSimulation();
// Tempo para processos arrancarem e estabelecerem conexões TCP
Thread.sleep(3000);
log(" Simulação em curso (duração config: " + durationSeconds + "s tempo simulado)...");
log(" A aguardar processo Coordenador completar...");
// Loop de polling para verificar se o Coordenador terminou
// Isso lida automaticamente com diferentes time scales (DES)
int checkInterval = 2; // Check every 2 seconds
int elapsed = 0;
int maxWaitSeconds = durationSeconds + 120; // Timeout de segurança
while (elapsed < maxWaitSeconds) {
if (shouldStop) {
processManager.stopSimulation();
return null;
}
// Check if simulation completed
if (!processManager.isSimulationRunning()) {
log(" Simulação terminou após " + elapsed + "s");
break;
}
Thread.sleep(checkInterval * 1000L);
elapsed += checkInterval;
// Atualização periódica do log
if (elapsed % 10 == 0 && elapsed < 60) {
log(" " + elapsed + "s decorridos...");
}
}
if (elapsed >= maxWaitSeconds) {
log(" Timeout atingido, forçando paragem...");
}
// Stop and collect results
log(" A terminar processos...");
processManager.stopSimulation();
Thread.sleep(2000); // Tempo para flushing de buffers
// Recolha de estatísticas (Prioridade: Dados reais do socket)
if (sharedStatistics != null) {
collectRealStatistics(result, sharedStatistics);
} else {
collectSimulatedStatistics(result, configFile, durationSeconds);
}
return result;
} catch (InterruptedException e) {
log("Interrompido: " + e.getMessage());
Thread.currentThread().interrupt();
stopSimulation(processManager);
return null;
} catch (IOException e) {
log("Erro IO: " + e.getMessage());
stopSimulation(processManager);
return null;
} catch (RuntimeException e) {
log("Erro Runtime: " + e.getMessage());
stopSimulation(processManager);
return null;
}
}
private void stopSimulation(SimulationProcessManager processManager) {
try {
processManager.stopSimulation();
} catch (Exception ex) {
// Ignora erros de cleanup
}
}
/**
* Popula o objeto de resultado com dados reais capturados pelo Dashboard.
*/
private void collectRealStatistics(SimulationRunResult result, DashboardStatistics stats) {
result.setTotalVehiclesGenerated(stats.getTotalVehiclesGenerated());
result.setTotalVehiclesCompleted(stats.getTotalVehiclesCompleted());
result.setAverageSystemTime(stats.getAverageSystemTime() / 1000.0); // Converte ms para segundos
result.setAverageWaitingTime(stats.getAverageWaitingTime() / 1000.0);
// Estimação de extremos (o DashboardStatistics deve ser expandido para guardar exatos se necessário)
result.setMinSystemTime(stats.getAverageSystemTime() / 1000.0 * 0.5);
result.setMaxSystemTime(stats.getAverageSystemTime() / 1000.0 * 2.0);
// Estatísticas por tipo
for (VehicleType type : VehicleType.values()) {
int count = stats.getVehicleTypeCount(type);
double waitTime = stats.getAverageWaitingTimeByType(type) / 1000.0;
result.setVehicleCountByType(type, count);
result.setAvgWaitTimeByType(type, waitTime);
}
// Estatísticas por interseção
for (var entry : stats.getAllIntersectionStats().entrySet()) {
String intersectionId = entry.getKey();
DashboardStatistics.IntersectionStats iStats = entry.getValue();
result.setVehiclesProcessed(intersectionId, iStats.getTotalDepartures());
result.setMaxQueueSize(intersectionId, iStats.getCurrentQueueSize());
result.setAvgQueueSize(intersectionId, (double) iStats.getCurrentQueueSize());
}
}
/**
* Gera dados simulados (mock) caso o dashboard não esteja conectado.
* Útil para testes de interface.
*/
private void collectSimulatedStatistics(SimulationRunResult result, String configFile, int durationSeconds) {
// Mock data based on load profile
int baseGenerated = durationSeconds / 3;
double loadFactor = configFile.contains("low") ? 0.2 :
configFile.contains("medium") ? 0.5 : 1.0;
int generated = (int)(baseGenerated * loadFactor * 3);
int completed = (int)(generated * (0.85 + Math.random() * 0.1)); // 85-95% completion rate
double baseSystemTime = 40.0;
double congestionFactor = configFile.contains("low") ? 1.0 :
configFile.contains("medium") ? 1.5 : 2.5;
result.setTotalVehiclesGenerated(generated);
result.setTotalVehiclesCompleted(completed);
result.setAverageSystemTime(baseSystemTime * congestionFactor + Math.random() * 10);
result.setMinSystemTime(20.0 + Math.random() * 5);
result.setMaxSystemTime(baseSystemTime * congestionFactor * 2 + Math.random() * 20);
result.setAverageWaitingTime(10.0 * congestionFactor + Math.random() * 5);
log(" Nota: A usar estatísticas simuladas (conexão real necessária)");
}
private void saveScenarioReport(MultiRunAnalyzer analyzer, String scenarioName) {
try {
File analysisDir = new File("analysis");
if (!analysisDir.exists()) {
analysisDir.mkdirs();
}
String timestamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
String reportFile = "analysis/" + scenarioName.replace(" ", "_") + "_" + timestamp + ".txt";
String csvFile = "analysis/" + scenarioName.replace(" ", "_") + "_" + timestamp + ".csv";
analyzer.saveReport(reportFile);
analyzer.saveCSV(csvFile);
log("Relatório guardado: " + reportFile);
log("CSV guardado: " + csvFile);
} catch (IOException e) {
log("Falha ao guardar relatório: " + e.getMessage());
}
}
private void savePartialReport(MultiRunAnalyzer analyzer, String scenarioName) {
if (analyzer.getRunCount() > 0) {
log("A guardar resultados parciais...");
saveScenarioReport(analyzer, scenarioName + "_PARTIAL");
}
}
// --- Helpers de UI Thread-Safe ---
private void log(String message) {
Platform.runLater(() -> {
logArea.appendText(message + "\n");
logArea.setScrollTop(Double.MAX_VALUE);
});
}
private void updateStatus(String status, int current, int total) {
Platform.runLater(() -> {
statusLabel.setText(status);
progressLabel.setText(current + " / " + total + " execuções completas");
});
}
private void updateProgress(int current, int total) {
Platform.runLater(() -> {
progressBar.setProgress((double) current / total);
});
}
private void updateProgress(double progress) {
Platform.runLater(() -> {
progressBar.setProgress(progress);
});
}
}

View File

@@ -0,0 +1,183 @@
package sd.dashboard;
import javafx.geometry.Insets;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* Componente de interface gráfica (GUI) responsável pela parametrização "fine-tuning" da simulação.
* <p>
* Esta classe apresenta um diálogo modal que permite ao operador sobrepor (override)
* as configurações estáticas carregadas do ficheiro {@code .properties} imediatamente
* antes da execução. Oferece controlo granular sobre:
* <ul>
* <li><b>Geração de Carga:</b> Alternância entre modelos estocásticos (Poisson) e determinísticos.</li>
* <li><b>Temporização:</b> Ajuste da escala de tempo (Time Scale) para visualização vs. performance pura.</li>
* <li><b>Mix de Veículos:</b> Definição das probabilidades de geração por tipo de agente.</li>
* </ul>
*/
public class ConfigurationDialog {
/**
* Exibe o diálogo de configuração avançada e captura as intenções do utilizador.
* <p>
* A interface é construída dinamicamente usando layouts JavaFX ({@link GridPane}, {@link VBox}).
* Inclui lógica de validação reativa (ex: desabilitar campos de intervalo fixo quando
* o modelo Poisson está selecionado).
* *
[Image of Poisson distribution graph]
*
* @param owner A janela pai (Stage) para bloquear a interação até o fecho do diálogo (Modalidade).
* @return {@code true} se o utilizador confirmou as alterações (OK), {@code false} se cancelou.
*/
public static boolean showAdvancedConfig(Stage owner) {
Dialog<ButtonType> dialog = new Dialog<>();
dialog.initOwner(owner);
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.setTitle("Configuração Avançada da Simulação");
dialog.setHeaderText("Ajustar parâmetros da simulação");
// Criar painel de configuração
VBox content = new VBox(15);
content.setPadding(new Insets(20));
// Seção 1: Parâmetros de Chegada
Label arrivalHeader = new Label("Parâmetros de Chegada de Veículos");
arrivalHeader.setStyle("-fx-font-weight: bold; -fx-font-size: 14px;");
GridPane arrivalGrid = new GridPane();
arrivalGrid.setHgap(10);
arrivalGrid.setVgap(10);
arrivalGrid.setPadding(new Insets(10));
// Modelo de chegada
Label modelLabel = new Label("Modelo de chegada:");
ComboBox<String> modelCombo = new ComboBox<>();
modelCombo.getItems().addAll("POISSON", "FIXED");
modelCombo.setValue("POISSON");
arrivalGrid.add(modelLabel, 0, 0);
arrivalGrid.add(modelCombo, 1, 0);
// Taxa de chegada (λ)
Label rateLabel = new Label("Taxa de chegada (λ) [veículos/s]:");
Spinner<Double> rateSpinner = new Spinner<>(0.1, 2.0, 0.5, 0.1);
rateSpinner.setEditable(true);
rateSpinner.setPrefWidth(100);
arrivalGrid.add(rateLabel, 0, 1);
arrivalGrid.add(rateSpinner, 1, 1);
// Intervalo fixo (se aplicável)
Label intervalLabel = new Label("Intervalo fixo [s]:");
Spinner<Double> intervalSpinner = new Spinner<>(0.5, 10.0, 2.0, 0.5);
intervalSpinner.setEditable(true);
intervalSpinner.setPrefWidth(100);
intervalSpinner.setDisable(true);
arrivalGrid.add(intervalLabel, 0, 2);
arrivalGrid.add(intervalSpinner, 1, 2);
// Habilitar/desabilitar intervalo baseado no modelo
modelCombo.setOnAction(e -> {
boolean isFixed = "FIXED".equals(modelCombo.getValue());
intervalSpinner.setDisable(!isFixed);
rateSpinner.setDisable(isFixed);
});
// Seção 2: Parâmetros de Tempo
Label timeHeader = new Label("Parâmetros de Tempo");
timeHeader.setStyle("-fx-font-weight: bold; -fx-font-size: 14px;");
GridPane timeGrid = new GridPane();
timeGrid.setHgap(10);
timeGrid.setVgap(10);
timeGrid.setPadding(new Insets(10));
// Duração da simulação
Label durationLabel = new Label("Duração da simulação [s]:");
Spinner<Integer> durationSpinner = new Spinner<>(60, 7200, 300, 60);
durationSpinner.setEditable(true);
durationSpinner.setPrefWidth(100);
timeGrid.add(durationLabel, 0, 0);
timeGrid.add(durationSpinner, 1, 0);
// Escala temporal (para visualização)
Label scaleLabel = new Label("Escala temporal (0=instantâneo, 1=tempo real):");
Spinner<Double> scaleSpinner = new Spinner<>(0.0, 1.0, 0.01, 0.01);
scaleSpinner.setEditable(true);
scaleSpinner.setPrefWidth(100);
timeGrid.add(scaleLabel, 0, 1);
timeGrid.add(scaleSpinner, 1, 1);
// Tempo de drenagem
Label drainLabel = new Label("Tempo de drenagem [s]:");
Spinner<Integer> drainSpinner = new Spinner<>(0, 300, 60, 10);
drainSpinner.setEditable(true);
drainSpinner.setPrefWidth(100);
timeGrid.add(drainLabel, 0, 2);
timeGrid.add(drainSpinner, 1, 2);
// Seção 3: Distribuição de Tipos de Veículos
Label vehicleHeader = new Label("Distribuição de Tipos de Veículos");
vehicleHeader.setStyle("-fx-font-weight: bold; -fx-font-size: 14px;");
GridPane vehicleGrid = new GridPane();
vehicleGrid.setHgap(10);
vehicleGrid.setVgap(10);
vehicleGrid.setPadding(new Insets(10));
Label bikeLabel = new Label("Bicicletas/Motos [%]:");
Spinner<Integer> bikeSpinner = new Spinner<>(0, 100, 10, 5);
bikeSpinner.setEditable(true);
bikeSpinner.setPrefWidth(100);
vehicleGrid.add(bikeLabel, 0, 0);
vehicleGrid.add(bikeSpinner, 1, 0);
Label lightLabel = new Label("Veículos Ligeiros [%]:");
Spinner<Integer> lightSpinner = new Spinner<>(0, 100, 70, 5);
lightSpinner.setEditable(true);
lightSpinner.setPrefWidth(100);
vehicleGrid.add(lightLabel, 0, 1);
vehicleGrid.add(lightSpinner, 1, 1);
Label heavyLabel = new Label("Veículos Pesados [%]:");
Spinner<Integer> heavySpinner = new Spinner<>(0, 100, 20, 5);
heavySpinner.setEditable(true);
heavySpinner.setPrefWidth(100);
vehicleGrid.add(heavyLabel, 0, 2);
vehicleGrid.add(heavySpinner, 1, 2);
// Nota informativa
Label noteLabel = new Label("Nota: Estes parâmetros sobrepõem os valores do ficheiro .properties selecionado.\n" +
"Para usar os valores padrão do ficheiro, deixe em branco ou cancele.");
noteLabel.setWrapText(true);
noteLabel.setStyle("-fx-font-size: 11px; -fx-text-fill: #666666;");
// Adicionar tudo ao conteúdo
content.getChildren().addAll(
arrivalHeader, arrivalGrid,
new Separator(),
timeHeader, timeGrid,
new Separator(),
vehicleHeader, vehicleGrid,
new Separator(),
noteLabel
);
dialog.getDialogPane().setContent(content);
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
// Mostrar diálogo e processar resultado
return dialog.showAndWait()
.map(buttonType -> buttonType == ButtonType.OK)
.orElse(false);
}
}

View File

@@ -0,0 +1,180 @@
package sd.dashboard;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;
import sd.model.MessageType;
import sd.protocol.MessageProtocol;
import sd.protocol.SocketConnection;
/**
* Worker responsável pelo processamento dedicado de uma conexão de cliente TCP no Dashboard.
* <p>
* Esta classe implementa o padrão <i>Thread-per-Client</i>. Cada instância executa numa
* thread separada, garantindo que a latência de rede ou o processamento de mensagens
* de um nó (Interseção/Coordenador) não bloqueie a receção de telemetria dos outros.
* <p>
* As suas principais funções são:
* <ol>
* <li>Manter a conexão persistente com o nó remoto.</li>
* <li>Desserializar mensagens de protocolo recebidas.</li>
* <li>Normalizar payloads JSON (resolvendo ambiguidades de tipagem do Gson).</li>
* <li>Atualizar o objeto partilhado {@link DashboardStatistics} de forma thread-safe.</li>
* </ol>
*/
public class DashboardClientHandler implements Runnable {
private final Socket clientSocket;
private final DashboardStatistics statistics;
/**
* Inicializa o handler com o socket ativo e a referência para o agregador de estatísticas.
*
* @param clientSocket O socket TCP conectado ao nó remoto.
* @param statistics O objeto singleton partilhado onde as métricas serão agregadas.
*/
public DashboardClientHandler(Socket clientSocket, DashboardStatistics statistics) {
this.clientSocket = clientSocket;
this.statistics = statistics;
}
/**
* Ciclo de vida da conexão.
* <p>
* Estabelece o wrapper {@link SocketConnection}, entra num loop de leitura bloqueante
* e gere exceções de I/O. Garante o fecho limpo do socket em caso de desconexão ou erro.
*/
@Override
public void run() {
String clientInfo = clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort();
try (SocketConnection connection = new SocketConnection(clientSocket)) {
System.out.println("[Handler] Started handling client: " + clientInfo);
while (!Thread.currentThread().isInterrupted()) {
try {
MessageProtocol message = connection.receiveMessage();
if (message == null) {
System.out.println("[Handler] Client disconnected: " + clientInfo);
break;
}
processMessage(message);
} catch (ClassNotFoundException e) {
System.err.println("[Handler] Unknown message class from " + clientInfo + ": " + e.getMessage());
} catch (IOException e) {
System.out.println("[Handler] Connection error with " + clientInfo + ": " + e.getMessage());
break;
}
}
} catch (IOException e) {
System.err.println("[Handler] Error initializing connection with " + clientInfo + ": " + e.getMessage());
} finally {
try {
if (!clientSocket.isClosed()) {
clientSocket.close();
}
} catch (IOException e) {
System.err.println("[Handler] Error closing socket for " + clientInfo + ": " + e.getMessage());
}
}
}
/**
* Valida e extrai os dados estatísticos da mensagem.
* <p>
* Implementa uma lógica de correção de tipagem para payloads desserializados via Gson.
* Frequentemente, objetos genéricos são desserializados como {@code LinkedHashMap} em vez
* da classe alvo {@link StatsUpdatePayload}. Este método deteta essa situação e realiza
* uma conversão "round-trip" (Map -> JSON -> Object) para garantir a integridade dos dados.
*
* @param message A mensagem recebida da rede.
*/
private void processMessage(MessageProtocol message) {
if (message.getType() != MessageType.STATS_UPDATE) {
System.out.println("[Handler] Ignoring non-statistics message type: " + message.getType());
return;
}
String senderId = message.getSourceNode();
Object payload = message.getPayload();
System.out.println("[Handler] Received STATS_UPDATE from: " + senderId);
// Handle both direct StatsUpdatePayload and Gson-deserialized Map
StatsUpdatePayload stats;
if (payload instanceof StatsUpdatePayload) {
stats = (StatsUpdatePayload) payload;
} else if (payload instanceof java.util.Map) {
// Gson deserialized as LinkedHashMap - re-serialize and deserialize properly
// This acts as a type-safety bridge for generic JSON payloads
com.google.gson.Gson gson = new com.google.gson.Gson();
String json = gson.toJson(payload);
stats = gson.fromJson(json, StatsUpdatePayload.class);
} else {
System.err.println("[Handler] Unknown payload type: " +
(payload != null ? payload.getClass().getName() : "null"));
return;
}
updateStatistics(senderId, stats);
}
/**
* Aplica os dados recebidos ao modelo global de estatísticas.
* <p>
* Distingue entre atualizações incrementais (ex: contagem de veículos) e
* substituições de estado (ex: tempo total de sistema reportado pelo nó de saída).
*
* @param senderId Identificador do nó que enviou a atualização (ex: "Cr1", "ExitNode").
* @param stats O objeto DTO contendo as métricas normalizadas.
*/
private void updateStatistics(String senderId, StatsUpdatePayload stats) {
if (stats.getTotalVehiclesGenerated() >= 0) {
statistics.updateVehiclesGenerated(stats.getTotalVehiclesGenerated());
}
if (stats.getTotalVehiclesCompleted() >= 0) {
statistics.updateVehiclesCompleted(stats.getTotalVehiclesCompleted());
}
// Exit Node sends cumulative totals, so we SET rather than ADD
if (stats.getTotalSystemTime() >= 0) {
statistics.setTotalSystemTime(stats.getTotalSystemTime());
}
if (stats.getTotalWaitingTime() >= 0) {
statistics.setTotalWaitingTime(stats.getTotalWaitingTime());
}
// Process vehicle type statistics (from Exit Node)
if (stats.getVehicleTypeCounts() != null && !stats.getVehicleTypeCounts().isEmpty()) {
Map<sd.model.VehicleType, Integer> counts = stats.getVehicleTypeCounts();
Map<sd.model.VehicleType, Long> waitTimes = stats.getVehicleTypeWaitTimes();
for (var entry : counts.entrySet()) {
sd.model.VehicleType type = entry.getKey();
int count = entry.getValue();
long waitTime = (waitTimes != null && waitTimes.containsKey(type))
? waitTimes.get(type) : 0L;
statistics.updateVehicleTypeStats(type, count, waitTime);
}
}
// Process intersection statistics (from Intersection processes)
if (senderId.startsWith("Cr") || senderId.startsWith("E")) {
statistics.updateIntersectionStats(
senderId,
stats.getIntersectionArrivals(),
stats.getIntersectionDepartures(),
stats.getIntersectionQueueSize()
);
}
System.out.println("[Handler] Successfully updated statistics from: " + senderId);
}
}

View File

@@ -0,0 +1,223 @@
package sd.dashboard;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import sd.config.SimulationConfig;
/**
* Servidor central de agregação de telemetria e estatísticas.
* <p>
* Este componente atua como o nó de monitorização do sistema distribuído.
* Implementa uma arquitetura de servidor concorrente utilizando um {@link ExecutorService}
* (Thread Pool) para gerir múltiplas conexões de entrada simultâneas provenientes
* das Interseções, Coordenador e Nó de Saída.
* <p>
* Suporta dois modos de operação:
* <ul>
* <li><b>Headless (CLI):</b> Renderização periódica de métricas no terminal (stdout).</li>
* <li><b>GUI (JavaFX):</b> Delegação do controlo para a interface gráfica {@link DashboardUI}.</li>
* </ul>
*/
public class DashboardServer {
private final int port;
/** Armazenamento em memória (Thread-safe) do estado global do sistema. */
private final DashboardStatistics statistics;
/** Pool de threads para isolamento de falhas e gestão de recursos de I/O. */
private final ExecutorService clientHandlerPool;
/** Flag atómica para controlo seguro do ciclo de vida do servidor. */
private final AtomicBoolean running;
private ServerSocket serverSocket;
/**
* Ponto de entrada (Bootstrap) da aplicação de monitorização.
* <p>
* Analisa os argumentos de linha de comando para determinar o modo de execução.
* Se a flag {@code --gui} ou {@code -g} estiver presente, inicia o subsistema JavaFX.
* Caso contrário, inicia o modo servidor de terminal padrão.
*
* @param args Argumentos de CLI (ex: caminho do config, flags de modo).
*/
public static void main(String[] args) {
// Check if GUI mode is requested
boolean useGUI = false;
String configFile = "src/main/resources/simulation.properties";
for (int i = 0; i < args.length; i++) {
if (args[i].equals("--gui") || args[i].equals("-g")) {
useGUI = true;
} else {
configFile = args[i];
}
}
if (useGUI) {
// Launch JavaFX UI
System.out.println("Launching Dashboard with JavaFX GUI...");
DashboardUI.main(args);
} else {
// Traditional terminal mode
System.out.println("=".repeat(60));
System.out.println("DASHBOARD SERVER - DISTRIBUTED TRAFFIC SIMULATION");
System.out.println("=".repeat(60));
try {
System.out.println("Loading configuration from: " + configFile);
SimulationConfig config = new SimulationConfig(configFile);
DashboardServer server = new DashboardServer(config);
// Start the server
System.out.println("\n" + "=".repeat(60));
server.start();
// Keep running until interrupted
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("\n\nShutdown signal received...");
server.stop();
}));
// Display statistics periodically
server.displayLoop();
} catch (IOException e) {
System.err.println("Failed to start Dashboard Server: " + e.getMessage());
System.exit(1);
}
}
}
/**
* Inicializa a infraestrutura do servidor.
*
* @param config A configuração carregada contendo a porta de escuta.
*/
public DashboardServer(SimulationConfig config) {
this.port = config.getDashboardPort();
this.statistics = new DashboardStatistics();
// Fixed pool limita o consumo de recursos, prevenindo exaustão sob carga alta
this.clientHandlerPool = Executors.newFixedThreadPool(10);
this.running = new AtomicBoolean(false);
}
/**
* Inicia a escuta por conexões (Bind & Listen) e a thread de despacho.
*
* @throws IOException Se a porta já estiver em uso ou ocorrer erro de bind.
*/
public void start() throws IOException {
if (running.get()) {
System.out.println("Dashboard Server is already running.");
return;
}
serverSocket = new ServerSocket(port);
running.set(true);
System.out.println("Dashboard Server started on port " + port);
System.out.println("Waiting for statistics updates from simulation processes...");
System.out.println("=".repeat(60));
Thread acceptThread = new Thread(this::acceptConnections, "DashboardServer-Accept");
acceptThread.setDaemon(false);
acceptThread.start();
}
/**
* Loop principal de aceitação de conexões (Dispatcher).
* <p>
* Bloqueia em {@code accept()} até que uma nova conexão chegue, delegando
* imediatamente o processamento para um {@link DashboardClientHandler} gerido
* pelo Thread Pool.
*/
private void acceptConnections() {
while (running.get()) {
try {
Socket clientSocket = serverSocket.accept();
System.out.println("[Connection] New client connected: " +
clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());
clientHandlerPool.execute(new DashboardClientHandler(clientSocket, statistics));
} catch (IOException e) {
if (running.get()) {
System.err.println("[Error] Failed to accept client connection: " + e.getMessage());
}
}
}
}
/**
* Ciclo de renderização de métricas para o modo CLI (Headless).
* Atualiza o ecrã a cada 5 segundos.
*/
@SuppressWarnings("BusyWait")
private void displayLoop() {
final long DISPLAY_INTERVAL_MS = 5000;
while (running.get()) {
try {
Thread.sleep(DISPLAY_INTERVAL_MS);
displayStatistics();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
/**
* Renderiza o snapshot atual das estatísticas no stdout.
*/
public void displayStatistics() {
System.out.println("\n" + "=".repeat(60));
System.out.println("REAL-TIME SIMULATION STATISTICS");
System.out.println("=".repeat(60));
statistics.display();
System.out.println("=".repeat(60));
}
/**
* Procedimento de encerramento gracioso (Graceful Shutdown).
* <p>
* 1. Altera flag de execução.
* 2. Fecha o socket do servidor para desbloquear a thread de aceitação.
* 3. Força o encerramento do pool de threads de clientes.
*/
public void stop() {
if (!running.get()) {
return;
}
System.out.println("\nStopping Dashboard Server...");
running.set(false);
try {
if (serverSocket != null && !serverSocket.isClosed()) {
serverSocket.close();
}
} catch (IOException e) {
System.err.println("Error closing server socket: " + e.getMessage());
}
clientHandlerPool.shutdownNow();
System.out.println("Dashboard Server stopped.");
}
public DashboardStatistics getStatistics() {
return statistics;
}
public boolean isRunning() {
return running.get();
}
}

View File

@@ -0,0 +1,301 @@
package sd.dashboard;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import sd.model.VehicleType;
/**
* Repositório central de estado da simulação, desenhado para acesso concorrente de alta frequência.
* <p>
* Esta classe atua como a "Single Source of Truth" para o Dashboard. Utiliza primitivas
* de concorrência do pacote {@code java.util.concurrent} (como {@link AtomicInteger} e
* {@link ConcurrentHashMap}) para permitir leituras e escritas simultâneas sem a necessidade
* de bloqueios explícitos (Lock-Free), minimizando a latência de processamento das mensagens
* recebidas dos múltiplos nós da rede.
*/
public class DashboardStatistics {
private final AtomicInteger totalVehiclesGenerated;
private final AtomicInteger totalVehiclesCompleted;
private final AtomicLong totalSystemTime;
private final AtomicLong totalWaitingTime;
/** Mapa thread-safe para armazenar métricas granulares por interseção. */
private final Map<String, IntersectionStats> intersectionStats;
private final Map<VehicleType, AtomicInteger> vehicleTypeCount;
private final Map<VehicleType, AtomicLong> vehicleTypeWaitTime;
/** Timestamp da última atualização de escrita, com garantia de visibilidade de memória (volatile). */
private volatile long lastUpdateTime;
/** Buffer para sinalização assíncrona de mudança de política (Dashboard -> Coordenador). */
private volatile String requestedRoutingPolicy;
/**
* Inicializa os contadores atómicos e as estruturas de dados concorrentes.
*/
public DashboardStatistics() {
this.totalVehiclesGenerated = new AtomicInteger(0);
this.totalVehiclesCompleted = new AtomicInteger(0);
this.totalSystemTime = new AtomicLong(0);
this.totalWaitingTime = new AtomicLong(0);
this.intersectionStats = new ConcurrentHashMap<>();
this.vehicleTypeCount = new ConcurrentHashMap<>();
this.vehicleTypeWaitTime = new ConcurrentHashMap<>();
for (VehicleType type : VehicleType.values()) {
vehicleTypeCount.put(type, new AtomicInteger(0));
vehicleTypeWaitTime.put(type, new AtomicLong(0));
}
this.lastUpdateTime = System.currentTimeMillis();
}
public void updateVehiclesGenerated(int count) {
totalVehiclesGenerated.set(count);
updateTimestamp();
}
public void incrementVehiclesGenerated() {
totalVehiclesGenerated.incrementAndGet();
updateTimestamp();
}
public void updateVehiclesCompleted(int count) {
totalVehiclesCompleted.set(count);
updateTimestamp();
}
public void incrementVehiclesCompleted() {
totalVehiclesCompleted.incrementAndGet();
updateTimestamp();
}
public void addSystemTime(long timeMs) {
totalSystemTime.addAndGet(timeMs);
updateTimestamp();
}
public void setTotalSystemTime(long timeMs) {
totalSystemTime.set(timeMs);
updateTimestamp();
}
public void addWaitingTime(long timeMs) {
totalWaitingTime.addAndGet(timeMs);
updateTimestamp();
}
public void setTotalWaitingTime(long timeMs) {
totalWaitingTime.set(timeMs);
updateTimestamp();
}
public void updateVehicleTypeStats(VehicleType type, int count, long waitTimeMs) {
vehicleTypeCount.get(type).set(count);
vehicleTypeWaitTime.get(type).set(waitTimeMs);
updateTimestamp();
}
public void incrementVehicleType(VehicleType type) {
vehicleTypeCount.get(type).incrementAndGet();
updateTimestamp();
}
/**
* Atualiza ou inicializa atomicamente as estatísticas de uma interseção específica.
* <p>
* Utiliza {@link Map#compute} para garantir que a criação do objeto {@link IntersectionStats}
* seja thread-safe sem necessidade de blocos synchronized externos.
*
* @param intersectionId ID da interseção.
* @param arrivals Total acumulado de chegadas.
* @param departures Total acumulado de partidas.
* @param currentQueueSize Tamanho instantâneo da fila.
*/
public void updateIntersectionStats(String intersectionId, int arrivals,
int departures, int currentQueueSize) {
intersectionStats.compute(intersectionId, (id, stats) -> {
if (stats == null) {
stats = new IntersectionStats(intersectionId);
}
stats.updateStats(arrivals, departures, currentQueueSize);
return stats;
});
updateTimestamp();
}
private void updateTimestamp() {
lastUpdateTime = System.currentTimeMillis();
}
// --- Getters e Métricas Calculadas ---
public int getTotalVehiclesGenerated() {
return totalVehiclesGenerated.get();
}
public int getTotalVehiclesCompleted() {
return totalVehiclesCompleted.get();
}
/**
* Calcula o tempo médio no sistema em tempo real.
* @return Média em milissegundos (0.0 se nenhum veículo completou).
*/
public double getAverageSystemTime() {
int completed = totalVehiclesCompleted.get();
if (completed == 0) return 0.0;
return (double) totalSystemTime.get() / completed;
}
/**
* Calcula o tempo médio de espera em tempo real.
* @return Média em milissegundos (0.0 se nenhum veículo completou).
*/
public double getAverageWaitingTime() {
int completed = totalVehiclesCompleted.get();
if (completed == 0) return 0.0;
return (double) totalWaitingTime.get() / completed;
}
public int getVehicleTypeCount(VehicleType type) {
return vehicleTypeCount.get(type).get();
}
public double getAverageWaitingTimeByType(VehicleType type) {
int count = vehicleTypeCount.get(type).get();
if (count == 0) return 0.0;
return (double) vehicleTypeWaitTime.get(type).get() / count;
}
public IntersectionStats getIntersectionStats(String intersectionId) {
return intersectionStats.get(intersectionId);
}
public Map<String, IntersectionStats> getAllIntersectionStats() {
return new HashMap<>(intersectionStats);
}
public long getLastUpdateTime() {
return lastUpdateTime;
}
/**
* Obtém um snapshot dos tamanhos atuais das filas de todas as interseções.
* <p>
* Utilizado primariamente pelo algoritmo de roteamento dinâmico (LEAST_CONGESTED)
* para tomar decisões de encaminhamento baseadas na carga atual da rede.
* * @return Mapa contendo {@code intersectionId -> queueSize}.
*/
public Map<String, Integer> getCurrentQueueSizes() {
Map<String, Integer> queueSizes = new HashMap<>();
for (Map.Entry<String, IntersectionStats> entry : intersectionStats.entrySet()) {
queueSizes.put(entry.getKey(), entry.getValue().getCurrentQueueSize());
}
return queueSizes;
}
/**
* Regista uma intenção de mudança de política de roteamento solicitada pela UI.
* O Coordenador fará polling deste valor periodicamente.
*/
public void setRequestedRoutingPolicy(String policy) {
this.requestedRoutingPolicy = policy;
}
/**
* Obtém e limpa atomicamente a política de roteamento solicitada.
* Implementa a semântica de consumo único (one-time consumption).
* * @return A política solicitada ou null se não houver mudança pendente.
*/
public synchronized String getAndClearRequestedRoutingPolicy() {
String policy = this.requestedRoutingPolicy;
this.requestedRoutingPolicy = null;
return policy;
}
/**
* Imprime um resumo formatado das estatísticas no stdout.
* Útil para o modo CLI (Headless).
*/
public void display() {
System.out.println("\n--- GLOBAL STATISTICS ---");
System.out.printf("Total Vehicles Generated: %d%n", getTotalVehiclesGenerated());
System.out.printf("Total Vehicles Completed: %d%n", getTotalVehiclesCompleted());
System.out.printf("Vehicles In Transit: %d%n",
getTotalVehiclesGenerated() - getTotalVehiclesCompleted());
System.out.printf("Average System Time: %.2f ms%n", getAverageSystemTime());
System.out.printf("Average Waiting Time: %.2f ms%n", getAverageWaitingTime());
System.out.println("\n--- VEHICLE TYPE STATISTICS ---");
for (VehicleType type : VehicleType.values()) {
int count = getVehicleTypeCount(type);
double avgWait = getAverageWaitingTimeByType(type);
System.out.printf("%s: %d vehicles, avg wait: %.2f ms%n",
type, count, avgWait);
}
System.out.println("\n--- INTERSECTION STATISTICS ---");
if (intersectionStats.isEmpty()) {
System.out.println("(No data received yet)");
} else {
for (IntersectionStats stats : intersectionStats.values()) {
stats.display();
}
}
System.out.printf("%nLast Update: %tT%n", lastUpdateTime);
}
/**
* Agregado de métricas específico para um nó de interseção.
* Mantém contadores atómicos para garantir consistência em atualizações concorrentes.
*/
public static class IntersectionStats {
private final String intersectionId;
private final AtomicInteger totalArrivals;
private final AtomicInteger totalDepartures;
private final AtomicInteger currentQueueSize;
public IntersectionStats(String intersectionId) {
this.intersectionId = intersectionId;
this.totalArrivals = new AtomicInteger(0);
this.totalDepartures = new AtomicInteger(0);
this.currentQueueSize = new AtomicInteger(0);
}
public void updateStats(int arrivals, int departures, int queueSize) {
this.totalArrivals.set(arrivals);
this.totalDepartures.set(departures);
this.currentQueueSize.set(queueSize);
}
public String getIntersectionId() {
return intersectionId;
}
public int getTotalArrivals() {
return totalArrivals.get();
}
public int getTotalDepartures() {
return totalDepartures.get();
}
public int getCurrentQueueSize() {
return currentQueueSize.get();
}
public void display() {
System.out.printf("%s: Arrivals=%d, Departures=%d, Queue=%d%n",
intersectionId, getTotalArrivals(), getTotalDepartures(), getCurrentQueueSize());
}
}
}

View File

@@ -0,0 +1,619 @@
package sd.dashboard;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import sd.config.SimulationConfig;
import sd.model.VehicleType;
/**
* Interface Gráfica (GUI) baseada em JavaFX para visualização de telemetria em tempo real.
* <p>
* Esta classe atua como a camada de apresentação (View) do sistema. Implementa o padrão
* <i>Observer</i> (via polling) para refletir o estado do modelo {@link DashboardStatistics}
* nos componentes visuais.
* <p>
* <b>Aspetos Técnicos Relevantes:</b>
* <ul>
* <li><b>Concorrência de UI:</b> Utiliza um {@link ScheduledExecutorService} para buscar dados
* em background e {@link Platform#runLater(Runnable)} para injetar atualizações na
* <i>JavaFX Application Thread</i>, evitando exceções de "Not on FX application thread".</li>
* <li><b>Data Binding:</b> Utiliza {@link TableView} com classes internas (DTOs) para
* renderização tabular eficiente de tipos de veículos e interseções.</li>
* <li><b>Controlo de Processos:</b> Integra com {@link SimulationProcessManager} para orquestrar
* o ciclo de vida (spawn/kill) dos processos externos da simulação.</li>
* </ul>
*/
public class DashboardUI extends Application {
private DashboardServer server;
private DashboardStatistics statistics;
// Global Statistics Labels
private Label lblVehiclesGenerated;
private Label lblVehiclesCompleted;
private Label lblVehiclesInTransit;
private Label lblAvgSystemTime;
private Label lblAvgWaitingTime;
private Label lblLastUpdate;
// Vehicle Type Table
private TableView<VehicleTypeRow> vehicleTypeTable;
// Intersection Table
private TableView<IntersectionRow> intersectionTable;
// Update scheduler (Background Thread)
private ScheduledExecutorService updateScheduler;
// Configuration controls
private ComboBox<String> configFileSelector;
private String selectedConfigFile = "simulation.properties";
private Label configInfoLabel;
/**
* Ponto de entrada da aplicação JavaFX.
* Configura o Stage primário, inicializa o servidor de backend e constrói a árvore de cena (Scene Graph).
*/
@Override
public void start(Stage primaryStage) {
try {
// Initialize server
String configFile = getParameters().getRaw().isEmpty()
? "src/main/resources/simulation.properties"
: getParameters().getRaw().get(0);
SimulationConfig config = new SimulationConfig(configFile);
server = new DashboardServer(config);
statistics = server.getStatistics();
// Start the dashboard server (Backend listening port)
server.start();
// Build UI Layout
BorderPane root = new BorderPane();
root.getStyleClass().add("root");
// Header (Top)
VBox header = createHeader();
root.setTop(header);
// Main content (Center)
VBox mainContent = createMainContent();
root.setCenter(mainContent);
// Footer (Bottom)
HBox footer = createFooter();
root.setBottom(footer);
// Create scene & apply CSS
Scene scene = new Scene(root, 1200, 850);
String cssUrl = getClass().getResource("/dashboard.css").toExternalForm();
scene.getStylesheets().add(cssUrl);
primaryStage.setTitle("Traffic Simulation Dashboard - Real-time Statistics");
primaryStage.setScene(scene);
primaryStage.show();
// Start periodic updates loop
startPeriodicUpdates();
// Handle window close (Graceful shutdown)
primaryStage.setOnCloseRequest(event -> {
shutdown();
});
} catch (Exception e) {
showErrorAlert("Failed to start Dashboard Server", e.getMessage());
e.printStackTrace();
Platform.exit();
}
}
private VBox createHeader() {
VBox header = new VBox(10);
header.getStyleClass().add("header");
header.setAlignment(Pos.CENTER);
Label title = new Label("DISTRIBUTED TRAFFIC SIMULATION DASHBOARD");
title.getStyleClass().add("header-title");
Label subtitle = new Label("Real-time Statistics and Monitoring");
subtitle.getStyleClass().add("header-subtitle");
// Configuration Panel
VBox configPanel = createConfigurationPanel();
// Control Buttons
HBox controls = new HBox(15);
controls.setAlignment(Pos.CENTER);
Button btnStart = new Button("START SIMULATION");
btnStart.getStyleClass().add("button-start");
Button btnStop = new Button("STOP SIMULATION");
btnStop.getStyleClass().add("button-stop");
btnStop.setDisable(true);
SimulationProcessManager processManager = new SimulationProcessManager();
btnStart.setOnAction(e -> {
try {
// Passar o ficheiro de configuração selecionado
processManager.setConfigFile(selectedConfigFile);
processManager.startSimulation();
// Toggle UI state
btnStart.setDisable(true);
btnStop.setDisable(false);
configFileSelector.setDisable(true); // Bloquear mudanças durante simulação
} catch (IOException ex) {
showErrorAlert("Start Failed", "Could not start simulation processes: " + ex.getMessage());
}
});
btnStop.setOnAction(e -> {
processManager.stopSimulation();
// Toggle UI state
btnStart.setDisable(false);
btnStop.setDisable(true);
configFileSelector.setDisable(false); // Desbloquear para nova simulação
});
controls.getChildren().addAll(btnStart, btnStop);
header.getChildren().addAll(title, subtitle, configPanel, controls);
return header;
}
/**
* Cria o painel de configuração com seleção de cenário e parâmetros.
*/
private VBox createConfigurationPanel() {
VBox configBox = new VBox(10);
configBox.setAlignment(Pos.CENTER);
configBox.setPadding(new Insets(10));
configBox.setStyle("-fx-background-color: rgba(255, 255, 255, 0.05); -fx-background-radius: 5;");
Label configLabel = new Label("Configuração da Simulação");
configLabel.setStyle("-fx-font-size: 14px; -fx-font-weight: bold;");
HBox configControls = new HBox(20);
configControls.setAlignment(Pos.CENTER);
// Scenario selector
VBox scenarioBox = new VBox(5);
scenarioBox.setAlignment(Pos.CENTER_LEFT);
Label scenarioLabel = new Label("Cenário:");
scenarioLabel.setStyle("-fx-font-size: 12px;");
configFileSelector = new ComboBox<>();
configFileSelector.getItems().addAll(
"simulation.properties",
"simulation-low.properties",
"simulation-medium.properties",
"simulation-high.properties"
);
configFileSelector.setValue("simulation.properties");
configFileSelector.setOnAction(e -> {
selectedConfigFile = configFileSelector.getValue();
updateConfigInfo();
System.out.println("Configuração selecionada: " + selectedConfigFile);
});
scenarioBox.getChildren().addAll(scenarioLabel, configFileSelector);
configControls.getChildren().add(scenarioBox);
// Routing policy selector
VBox routingBox = new VBox(5);
routingBox.setAlignment(Pos.CENTER_LEFT);
Label routingLabel = new Label("Política de Roteamento:");
routingLabel.setStyle("-fx-font-size: 12px;");
ComboBox<String> routingPolicySelector = new ComboBox<>();
routingPolicySelector.getItems().addAll(
"RANDOM",
"SHORTEST_PATH",
"LEAST_CONGESTED"
);
routingPolicySelector.setValue("RANDOM");
routingPolicySelector.setOnAction(e -> {
String selectedPolicy = routingPolicySelector.getValue();
System.out.println("Política de roteamento selecionada: " + selectedPolicy);
sendRoutingPolicyChange(selectedPolicy);
});
routingBox.getChildren().addAll(routingLabel, routingPolicySelector);
configControls.getChildren().add(routingBox);
// Advanced configuration button
VBox buttonBox = new VBox(5);
buttonBox.setAlignment(Pos.CENTER_LEFT);
Label spacerLabel = new Label(" ");
spacerLabel.setStyle("-fx-font-size: 12px;");
Button btnAdvancedConfig = new Button("Configuração Avançada...");
btnAdvancedConfig.setStyle("-fx-font-size: 11px;");
btnAdvancedConfig.setOnAction(e -> {
ConfigurationDialog.showAdvancedConfig((Stage) configBox.getScene().getWindow());
});
Button btnBatchAnalysis = new Button("Análise em Lote...");
btnBatchAnalysis.setStyle("-fx-font-size: 11px;");
btnBatchAnalysis.setOnAction(e -> {
BatchAnalysisDialog.show((Stage) configBox.getScene().getWindow(), statistics);
});
buttonBox.getChildren().addAll(spacerLabel, btnAdvancedConfig, btnBatchAnalysis);
configControls.getChildren().add(buttonBox);
// Configuration info display
configInfoLabel = new Label();
configInfoLabel.setStyle("-fx-font-size: 11px; -fx-text-fill: #aaaaaa;");
configInfoLabel.setWrapText(true);
configInfoLabel.setMaxWidth(800);
configInfoLabel.setAlignment(Pos.CENTER);
updateConfigInfo();
configBox.getChildren().addAll(configLabel, configControls, configInfoLabel);
return configBox;
}
private VBox createMainContent() {
VBox mainContent = new VBox(20);
mainContent.setPadding(new Insets(20));
// Global Statistics Panel
VBox globalStatsCard = createGlobalStatisticsPanel();
// Tables Container
HBox tablesContainer = new HBox(20);
tablesContainer.setAlignment(Pos.TOP_CENTER);
// Vehicle Type Statistics Panel
VBox vehicleTypeCard = createVehicleTypePanel();
HBox.setHgrow(vehicleTypeCard, Priority.ALWAYS);
// Intersection Statistics Panel
VBox intersectionCard = createIntersectionPanel();
HBox.setHgrow(intersectionCard, Priority.ALWAYS);
tablesContainer.getChildren().addAll(vehicleTypeCard, intersectionCard);
mainContent.getChildren().addAll(globalStatsCard, tablesContainer);
return mainContent;
}
private VBox createGlobalStatisticsPanel() {
VBox card = new VBox();
card.getStyleClass().add("card");
// Card Header
HBox cardHeader = new HBox();
cardHeader.getStyleClass().add("card-header");
Label cardTitle = new Label("Global Statistics");
cardTitle.getStyleClass().add("card-title");
cardHeader.getChildren().add(cardTitle);
// Card Content
GridPane grid = new GridPane();
grid.getStyleClass().add("card-content");
grid.setHgap(40);
grid.setVgap(15);
grid.setAlignment(Pos.CENTER);
// Initialize labels
lblVehiclesGenerated = createStatValueLabel("0");
lblVehiclesCompleted = createStatValueLabel("0");
lblVehiclesInTransit = createStatValueLabel("0");
lblAvgSystemTime = createStatValueLabel("0.00 s");
lblAvgWaitingTime = createStatValueLabel("0.00 s");
// Add labels with descriptions
addStatRow(grid, 0, 0, "Total Vehicles Generated", lblVehiclesGenerated);
addStatRow(grid, 1, 0, "Total Vehicles Completed", lblVehiclesCompleted);
addStatRow(grid, 2, 0, "Vehicles In Transit", lblVehiclesInTransit);
addStatRow(grid, 0, 1, "Average System Time", lblAvgSystemTime);
addStatRow(grid, 1, 1, "Average Waiting Time", lblAvgWaitingTime);
card.getChildren().addAll(cardHeader, grid);
return card;
}
private VBox createVehicleTypePanel() {
VBox card = new VBox();
card.getStyleClass().add("card");
// Card Header
HBox cardHeader = new HBox();
cardHeader.getStyleClass().add("card-header");
Label cardTitle = new Label("Vehicle Type Statistics");
cardTitle.getStyleClass().add("card-title");
cardHeader.getChildren().add(cardTitle);
// Table
vehicleTypeTable = new TableView<>();
vehicleTypeTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
vehicleTypeTable.setPrefHeight(300);
TableColumn<VehicleTypeRow, String> typeCol = new TableColumn<>("Vehicle Type");
typeCol.setCellValueFactory(new PropertyValueFactory<>("vehicleType"));
TableColumn<VehicleTypeRow, Integer> countCol = new TableColumn<>("Count");
countCol.setCellValueFactory(new PropertyValueFactory<>("count"));
TableColumn<VehicleTypeRow, String> avgWaitCol = new TableColumn<>("Avg Wait Time");
avgWaitCol.setCellValueFactory(new PropertyValueFactory<>("avgWaitTime"));
vehicleTypeTable.getColumns().addAll(typeCol, countCol, avgWaitCol);
card.getChildren().addAll(cardHeader, vehicleTypeTable);
return card;
}
private VBox createIntersectionPanel() {
VBox card = new VBox();
card.getStyleClass().add("card");
// Card Header
HBox cardHeader = new HBox();
cardHeader.getStyleClass().add("card-header");
Label cardTitle = new Label("Intersection Statistics");
cardTitle.getStyleClass().add("card-title");
cardHeader.getChildren().add(cardTitle);
// Table
intersectionTable = new TableView<>();
intersectionTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
intersectionTable.setPrefHeight(300);
TableColumn<IntersectionRow, String> idCol = new TableColumn<>("Intersection ID");
idCol.setCellValueFactory(new PropertyValueFactory<>("intersectionId"));
TableColumn<IntersectionRow, Integer> arrivalsCol = new TableColumn<>("Total Arrivals");
arrivalsCol.setCellValueFactory(new PropertyValueFactory<>("arrivals"));
TableColumn<IntersectionRow, Integer> departuresCol = new TableColumn<>("Total Departures");
departuresCol.setCellValueFactory(new PropertyValueFactory<>("departures"));
TableColumn<IntersectionRow, Integer> queueCol = new TableColumn<>("Current Queue");
queueCol.setCellValueFactory(new PropertyValueFactory<>("queueSize"));
intersectionTable.getColumns().addAll(idCol, arrivalsCol, departuresCol, queueCol);
card.getChildren().addAll(cardHeader, intersectionTable);
return card;
}
private HBox createFooter() {
HBox footer = new HBox(10);
footer.getStyleClass().add("footer");
footer.setAlignment(Pos.CENTER_LEFT);
Label statusLabel = new Label("Status:");
statusLabel.getStyleClass().add("footer-text");
statusLabel.setStyle("-fx-font-weight: bold;");
Circle statusIndicator = new Circle(6);
statusIndicator.setFill(javafx.scene.paint.Color.LIME);
Label statusText = new Label("Connected and Receiving Data");
statusText.getStyleClass().add("footer-text");
lblLastUpdate = new Label("Last Update: --:--:--");
lblLastUpdate.getStyleClass().add("footer-text");
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
footer.getChildren().addAll(statusLabel, statusIndicator, statusText, spacer, lblLastUpdate);
return footer;
}
private Label createStatValueLabel(String initialValue) {
Label label = new Label(initialValue);
label.getStyleClass().add("stat-value");
return label;
}
private void addStatRow(GridPane grid, int row, int colGroup, String description, Label valueLabel) {
VBox container = new VBox(5);
container.setAlignment(Pos.CENTER_LEFT);
Label descLabel = new Label(description);
descLabel.getStyleClass().add("stat-label");
container.getChildren().addAll(descLabel, valueLabel);
grid.add(container, colGroup, row);
}
/**
* Inicia o ciclo de polling em background.
* Atualiza a UI a cada 100ms.
*/
private void startPeriodicUpdates() {
updateScheduler = Executors.newSingleThreadScheduledExecutor();
updateScheduler.scheduleAtFixedRate(() -> {
// Crucial: Encapsular atualização de UI em Platform.runLater
// para garantir execução na JavaFX Application Thread
Platform.runLater(this::updateUI);
}, 0, 100, TimeUnit.MILLISECONDS);
}
/**
* Sincroniza o estado atual do objeto Statistics com os controlos JavaFX.
* Chamado periodicamente pela thread de UI.
*/
private void updateUI() {
// Update global statistics
lblVehiclesGenerated.setText(String.valueOf(statistics.getTotalVehiclesGenerated()));
lblVehiclesCompleted.setText(String.valueOf(statistics.getTotalVehiclesCompleted()));
lblVehiclesInTransit.setText(String.valueOf(
statistics.getTotalVehiclesGenerated() - statistics.getTotalVehiclesCompleted()));
lblAvgSystemTime.setText(String.format("%.2f s", statistics.getAverageSystemTime() / 1000.0));
lblAvgWaitingTime.setText(String.format("%.2f s", statistics.getAverageWaitingTime() / 1000.0));
lblLastUpdate.setText(String.format("Last Update: %tT", statistics.getLastUpdateTime()));
// Update vehicle type table
vehicleTypeTable.getItems().clear();
for (VehicleType type : VehicleType.values()) {
int count = statistics.getVehicleTypeCount(type);
double avgWait = statistics.getAverageWaitingTimeByType(type);
vehicleTypeTable.getItems().add(new VehicleTypeRow(
type.toString(), count, String.format("%.2f s", avgWait / 1000.0)));
}
// Update intersection table
intersectionTable.getItems().clear();
Map<String, DashboardStatistics.IntersectionStats> intersectionStats = statistics.getAllIntersectionStats();
for (DashboardStatistics.IntersectionStats stats : intersectionStats.values()) {
intersectionTable.getItems().add(new IntersectionRow(
stats.getIntersectionId(),
stats.getTotalArrivals(),
stats.getTotalDepartures(),
stats.getCurrentQueueSize()));
}
}
/**
* Atualiza a informação exibida sobre a configuração selecionada.
*/
private void updateConfigInfo() {
String info = "";
switch (selectedConfigFile) {
case "simulation-low.properties":
info = "CARGA BAIXA: 0.2 veículos/s (~720/hora) | Sem congestionamento esperado";
break;
case "simulation-medium.properties":
info = "CARGA MÉDIA: 0.5 veículos/s (~1800/hora) | Algum congestionamento esperado";
break;
case "simulation-high.properties":
info = "CARGA ALTA: 1.0 veículo/s (~3600/hora) | Congestionamento significativo esperado";
break;
default:
info = "⚙️ CONFIGURAÇÃO PADRÃO: Verificar ficheiro para parâmetros";
break;
}
configInfoLabel.setText(info);
}
private void shutdown() {
System.out.println("Shutting down Dashboard UI...");
if (updateScheduler != null && !updateScheduler.isShutdown()) {
updateScheduler.shutdownNow();
}
if (server != null) {
server.stop();
}
Platform.exit();
}
private void showErrorAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
/**
* Envia mensagem para o servidor do dashboard que notificará o coordenador.
* Usa uma abordagem indireta: salva a política desejada e o coordenador lerá na próxima geração.
*/
private void sendRoutingPolicyChange(String newPolicy) {
// Store the policy change request in statistics
// The coordinator will check this periodically
if (server != null && statistics != null) {
statistics.setRequestedRoutingPolicy(newPolicy);
System.out.println("Política de roteamento solicitada: " + newPolicy);
System.out.println(" - A mudança será aplicada pelo coordenador na próxima atualização");
// Mostrar confirmação visual
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Política Solicitada");
alert.setHeaderText(null);
alert.setContentText("Política de roteamento solicitada: " + newPolicy + "\nSerá aplicada em breve.");
alert.show();
});
} else {
Platform.runLater(() -> {
showErrorAlert("Erro", "Dashboard não está conectado. Inicie a simulação primeiro.");
});
}
}
public static void main(String[] args) {
launch(args);
}
// --- DTOs para Data Binding nas Tabelas ---
/** DTO para linhas da tabela de Tipos de Veículo. */
public static class VehicleTypeRow {
private final String vehicleType;
private final int count;
private final String avgWaitTime;
public VehicleTypeRow(String vehicleType, int count, String avgWaitTime) {
this.vehicleType = vehicleType;
this.count = count;
this.avgWaitTime = avgWaitTime;
}
public String getVehicleType() { return vehicleType; }
public int getCount() { return count; }
public String getAvgWaitTime() { return avgWaitTime; }
}
/** DTO para linhas da tabela de Interseções. */
public static class IntersectionRow {
private final String intersectionId;
private final int arrivals;
private final int departures;
private final int queueSize;
public IntersectionRow(String intersectionId, int arrivals, int departures, int queueSize) {
this.intersectionId = intersectionId;
this.arrivals = arrivals;
this.departures = departures;
this.queueSize = queueSize;
}
public String getIntersectionId() { return intersectionId; }
public int getArrivals() { return arrivals; }
public int getDepartures() { return departures; }
public int getQueueSize() { return queueSize; }
}
}

View File

@@ -0,0 +1,7 @@
package sd.dashboard;
public class Launcher {
public static void main(String[] args) {
DashboardUI.main(args);
}
}

View File

@@ -0,0 +1,184 @@
package sd.dashboard;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Orquestrador de processos para o ambiente de simulação distribuída.
* <p>
* Esta classe atua como um supervisor (Process Manager), responsável pelo <i>bootstrapping</i>
* e <i>teardown</i> das múltiplas Java Virtual Machines (JVMs) que compõem o sistema.
* <p>
* Funcionalidades principais:
* <ul>
* <li><b>Isolamento:</b> Cada nó (Interseção, Coordinator, ExitNode) corre no seu próprio processo OS.</li>
* <li><b>Ordem de Arranque:</b> Garante que os servidores (Interseções) estão online antes dos clientes (Coordenador).</li>
* <li><b>Gestão de Logs:</b> Redireciona stdout/stderr de cada processo filho para ficheiros temporários para facilitar o debug.</li>
* </ul>
*/
public class SimulationProcessManager {
private final List<Process> runningProcesses;
private final String classpath;
private String configFile;
/**
* Inicializa o gestor capturando o classpath da JVM atual.
* Isto garante que os processos filhos herdam as mesmas dependências e configurações de ambiente.
*/
public SimulationProcessManager() {
this.runningProcesses = new ArrayList<>();
this.classpath = System.getProperty("java.class.path");
this.configFile = "src/main/resources/simulation.properties";
}
/**
* Define o perfil de configuração a ser injetado nos processos filhos.
* Útil para alternar entre cenários (Low/Medium/High Load) dinamicamente.
* * @param configFile Nome do ficheiro de propriedades (ex: "simulation-low.properties").
*/
public void setConfigFile(String configFile) {
this.configFile = "src/main/resources/" + configFile;
System.out.println("Configuration file set to: " + this.configFile);
}
/**
* Executa o procedimento de arranque (Bootstrap) da simulação distribuída.
* <p>
* A ordem de inicialização é crítica para evitar <i>Race Conditions</i> na conexão TCP:
* <ol>
* <li><b>Workers (Interseções):</b> Iniciam os ServerSockets.</li>
* <li><b>Sink (Exit Node):</b> Prepara-se para receber métricas finais.</li>
* <li><b>Delay de Estabilização:</b> Pausa de 1s para garantir que os sockets estão em LISTENING.</li>
* <li><b>Source (Coordinator):</b> Inicia a geração de carga e conecta-se aos nós.</li>
* </ol>
* * @throws IOException Se falhar o fork de algum processo.
*/
public void startSimulation() throws IOException {
if (!runningProcesses.isEmpty()) {
stopSimulation();
}
System.out.println("Starting simulation processes...");
// 1. Start Intersections (Cr1 - Cr5)
String[] intersectionIds = { "Cr1", "Cr2", "Cr3", "Cr4", "Cr5" };
for (String id : intersectionIds) {
startProcess("sd.IntersectionProcess", id);
}
// 2. Start Exit Node
startProcess("sd.ExitNodeProcess", null);
// 3. Start Coordinator (Wait a bit for others to initialize)
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
startProcess("sd.coordinator.CoordinatorProcess", null);
System.out.println("All simulation processes started.");
}
/**
* Verifica o estado de "liveness" da simulação monitorizando o processo Coordenador.
* <p>
* Como o Coordenador gere o relógio DES e a geração de eventos, a sua terminação
* (após o drain time) sinaliza o fim efetivo da simulação.
* * @return true se o Coordenador ainda estiver ativo (alive).
*/
public boolean isSimulationRunning() {
if (runningProcesses.isEmpty()) {
return false;
}
// Coordinator is the last process in the list
Process coordinator = runningProcesses.get(runningProcesses.size() - 1);
return coordinator.isAlive();
}
/**
* Bloqueia a thread atual até que a simulação termine naturalmente ou ocorra timeout.
* * @param timeoutSeconds Tempo máximo de espera.
* @return true se terminou, false se o timeout expirou.
* @throws InterruptedException Se a espera for interrompida.
*/
public boolean waitForCompletion(long timeoutSeconds) throws InterruptedException {
if (runningProcesses.isEmpty()) {
return true;
}
Process coordinator = runningProcesses.get(runningProcesses.size() - 1);
return coordinator.waitFor(timeoutSeconds, java.util.concurrent.TimeUnit.SECONDS);
}
/**
* Executa o procedimento de encerramento (Teardown) de todos os processos.
* <p>
* Tenta primeiro uma paragem graciosa (`SIGTERM`), aguarda meio segundo, e
* força a paragem (`SIGKILL`) para processos persistentes, garantindo que não
* ficam processos órfãos no SO.
*/
public void stopSimulation() {
System.out.println("Stopping simulation processes...");
for (Process process : runningProcesses) {
if (process.isAlive()) {
process.destroy(); // Try graceful termination first
}
}
// Wait a bit and force kill if necessary
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
for (Process process : runningProcesses) {
if (process.isAlive()) {
process.destroyForcibly();
}
}
runningProcesses.clear();
System.out.println("All simulation processes stopped.");
}
/**
* Helper de baixo nível para construção e lançamento de processos Java.
* Configura o redirecionamento de I/O para ficheiros de log na diretoria temporária do SO.
*/
private void startProcess(String className, String arg) throws IOException {
String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
ProcessBuilder builder;
if (arg != null) {
builder = new ProcessBuilder(javaBin, "-cp", classpath, className, arg, configFile);
} else {
builder = new ProcessBuilder(javaBin, "-cp", classpath, className, configFile);
}
// get the OS temp folder
// Linux: /tmp/
// Windows: %AppData%\Local\Temp\
String tempDir = System.getProperty("java.io.tmpdir");
String logName = className.substring(className.lastIndexOf('.') + 1) + (arg != null ? "-" + arg : "") + ".log";
// use the (File parent, String child) constructor to handle slash/backslash
// automatically
File logFile = new File(tempDir, logName);
builder.redirectOutput(logFile);
builder.redirectError(logFile);
Process process = builder.start();
runningProcesses.add(process);
System.out.println("Started " + className + (arg != null ? " " + arg : ""));
// print where the logs are actually going
System.out.println("Logs redirected to: " + logFile.getAbsolutePath());
}
}

View File

@@ -0,0 +1,77 @@
package sd.dashboard;
import sd.model.MessageType;
import sd.protocol.MessageProtocol;
/**
* Implementação concreta do protocolo de mensagens destinada ao transporte de telemetria.
* <p>
* Esta classe atua como um envelope especializado para o envio de dados estatísticos
* (encapsulados em {@link StatsUpdatePayload}) dos nós operacionais (Interseções, Coordenador)
* para o servidor de Dashboard centralizado.
* <p>
* Diferencia-se das mensagens de controlo genéricas por ter o destino fixado no
* "DashboardServer" e um tipo de mensagem imutável ({@code STATS_UPDATE}).
*/
public class StatsMessage implements MessageProtocol {
private static final long serialVersionUID = 1L;
private final String sourceNode;
private final String destinationNode;
private final StatsUpdatePayload payload;
/**
* Cria uma nova mensagem de estatística.
*
* @param sourceNode O ID do nó que gerou as estatísticas (ex: "Cr1", "ExitNode").
* @param payload O objeto DTO contendo os dados estatísticos brutos ou agregados.
*/
public StatsMessage(String sourceNode, StatsUpdatePayload payload) {
this.sourceNode = sourceNode;
this.destinationNode = "DashboardServer"; // Destino implícito e fixo
this.payload = payload;
}
/**
* Retorna o tipo da mensagem, que identifica semanticamente o conteúdo para o recetor.
* @return Sempre {@link MessageType#STATS_UPDATE}.
*/
@Override
public MessageType getType() {
return MessageType.STATS_UPDATE;
}
/**
* Obtém a carga útil da mensagem.
* @return O objeto {@link StatsUpdatePayload} associado.
*/
@Override
public Object getPayload() {
return payload;
}
/**
* Identifica a origem da mensagem.
* @return O ID do nó remetente.
*/
@Override
public String getSourceNode() {
return sourceNode;
}
/**
* Identifica o destino da mensagem.
* @return Sempre "DashboardServer".
*/
@Override
public String getDestinationNode() {
return destinationNode;
}
@Override
public String toString() {
return String.format("StatsMessage[from=%s, to=%s, payload=%s]",
sourceNode, destinationNode, payload);
}
}

View File

@@ -0,0 +1,158 @@
package sd.dashboard;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import sd.model.VehicleType;
/**
* Objeto de Transferência de Dados (DTO) otimizado para transporte de telemetria.
* <p>
* Esta classe encapsula as métricas de desempenho enviadas pelos nós da simulação (Coordenador,
* Interseções, ExitNode) para o Dashboard. Foi desenhada para suportar <b>atualizações parciais</b>
* (Sparse Updates):
* <ul>
* <li>Campos globais inicializados com {@code -1} indicam "sem alteração" (no-op). O Dashboard
* deve ignorar estes campos e manter o valor acumulado anterior.</li>
* <li>Campos de interseção ({@code arrivals}, {@code departures}) representam deltas ou snapshots
* específicos do nó remetente.</li>
* </ul>
* Implementa {@link Serializable} para transmissão direta via Java Sockets.
*
[Image of data transfer object pattern]
*/
public class StatsUpdatePayload implements Serializable {
private static final long serialVersionUID = 1L;
// Global Metrics (Coordinator/ExitNode)
/** Total gerado. Valor -1 indica que este campo deve ser ignorado na atualização. */
private int totalVehiclesGenerated = -1;
/** Total completado. Valor -1 indica que este campo deve ser ignorado. */
private int totalVehiclesCompleted = -1;
/** Tempo total de sistema acumulado (ms). Valor -1 indica que deve ser ignorado. */
private long totalSystemTime = -1;
/** Tempo total de espera acumulado (ms). Valor -1 indica que deve ser ignorado. */
private long totalWaitingTime = -1;
// Intersection Metrics (Worker Nodes)
/** Número de veículos que entraram na interseção desde o último reporte. */
private int intersectionArrivals = 0;
/** Número de veículos que saíram da interseção desde o último reporte. */
private int intersectionDepartures = 0;
/** Snapshot do tamanho atual da fila na interseção. */
private int intersectionQueueSize = 0;
// Detailed Breakdowns
/** Contagem acumulada por tipo de veículo. */
private Map<VehicleType, Integer> vehicleTypeCounts;
/** Tempos de espera acumulados por tipo de veículo. */
private Map<VehicleType, Long> vehicleTypeWaitTimes;
/**
* Inicializa o payload com os mapas vazios e contadores globais a -1 (estado neutro).
*/
public StatsUpdatePayload() {
this.vehicleTypeCounts = new HashMap<>();
this.vehicleTypeWaitTimes = new HashMap<>();
}
public int getTotalVehiclesGenerated() {
return totalVehiclesGenerated;
}
public int getTotalVehiclesCompleted() {
return totalVehiclesCompleted;
}
public long getTotalSystemTime() {
return totalSystemTime;
}
public long getTotalWaitingTime() {
return totalWaitingTime;
}
public int getIntersectionArrivals() {
return intersectionArrivals;
}
public int getIntersectionDepartures() {
return intersectionDepartures;
}
public int getIntersectionQueueSize() {
return intersectionQueueSize;
}
public Map<VehicleType, Integer> getVehicleTypeCounts() {
return vehicleTypeCounts;
}
public Map<VehicleType, Long> getVehicleTypeWaitTimes() {
return vehicleTypeWaitTimes;
}
// Setters implementam Fluent Interface para construção encadeada
public StatsUpdatePayload setTotalVehiclesGenerated(int totalVehiclesGenerated) {
this.totalVehiclesGenerated = totalVehiclesGenerated;
return this;
}
public StatsUpdatePayload setTotalVehiclesCompleted(int totalVehiclesCompleted) {
this.totalVehiclesCompleted = totalVehiclesCompleted;
return this;
}
public StatsUpdatePayload setTotalSystemTime(long totalSystemTime) {
this.totalSystemTime = totalSystemTime;
return this;
}
public StatsUpdatePayload setTotalWaitingTime(long totalWaitingTime) {
this.totalWaitingTime = totalWaitingTime;
return this;
}
public StatsUpdatePayload setIntersectionArrivals(int intersectionArrivals) {
this.intersectionArrivals = intersectionArrivals;
return this;
}
public StatsUpdatePayload setIntersectionDepartures(int intersectionDepartures) {
this.intersectionDepartures = intersectionDepartures;
return this;
}
public StatsUpdatePayload setIntersectionQueueSize(int intersectionQueueSize) {
this.intersectionQueueSize = intersectionQueueSize;
return this;
}
public StatsUpdatePayload setVehicleTypeCounts(Map<VehicleType, Integer> vehicleTypeCounts) {
this.vehicleTypeCounts = vehicleTypeCounts;
return this;
}
public StatsUpdatePayload setVehicleTypeWaitTimes(Map<VehicleType, Long> vehicleTypeWaitTimes) {
this.vehicleTypeWaitTimes = vehicleTypeWaitTimes;
return this;
}
@Override
public String toString() {
return String.format("StatsUpdatePayload[generated=%d, completed=%d, arrivals=%d, departures=%d, queueSize=%d]",
totalVehiclesGenerated, totalVehiclesCompleted, intersectionArrivals,
intersectionDepartures, intersectionQueueSize);
}
}

View File

@@ -0,0 +1,39 @@
package sd.des;
/**
* Tipos de eventos discretos da simulação.
*
* <p>Representa os eventos DES que avançam o estado da simulação,
* não categorias de logging (EventType está noutro package).
*/
public enum DESEventType {
/** Gerar novo veículo num ponto de entrada */
VEHICLE_GENERATION,
/** Veículo chega a uma interseção */
VEHICLE_ARRIVAL,
/** Veículo começa a atravessar o semáforo */
VEHICLE_CROSSING_START,
/** Veículo termina a travessia */
VEHICLE_CROSSING_END,
/** Veículo parte para o próximo destino */
VEHICLE_DEPARTURE,
/** Veículo sai do sistema no nó de saída */
VEHICLE_EXIT,
/** Semáforo muda de estado (VERMELHO para VERDE ou vice-versa) */
TRAFFIC_LIGHT_CHANGE,
/** Processar veículos que esperam num semáforo recém-verde */
PROCESS_GREEN_LIGHT,
/** Atualização periódica de estatísticas */
STATISTICS_UPDATE,
/** Terminação da simulação */
SIMULATION_END
}

View File

@@ -0,0 +1,137 @@
package sd.des;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
/**
* Gere a Lista de Eventos Futuros (FEL) para Simulação de Eventos Discretos.
*
* <p>A FEL é uma fila de prioridade que mantém todos os eventos futuros agendados,
* ordenados por timestamp. Este é o coração do paradigma DES - a simulação avança
* processando eventos em ordem cronológica.</p>
*/
public class EventQueue {
private final PriorityQueue<SimulationEvent> queue;
private final List<SimulationEvent> processedEvents; // For logging and analysis
private final boolean trackHistory;
public EventQueue() {
this(true);
}
public EventQueue(boolean trackHistory) {
this.queue = new PriorityQueue<>();
this.processedEvents = trackHistory ? new ArrayList<>() : null;
this.trackHistory = trackHistory;
}
/**
* Agenda um novo evento.
*
* @param event evento a agendar
*/
public void schedule(SimulationEvent event) {
queue.offer(event);
}
/**
* Agenda um evento com um atraso relativo ao tempo atual.
*
* @param currentTime tempo atual da simulação
* @param delay atraso em segundos
* @param type tipo de evento
* @param payload dados do evento
* @param location localização do evento
*/
public void scheduleIn(double currentTime, double delay, DESEventType type,
Object payload, String location) {
double eventTime = currentTime + delay;
schedule(new SimulationEvent(eventTime, type, payload, location));
}
/** Obtém o próximo evento sem o remover */
public SimulationEvent peek() {
return queue.peek();
}
/**
* Obtém e remove o próximo evento.
* Se o rastreamento de histórico estiver ativo, adiciona-o aos eventos processados.
*/
public SimulationEvent poll() {
SimulationEvent event = queue.poll();
if (event != null && trackHistory) {
processedEvents.add(event);
}
return event;
}
/** Verifica se existem eventos pendentes */
public boolean isEmpty() {
return queue.isEmpty();
}
/** @return número de eventos pendentes */
public int size() {
return queue.size();
}
/** Limpa todos os eventos pendentes */
public void clear() {
queue.clear();
}
/**
* Obtém todos os eventos processados (se o rastreamento estiver ativo).
* Retorna uma cópia para evitar modificações.
*/
public List<SimulationEvent> getProcessedEvents() {
if (!trackHistory) {
throw new UnsupportedOperationException("History tracking is disabled");
}
return new ArrayList<>(processedEvents);
}
/** @return número de eventos processados */
public int getProcessedCount() {
return trackHistory ? processedEvents.size() : 0;
}
/**
* Exporta o histórico de eventos para uma string formatada.
* Útil para debugging e visualização da lista completa de eventos.
*/
public String exportEventHistory() {
if (!trackHistory) {
return "Event history tracking is disabled";
}
StringBuilder sb = new StringBuilder();
sb.append("=".repeat(80)).append("\n");
sb.append("SIMULATION EVENT HISTORY\n");
sb.append("Total Events Processed: ").append(processedEvents.size()).append("\n");
sb.append("=".repeat(80)).append("\n");
sb.append(String.format("%-10s | %-25s | %-20s | %s\n",
"Time", "Event Type", "Location", "Details"));
sb.append("-".repeat(80)).append("\n");
for (SimulationEvent event : processedEvents) {
String details = event.getPayload() != null ?
event.getPayload().getClass().getSimpleName() : "null";
sb.append(String.format("%-10.3f | %-25s | %-20s | %s\n",
event.getTimestamp(),
event.getType(),
event.getLocation() != null ? event.getLocation() : "N/A",
details));
}
return sb.toString();
}
@Override
public String toString() {
return String.format("EventQueue[pending=%d, processed=%d]",
queue.size(), getProcessedCount());
}
}

View File

@@ -0,0 +1,71 @@
package sd.des;
/**
* Gere o tempo de simulação para Simulação de Eventos Discretos.
*
* <p>
* No DES, o tempo avança em saltos discretos de evento para evento,
* não de forma contínua como o tempo real.
* </p>
*
* <p>
* Esta classe garante que todos os processos no sistema distribuído
* mantêm uma visão sincronizada do tempo de simulação.
* </p>
*/
public class SimulationClock {
private double currentTime;
private final double startTime;
private final long wallClockStart;
public SimulationClock() {
this(0.0);
}
public SimulationClock(double startTime) {
this.currentTime = startTime;
this.startTime = startTime;
this.wallClockStart = System.currentTimeMillis();
}
/**
* Avança o tempo de simulação para o timestamp dado.
* O tempo só pode avançar, nunca recuar.
*
* @param newTime novo tempo de simulação
* @throws IllegalArgumentException se newTime for anterior ao tempo atual
*/
public void advanceTo(double newTime) {
if (newTime < currentTime) {
throw new IllegalArgumentException(
String.format("Cannot move time backwards: %.3f -> %.3f", currentTime, newTime));
}
this.currentTime = newTime;
}
/** @return tempo atual da simulação */
public double getCurrentTime() {
return currentTime;
}
/** @return tempo de simulação decorrido desde o início */
public double getElapsedTime() {
return currentTime - startTime;
}
/** @return tempo real decorrido em milissegundos */
public long getWallClockElapsed() {
return System.currentTimeMillis() - wallClockStart;
}
/** Reinicia o relógio para o tempo inicial */
public void reset() {
this.currentTime = startTime;
}
@Override
public String toString() {
return String.format("SimulationClock[time=%.3fs, elapsed=%.3fs]",
currentTime, getElapsedTime());
}
}

View File

@@ -0,0 +1,130 @@
package sd.des;
import java.io.Serializable;
/**
* Representa um evento atómico e imutável no contexto da Simulação de Eventos Discretos (DES).
* <p>
* Esta classe é a unidade fundamental de processamento. Numa arquitetura DES, o estado do sistema
* não muda continuamente, mas sim em instantes discretos definidos por estes eventos.
* <p>
* Características principais:
* <ul>
* <li><b>Ordenação Temporal:</b> Implementa {@link Comparable} para ser armazenado numa Fila de
* Eventos Futuros (FEL - Future Event List), garantindo execução cronológica.</li>
* <li><b>Distribuído:</b> Implementa {@link Serializable} para permitir que eventos gerados num nó
* (ex: Coordenador) sejam transmitidos e executados noutro (ex: Interseção).</li>
* <li><b>Polimórfico:</b> Transporta um {@code payload} genérico, permitindo associar qualquer
* entidade (Veículo, Sinal, etc.) ao evento.</li>
* </ul>
*/
public class SimulationEvent implements Comparable<SimulationEvent>, Serializable {
private static final long serialVersionUID = 1L;
/** O instante virtual exato em que o evento deve ser processado. */
private final double timestamp;
/** A categoria do evento (ex: VEHICLE_ARRIVAL, LIGHT_CHANGE). */
private final DESEventType type;
/** Dados contextuais associados (ex: o objeto Vehicle que chegou). */
private final Object payload;
/**
* O identificador do nó de destino onde o evento deve ser executado.
* (ex: "Cr1", "Coordinator", "ExitNode"). Se null, é um evento local.
*/
private final String location;
/**
* Instancia um novo evento de simulação completo.
*
* @param timestamp Instante de execução (segundos virtuais).
* @param type Tipo enumerado do evento.
* @param payload Objeto de dados associado (pode ser null).
* @param location ID do processo alvo para execução distribuída.
*/
public SimulationEvent(double timestamp, DESEventType type, Object payload, String location) {
this.timestamp = timestamp;
this.type = type;
this.payload = payload;
this.location = location;
}
/**
* Construtor de conveniência para eventos locais (dentro do mesmo processo).
* Define {@code location} como null.
*
* @param timestamp Instante de execução.
* @param type Tipo do evento.
* @param payload Objeto de dados associado.
*/
public SimulationEvent(double timestamp, DESEventType type, Object payload) {
this(timestamp, type, payload, null);
}
public double getTimestamp() {
return timestamp;
}
public DESEventType getType() {
return type;
}
public Object getPayload() {
return payload;
}
public String getLocation() {
return location;
}
/**
* Define a ordem natural de processamento na Fila de Prioridade.
* <p>
* <b>Lógica de Ordenação:</b>
* <ol>
* <li><b>Primária (Tempo):</b> Eventos com menor timestamp ocorrem primeiro.</li>
* <li><b>Secundária (Determinismo):</b> Em caso de empate temporal (simultaneidade),
* ordena alfabeticamente pelo nome do tipo. Isto garante que execuções repetidas
* da simulação produzam exatamente o mesmo resultado (determinismo estrito).</li>
* </ol>
*
* @param other O outro evento a comparar.
* @return Inteiro negativo, zero ou positivo conforme a ordem.
*/
@Override
public int compareTo(SimulationEvent other) {
int timeComparison = Double.compare(this.timestamp, other.timestamp);
if (timeComparison != 0) {
return timeComparison;
}
// Tie-breaker: order by event type name to ensure reproducible runs
return this.type.name().compareTo(other.type.name());
}
@Override
public String toString() {
return String.format("Event[t=%.3f, type=%s, location=%s]",
timestamp, type, location);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof SimulationEvent)) return false;
SimulationEvent other = (SimulationEvent) obj;
return Double.compare(timestamp, other.timestamp) == 0 &&
type == other.type &&
(location == null ? other.location == null : location.equals(other.location));
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + Double.hashCode(timestamp);
result = 31 * result + type.hashCode();
result = 31 * result + (location != null ? location.hashCode() : 0);
return result;
}
}

View File

@@ -0,0 +1,55 @@
package sd.des;
import sd.model.TrafficLight;
/**
* Encapsula o contexto de dados para eventos de mudança de estado de semáforos.
* <p>
* Este objeto atua como o <i>payload</i> transportado por um {@link SimulationEvent}
* quando o tipo de evento é relacionado com controlo de tráfego (ex: mudança Verde -> Amarelo).
* Permite que o motor DES identifique exatamente qual instância de {@link TrafficLight}
* deve ser atualizada numa determinada interseção e direção.
*/
public class TrafficLightEvent {
private final TrafficLight light;
private final String direction;
private final String intersectionId;
/**
* Cria um novo payload de evento de semáforo.
* @param light A instância do objeto semáforo a ser manipulado.
* @param direction A direção cardeal associada (ex: "North", "East").
* @param intersectionId O identificador da interseção onde o semáforo reside.
*/
public TrafficLightEvent(TrafficLight light, String direction, String intersectionId) {
this.light = light;
this.direction = direction;
this.intersectionId = intersectionId;
}
/**
* @return A referência direta para o objeto de domínio do semáforo.
*/
public TrafficLight getLight() {
return light;
}
/**
* @return A direção do fluxo controlado por este semáforo.
*/
public String getDirection() {
return direction;
}
/**
* @return O ID da interseção pai.
*/
public String getIntersectionId() {
return intersectionId;
}
@Override
public String toString() {
return String.format("TrafficLightEvent[%s-%s]", intersectionId, direction);
}
}

View File

@@ -0,0 +1,251 @@
package sd.logging;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Motor de logging assíncrono e thread-safe para a simulação distribuída.
* <p>
* Implementa o padrão <i>Singleton</i> para garantir um ponto centralizado de registo.
* Utiliza o padrão <i>Producer-Consumer</i> com uma {@link BlockingQueue} para desacoplar
* a geração de eventos (crítica para a performance da simulação) da persistência em disco
* (operação de I/O lenta).
* <p>
* <b>Garantias:</b>
* <ul>
* <li>Non-blocking writes (para a thread chamadora, na maioria dos casos).</li>
* <li>Ordering cronológico aproximado (FIFO na fila).</li>
* <li>Graceful Shutdown (flush de logs pendentes ao terminar).</li>
* </ul>
*/
public class EventLogger {
private static EventLogger instance;
private static final Object instanceLock = new Object();
private final PrintWriter writer;
/** Buffer de memória para absorver picos de eventos (Burst traffic). */
private final BlockingQueue<LogEntry> logQueue;
/** Thread dedicada (Consumer) para escrita em ficheiro. */
private final Thread writerThread;
private final AtomicBoolean running;
private final SimpleDateFormat timestampFormat;
private final long simulationStartMillis;
/**
* Inicializa o sistema de logs.
* Abre o ficheiro, escreve o cabeçalho e inicia a thread consumidora.
*
* @param logFilePath Caminho relativo ou absoluto do ficheiro de log.
* @throws IOException Se não for possível criar ou escrever no ficheiro.
*/
private EventLogger(String logFilePath) throws IOException {
// Auto-flush ativado para garantir persistência, mas gerido pelo buffer do BufferedWriter
this.writer = new PrintWriter(new BufferedWriter(new FileWriter(logFilePath, false)), true);
this.logQueue = new LinkedBlockingQueue<>(10000); // Backpressure: limita a 10k eventos pendentes
this.running = new AtomicBoolean(true);
this.timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
this.simulationStartMillis = System.currentTimeMillis();
// Header inicial do log
writer.println("=".repeat(80));
writer.println("SIMULATION EVENT LOG");
writer.println("Started: " + timestampFormat.format(new Date()));
writer.println("=".repeat(80));
writer.println();
writer.printf("%-23s | %-8s | %-20s | %-15s | %s\n",
"TIMESTAMP", "REL_TIME", "EVENT_TYPE", "COMPONENT", "DESCRIPTION");
writer.println("-".repeat(80));
writer.flush();
this.writerThread = new Thread(this::processLogQueue, "EventLogger-Writer");
this.writerThread.setDaemon(true); // Permite que a JVM termine se apenas esta thread sobrar
this.writerThread.start();
}
/**
* Obtém a instância única do logger (Lazy Initialization).
* Se não existir, cria uma predefinida em "logs/simulation-events.log".
*
* @return A instância singleton.
*/
public static EventLogger getInstance() {
if (instance == null) {
synchronized (instanceLock) {
if (instance == null) {
try {
String logFile = "logs/simulation-events.log";
java.nio.file.Files.createDirectories(
java.nio.file.Paths.get("logs"));
instance = new EventLogger(logFile);
} catch (IOException e) {
System.err.println("Failed to initialize EventLogger: " + e.getMessage());
e.printStackTrace();
}
}
}
}
return instance;
}
/**
* Reinicializa o logger com um ficheiro específico.
* Útil para testes ou configurações personalizadas.
*/
public static void initialize(String logFilePath) throws IOException {
synchronized (instanceLock) {
if (instance != null) {
instance.shutdown();
}
instance = new EventLogger(logFilePath);
}
}
/**
* Regista um evento genérico.
* Esta operação é não-bloqueante (retorna imediatamente após colocar na fila),
* exceto se a fila estiver cheia (backpressure).
*
* @param eventType Categoria do evento.
* @param component Nome do componente (ex: "Coordinator", "IntersectionProcess").
* @param description Detalhes do evento.
*/
public void log(EventType eventType, String component, String description) {
if (!running.get()) return;
LogEntry entry = new LogEntry(
System.currentTimeMillis(),
eventType,
component,
description
);
// Non-blocking offer - if queue is full, drop oldest or warn
if (!logQueue.offer(entry)) {
// Queue full - this shouldn't happen with 10k buffer, but handle gracefully
System.err.println("EventLogger queue full - dropping event: " + eventType);
}
}
/**
* Regista um evento associado a um veículo específico (Helper method).
*/
public void logVehicle(EventType eventType, String component, String vehicleId, String description) {
log(eventType, component, "[" + vehicleId + "] " + description);
}
/**
* Regista um erro ou exceção com formatação apropriada.
*/
public void logError(String component, String description, Exception e) {
String fullDescription = description + (e != null ? ": " + e.getMessage() : "");
log(EventType.ERROR, component, fullDescription);
}
/**
* Lógica da thread consumidora (Worker Thread).
* Retira eventos da fila e escreve no disco continuamente.
*/
private void processLogQueue() {
while (running.get() || !logQueue.isEmpty()) {
try {
// Poll com timeout para permitir verificar a flag 'running' periodicamente
LogEntry entry = logQueue.poll(100, java.util.concurrent.TimeUnit.MILLISECONDS);
if (entry != null) {
writeEntry(entry);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
// Flush final: garantir que eventos restantes na fila são escritos antes de morrer
while (!logQueue.isEmpty()) {
LogEntry entry = logQueue.poll();
if (entry != null) {
writeEntry(entry);
}
}
}
/**
* Formata e escreve uma entrada de log no PrintWriter.
*/
private void writeEntry(LogEntry entry) {
String timestamp = timestampFormat.format(new Date(entry.timestampMillis));
double relativeTime = (entry.timestampMillis - simulationStartMillis) / 1000.0;
writer.printf("%-23s | %8.3fs | %-20s | %-15s | %s\n",
timestamp,
relativeTime,
entry.eventType.toString(),
truncate(entry.component, 15),
entry.description
);
// Flush periódico inteligente: se a carga for baixa, garante que vemos logs em tempo real
if (logQueue.size() < 10) {
writer.flush();
}
}
private String truncate(String str, int maxLength) {
if (str == null) return "";
return str.length() <= maxLength ? str : str.substring(0, maxLength);
}
/**
* Encerra o logger de forma segura.
* Desativa a aceitação de novos eventos, aguarda que a fila esvazie (flush)
* e fecha o ficheiro.
*/
public void shutdown() {
if (!running.compareAndSet(true, false)) {
return; // Já encerrado
}
try {
// Wait for writer thread to finish flushing
writerThread.join(5000); // Wait up to 5 seconds
// Write footer
writer.println();
writer.println("-".repeat(80));
writer.println("SIMULATION ENDED");
writer.println("Ended: " + timestampFormat.format(new Date()));
writer.println("=".repeat(80));
writer.close();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
/**
* DTO interno imutável para armazenar dados do evento na fila.
*/
private static class LogEntry {
final long timestampMillis;
final EventType eventType;
final String component;
final String description;
LogEntry(long timestampMillis, EventType eventType, String component, String description) {
this.timestampMillis = timestampMillis;
this.eventType = eventType;
this.component = component;
this.description = description;
}
}
}

View File

@@ -0,0 +1,60 @@
package sd.logging;
/**
* Taxonomia oficial de eventos para o subsistema de logging centralizado.
* <p>
* Este enumerado padroniza a categorização de todas as ocorrências na simulação, permitindo:
* <ul>
* <li>Filtragem granular de logs (ex: ver apenas erros ou apenas tráfego de rede).</li>
* <li>Análise estatística post-mortem (parsear logs para calcular latências).</li>
* <li>Correlação de eventos distribuídos (seguir o rastro de um veículo através de vários nós).</li>
* </ul>
*/
public enum EventType {
// --- Ciclo de Vida do Veículo ---
VEHICLE_GENERATED("Vehicle Generated"),
VEHICLE_ARRIVED("Vehicle Arrived"),
VEHICLE_QUEUED("Vehicle Queued"),
VEHICLE_DEPARTED("Vehicle Departed"),
VEHICLE_EXITED("Vehicle Exited"),
// --- Controlo de Semáforos e Exclusão Mútua ---
LIGHT_CHANGED_GREEN("Light Changed to Green"),
LIGHT_CHANGED_RED("Light Changed to Red"),
LIGHT_REQUEST_GREEN("Light Requested Green"),
LIGHT_RELEASE_GREEN("Light Released Green"),
// --- Ciclo de Vida da Simulação/Processos ---
SIMULATION_STARTED("Simulation Started"),
SIMULATION_STOPPED("Simulation Stopped"),
PROCESS_STARTED("Process Started"),
PROCESS_STOPPED("Process Stopped"),
// --- Configuração e Telemetria ---
STATS_UPDATE("Statistics Update"),
CONFIG_CHANGED("Configuration Changed"),
// --- Camada de Rede (TCP/Sockets) ---
CONNECTION_ESTABLISHED("Connection Established"),
CONNECTION_LOST("Connection Lost"),
MESSAGE_SENT("Message Sent"),
MESSAGE_RECEIVED("Message Received"),
// --- Tratamento de Exceções ---
ERROR("Error");
private final String displayName;
EventType(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
@Override
public String toString() {
return displayName;
}
}

View File

@@ -0,0 +1,364 @@
package sd.logging;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import sd.model.Vehicle;
/**
* Subsistema de auditoria granular responsável pelo rastreio detalhado (Tracing) de veículos individuais.
* <p>
* Diferente do {@link EventLogger} (que regista eventos globais do sistema), esta classe foca-se
* na perspetiva do <b>agente</b>. Cria um ficheiro de rastro dedicado (`.trace`) para cada veículo
* monitorizado, registando cronologicamente cada interação com a infraestrutura (interseções,
* filas, semáforos).
* <p>
* <b>Funcionalidades:</b>
* <ul>
* <li>Análise forense de percursos individuais.</li>
* <li>Validação de tempos de espera e travessia por nó.</li>
* <li>Cálculo de eficiência de rota (tempo em movimento vs. tempo parado).</li>
* </ul>
*/
public class VehicleTracer {
private static VehicleTracer instance;
private static final Object instanceLock = new Object();
/** Mapa thread-safe de sessões de trace ativas (VehicleID -> TraceHandler). */
private final Map<String, VehicleTrace> trackedVehicles;
private final SimpleDateFormat timestampFormat;
private final long simulationStartMillis;
private final String traceDirectory;
/**
* Inicializa o tracer e prepara o diretório de saída.
*
* @param traceDirectory Caminho para armazenamento dos ficheiros .trace.
*/
private VehicleTracer(String traceDirectory) {
this.trackedVehicles = new ConcurrentHashMap<>();
this.timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
this.simulationStartMillis = System.currentTimeMillis();
this.traceDirectory = traceDirectory;
try {
java.nio.file.Files.createDirectories(java.nio.file.Paths.get(traceDirectory));
} catch (IOException e) {
System.err.println("Failed to create trace directory: " + e.getMessage());
}
}
/**
* Obtém a instância única do tracer (Singleton).
* @return A instância global.
*/
public static VehicleTracer getInstance() {
if (instance == null) {
synchronized (instanceLock) {
if (instance == null) {
instance = new VehicleTracer("logs/traces");
}
}
}
return instance;
}
/**
* Reinicializa o tracer com um diretório personalizado.
* Útil para isolar logs de diferentes execuções em lote.
*/
public static void initialize(String traceDirectory) {
synchronized (instanceLock) {
if (instance != null) {
instance.shutdown();
}
instance = new VehicleTracer(traceDirectory);
}
}
/**
* Inicia a sessão de rastreio para um veículo específico.
* Cria o ficheiro {@code logs/traces/vehicle-{id}.trace} e escreve o cabeçalho.
*
* @param vehicleId O identificador único do veículo.
*/
public void startTracking(String vehicleId) {
if (trackedVehicles.containsKey(vehicleId)) {
return; // Já está a ser rastreado
}
VehicleTrace trace = new VehicleTrace(vehicleId, traceDirectory);
trackedVehicles.put(vehicleId, trace);
trace.logEvent("TRACKING_STARTED", "", "Started tracking vehicle " + vehicleId);
}
/**
* Encerra a sessão de rastreio, fecha o descritor de ficheiro e remove da memória.
*/
public void stopTracking(String vehicleId) {
VehicleTrace trace = trackedVehicles.remove(vehicleId);
if (trace != null) {
trace.logEvent("TRACKING_STOPPED", "", "Stopped tracking vehicle " + vehicleId);
trace.close();
}
}
/**
* Verifica se um veículo está atualmente sob auditoria.
*/
public boolean isTracking(String vehicleId) {
return trackedVehicles.containsKey(vehicleId);
}
/**
* Regista o evento de instanciação do veículo pelo Coordenador.
*/
public void logGenerated(Vehicle vehicle) {
if (!isTracking(vehicle.getId()))
return;
VehicleTrace trace = trackedVehicles.get(vehicle.getId());
if (trace != null) {
trace.logEvent("GENERATED", "Coordinator",
String.format("Type: %s, Entry Time: %.2fs, Route: %s",
vehicle.getType(), vehicle.getEntryTime(), vehicle.getRoute()));
}
}
/**
* Regista a chegada física do veículo à zona de deteção de uma interseção.
*/
public void logArrival(String vehicleId, String intersection, double simulationTime) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("ARRIVED", intersection,
String.format("Arrived at %s (sim time: %.2fs)", intersection, simulationTime));
}
}
/**
* Regista a entrada do veículo na estrutura de fila de um semáforo.
*/
public void logQueued(String vehicleId, String intersection, String direction, int queuePosition) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("QUEUED", intersection,
String.format("Queued at %s-%s (position: %d)", intersection, direction, queuePosition));
}
}
/**
* Regista o início da espera ativa (veículo parado no Vermelho).
*/
public void logWaitingStart(String vehicleId, String intersection, String direction) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("WAITING_START", intersection,
String.format("Started waiting at %s-%s (light is RED)", intersection, direction));
}
}
/**
* Regista o fim da espera (Sinal Verde).
* @param waitTime Duração total da paragem nesta instância.
*/
public void logWaitingEnd(String vehicleId, String intersection, String direction, double waitTime) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("WAITING_END", intersection,
String.format("Finished waiting at %s-%s (waited %.2fs)", intersection, direction, waitTime));
}
}
/**
* Regista o início da travessia da interseção (ocupação da zona crítica).
*/
public void logCrossingStart(String vehicleId, String intersection, String direction) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("CROSSING_START", intersection,
String.format("Started crossing %s-%s (light is GREEN)", intersection, direction));
}
}
/**
* Regista a libertação da zona crítica da interseção.
*/
public void logCrossingEnd(String vehicleId, String intersection, double crossingTime) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("CROSSING_END", intersection,
String.format("Finished crossing %s (took %.2fs)", intersection, crossingTime));
}
}
/**
* Regista a partida da interseção em direção ao próximo nó.
*/
public void logDeparture(String vehicleId, String intersection, String nextDestination) {
if (!isTracking(vehicleId))
return;
VehicleTrace trace = trackedVehicles.get(vehicleId);
if (trace != null) {
trace.logEvent("DEPARTED", intersection,
String.format("Departed from %s toward %s", intersection, nextDestination));
}
}
/**
* Regista a saída do sistema (no Exit Node).
* <p>
* Este método também desencadeia a escrita do <b>Sumário de Viagem</b> no final do log
* e fecha o ficheiro automaticamente.
*/
public void logExit(Vehicle vehicle, double systemTime) {
if (!isTracking(vehicle.getId()))
return;
VehicleTrace trace = trackedVehicles.get(vehicle.getId());
if (trace != null) {
trace.logEvent("EXITED", "Exit Node",
String.format("Exited system - Total time: %.2fs, Waiting: %.2fs, Crossing: %.2fs",
systemTime, vehicle.getTotalWaitingTime(), vehicle.getTotalCrossingTime()));
// Escreve estatísticas sumarizadas
trace.writeSummary(vehicle, systemTime);
// Stop tracking and close file
stopTracking(vehicle.getId());
}
}
/**
* Fecha forçosamente todos os traces abertos.
* Deve ser chamado no shutdown da simulação para evitar corrupção de logs.
*/
public void shutdown() {
for (VehicleTrace trace : trackedVehicles.values()) {
trace.close();
}
trackedVehicles.clear();
}
/**
* Classe interna auxiliar que gere o descritor de ficheiro e a formatação para um único veículo.
*/
private class VehicleTrace {
private final String vehicleId;
private final PrintWriter writer;
private final long traceStartMillis;
VehicleTrace(String vehicleId, String directory) {
this.vehicleId = vehicleId;
this.traceStartMillis = System.currentTimeMillis();
PrintWriter w = null;
try {
String filename = String.format("%s/vehicle-%s.trace", directory, vehicleId);
w = new PrintWriter(new BufferedWriter(new FileWriter(filename, false)), true);
// Write header
w.println("=".repeat(80));
w.println("VEHICLE TRACE: " + vehicleId);
w.println("Trace Started: " + timestampFormat.format(new Date()));
w.println("=".repeat(80));
w.println();
w.printf("%-23s | %-8s | %-15s | %-15s | %s\n",
"TIMESTAMP", "REL_TIME", "EVENT", "LOCATION", "DESCRIPTION");
w.println("-".repeat(80));
} catch (IOException e) {
System.err.println("Failed to create trace file for " + vehicleId + ": " + e.getMessage());
}
this.writer = w;
}
void logEvent(String eventType, String location, String description) {
if (writer == null)
return;
long now = System.currentTimeMillis();
String timestamp = timestampFormat.format(new Date(now));
double relativeTime = (now - traceStartMillis) / 1000.0;
writer.printf("%-23s | %8.3fs | %-15s | %-15s | %s\n",
timestamp,
relativeTime,
truncate(eventType, 15),
truncate(location, 15),
description);
writer.flush();
}
void writeSummary(Vehicle vehicle, double systemTime) {
if (writer == null)
return;
writer.println();
writer.println("=".repeat(80));
writer.println("JOURNEY SUMMARY");
writer.println("=".repeat(80));
writer.println("Vehicle ID: " + vehicle.getId());
writer.println("Vehicle Type: " + vehicle.getType());
writer.println("Route: " + vehicle.getRoute());
writer.println();
writer.printf("Entry Time: %.2f seconds\n", vehicle.getEntryTime());
writer.printf("Total System Time: %.2f seconds\n", systemTime);
writer.printf("Total Waiting Time: %.2f seconds (%.1f%%)\n",
vehicle.getTotalWaitingTime(),
100.0 * vehicle.getTotalWaitingTime() / systemTime);
writer.printf("Total Crossing Time: %.2f seconds (%.1f%%)\n",
vehicle.getTotalCrossingTime(),
100.0 * vehicle.getTotalCrossingTime() / systemTime);
writer.printf("Travel Time: %.2f seconds (%.1f%%)\n",
systemTime - vehicle.getTotalWaitingTime() - vehicle.getTotalCrossingTime(),
100.0 * (systemTime - vehicle.getTotalWaitingTime() - vehicle.getTotalCrossingTime()) / systemTime);
writer.println("=".repeat(80));
}
void close() {
if (writer != null) {
writer.println();
writer.println("-".repeat(80));
writer.println("END OF TRACE");
writer.println("=".repeat(80));
writer.close();
}
}
private String truncate(String str, int maxLength) {
if (str == null)
return "";
return str.length() <= maxLength ? str : str.substring(0, maxLength);
}
}
}

View File

@@ -0,0 +1,228 @@
package sd.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Representa uma interseção na simulação de tráfego.
*
* <p>Uma interseção funciona como um nó central da rede. Não controla lógica diretamente,
* mas gere um conjunto de semáforos ({@link TrafficLight}).</p>
*
* <p>Responsabilidades principais:</p>
* <ul>
* <li>Manter um {@link TrafficLight} para cada direção (Norte, Este, etc.)</li>
* <li>Gerir uma tabela de encaminhamento que mapeia destinos para direções</li>
* <li>Receber veículos e colocá-los na fila do semáforo correto</li>
* <li>Acompanhar estatísticas agregadas do tráfego</li>
* </ul>
*/
public class Intersection {
/** Identificador único da interseção (ex: "Cr1", "Cr2") */
private final String id;
/**
* Mapa com todos os semáforos desta interseção.
* Chave: Direção (String, ex: "Norte", "Este")
* Valor: Objeto {@link TrafficLight} correspondente
*/
private final Map<String, TrafficLight> trafficLights;
/**
* Tabela de encaminhamento da interseção.
* Chave: Próximo destino (String, ex: "Cr3", "S" para saída)
* Valor: Direção que o veículo deve tomar nesta interseção
*/
private final Map<String, String> routing;
/** Número total de veículos recebidos por esta interseção */
private int totalVehiclesReceived;
/** Número total de veículos que partiram desta interseção */
private int totalVehiclesSent;
/** Média acumulada do tempo de espera dos veículos nesta interseção */
private double averageWaitingTime;
/**
* Cria uma nova interseção.
* Inicializa mapas vazios para semáforos e encaminhamento.
*
* @param id identificador único da interseção (ex: "Cr1")
*/
public Intersection(String id) {
this.id = id;
this.trafficLights = new HashMap<>();
this.routing = new HashMap<>();
this.totalVehiclesReceived = 0;
this.totalVehiclesSent = 0;
this.averageWaitingTime = 0.0;
}
/**
* Regista um novo semáforo nesta interseção.
* O semáforo é mapeado pela sua direção.
*
* @param trafficLight o semáforo a adicionar
*/
public void addTrafficLight(TrafficLight trafficLight) {
trafficLights.put(trafficLight.getDirection(), trafficLight);
}
/**
* Define uma regra de encaminhamento para esta interseção.
*
* <p>Por exemplo, {@code configureRoute("Cr3", "Este")} significa:
* "Qualquer veículo que chegue aqui com destino 'Cr3' deve ser enviado
* para a fila do semáforo da direção Este."</p>
*
* @param nextDestination ID da próxima interseção ou saída (ex: "Cr3", "S")
* @param direction direção (e respetivo semáforo) a usar nesta interseção
*/
public void configureRoute(String nextDestination, String direction) {
routing.put(nextDestination, direction);
}
/**
* Recebe um novo veículo e coloca-o na fila do semáforo apropriado.
* A direção é escolhida com base na tabela de encaminhamento.
*
* @param vehicle o veículo que está a chegar a esta interseção
* @param simulationTime o tempo de simulação atual (em segundos)
*/
public void receiveVehicle(Vehicle vehicle, double simulationTime) {
totalVehiclesReceived++;
// Note: Route advancement is handled by SimulationEngine.handleVehicleArrival()
// before calling this method, so we don't advance here.
String nextDestination = vehicle.getCurrentDestination();
// Check if vehicle reached final destination
if (nextDestination == null) {
System.out.printf("[%s] Vehicle %s reached final destination%n",
this.id, vehicle.getId());
return;
}
String direction = routing.get(nextDestination);
if (direction != null && trafficLights.containsKey(direction)) {
// Found a valid route and light, add vehicle to the queue
trafficLights.get(direction).addVehicle(vehicle, simulationTime);
} else {
// Routing error: No rule for this destination or no light for that direction
System.err.printf(
"Routing error at %s: could not place vehicle %s (destination: %s, found direction: %s)%n",
this.id, vehicle.getId(), nextDestination, direction
);
}
} /**
* Retorna a direção que um veículo deve tomar para alcançar um destino.
*
* @param destination o próximo destino (ex: "Cr3", "S")
* @return a direção (ex: "Este"), ou null se não houver rota configurada
*/
public String getDirectionForDestination(String destination) {
return routing.get(destination);
}
/**
* Retorna o semáforo que controla uma determinada direção.
*
* @param direction a direção (ex: "Norte")
* @return o objeto {@link TrafficLight}, ou null se não existir
*/
public TrafficLight getTrafficLight(String direction) {
return trafficLights.get(direction);
}
/**
* Retorna uma lista com todos os semáforos desta interseção.
*
* @return uma nova {@link List} com todos os semáforos
*/
public List<TrafficLight> getTrafficLights() {
return new ArrayList<>(trafficLights.values());
}
/**
* Retorna o número total de veículos em fila em todos os semáforos.
* Usa Java Stream API para somar os tamanhos de todas as filas.
*
* @return a soma dos tamanhos de todas as filas
*/
public int getTotalQueueSize() {
return trafficLights.values().stream()
.mapToInt(TrafficLight::getQueueSize)
.sum();
}
/**
* @return o identificador único desta interseção
*/
public String getId() {
return id;
}
/**
* @return o número total de veículos que chegaram a esta interseção
*/
public int getTotalVehiclesReceived() {
return totalVehiclesReceived;
}
/**
* @return o número total de veículos que partiram desta interseção
*/
public int getTotalVehiclesSent() {
return totalVehiclesSent;
}
/**
* Incrementa o contador de veículos que partiram com sucesso.
* Tipicamente chamado após um veículo completar a travessia.
*/
public void incrementVehiclesSent() {
totalVehiclesSent++;
}
/**
* @return a média do tempo de espera dos veículos nesta interseção
*/
public double getAverageWaitingTime() {
return averageWaitingTime;
}
/**
* Atualiza a média do tempo de espera com uma nova amostra.
* Usa a fórmula: Nova Média = (Média Antiga * (N-1) + Novo Valor) / N
*
* @param newTime tempo de espera (em segundos) do veículo que acabou de partir
*/
public void updateAverageWaitingTime(double newTime) {
if (totalVehiclesSent > 0) {
averageWaitingTime = (averageWaitingTime * (totalVehiclesSent - 1) + newTime)
/ totalVehiclesSent;
} else if (totalVehiclesSent == 1) {
averageWaitingTime = newTime;
}
}
/**
* @return representação textual do estado atual da interseção
*/
@Override
public String toString() {
return String.format(
"Intersection{id='%s', lights=%d, queues=%d, received=%d, sent=%d}",
id,
trafficLights.size(),
getTotalQueueSize(),
totalVehiclesReceived,
totalVehiclesSent
);
}
}

View File

@@ -0,0 +1,157 @@
package sd.model;
import java.util.UUID;
import sd.protocol.MessageProtocol;
/**
* Envelope fundamental do protocolo de comunicação entre processos distribuídos (IPC).
* <p>
* Esta classe atua como a Unidade de Dados de Aplicação (ADU), encapsulando tanto
* os metadados de roteamento (origem, destino, tipo) quanto a carga útil (payload)
* polimórfica. É agnóstica ao conteúdo, servindo como contentor genérico para
* transferência de estado (Veículos, Estatísticas) ou sinais de controlo (Semáforos).
* <p>
* A imutabilidade dos campos (exceto via serialização) garante a integridade da mensagem
* durante o trânsito na rede.
*/
public class Message implements MessageProtocol {
private static final long serialVersionUID = 1L;
/** * Identificador único universal (UUID).
* Essencial para rastreabilidade (tracing), logs de auditoria e mecanismos de deduplicação.
*/
private final String messageId;
/** Discriminador semântico que define como o recetor deve processar o payload. */
private final MessageType type;
/** Identificador lógico do nó emissor (ex: "Cr1", "Coordinator"). */
private final String senderId;
/** * Identificador lógico do nó recetor.
* Se {@code null}, a mensagem deve ser tratada como <b>Broadcast</b>.
*/
private final String destinationId;
/** * Carga útil polimórfica.
* Deve implementar {@link java.io.Serializable} para garantir transmissão correta.
*/
private final Object payload;
/** Marca temporal da criação da mensagem (Unix Timestamp), usada para cálculo de latência de rede. */
private final long timestamp;
/**
* Construtor completo para reconstrução de mensagens ou envio com timestamp manual.
*
* @param type Classificação semântica da mensagem.
* @param senderId ID do processo origem.
* @param destinationId ID do processo destino (ou null para broadcast).
* @param payload Objeto de domínio a ser transportado.
* @param timestamp Instante de criação (ms).
*/
public Message(MessageType type, String senderId, String destinationId,
Object payload, long timestamp) {
this.messageId = UUID.randomUUID().toString();
this.type = type;
this.senderId = senderId;
this.destinationId = destinationId;
this.payload = payload;
this.timestamp = timestamp;
}
/**
* Construtor de conveniência que atribui automaticamente o timestamp atual do sistema.
*
* @param type Classificação semântica.
* @param senderId ID do processo origem.
* @param destinationId ID do processo destino.
* @param payload Objeto de domínio.
*/
public Message(MessageType type, String senderId, String destinationId, Object payload) {
this(type, senderId, destinationId, payload, System.currentTimeMillis());
}
/**
* Construtor de conveniência para mensagens de difusão (Broadcast).
* Define {@code destinationId} como null.
*
* @param type Classificação semântica.
* @param senderId ID do processo origem.
* @param payload Objeto de domínio.
*/
public Message(MessageType type, String senderId, Object payload) {
this(type, senderId, null, payload, System.currentTimeMillis());
}
//Getters
public String getMessageId() {
return messageId;
}
public MessageType getType() {
return type;
}
public String getSenderId() {
return senderId;
}
public String getDestinationId() {
return destinationId;
}
public Object getPayload() {
return payload;
}
public long getTimestamp() {
return timestamp;
}
/**
* Verifica se a mensagem se destina a todos os nós da rede.
*
* @return {@code true} se o destinationId for nulo.
*/
public boolean isBroadcast() {
return destinationId == null;
}
/**
* Utilitário para casting seguro e fluente do payload.
* <p>
* Evita a necessidade de casts explícitos e supressão de warnings no código cliente.
*
* @param <T> O tipo esperado do payload.
* @param clazz A classe do tipo esperado para verificação em runtime (opcional no uso, mas boa prática).
* @return O payload convertido para o tipo T.
* @throws ClassCastException Se o payload não for compatível com o tipo solicitado.
*/
@SuppressWarnings("unchecked")
public <T> T getPayloadAs(Class<T> clazz) {
return (T) payload;
}
// Impl MessageProtocol interface
@Override
public String getSourceNode() {
return senderId;
}
@Override
public String getDestinationNode() {
return destinationId;
}
@Override
public String toString() {
return String.format("Message[id=%s, type=%s, from=%s, to=%s, timestamp=%d]",
messageId, type, senderId,
destinationId != null ? destinationId : "BROADCAST",
timestamp);
}
}

View File

@@ -0,0 +1,49 @@
package sd.model;
/**
* Enumeração que representa todos os tipos de mensagens possíveis para
* comunicação distribuída.
* Estes tipos são usados para a comunicação entre processos dos diferentes
* componentes
* do sistema de simulação de tráfego distribuído.
*/
public enum MessageType {
/**
* Mensagem para transferir um veículo entre interseções ou processos.
* Payload: Objeto Vehicle com o estado atual
*/
VEHICLE_TRANSFER,
/**
* Mensagem para atualizar estatísticas em todo o sistema distribuído.
* Payload: Dados estatísticos (tempos de espera, tamanhos de fila, etc.)
*/
STATS_UPDATE,
/**
* Mensagem para sincronizar a hora de início da simulação em todos os
* processos.
* Payload: Timestamp de início (long milissegundos)
*/
SIMULATION_START,
/**
* Mensagem para notificar sobre a geração de um novo veículo.
* Payload: Parâmetros de geração do veículo
*/
VEHICLE_SPAWN,
/**
* Mensagem para sinalizar o encerramento de um processo.
* Payload: ID do processo e motivo
*/
SHUTDOWN,
/**
* Mensagem para alterar a política de roteamento durante a simulação.
* Payload: String com o nome da nova política (RANDOM, SHORTEST_PATH, LEAST_CONGESTED)
*/
ROUTING_POLICY_CHANGE,
}

View File

@@ -0,0 +1,269 @@
package sd.model;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Representa um semáforo numa interseção.
*
* <p>Cada semáforo controla uma direção específica e mantém uma fila de veículos à espera.
* Alterna entre os estados VERDE e VERMELHO de acordo com a temporização configurada.</p>
*
* <p><strong>Thread-safety:</strong> Usa locks para permitir acesso concorrente seguro entre
* a thread de processamento de eventos e as threads de I/O de rede.</p>
*/
public class TrafficLight {
/** Identificador único do semáforo (ex: "Cr1-N") */
private final String id;
/** Direção que este semáforo controla (ex: "Norte", "Sul") */
private final String direction;
/** Estado atual do semáforo (VERDE ou VERMELHO) */
private TrafficLightState state;
/** Fila de veículos à espera neste semáforo */
private final Queue<Vehicle> queue;
/**
* Lock para proteger o estado mutável ({@link #queue} e {@link #state})
* de acesso concorrente.
*/
private final Lock lock;
/** Variável de condição para sinalizar adição de veículos (uso futuro) */
private final Condition vehicleAdded;
/** Variável de condição para sinalizar que o semáforo ficou verde (uso futuro) */
private final Condition lightGreen;
/** Duração (segundos) que o semáforo permanece VERDE */
private double greenTime;
/** Duração (segundos) que o semáforo permanece VERMELHO */
private double redTime;
/** Número total de veículos processados por este semáforo */
private int totalVehiclesProcessed;
/**
* Regista quando os veículos chegam ao semáforo para cálculo do tempo de espera.
* Mapeia ID do veículo para tempo de simulação de chegada (segundos).
*/
private final Map<String, Double> vehicleArrivalTimes;
/**
* Cria um novo semáforo.
*
* @param id identificador único (ex: "Cr1-N")
* @param direction direção controlada (ex: "Norte")
* @param greenTime duração do estado VERDE em segundos
* @param redTime duração do estado VERMELHO em segundos
*/
public TrafficLight(String id, String direction, double greenTime, double redTime) {
this.id = id;
this.direction = direction;
this.state = TrafficLightState.RED;
this.queue = new LinkedList<>();
this.lock = new ReentrantLock();
this.vehicleAdded = lock.newCondition();
this.lightGreen = lock.newCondition();
this.greenTime = greenTime;
this.redTime = redTime;
this.vehicleArrivalTimes = new HashMap<>();
this.totalVehiclesProcessed = 0;
}
/**
* Coloca um veículo na fila deste semáforo.
*
* Registamos a hora de chegada para podermos calcular mais tarde quanto tempo o
* veículo esperou.
*
* @param vehicle O veículo que chega ao semáforo.
* @param simulationTime O tempo de simulação atual (em segundos).
*/
public void addVehicle(Vehicle vehicle, double simulationTime) {
lock.lock();
try {
queue.offer(vehicle);
vehicleArrivalTimes.put(vehicle.getId(), simulationTime);
vehicleAdded.signalAll();
} finally {
lock.unlock();
}
}
/**
* Remove um veículo da fila para travessia.
*
* <p>Só remove se:</p>
* <ul>
* <li>O semáforo estiver VERDE</li>
* <li>Existir pelo menos um veículo na fila</li>
* </ul>
*
* <p>Atualiza automaticamente as estatísticas de tempo de espera do veículo.</p>
*
* @param simulationTime O tempo de simulação atual (em segundos).
* @return o veículo que vai atravessar, ou null se não for possível
*/
public Vehicle removeVehicle(double simulationTime) {
lock.lock();
try {
if (state == TrafficLightState.GREEN && !queue.isEmpty()) {
Vehicle vehicle = queue.poll();
if (vehicle != null) {
totalVehiclesProcessed++;
Double arrivalTime = vehicleArrivalTimes.remove(vehicle.getId());
if (arrivalTime != null) {
double waitTimeSeconds = simulationTime - arrivalTime;
vehicle.addWaitingTime(waitTimeSeconds);
}
}
return vehicle;
}
return null;
} finally {
lock.unlock();
}
}
/**
* Muda o estado do semáforo.
*
* @param newState novo estado (VERDE ou VERMELHO)
*/
public void changeState(TrafficLightState newState) {
lock.lock();
try {
this.state = newState;
if (newState == TrafficLightState.GREEN) {
lightGreen.signalAll();
}
} finally {
lock.unlock();
}
}
/**
* Retorna quantos veículos estão atualmente na fila.
* Método thread-safe.
*
* @return tamanho da fila
*/
public int getQueueSize() {
lock.lock();
try {
return queue.size();
} finally {
lock.unlock();
}
}
/**
* Verifica se a fila está vazia.
* Método thread-safe.
*
* @return {@code true} se não houver veículos, {@code false} caso contrário
*/
public boolean isQueueEmpty() {
lock.lock();
try {
return queue.isEmpty();
} finally {
lock.unlock();
}
}
/** @return identificador único do semáforo */
public String getId() {
return id;
}
/** @return direção controlada por este semáforo */
public String getDirection() {
return direction;
}
/**
* Obtém o estado atual do semáforo.
* Método thread-safe.
*
* @return estado atual (VERDE ou VERMELHO)
*/
public TrafficLightState getState() {
lock.lock();
try {
return state;
} finally {
lock.unlock();
}
}
/** @return duração configurada do sinal verde em segundos */
public double getGreenTime() {
return greenTime;
}
/**
* Define a duração do sinal verde.
*
* @param greenTime nova duração em segundos
*/
public void setGreenTime(double greenTime) {
this.greenTime = greenTime;
}
/** @return duração configurada do sinal vermelho em segundos */
public double getRedTime() {
return redTime;
}
/**
* Define a duração do sinal vermelho.
*
* @param redTime nova duração em segundos
*/
public void setRedTime(double redTime) {
this.redTime = redTime;
}
/** @return número total de veículos processados por este semáforo */
public int getTotalVehiclesProcessed() {
return totalVehiclesProcessed;
}
/** @return objeto {@link Lock} para sincronização avançada */
public Lock getLock() {
return lock;
}
/** @return condição para adição de veículos */
public Condition getVehicleAdded() {
return vehicleAdded;
}
/** @return condição para semáforo ficar verde */
public Condition getLightGreen() {
return lightGreen;
}
/** @return representação textual do estado atual do semáforo */
@Override
public String toString() {
return String.format(
"TrafficLight{id='%s', direction='%s', state=%s, queueSize=%d}",
id, direction, getState(), getQueueSize()
);
}
}

View File

@@ -0,0 +1,13 @@
package sd.model;
/**
* Estados possíveis de um semáforo ({@link TrafficLight}).
*/
public enum TrafficLightState {
/** Sinal verde - veículos podem passar */
GREEN,
/** Sinal vermelho - veículos aguardam na fila */
RED
}

View File

@@ -0,0 +1,183 @@
package sd.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Representa um veículo que se move pela rede de interseções.
*
* <p>
* Esta classe é o "gémeo digital" de um carro, mota ou camião.
* Mantém toda a informação necessária:
* </p>
* <ul>
* <li>Identificação e tipo do veículo</li>
* <li>Rota completa a percorrer</li>
* <li>Métricas de tempo (espera, travessia, total)</li>
* </ul>
*
* <p>
* O objeto é serializado e enviado pela rede à medida que o veículo
* se move entre processos distribuídos.
* </p>
*/
public class Vehicle implements Serializable {
private static final long serialVersionUID = 1L;
/** Identificador único do veículo (ex: "V1", "V2") */
private final String id;
/** Tipo de veículo (BIKE, LIGHT, HEAVY) */
private final VehicleType type;
/** Tempo de simulação (em segundos) em que o veículo foi gerado */
private final double entryTime;
/**
* Lista ordenada completa de destinos (IDs de interseções e saída "S").
* Exemplo: ["Cr1", "Cr3", "S"]
*/
private final List<String> route;
/**
* Índice que acompanha o progresso do veículo ao longo da {@link #route}.
* {@code route.get(currentRouteIndex)} é o destino *atual* do veículo.
*/
private int currentRouteIndex;
/**
* Tempo total acumulado (segundos) que o veículo passou à espera em semáforos
* vermelhos
*/
private double totalWaitingTime;
/**
* Tempo total acumulado (segundos) que o veículo passou a atravessar
* interseções
*/
private double totalCrossingTime;
/**
* Cria um novo veículo pronto para se fazer à estrada.
*
* @param id Identificador único (ex: "V1").
* @param type O tipo de veículo (determina velocidade/tamanho).
* @param entryTime Quando este veículo entrou na simulação (segundos).
* @param route A lista ordenada de paragens (Interseções -> Saída).
*/
public Vehicle(String id, VehicleType type, double entryTime, List<String> route) {
this.id = id;
this.type = type;
this.entryTime = entryTime;
this.route = new ArrayList<>(route);
this.currentRouteIndex = 0;
this.totalWaitingTime = 0.0;
this.totalCrossingTime = 0.0;
}
/**
* Move o GPS interno do veículo para o próximo destino.
*
* Chame isto quando um veículo chega a uma interseção para atualizar para onde
* deve ir a seguir.
*
* @return true se houver mais paragens, false se a viagem terminou.
*/
public boolean advanceRoute() {
currentRouteIndex++;
return currentRouteIndex < route.size();
}
/**
* Obtém o destino atual (próxima interseção ou saída) para onde o veículo se
* dirige.
*
* @return ID do destino atual (ex: "Cr1"), ou {@code null} se a rota terminou
*/
public String getCurrentDestination() {
return (currentRouteIndex < route.size()) ? route.get(currentRouteIndex) : null;
}
/**
* Verifica se o veículo completou toda a sua rota.
*
* @return {@code true} se chegou ao fim da rota, {@code false} caso contrário
*/
public boolean hasReachedEnd() {
return currentRouteIndex >= route.size();
}
/** @return identificador único do veículo */
public String getId() {
return id;
}
/** @return tipo do veículo */
public VehicleType getType() {
return type;
}
/** @return tempo de simulação em que o veículo entrou no sistema */
public double getEntryTime() {
return entryTime;
}
/** @return cópia da rota completa do veículo */
public List<String> getRoute() {
return new ArrayList<>(route);
}
/** @return índice atual apontando para o destino do veículo na sua rota */
public int getCurrentRouteIndex() {
return currentRouteIndex;
}
/** @return tempo total acumulado de espera em segundos */
public double getTotalWaitingTime() {
return totalWaitingTime;
}
/**
* Adiciona uma duração ao tempo total de espera do veículo.
* Chamado quando um veículo começa a atravessar uma interseção.
*
* @param time duração (em segundos) a adicionar
*/
public void addWaitingTime(double time) {
totalWaitingTime += time;
}
/** @return tempo total acumulado de travessia em segundos */
public double getTotalCrossingTime() {
return totalCrossingTime;
}
/**
* Adiciona uma duração ao tempo total de travessia do veículo.
* Chamado quando um veículo termina de atravessar uma interseção.
*
* @param time duração (em segundos) a adicionar
*/
public void addCrossingTime(double time) {
totalCrossingTime += time;
}
/**
* Calcula o tempo total que o veículo passou no sistema até agora.
*
* @param currentTime tempo atual da simulação
* @return tempo total decorrido (em segundos) desde que o veículo foi gerado
*/
public double getTotalTravelTime(double currentTime) {
return currentTime - entryTime;
}
/** @return representação textual do estado atual do veículo */
@Override
public String toString() {
return String.format(
"Vehicle{id='%s', type=%s, next='%s', route=%s}",
id, type, getCurrentDestination(), route);
}
}

View File

@@ -0,0 +1,19 @@
package sd.model;
/**
* Enumeração dos diferentes tipos de veículos na simulação.
*
* <p>Cada tipo pode ter propriedades diferentes como tempo de travessia
* e probabilidade de geração, definidas na {@link sd.config.SimulationConfig}.</p>
*/
public enum VehicleType {
/** Bicicleta ou motocicleta - tempo de travessia curto */
BIKE,
/** Veículo ligeiro padrão (carro) - tipo mais comum */
LIGHT,
/** Veículo pesado (camião ou autocarro) - tempo de travessia longo */
HEAVY
}

View File

@@ -0,0 +1,45 @@
package sd.protocol;
import java.io.Serializable;
import sd.model.MessageType;
/**
* Contrato para todas as mensagens trocadas no simulador.
*
* <p>Garante que mensagens podem ser identificadas e encaminhadas.
* Extende Serializable para permitir envio via sockets.
*/
public interface MessageProtocol extends Serializable {
/**
* Tipo da mensagem, indicando o seu propósito.
* @return tipo (ex: VEHICLE_TRANSFER, STATS_UPDATE)
*/
MessageType getType();
/**
* Dados (payload) que esta mensagem transporta.
*
* <p>Tipo depende do MessageType:
* <ul>
* <li>VEHICLE_TRANSFER → objeto Vehicle
* <li>STATS_UPDATE → objeto de estatísticas
* </ul>
*
* @return payload (deve ser Serializable)
*/
Object getPayload();
/**
* ID do nó (processo) que enviou a mensagem.
* @return ID de origem (ex: "Cr1", "Cr5", "S")
*/
String getSourceNode();
/**
* ID do nó de destino.
* @return ID de destino (ex: "Cr2", "DashboardServer")
*/
String getDestinationNode();
}

View File

@@ -0,0 +1,232 @@
package sd.protocol;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import sd.serialization.MessageSerializer;
import sd.serialization.SerializationException;
import sd.serialization.SerializerFactory;
/**
* Wrapper de alto nível para gestão robusta de conexões TCP.
* <p>
* Esta classe abstrai a complexidade da API nativa {@link java.net.Socket}, oferecendo:
* <ol>
* <li><b>Resiliência:</b> Lógica de reconexão automática (Retry Loop) no arranque, crucial para sistemas
* distribuídos onde a ordem de inicialização dos nós não é garantida.</li>
* <li><b>Framing:</b> Implementação transparente do protocolo "Length-Prefix" (4 bytes de tamanho + payload),
* resolvendo o problema de fragmentação de stream TCP.</li>
* <li><b>Serialização:</b> Integração direta com a camada de serialização JSON.</li>
* </ol>
*/
public class SocketConnection implements Closeable {
// --- Network Resources ---
/**
* The underlying TCP socket used for network communication.
*/
private final Socket socket;
/**
* The raw output stream for writing bytes to the network.
* Wrapped by {@link DataOutputStream} during message sending.
*/
private final OutputStream outputStream;
/**
* The raw input stream for reading bytes from the network.
* Wrapped by {@link DataInputStream} during message reception.
*/
private final InputStream inputStream;
// --- Serialization ---
/**
* The serializer strategy used to convert objects to/from byte arrays (e.g., JSON).
*/
private final MessageSerializer serializer;
/** Número máximo de tentativas de ligação antes de desistir (Fail-fast). */
private static final int MAX_RETRIES = 5;
/** Janela de espera (backoff) linear entre tentativas (em milissegundos). */
private static final long RETRY_DELAY_MS = 1000;
/**
* Construtor para clientes (Active Open).
* Tenta estabelecer uma conexão TCP com um servidor, aplicando lógica de retry.
* <p>
* Este comportamento é vital quando o processo Coordenador inicia antes das Interseções estarem
* prontas para aceitar conexões ({@code accept()}).
*
* @param host Endereço do nó de destino (ex: "localhost").
* @param port Porta de serviço.
* @throws IOException Se a conexão falhar após todas as {@code MAX_RETRIES} tentativas.
* @throws UnknownHostException Se o DNS não resolver o hostname.
* @throws InterruptedException Se a thread for interrompida durante o sleep de retry.
*/
public SocketConnection(String host, int port) throws IOException, UnknownHostException, InterruptedException {
Socket tempSocket = null;
IOException lastException = null;
System.out.printf("[SocketConnection] Attempting to connect to %s:%d...%n", host, port);
// --- Retry Loop ---
for (int attempt = 1; attempt <= MAX_RETRIES; attempt++) {
try {
// Try to establish the connection (SYN -> SYN-ACK -> ACK)
tempSocket = new Socket(host, port);
// If successful, break out of the retry loop
System.out.printf("[SocketConnection] Connected successfully on attempt %d.%n", attempt);
lastException = null; // Clear last error on success
break;
} catch (ConnectException | SocketTimeoutException e) {
// Common errors: "Connection refused" (server not up) or "Timeout" (firewall/network)
lastException = e;
System.out.printf("[SocketConnection] Attempt %d/%d failed: %s. Retrying in %d ms...%n",
attempt, MAX_RETRIES, e.getMessage(), RETRY_DELAY_MS);
if (attempt < MAX_RETRIES) {
// Blocking wait before next attempt
TimeUnit.MILLISECONDS.sleep(RETRY_DELAY_MS);
}
} catch (IOException e) {
// Other IO errors
lastException = e;
System.out.printf("[SocketConnection] Attempt %d/%d failed with IOException: %s. Retrying in %d ms...%n",
attempt, MAX_RETRIES, e.getMessage(), RETRY_DELAY_MS);
if (attempt < MAX_RETRIES) {
TimeUnit.MILLISECONDS.sleep(RETRY_DELAY_MS);
}
}
} // --- End of Retry Loop ---
// Final validation
if (tempSocket == null) {
System.err.printf("[SocketConnection] Failed to connect to %s:%d after %d attempts.%n", host, port, MAX_RETRIES);
if (lastException != null) {
throw lastException; // Propagate the root cause
} else {
throw new IOException("Failed to connect after " + MAX_RETRIES + " attempts, reason unknown.");
}
}
// Initialize streams
this.socket = tempSocket;
this.outputStream = socket.getOutputStream();
this.inputStream = socket.getInputStream();
this.serializer = SerializerFactory.createDefault();
}
/**
* Construtor para servidores (Passive Open).
* Envolve um socket já conectado (retornado por {@code serverSocket.accept()}).
* Não necessita de retry logic pois a conexão física já existe.
*
* @param acceptedSocket O socket ativo retornado pelo SO.
* @throws IOException Se falhar a obtenção dos streams de I/O.
*/
public SocketConnection(Socket acceptedSocket) throws IOException {
this.socket = acceptedSocket;
this.outputStream = socket.getOutputStream();
this.inputStream = socket.getInputStream();
this.serializer = SerializerFactory.createDefault();
}
/**
* Serializa e transmite uma mensagem através do canal.
* <p>
* Utiliza sincronização ({@code synchronized}) para garantir que escritas concorrentes
* na mesma conexão não corrompem a stream de bytes (thread-safety).
*
* @param message O objeto de protocolo a enviar.
* @throws IOException Se o socket estiver fechado ou ocorrer erro de escrita.
*/
public synchronized void sendMessage(MessageProtocol message) throws IOException {
if (socket == null || !socket.isConnected()) {
throw new IOException("Socket is not connected");
}
try {
// Serializa para bytes JSON
byte[] data = serializer.serialize(message);
// Write 4-byte length prefix (Framing)
DataOutputStream dataOut = new DataOutputStream(outputStream);
dataOut.writeInt(data.length);
dataOut.write(data);
dataOut.flush(); // Force transmission immediately
} catch (SerializationException e) {
throw new IOException("Failed to serialize message", e);
}
}
/**
* Bloqueia à espera de uma mensagem completa do socket.
* <p>
* Lê primeiro o cabeçalho de tamanho (4 bytes) e depois o payload exato,
* garantindo que processa mensagens completas mesmo se chegarem fragmentadas em múltiplos pacotes TCP.
*
* @return O objeto {@link MessageProtocol} reconstruído.
* @throws IOException Se a conexão for perdida (EOF) ou o stream corrompido.
* @throws ClassNotFoundException Se o tipo desserializado não for encontrado no classpath.
*/
public MessageProtocol receiveMessage() throws IOException, ClassNotFoundException {
if (socket == null || !socket.isConnected()) {
throw new IOException("Socket is not connected");
}
try {
DataInputStream dataIn = new DataInputStream(inputStream);
int length = dataIn.readInt();
// Sanity check para evitar OutOfMemory em caso de corrupção de stream
if (length <= 0 || length > 10_000_000) { // Max 10MB payload
throw new IOException("Invalid message length: " + length);
}
// Ler dados exatos da mensagem
byte[] data = new byte[length];
dataIn.readFully(data);
// Deserialize do JSON - força o tipo concreto Message
return serializer.deserialize(data, sd.model.Message.class);
} catch (SerializationException e) {
throw new IOException("Failed to deserialize message", e);
}
}
/**
* Encerra a conexão e liberta os descritores de ficheiro.
* Operação idempotente.
*/
@Override
public void close() throws IOException {
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
if (socket != null) socket.close();
}
/**
* Verifica o estado operacional da conexão.
* @return true se o socket está aberto e conectado.
*/
public boolean isConnected() {
return socket != null && socket.isConnected() && !socket.isClosed();
}
}

View File

@@ -0,0 +1,151 @@
package sd.routing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Implementação da política de roteamento por menor congestionamento.
*
* <p>Esta política escolhe dinamicamente a rota que passa pelos cruzamentos
* menos congestionados, com base no tamanho atual das filas em cada interseção.
* É uma política dinâmica que adapta as decisões ao estado da rede.</p>
*
* <p>Objetivo: Distribuir o tráfego pela rede, evitando bottlenecks e
* minimizando o tempo de espera total.</p>
*
* <p><strong>Algoritmo:</strong></p>
* <ol>
* <li>Para cada rota possível, calcula a carga total (soma das filas)</li>
* <li>Escolhe a rota com menor carga total</li>
* <li>Em caso de empate ou falta de informação, usa a rota mais curta</li>
* </ol>
*/
public class LeastCongestedRouteSelector implements RouteSelector {
/** Rotas possíveis a partir do ponto de entrada E1 */
private final List<List<String>> e1Routes;
/** Rotas possíveis a partir do ponto de entrada E2 */
private final List<List<String>> e2Routes;
/** Rotas possíveis a partir do ponto de entrada E3 */
private final List<List<String>> e3Routes;
/**
* Cria um novo seletor de rotas baseado em menor congestionamento.
*/
public LeastCongestedRouteSelector() {
this.e1Routes = new ArrayList<>();
this.e2Routes = new ArrayList<>();
this.e3Routes = new ArrayList<>();
initializeRoutes();
}
/**
* Inicializa as rotas possíveis para cada ponto de entrada.
*/
private void initializeRoutes() {
// Rotas de E1 (entrada Norte)
e1Routes.add(Arrays.asList("Cr1", "Cr4", "Cr5", "S"));
e1Routes.add(Arrays.asList("Cr1", "Cr2", "Cr5", "S"));
e1Routes.add(Arrays.asList("Cr1", "Cr2", "Cr3", "S"));
// Rotas de E2 (entrada Oeste)
e2Routes.add(Arrays.asList("Cr2", "Cr5", "S"));
e2Routes.add(Arrays.asList("Cr2", "Cr3", "S"));
e2Routes.add(Arrays.asList("Cr2", "Cr1", "Cr4", "Cr5", "S"));
// Rotas de E3 (entrada Sul)
e3Routes.add(Arrays.asList("Cr3", "S"));
e3Routes.add(Arrays.asList("Cr3", "Cr2", "Cr5", "S"));
e3Routes.add(Arrays.asList("Cr3", "Cr2", "Cr1", "Cr4", "Cr5", "S"));
}
@Override
public List<String> selectRoute(String entryPoint, Map<String, Integer> queueSizes) {
List<List<String>> availableRoutes = getRoutesForEntryPoint(entryPoint);
// Se não temos informação sobre filas, usa a rota mais curta como fallback
if (queueSizes == null || queueSizes.isEmpty()) {
return selectShortestRoute(availableRoutes);
}
// Calcula a carga de cada rota e escolhe a menos congestionada
List<String> bestRoute = null;
int minLoad = Integer.MAX_VALUE;
for (List<String> route : availableRoutes) {
int routeLoad = calculateRouteLoad(route, queueSizes);
if (routeLoad < minLoad) {
minLoad = routeLoad;
bestRoute = route;
}
}
// Fallback: se não conseguimos calcular carga, usa a primeira rota
if (bestRoute == null) {
bestRoute = availableRoutes.get(0);
}
return new ArrayList<>(bestRoute);
}
/**
* Calcula a carga total de uma rota (soma do tamanho das filas em todos os cruzamentos).
*
* @param route rota a avaliar
* @param queueSizes mapa com tamanho das filas por interseção
* @return carga total da rota (soma das filas)
*/
private int calculateRouteLoad(List<String> route, Map<String, Integer> queueSizes) {
int totalLoad = 0;
for (String intersection : route) {
// Ignora "S" (saída) e apenas conta cruzamentos reais
if (!intersection.equals("S") && queueSizes.containsKey(intersection)) {
totalLoad += queueSizes.get(intersection);
}
}
return totalLoad;
}
/**
* Seleciona a rota mais curta (menor número de nós) como fallback.
*
* @param routes lista de rotas disponíveis
* @return a rota mais curta
*/
private List<String> selectShortestRoute(List<List<String>> routes) {
List<String> shortest = routes.get(0);
for (List<String> route : routes) {
if (route.size() < shortest.size()) {
shortest = route;
}
}
return new ArrayList<>(shortest);
}
/**
* Obtém as rotas disponíveis para um ponto de entrada.
*
* @param entryPoint ponto de entrada (E1, E2 ou E3)
* @return lista de rotas disponíveis
*/
private List<List<String>> getRoutesForEntryPoint(String entryPoint) {
switch (entryPoint.toUpperCase()) {
case "E1":
return e1Routes;
case "E2":
return e2Routes;
case "E3":
return e3Routes;
default:
System.err.printf("Unknown entry point: %s, defaulting to E1%n", entryPoint);
return e1Routes;
}
}
}

View File

@@ -0,0 +1,122 @@
package sd.routing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Implementação da política de roteamento aleatória (baseline).
*
* <p>Esta política seleciona rotas com base em probabilidades predefinidas,
* sem considerar o estado atual da rede. É a implementação de referência
* para comparação com outras políticas.</p>
*
* <p>As rotas são organizadas por ponto de entrada (E1, E2, E3) e cada rota
* tem uma probabilidade de seleção associada.</p>
*/
public class RandomRouteSelector implements RouteSelector {
/** Rotas possíveis a partir do ponto de entrada E1 */
private final List<RouteWithProbability> e1Routes;
/** Rotas possíveis a partir do ponto de entrada E2 */
private final List<RouteWithProbability> e2Routes;
/** Rotas possíveis a partir do ponto de entrada E3 */
private final List<RouteWithProbability> e3Routes;
/**
* Cria um novo seletor de rotas aleatórias com rotas predefinidas.
*/
public RandomRouteSelector() {
this.e1Routes = new ArrayList<>();
this.e2Routes = new ArrayList<>();
this.e3Routes = new ArrayList<>();
initializePossibleRoutes();
}
/**
* Define todas as rotas possíveis que os veículos podem tomar.
* As rotas são organizadas por ponto de entrada (E1, E2, E3).
* Cada rota tem uma probabilidade que determina a frequência com que é escolhida.
*/
private void initializePossibleRoutes() {
// Rotas de E1 (entrada Norte)
e1Routes.add(new RouteWithProbability(
Arrays.asList("Cr1", "Cr4", "Cr5", "S"), 0.34));
e1Routes.add(new RouteWithProbability(
Arrays.asList("Cr1", "Cr2", "Cr5", "S"), 0.33));
e1Routes.add(new RouteWithProbability(
Arrays.asList("Cr1", "Cr2", "Cr3", "S"), 0.33));
// Rotas de E2 (entrada Oeste)
e2Routes.add(new RouteWithProbability(
Arrays.asList("Cr2", "Cr5", "S"), 0.34));
e2Routes.add(new RouteWithProbability(
Arrays.asList("Cr2", "Cr3", "S"), 0.33));
e2Routes.add(new RouteWithProbability(
Arrays.asList("Cr2", "Cr1", "Cr4", "Cr5", "S"), 0.33));
// Rotas de E3 (entrada Sul)
e3Routes.add(new RouteWithProbability(
Arrays.asList("Cr3", "S"), 0.34));
e3Routes.add(new RouteWithProbability(
Arrays.asList("Cr3", "Cr2", "Cr5", "S"), 0.33));
e3Routes.add(new RouteWithProbability(
Arrays.asList("Cr3", "Cr2", "Cr1", "Cr4", "Cr5", "S"), 0.33));
}
@Override
public List<String> selectRoute(String entryPoint, Map<String, Integer> queueSizes) {
// Ignora queueSizes - seleção aleatória não depende do estado da rede
List<RouteWithProbability> selectedRoutes = getRoutesForEntryPoint(entryPoint);
// Seleciona uma rota baseada em probabilidades cumulativas
double rand = Math.random();
double cumulative = 0.0;
for (RouteWithProbability routeWithProb : selectedRoutes) {
cumulative += routeWithProb.probability;
if (rand <= cumulative) {
// Retorna uma cópia da rota para prevenir modificações
return new ArrayList<>(routeWithProb.route);
}
}
// Fallback: retorna a primeira rota
return new ArrayList<>(selectedRoutes.get(0).route);
}
/**
* Obtém as rotas disponíveis para um ponto de entrada.
*
* @param entryPoint ponto de entrada (E1, E2 ou E3)
* @return lista de rotas com probabilidades
*/
private List<RouteWithProbability> getRoutesForEntryPoint(String entryPoint) {
switch (entryPoint.toUpperCase()) {
case "E1":
return e1Routes;
case "E2":
return e2Routes;
case "E3":
return e3Routes;
default:
System.err.printf("Unknown entry point: %s, defaulting to E1%n", entryPoint);
return e1Routes;
}
}
/**
* Classe interna para associar uma rota com sua probabilidade de seleção.
*/
private static class RouteWithProbability {
final List<String> route;
final double probability;
RouteWithProbability(List<String> route, double probability) {
this.route = route;
this.probability = probability;
}
}
}

View File

@@ -0,0 +1,25 @@
package sd.routing;
import java.util.List;
import java.util.Map;
/**
* Interface para implementação de políticas de seleção de rotas.
*
* <p>Define o contrato que todas as políticas de roteamento devem seguir.
* Permite a implementação de diferentes estratégias de roteamento
* (aleatória, caminho mais curto, menor congestionamento, etc.).</p>
*/
public interface RouteSelector {
/**
* Seleciona uma rota para um veículo a partir de um ponto de entrada.
*
* @param entryPoint ponto de entrada (E1, E2 ou E3)
* @param queueSizes mapa com o tamanho das filas em cada interseção (opcional, pode ser null).
* Chave: ID da interseção (ex: "Cr1", "Cr2")
* Valor: número total de veículos em espera nessa interseção
* @return lista de IDs representando a rota escolhida (ex: ["Cr1", "Cr2", "Cr5", "S"])
*/
List<String> selectRoute(String entryPoint, Map<String, Integer> queueSizes);
}

View File

@@ -0,0 +1,36 @@
package sd.routing;
/**
* Enumeração que define as políticas de roteamento disponíveis para a simulação.
*
* <p>As políticas de roteamento determinam como os veículos escolhem o caminho
* a seguir desde o ponto de entrada até à saída da rede de interseções.</p>
*
* <ul>
* <li><strong>RANDOM:</strong> Seleção aleatória de rotas baseada em probabilidades predefinidas</li>
* <li><strong>SHORTEST_PATH:</strong> Escolhe sempre a rota com o menor número de cruzamentos</li>
* <li><strong>LEAST_CONGESTED:</strong> Escolhe a rota evitando cruzamentos mais congestionados</li>
* </ul>
*/
public enum RoutingPolicy {
/**
* Política aleatória (baseline).
* Seleciona rotas com base em probabilidades predefinidas, sem considerar
* o estado atual da rede.
*/
RANDOM,
/**
* Política do caminho mais curto.
* Sempre escolhe a rota com o menor número de cruzamentos entre o ponto
* de entrada e a saída, minimizando a distância teórica.
*/
SHORTEST_PATH,
/**
* Política das menores filas (roteamento dinâmico).
* Escolhe a rota que passa pelos cruzamentos menos congestionados,
* com base no tamanho atual das filas em cada interseção.
*/
LEAST_CONGESTED
}

View File

@@ -0,0 +1,89 @@
package sd.routing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Implementação da política de roteamento por caminho mais curto.
*
* <p>Esta política sempre escolhe a rota com o menor número de cruzamentos
* entre o ponto de entrada e a saída. É uma política determinística que
* não considera o estado da rede (tamanho das filas).</p>
*
* <p>Objetivo: Minimizar a distância teórica percorrida pelos veículos.</p>
*/
public class ShortestPathRouteSelector implements RouteSelector {
/** Rotas possíveis a partir do ponto de entrada E1, ordenadas por comprimento */
private final List<List<String>> e1Routes;
/** Rotas possíveis a partir do ponto de entrada E2, ordenadas por comprimento */
private final List<List<String>> e2Routes;
/** Rotas possíveis a partir do ponto de entrada E3, ordenadas por comprimento */
private final List<List<String>> e3Routes;
/**
* Cria um novo seletor de rotas por caminho mais curto.
* As rotas são ordenadas por comprimento (número de cruzamentos).
*/
public ShortestPathRouteSelector() {
this.e1Routes = new ArrayList<>();
this.e2Routes = new ArrayList<>();
this.e3Routes = new ArrayList<>();
initializeRoutes();
}
/**
* Inicializa as rotas possíveis para cada ponto de entrada.
* As rotas são organizadas da mais curta para a mais longa.
*/
private void initializeRoutes() {
// Rotas de E1 (entrada Norte) - ordenadas por comprimento
e1Routes.add(Arrays.asList("Cr1", "Cr2", "Cr3", "S")); // 4 nós
e1Routes.add(Arrays.asList("Cr1", "Cr2", "Cr5", "S")); // 4 nós
e1Routes.add(Arrays.asList("Cr1", "Cr4", "Cr5", "S")); // 4 nós
// Rotas de E2 (entrada Oeste) - ordenadas por comprimento
e2Routes.add(Arrays.asList("Cr2", "Cr3", "S")); // 3 nós (mais curta!)
e2Routes.add(Arrays.asList("Cr2", "Cr5", "S")); // 3 nós
e2Routes.add(Arrays.asList("Cr2", "Cr1", "Cr4", "Cr5", "S")); // 5 nós
// Rotas de E3 (entrada Sul) - ordenadas por comprimento
e3Routes.add(Arrays.asList("Cr3", "S")); // 2 nós (mais curta!)
e3Routes.add(Arrays.asList("Cr3", "Cr2", "Cr5", "S")); // 4 nós
e3Routes.add(Arrays.asList("Cr3", "Cr2", "Cr1", "Cr4", "Cr5", "S")); // 6 nós
}
@Override
public List<String> selectRoute(String entryPoint, Map<String, Integer> queueSizes) {
// Ignora queueSizes - política baseada apenas no comprimento do caminho
List<List<String>> availableRoutes = getRoutesForEntryPoint(entryPoint);
// Retorna a rota mais curta (primeira da lista)
List<String> shortestRoute = availableRoutes.get(0);
return new ArrayList<>(shortestRoute);
}
/**
* Obtém as rotas disponíveis para um ponto de entrada.
*
* @param entryPoint ponto de entrada (E1, E2 ou E3)
* @return lista de rotas ordenadas por comprimento
*/
private List<List<String>> getRoutesForEntryPoint(String entryPoint) {
switch (entryPoint.toUpperCase()) {
case "E1":
return e1Routes;
case "E2":
return e2Routes;
case "E3":
return e3Routes;
default:
System.err.printf("Unknown entry point: %s, defaulting to E1%n", entryPoint);
return e1Routes;
}
}
}

View File

@@ -0,0 +1,128 @@
package sd.serialization;
import java.nio.charset.StandardCharsets;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
/**
* Implementação baseada em JSON da estratégia {@link MessageSerializer}, utilizando a biblioteca Gson.
* <p>
* Este serializador converte objetos Java para o formato de texto JSON antes da transmissão.
* Oferece várias vantagens técnicas sobre a serialização nativa do Java:
* <ul>
* <li><b>Legibilidade:</b> O formato de texto facilita a depuração (sniffing de rede) sem ferramentas especializadas.</li>
* <li><b>Interoperabilidade:</b> Permite futura integração com componentes não-Java (ex: Dashboards web em JS).</li>
* <li><b>Segurança:</b> Reduz a superfície de ataque para execução remota de código (RCE), pois não desserializa classes arbitrárias, apenas dados.</li>
* </ul>
* <p>
* <b>Thread-Safety:</b> A instância interna do {@code Gson} é imutável e thread-safe, permitindo
* que este serializador seja partilhado entre múltiplas threads (ex: no pool do DashboardServer).
* * @see MessageSerializer
*/
public class JsonMessageSerializer implements MessageSerializer {
private final Gson gson;
private final boolean prettyPrint;
/**
* Cria um novo serializador JSON com configuração otimizada para produção (compacto).
*/
public JsonMessageSerializer() {
this(false);
}
/**
* Cria um novo serializador JSON com formatação opcional.
* * @param prettyPrint Se {@code true}, o JSON gerado incluirá indentação e quebras de linha.
* Útil para debug, mas aumenta significativamente o tamanho do payload.
*/
public JsonMessageSerializer(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
GsonBuilder builder = new GsonBuilder();
if (prettyPrint) {
builder.setPrettyPrinting();
}
// Register custom type adapters here if needed
// builder.registerTypeAdapter(Vehicle.class, new VehicleAdapter());
this.gson = builder.create();
}
/**
* Converte um objeto em memória para um array de bytes JSON (UTF-8).
*
* @param object O objeto a ser serializado.
* @return O payload em bytes pronto para transmissão TCP.
* @throws SerializationException Se o objeto não for compatível com JSON ou ocorrer erro de encoding.
*/
@Override
public byte[] serialize(Object object) throws SerializationException {
if (object == null) {
throw new IllegalArgumentException("Cannot serialize null object");
}
try {
String json = gson.toJson(object);
return json.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
throw new SerializationException(
"Failed to serialize object of type " + object.getClass().getName(), e);
}
}
/**
* Reconstrói um objeto Java a partir de um array de bytes JSON.
* <p>
* Realiza a validação sintática do JSON e a validação de tipo baseada na classe alvo.
*
* @param data O array de bytes recebido da rede.
* @param clazz A classe do objeto esperado (Type Token).
* @return A instância do objeto reconstruído.
* @throws SerializationException Se o JSON for malformado ou incompatível com a classe alvo.
*/
@Override
public <T> T deserialize(byte[] data, Class<T> clazz) throws SerializationException {
if (data == null) {
throw new IllegalArgumentException("Cannot deserialize null data");
}
if (clazz == null) {
throw new IllegalArgumentException("Class type cannot be null");
}
try {
String json = new String(data, StandardCharsets.UTF_8);
return gson.fromJson(json, clazz);
} catch (JsonSyntaxException e) {
throw new SerializationException(
"Failed to parse JSON for type " + clazz.getName(), e);
} catch (Exception e) {
throw new SerializationException(
"Failed to deserialize object of type " + clazz.getName(), e);
}
}
@Override
public String getName() {
return "JSON (Gson)";
}
/**
* Retorna a instância subjacente do Gson para configurações avançadas.
* * @return A instância Gson configurada.
*/
public Gson getGson() {
return gson;
}
/**
* Verifica se a formatação "pretty print" está ativa.
* * @return true se a indentação estiver habilitada.
*/
public boolean isPrettyPrint() {
return prettyPrint;
}
}

View File

@@ -0,0 +1,49 @@
package sd.serialization;
/**
* Interface que define o contrato para estratégias de serialização e desserialização de objetos.
* <p>
* Esta abstração permite desacoplar a camada de transporte (Sockets TCP) da camada de
* apresentação de dados. Ao implementar o padrão <b>Strategy</b>, o sistema ganha flexibilidade
* para alternar entre diferentes formatos de codificação (JSON, Binário Nativo, XML, Protobuf)
* sem necessidade de refatorização da lógica de rede.
* <p>
* <b>Requisitos para Implementações:</b>
* <ul>
* <li><b>Thread-Safety:</b> As implementações devem ser seguras para uso concorrente, dado que
* instâncias únicas podem ser partilhadas por múltiplos <i>ClientHandlers</i>.</li>
* <li><b>Robustez:</b> Falhas de parsing devem resultar em exceções tipificadas ({@link SerializationException}),
* nunca em falhas silenciosas ou estados inconsistentes.</li>
* </ul>
* * @see JsonMessageSerializer
*/
public interface MessageSerializer {
/**
* Converte (Marshals) um objeto em memória para uma sequência de bytes para transmissão.
* * @param object O objeto de domínio a ser serializado (não pode ser nulo).
* @return Um array de bytes contendo a representação codificada do objeto.
* @throws SerializationException Se ocorrer um erro durante a codificação (ex: ciclo de referências).
* @throws IllegalArgumentException Se o objeto fornecido for nulo.
*/
byte[] serialize(Object object) throws SerializationException;
/**
* Reconstrói (Unmarshals) um objeto a partir de uma sequência de bytes.
* * @param <T> O tipo genérico do objeto esperado.
* @param data O array de bytes contendo os dados serializados (não pode ser nulo).
* @param clazz A classe do tipo esperado para verificação e instancialização.
* @return A instância do objeto reconstruído com o seu estado restaurado.
* @throws SerializationException Se os dados estiverem corrompidos ou incompatíveis com a classe alvo.
* @throws IllegalArgumentException Se os dados ou a classe forem nulos.
*/
<T> T deserialize(byte[] data, Class<T> clazz) throws SerializationException;
/**
* Obtém o identificador legível desta estratégia de serialização (ex: "JSON (Gson)", "Native").
* Utilizado primariamente para logging, auditoria e negociação de conteúdo.
* * @return O nome descritivo do serializador.
*/
String getName();
}

View File

@@ -0,0 +1,40 @@
package sd.serialization;
/**
* Exceção verificada (Checked Exception) que sinaliza falhas no processo de transformação de dados.
* <p>
* Esta classe atua como um wrapper unificador para erros ocorridos na camada de serialização,
* abstraindo falhas de baixo nível (como erros de I/O, sintaxe JSON inválida ou incompatibilidade
* de tipos) numa única exceção de domínio. Permite que o código cliente trate falhas de
* protocolo de forma consistente, independentemente da implementação subjacente (Gson, Nativa, etc.).
*/
public class SerializationException extends Exception {
private static final long serialVersionUID = 1L; // Long(64bits) instead of int(32bits)
/**
* Constrói uma nova exceção de serialização com uma mensagem descritiva.
* * @param message A mensagem detalhando o erro.
*/
public SerializationException(String message) {
super(message);
}
/**
* Constrói uma nova exceção encapsulando a causa raiz do problema.
* Útil para preservar a stack trace original de erros de bibliotecas terceiras (ex: Gson).
* * @param message A mensagem detalhando o erro.
* @param cause A exceção original que causou a falha.
*/
public SerializationException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constrói uma nova exceção baseada apenas na causa raiz.
* * @param cause A exceção original.
*/
public SerializationException(Throwable cause) {
super(cause);
}
}

View File

@@ -0,0 +1,64 @@
package sd.serialization;
/**
* Fábrica estática (Factory Pattern) para instanciação controlada de {@link MessageSerializer}.
* <p>
* Esta classe centraliza a criação de estratégias de serialização, garantindo consistência
* de configuração em todo o sistema distribuído. Permite a injeção de configurações via
* Propriedades de Sistema (System Properties), facilitando a alternância entre modos de
* depuração (Pretty Print) e produção (Compacto) sem recompilação.
* <p>
* <b>Exemplo de Uso:</b>
* <pre>
* MessageSerializer serializer = SerializerFactory.createDefault();
* byte[] data = serializer.serialize(myObject);
* </pre>
*/
public class SerializerFactory {
/**
* Chave da propriedade de sistema para ativar a formatação JSON legível (Pretty Print).
* Defina {@code -Dsd.serialization.json.prettyPrint=true} na JVM para ativar.
*/
public static final String JSON_PRETTY_PRINT_PROPERTY = "sd.serialization.json.prettyPrint";
// Default configuration (Production-ready)
private static final boolean DEFAULT_JSON_PRETTY_PRINT = false;
/**
* Construtor privado para prevenir instanciação acidental desta classe utilitária.
*/
private SerializerFactory() {
throw new UnsupportedOperationException("Factory class cannot be instantiated");
}
/**
* Cria um serializador JSON configurado dinamicamente pelo ambiente.
* <p>
* Verifica a propriedade de sistema {@value #JSON_PRETTY_PRINT_PROPERTY}.
* Se não definida, assume o padrão de produção (falso/compacto).
* * @return Uma instância configurada de {@link JsonMessageSerializer}.
*/
public static MessageSerializer createDefault() {
boolean prettyPrint = Boolean.getBoolean(JSON_PRETTY_PRINT_PROPERTY);
return new JsonMessageSerializer(prettyPrint);
}
/**
* Cria um serializador JSON com configuração padrão otimizada (sem indentação).
* Ideal para ambientes de produção onde a largura de banda é prioritária.
* * @return Uma instância compacta de {@link JsonMessageSerializer}.
*/
public static MessageSerializer createSerializer() {
return createSerializer(DEFAULT_JSON_PRETTY_PRINT);
}
/**
* Cria um serializador JSON com configuração explícita de formatação.
* * @param prettyPrint {@code true} para ativar indentação (Debug), {@code false} para compacto.
* @return Uma instância personalizada de {@link JsonMessageSerializer}.
*/
public static MessageSerializer createSerializer(boolean prettyPrint) {
return new JsonMessageSerializer(prettyPrint);
}
}

View File

@@ -0,0 +1,108 @@
package sd.util;
import java.util.Random;
/**
* Utilitário central de geração estocástica para a simulação.
* <p>
* Esta classe fornece primitivas para geração de números pseudo-aleatórios, abstraindo
* a complexidade de distribuições estatísticas.
* <p>
* <b>Funcionalidades Principais:</b>
* <ul>
* <li><b>Modelagem de Poisson:</b> Geração de tempos entre chegadas usando distribuição exponencial inversa.</li>
* <li><b>Amostragem Uniforme:</b> Geração de inteiros e doubles em intervalos fechados/abertos.</li>
* <li><b>Decisão Probabilística:</b> Avaliação de eventos booleanos baseados em pesos (Bernoulli trials).</li>
* <li><b>Determinismo:</b> Suporte a sementes (seeds) manuais para reprodutibilidade exata de cenários de teste.</li>
* </ul>
*/
public class RandomGenerator {
/** * Instância singleton estática do gerador PRNG (Pseudo-Random Number Generator).
* Thread-safe (java.util.Random é sincronizado), embora possa haver contenção em alta concorrência.
*/
private static final Random random = new Random();
/**
* Gera um intervalo de tempo seguindo uma Distribuição Exponencial.
* <p>
* Este método implementa o algoritmo de <i>Inverse Transform Sampling</i> para simular
* um Processo de Poisson homogêneo. É fundamental para modelar a chegada natural de
* veículos, onde eventos independentes ocorrem a uma taxa média constante.
* <p>
* <b>Fórmula Matemática:</b> {@code T = -ln(1 - U) / λ}
* <br>Onde:
* <ul>
* <li>{@code U}: Variável aleatória uniforme no intervalo [0, 1).</li>
* <li>{@code λ (lambda)}: Taxa média de eventos por unidade de tempo (ex: veículos/segundo).</li>
* </ul>
*
* @param lambda A taxa média de chegada (λ > 0).
* @return O intervalo de tempo (delta t) até o próximo evento, em segundos.
*/
public static double generateExponentialInterval(double lambda) {
return Math.log(1 - random.nextDouble()) / -lambda;
}
/**
* Gera um número inteiro uniformemente distribuído no intervalo fechado {@code [min, max]}.
*
* @param min Limite inferior (inclusivo).
* @param max Limite superior (inclusivo).
* @return Um inteiro aleatório I tal que {@code min <= I <= max}.
*/
public static int generateRandomInt(int min, int max) {
return random.nextInt(max - min + 1) + min;
}
/**
* Gera um número de ponto flutuante uniformemente distribuído no intervalo semi-aberto {@code [min, max)}.
*
* @param min Limite inferior (inclusivo).
* @param max Limite superior (exclusivo).
* @return Um double aleatório D tal que {@code min <= D < max}.
*/
public static double generateRandomDouble(double min, double max) {
return min + (max - min) * random.nextDouble();
}
/**
* Realiza um teste de Bernoulli (Sim/Não) com uma probabilidade de sucesso especificada.
* <p>
* Utilizado para decisões de ramificação estocástica (ex: "Este veículo é um camião?").
*
* @param probability A probabilidade de retorno {@code true} (0.0 a 1.0).
* @return {@code true} se o evento ocorrer, {@code false} caso contrário.
*/
public static boolean occursWithProbability(double probability) {
return random.nextDouble() < probability;
}
/**
* Seleciona aleatoriamente um elemento de um array genérico (Amostragem Uniforme Discreta).
*
* @param <T> O tipo dos elementos no array.
* @param array A população de onde escolher.
* @return O elemento selecionado.
* @throws IllegalArgumentException Se o array for nulo ou vazio.
*/
public static <T> T chooseRandom(T[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("Array cannot be null or empty.");
}
return array[random.nextInt(array.length)];
}
/**
* Reinicializa a semente (seed) do gerador global.
* <p>
* <b>Importância Crítica:</b> Permite tornar a simulação determinística. Ao fixar a seed,
* a sequência de números "aleatórios" gerada será idêntica em execuções subsequentes,
* facilitando a depuração de race conditions ou lógica complexa.
*
* @param seed O valor da semente inicial (ex: timestamp ou constante).
*/
public static void setSeed(long seed) {
random.setSeed(seed);
}
}

View File

@@ -0,0 +1,167 @@
package sd.util;
import java.util.List;
import java.util.Map;
import sd.config.SimulationConfig;
import sd.model.Vehicle;
import sd.model.VehicleType;
import sd.routing.RouteSelector;
/**
* Motor de injeção de carga (Load Injector) para a simulação de tráfego.
* <p>
* Esta classe atua como uma fábrica estocástica de veículos, sendo responsável por:
* <ol>
* <li><b>Modelagem Temporal:</b> Determinar os instantes de chegada (Inter-arrival times)
* usando processos de Poisson (estocástico) ou intervalos determinísticos.</li>
* <li><b>Caracterização da Entidade:</b> Atribuir tipos de veículo (Bike, Light, Heavy)
* baseado numa Distribuição de Probabilidade Cumulativa (CDF).</li>
* <li><b>Inicialização Espacial:</b> Distribuir a carga uniformemente entre os pontos de entrada (E1-E3).</li>
* <li><b>Atribuição de Rota:</b> Delegar a escolha do percurso à estratégia {@link RouteSelector} ativa.</li>
* </ol>
*/
public class VehicleGenerator {
private final SimulationConfig config;
private final String arrivalModel;
/** Parâmetro Lambda (λ) para a distribuição de Poisson (taxa de chegada). */
private final double arrivalRate;
/** Intervalo determinístico para geração constante (modo debug/teste). */
private final double fixedInterval;
/** * Estratégia de roteamento atual.
* Não é final para permitir Hot-Swapping durante a execução.
*/
private RouteSelector routeSelector;
/**
* Inicializa o gerador com as configurações de simulação e estratégia de roteamento.
*
* @param config A configuração global contendo as taxas e probabilidades.
* @param routeSelector A estratégia inicial de seleção de rotas.
*/
public VehicleGenerator(SimulationConfig config, RouteSelector routeSelector) {
this.config = config;
this.routeSelector = routeSelector;
// Cache de valores de configuração para evitar lookups repetitivos em hot-path
this.arrivalModel = config.getArrivalModel();
this.arrivalRate = config.getArrivalRate();
this.fixedInterval = config.getFixedArrivalInterval();
}
/**
* Calcula o timestamp absoluto para a próxima injeção de veículo.
* <p>
* Se o modelo for "POISSON", utiliza a técnica de <i>Inverse Transform Sampling</i>
* (via {@link RandomGenerator}) para gerar intervalos exponencialmente distribuídos,
* simulando a aleatoriedade natural do tráfego.
* * @param currentTime O tempo atual da simulação (base de cálculo).
* @return O instante futuro (t + delta) para agendamento do evento de geração.
*/
public double getNextArrivalTime(double currentTime) {
if ("POISSON".equalsIgnoreCase(arrivalModel)) {
double interval = RandomGenerator.generateExponentialInterval(arrivalRate);
return currentTime + interval;
} else {
return currentTime + fixedInterval;
}
}
/**
* Instancia (Spawn) um novo veículo configurado e roteado.
* <p>
* O processo de criação segue um pipeline:
* <ol>
* <li>Seleção de Tipo (Roda da Fortuna / CDF).</li>
* <li>Seleção de Entrada (Uniforme).</li>
* <li>Cálculo de Rota (Delegado ao Strategy).</li>
* </ol>
*
* @param vehicleId O identificador único sequencial (ex: "V104").
* @param entryTime O timestamp de criação.
* @param queueSizes Snapshot atual das filas (usado apenas por estratégias dinâmicas como LEAST_CONGESTED).
* @return A entidade {@link Vehicle} pronta para inserção na malha.
*/
public Vehicle generateVehicle(String vehicleId, double entryTime, Map<String, Integer> queueSizes) {
VehicleType type = selectVehicleType();
String entryPoint = selectRandomEntryPoint();
List<String> route = routeSelector.selectRoute(entryPoint, queueSizes);
return new Vehicle(vehicleId, type, entryTime, route);
}
/**
* Seleciona o tipo de veículo usando Amostragem por Probabilidade Cumulativa.
* <p>
* Normaliza as probabilidades configuradas e mapeia um número aleatório [0, 1)
* para o intervalo correspondente ao tipo de veículo.
*
* @return O tipo enumerado {@link VehicleType} selecionado.
*/
private VehicleType selectVehicleType() {
double bikeProbability = config.getBikeVehicleProbability();
double lightProbability = config.getLightVehicleProbability();
double heavyProbability = config.getHeavyVehicleProbability();
double total = bikeProbability + lightProbability + heavyProbability;
if (total == 0) return VehicleType.LIGHT; // Fallback de segurança
// Normalização
bikeProbability /= total;
lightProbability /= total;
double rand = Math.random();
if (rand < bikeProbability) {
return VehicleType.BIKE;
} else if (rand < bikeProbability + lightProbability) {
return VehicleType.LIGHT;
} else {
return VehicleType.HEAVY;
}
}
/**
* Seleciona um ponto de injeção na borda da rede (Edge Node).
* Distribuição Uniforme: ~33.3% para cada entrada (E1, E2, E3).
*
* @return O ID da interseção de entrada.
*/
private String selectRandomEntryPoint() {
double rand = Math.random();
if (rand < 0.333) {
return "E1";
} else if (rand < 0.666) {
return "E2";
} else {
return "E3";
}
}
/**
* Atualiza a estratégia de roteamento em tempo de execução (Hot-Swap).
* <p>
* Permite que o Coordenador altere o comportamento da frota (ex: de RANDOM para SHORTEST_PATH)
* sem necessidade de reiniciar a simulação.
* * @param newRouteSelector A nova implementação de estratégia a utilizar.
*/
public void setRouteSelector(RouteSelector newRouteSelector) {
this.routeSelector = newRouteSelector;
}
/**
* Retorna uma representação textual do estado interno do gerador.
* Útil para logs de auditoria e debugging.
*/
public String getInfo() {
return String.format(
"VehicleGenerator{model=%s, rate=%.2f, interval=%.2f, routeSelector=%s}",
arrivalModel, arrivalRate, fixedInterval, routeSelector.getClass().getSimpleName()
);
}
}

View File

@@ -1,95 +0,0 @@
package server;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import server.structs.SystemStateManager;
import server.handlers.ActiveUsersHandler;
import server.handlers.BroadcastHandler;
import server.handlers.UnicastHandler;
import server.handlers.DataPersistence;
import server.handlers.ThingHandler;
import server.handlers.MulticastHandler;
//import server.handlers.RequestsStatsThread;
/**
* The Server class represents the main server application.
* It handles direct connections, broadcasts, multicasts, events, active users, request statistics,
* data persistence, and provides methods for starting and closing the server.
*/
public class Server {
private static final Logger logger = Logger.getLogger(Server.class.getName());
private static final ExecutorService executorService = Executors.newFixedThreadPool(100);
public static final int BUFFER_SIZE = 1024;
public static final int SERVER_PORT = 7500;
public static final int USER_PORT = 7501;
public static final int MULTICAST_PORT = 7502;
public static final String BROADCAST_ADDRESS = "255.255.255.255";
/**
* The main method of the Server class.
* It loads the shared data, creates sockets, and starts various threads for server operations.
*
* @param args The command line arguments.
*/
public static void main(String[] args) {
try {
SystemStateManager.loadData();
} catch (Exception ignored) {
}
executorService.execute(() -> handleUnicast(SERVER_PORT));
try {
SystemStateManager.setMulticastSocket(new MulticastSocket(MULTICAST_PORT));
DatagramSocket broadcastSocket = new DatagramSocket(USER_PORT, InetAddress.getByName(BROADCAST_ADDRESS));
broadcastSocket.setBroadcast(true);
SystemStateManager.setBroadcastSocket(broadcastSocket);
} catch (IOException io) {
logger.severe("Error Creating Sockets! " + io.getMessage());
close();
}
executorService.execute(new BroadcastHandler());
executorService.execute(new MulticastHandler());
executorService.execute(new ThingHandler());
executorService.execute(new ActiveUsersHandler());
//executorService.execute(new RequestsStats());
executorService.execute(new DataPersistence());
}
/**
* Handles direct connections on the specified port.
*
* @param port The port number for direct connections.
*/
public static void handleUnicast(int port) {
try (ServerSocket serverSocket = new ServerSocket(port)) {
while (true) {
executorService.execute(new UnicastHandler(serverSocket.accept()));
}
} catch (Exception e) {
logger.severe("Error Handling Unicast Connection! " + e.getMessage());
close();
}
}
/**
* Closes the server by shutting down the executor service, saving the shared data, and exiting the application.
*/
public static void close() {
try {
executorService.shutdown();
SystemStateManager.saveData();
System.exit(0);
} catch (Exception ignored) {
}
}
}

View File

@@ -1,195 +0,0 @@
package server.handlers;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import server.Server;
import server.structs.SystemStateManager;
import server.structs.intfaces.Request;
import server.structs.intfaces.User;
import server.utils.MessageProtocolHandler;
import server.utils.UserHandler;
import shared.enums.ConnType;
/**
* Handles asynchronous request acceptance in the emergency communication system.
* Manages the workflow of request acceptance based on connection type and user hierarchy.
*
* Features:
* - Multi-mode communication support (unicast, multicast, broadcast)
* - Hierarchy-based request handling
* - Asynchronous operation
* - Request acknowledgment tracking
*
* @author 0x1eo
* @since 2024-12-13
*/
public class AcceptRequestHandler implements Runnable {
private static final Logger logger = Logger.getLogger(AcceptRequestHandler.class.getName());
private static final String MULTICAST_ADDRESS_PATTERN =
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
private final ConnType connectionType;
private final Request request;
/**
* Creates a new request acceptance handler.
*
* @param connectionType the type of network connection to use
* @param request the request to be processed
*/
public AcceptRequestHandler(ConnType connectionType, Request request) {
this.connectionType = connectionType;
this.request = request;
}
@Override
public void run() {
try {
JSONObject requestJson = createRequestJson();
handleRequestByConnectionType(requestJson);
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to send request answer", e);
} catch (JSONException e) {
logger.log(Level.SEVERE, "Failed to create request JSON", e);
}
}
private JSONObject createRequestJson() throws JSONException {
JSONObject json = new JSONObject();
json.put("command", "requestAnswer");
json.put("from", request.getSender());
json.put("content", request.getMessage());
json.put("to", determineReceiver());
return json;
}
private String determineReceiver() {
Object receiver = request.getReceiver();
if (receiver instanceof User) {
return ((User) receiver).getUsername();
}
if (receiver instanceof String) {
String receiverStr = (String) receiver;
if ("broadcast".equals(receiverStr) || receiverStr.matches(MULTICAST_ADDRESS_PATTERN)) {
return receiverStr;
}
logger.severe("Invalid receiver string format");
throw new IllegalStateException("Invalid receiver format");
}
logger.severe("Invalid receiver type");
throw new IllegalStateException("Invalid receiver type");
}
private void handleRequestByConnectionType(JSONObject requestJson) throws IOException, JSONException {
switch (connectionType) {
case UNICAST:
handleUnicastRequest(requestJson);
break;
case MULTICAST:
handleMulticastRequest(requestJson);
break;
case BROADCAST:
handleBroadcastRequest(requestJson);
break;
default:
logger.warning("Unsupported connection type: " + connectionType);
}
}
private void handleUnicastRequest(JSONObject requestJson) throws IOException, JSONException {
User receiver = request.getReceiver();
String response = UserHandler.sendAndReceiveSomething(receiver, requestJson.toString());
if (response != null && new JSONObject(response).getString("response").equals("YES")) {
request.setTruster(receiver);
notifyUsers(receiver);
}
}
private void handleMulticastRequest(JSONObject requestJson) throws IOException, JSONException {
User group = request.getReceiver(); // Now correctly returns a User
List<User> eligibleUsers = getEligibleUsers(
new ArrayList<>(SystemStateManager.getUsersFromGroup(group.getUsername())), // Assuming you want to get users from the group name
request.getSender()
);
for (User user : eligibleUsers) {
if (tryAcceptRequest(user, requestJson)) {
sendMulticastNotification(group.toString());
break;
}
}
}
private void handleBroadcastRequest(JSONObject requestJson) throws IOException, JSONException {
List<User> eligibleUsers = getEligibleUsers(
new ArrayList<>(SystemStateManager.getUsers()),
request.getSender()
);
for (User user : eligibleUsers) {
if (tryAcceptRequest(user, requestJson)) {
sendBroadcastNotification();
break;
}
}
}
private List<User> getEligibleUsers(List<User> users, User sender) {
users.remove(sender);
users.removeIf(user -> !SystemStateManager.getOnlineUsers().contains(user));
users.removeIf(user -> !user.getHierarchy().isHigherThan(sender.getHierarchy()));
users.sort((u1, u2) -> u2.getHierarchy().getValue() - u1.getHierarchy().getValue());
return users;
}
private boolean tryAcceptRequest(User user, JSONObject requestJson) throws IOException, JSONException {
String response = UserHandler.sendAndReceiveSomething(user, requestJson.toString());
if (response != null && new JSONObject(response).getString("response").equals("YES")) {
request.setTruster(user);
return true;
}
return false;
}
private void notifyUsers(User receiver) throws IOException {
UserHandler.sendSomething(request.getSender(),
MessageProtocolHandler.notificationToJson(request).toString());
UserHandler.sendSomething(receiver,
MessageProtocolHandler.notificationToJson(request).toString());
}
private void sendMulticastNotification(String group) throws IOException {
String eventJson = MessageProtocolHandler.notificationToJson(request).toString();
DatagramPacket packet = new DatagramPacket(
eventJson.getBytes(),
eventJson.length(),
InetAddress.getByName(group),
Server.MULTICAST_PORT
);
SystemStateManager.getMulticastSocket().send(packet);
}
private void sendBroadcastNotification() throws IOException {
String eventJson = MessageProtocolHandler.notificationToJson(request).toString();
DatagramPacket packet = new DatagramPacket(
eventJson.getBytes(),
eventJson.length(),
InetAddress.getByName(Server.BROADCAST_ADDRESS),
Server.USER_PORT
);
SystemStateManager.getBroadcastSocket().send(packet);
}
}

View File

@@ -1,61 +0,0 @@
package server.handlers;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import server.structs.SystemStateManager;
import server.structs.intfaces.User;
import server.utils.UserHandler;
/**
* This class represents a thread that periodically checks the number of online users and sends a message to the user with the highest role.
*/
public class ActiveUsersHandler implements Runnable {
private static final Logger logger = Logger.getLogger(ActiveUsersHandler.class.getName());
/**
* The run method of the ActiveUsersThread class.
* This method is executed when the thread starts.
* It periodically checks the number of online users and sends a message to the user with the highest role.
*/
@Override
public void run() {
// Number of Online Users Only for the highest role
while (true) {
try {
Thread.sleep(10000);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
List<User> onlineUsers = SystemStateManager.getOnlineUsers();
logger.info("Number of Online Users: " + onlineUsers.size());
if (onlineUsers.size() == 0) {
continue;
}
User highestRoleUser = SystemStateManager.getHighestHierarchyUser(onlineUsers);
if (highestRoleUser == null) {
logger.severe("Highest Role User is null!");
continue;
}
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("command", "message");
jsonObject.put("from", "server");
jsonObject.put("to", highestRoleUser.getUsername());
jsonObject.put("content", "Number of Online Users: " + onlineUsers.size());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
jsonObject.put("date", sdf.format(new Date()));
UserHandler.sendSomething(highestRoleUser, jsonObject.toString());
} catch (IOException | JSONException ignored) {
logger.severe("Error Sending Active Users! " + ignored.getMessage());
}
}
}
}

View File

@@ -1,57 +0,0 @@
package server.handlers;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.logging.Logger;
import server.Server;
import server.structs.SystemStateManager;
import server.utils.InputCommandRouter;
import server.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* The BroadcastThread class represents a thread that handles broadcasting messages to all connected clients.
*/
public class BroadcastHandler implements Runnable {
private static final Logger logger = Logger.getLogger(BroadcastHandler.class.getName());
/**
* Constructs a new BroadcastThread.
*/
public BroadcastHandler() {}
/**
* Runs the broadcast thread.
*/
@Override
public void run() {
try (
DatagramSocket broadcastSocket = SystemStateManager.getBroadcastSocket();
) {
while (true) {
byte[] buffer = new byte[Server.BUFFER_SIZE];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
broadcastSocket.receive(packet);
if (packet.getAddress().equals(InetAddress.getLocalHost())) {
continue;
}
String input = new String(packet.getData());
String output = InputCommandRouter.processInput(ConnType.BROADCAST, packet, input);
if (output == null) {
continue;
}
DatagramPacket response = new DatagramPacket(output.getBytes(), output.length(), packet.getAddress(), packet.getPort());
try {
broadcastSocket.send(response);
} catch (IOException io) {
logger.severe("Error Sending Broadcast Response: " + io.getMessage());
}
}
} catch (IOException io) {
logger.severe("Error Handling Broadcast Connection! " + io.getMessage());
}
}
}

View File

@@ -1,30 +0,0 @@
package server.handlers;
import java.util.logging.Logger;
import server.structs.SystemStateManager;
/**
* This class represents a thread responsible for persisting data at regular intervals.
*/
public class DataPersistence implements Runnable {
private static final Logger logger = Logger.getLogger(DataPersistence.class.getName());
/**
* The run method of the DataPersistenceThread.
* This method is responsible for saving data at regular intervals.
*/
@Override
public void run() {
while (true) {
try {
Thread.sleep(10000);
SystemStateManager.saveData();
logger.info("Data Saved");
} catch (Exception e) {
logger.severe("Error Saving Data! " + e.getMessage());
}
}
}
}

View File

@@ -1,57 +0,0 @@
package server.handlers;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import server.structs.SystemStateManager;
import server.structs.intfaces.Notification;
import server.structs.intfaces.User;
import server.utils.MessageProtocolHandler;
import server.utils.UserHandler;
/**
* This class represents a thread that retrieves and sends message history for a user.
*/
public class MessageHistoryHandler implements Runnable {
private static final Logger logger = Logger.getLogger(MessageHistoryHandler.class.getName());
private User user;
/**
* Constructs a new MessageHistoryThread object.
*
* @param user the user for whom the message history will be retrieved and sent
*/
public MessageHistoryHandler(User user) {
this.user = user;
}
/**
* Runs the thread, retrieving and sending the message history for the user.
*/
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
List<? extends Notification> notifications = SystemStateManager.getUserNotifications(user);
if (notifications.isEmpty()) {
return;
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("command", "history");
jsonObject.put("notifications", MessageProtocolHandler.notificationsToJson(notifications));
UserHandler.sendSomething(user, jsonObject.toString());
} catch (JSONException | IOException error) {
logger.severe("Error Sending Message History! " + error.getMessage());
}
}
}

View File

@@ -1,58 +0,0 @@
package server.handlers;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.logging.Logger;
import server.Server;
import server.structs.SystemStateManager;
import server.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* MulticastThread class.
* This class is responsible for handling the multicast connection.
* It receives multicast packets, processes them, and sends responses back to the clients.
*/
public class MulticastHandler implements Runnable {
private static final Logger logger = Logger.getLogger(MulticastHandler.class.getName());
/**
* MulticastThread constructor.
*/
public MulticastHandler() {}
/**
* Runs the multicast thread.
* It continuously receives multicast packets, processes them, and sends responses back to the clients.
*/
@Override
public void run() {
try (
MulticastSocket multicastSocket = SystemStateManager.getMulticastSocket()) {
while (true) {
byte[] buffer = new byte[Server.BUFFER_SIZE];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
multicastSocket.receive(packet);
if (packet.getAddress().equals(InetAddress.getLocalHost())) {
continue;
}
String input = new String(packet.getData());
String output = InputCommandRouter.processInput(ConnType.MULTICAST, packet, input);
if (output == null) {
continue;
}
DatagramPacket response = new DatagramPacket(output.getBytes(), output.length(), packet.getAddress(), packet.getPort());
try {
multicastSocket.send(response);
} catch (IOException io) {
logger.severe("Error sending Multicast Response: " + io.getMessage());
}
}
} catch (IOException io) {
logger.severe("Error Handling Multicast Connection! " + io.getMessage());
}
}
}

View File

@@ -1,119 +0,0 @@
package server.handlers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import server.Server;
import server.structs.SystemStateManager;
import server.structs.intfaces.Notification;
import server.structs.intfaces.Message;
import server.structs.intfaces.Request;
import server.structs.intfaces.User;
import server.utils.MessageProtocolHandler;
/**
* Handles the delivery of notifications to users in a server-side communication system.
*
* This class is a runnable thread responsible for continuously processing and delivering
* notifications from the system state manager to their intended recipients. It manages
* different types of notifications such as messages and requests, ensuring they are
* transmitted to the appropriate users via network sockets.
*
* Key responsibilities:
* <ul>
* <li>Periodically retrieves pending notifications from the system state manager</li>
* <li>Validates and delivers notifications to their intended users</li>
* <li>Handles socket connections and communication protocols</li>
* <li>Manages error scenarios such as closed sockets or failed JSON conversions</li>
* </ul>
*
* The handler operates in an infinite loop, sleeping briefly between notification checks
* to prevent excessive CPU usage. It supports different notification types and logs
* critical events for monitoring and debugging purposes.
*
* @author 0x1eo
* @since 2024-12-13
* @see SystemStateManager
* @see Notification
* @see Message
* @see Request
*/
public class ThingHandler implements Runnable {
private static final Logger logger = Logger.getLogger(ThingHandler.class.getName());
/**
* The run method is the entry point for the thread.
* It continuously checks for events in the shared object and delivers them to the appropriate users.
*/
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
List<? extends Notification> notifications = SystemStateManager.getNotificationsToDeliver();
logger.info("Notifications to deliver: " + notifications.size());
for (int i = 0; i < notifications.size(); i++) {
Notification notification = notifications.get(i);
Object receiver = notification.getReceiver();
if (receiver instanceof User) {
User user = ((User) receiver);
logger.info("Event to deliver to " + user.getUsername());
Socket socket = SystemStateManager.getUserSocket(user);
if (socket == null || socket.isClosed() || !socket.isConnected()) {
logger.severe("User Socket is Null or Closed");
SystemStateManager.removeNotificationDelivered(notification);
continue;
}
try (
Socket newSocket = new Socket(socket.getInetAddress(), Server.USER_PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));
PrintWriter out = new PrintWriter(newSocket.getOutputStream(), true)) {
if (notification instanceof Message) {
Message message = ((Message) notification);
JSONObject json = MessageProtocolHandler.notificationToJson(message);
if (json == null) {
logger.severe("Event to JSON returned null!");
SystemStateManager.removeNotificationDelivered(notification);
continue;
}
out.println(MessageProtocolHandler.notificationToJson(message).toString());
logger.info("Message delivered to " + user.getUsername());
} else if (notification instanceof Request) {
Request request = ((Request) notification);
JSONObject json = MessageProtocolHandler.notificationToJson(request);
if (json == null) {
logger.severe("Event to JSON returned null!");
SystemStateManager.removeNotificationDelivered(notification);
continue;
}
out.println(MessageProtocolHandler.notificationToJson(request).toString());
logger.info("Request delivered to " + user.getUsername());
}
SystemStateManager.removeNotificationDelivered(notification);
} catch (IOException io) {
SystemStateManager.removeUserSocket(user);
} catch (JSONException json) {
SystemStateManager.removeNotificationDelivered(notification);
}
} else {
logger.severe("Receiver is not a user!");
SystemStateManager.removeNotificationDelivered(notification);
}
}
}
}
}

View File

@@ -1,60 +0,0 @@
package server.handlers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Logger;
import server.utils.InputCommandRouter;
import shared.enums.ConnType;
/**
* Represents a thread that handles direct connections with clients.
*/
public class UnicastHandler implements Runnable {
private static final Logger logger = Logger.getLogger(UnicastHandler.class.getName());
private Socket socket;
/**
* Constructs a DirectThread object with the specified socket.
*
* @param socket the socket representing the client connection
*/
public UnicastHandler(Socket socket) {
this.socket = socket;
}
/**
* Runs the thread and handles the communication with the client.
*/
@Override
public void run() {
try (
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);) {
try {
while (true) {
String input = in.readLine();
if (input == null) {
in.close();
out.close();
if (!socket.isClosed()) socket.close();
return;
}
String output = InputCommandRouter.processInput(ConnType.UNICAST, socket, input);
if (output == null) {
continue;
}
out.println(output);
}
} catch (IOException io) {
logger.severe("Error Handling Direct Message! " + io.getMessage());
}
} catch (IOException io) {
logger.severe("Error Handling Direct Connection! " + io.getMessage());
}
}
}

View File

@@ -1,453 +0,0 @@
package server.structs;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.stream.Collectors;
import server.structs.intfaces.*;
import shared.enums.Hierarchy;
/**
* Manages network communications and user data for the emergency chat server.
* Implements thread-safe operations for managing users, connections, and messages.
*
* @author 0x1eo
* @since 2024-12-12
*/
public class SystemStateManager {
// Maps usernames to their corresponding User objects for quick lookup
private static final Map<String, User> users = new HashMap<>();
// Maintains active socket connections for online users
private static final Map<User, Socket> userSockets = new HashMap<>();
// Stores pending notifications for each user in priority order
private static final Map<User, TreeSet<Notification>> userNotifications = new HashMap<>();
// Queue of notifications pending delivery to users
private static final List<Notification> notificationsToDeliver = new ArrayList<>();
// Maps multicast group addresses to their member users
private static final Map<String, List<User>> groups = new HashMap<>();
// Socket for handling multicast communication
private static MulticastSocket multicastSocket;
// Socket for handling broadcast messages
private static DatagramSocket broadcastSocket;
// Private constructor to prevent instantiation
private SystemStateManager() {
throw new AssertionError("Utility class - do not instantiate");
}
//#region User Management
/**
* Adds a new user to the system.
*
* @param user the user to be added
* @throws IllegalArgumentException if user is null or if username already exists
*/
public static void addUser(User user) {
validateNotNull("User", user);
synchronized (users) {
if (users.containsKey(user.getUsername())) {
throw new IllegalArgumentException("User already exists!");
}
users.put(user.getUsername(), user);
}
}
/**
* Retrieves a user by their username.
*
* @param username the username to look up
* @return the User object if found, null otherwise
* @throws IllegalArgumentException if username is null or empty
*/
public static User getUser(String username) {
validateNotEmpty("Username", username);
synchronized (users) {
return users.get(username);
}
}
/**
* Returns a list of all registered users in the system.
*
* @return new ArrayList containing all users
*/
public static List<User> getUsers() {
synchronized (users) {
return new ArrayList<>(users.values());
}
}
/**
* Gets the user with the highest hierarchy level from a list of users.
*
* @param userList list of users to check
* @return user with highest hierarchy level, or null if list is empty
*/
public static User getHighestHierarchyUser(List<User> userList) {
if (userList == null || userList.isEmpty()) {
return null;
}
return userList.stream()
.max((u1, u2) -> {
Hierarchy h1 = u1.getHierarchy();
Hierarchy h2 = u2.getHierarchy();
return Integer.compare(h1.getValue(), h2.getValue());
})
.orElse(null);
}
//#endregion
//#region Socket Management
/**
* Associates a socket connection with a user.
*
* @param user the user to associate the socket with
* @param socket the socket connection
* @throws IllegalArgumentException if either parameter is null
*/
public static void addUserSocket(User user, Socket socket) {
validateNotNull("User", user);
validateNotNull("Socket", socket);
synchronized (userSockets) {
userSockets.put(user, socket);
}
}
/**
* Retrieves the active socket connection for a user.
*
* @param user the user whose socket to retrieve
* @return the Socket object if found, null otherwise
* @throws IllegalArgumentException if user is null
*/
public static Socket getUserSocket(User user) {
validateNotNull("User", user);
synchronized (userSockets) {
return userSockets.get(user);
}
}
/**
* Returns a list of currently online users.
* A user is considered online if they have an active socket connection.
*
* @return list of users with active socket connections
*/
public static List<User> getOnlineUsers() {
synchronized (userSockets) {
return userSockets.entrySet().stream()
.filter(entry -> isSocketActive(entry.getValue()))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
}
}
/**
* Checks if a socket connection is active and valid.
*
* @param socket the socket to check
* @return true if socket is active and operational
*/
private static boolean isSocketActive(Socket socket) {
return socket != null &&
!socket.isClosed() &&
socket.isConnected() &&
!socket.isInputShutdown() &&
!socket.isOutputShutdown();
}
/**
* Removes a user's socket connection.
*
* @param user the user whose socket to remove
* @throws IllegalArgumentException if user is null
*/
public static void removeUserSocket(User user) {
validateNotNull("User", user);
synchronized (userSockets) {
userSockets.remove(user);
}
}
//#endregion
//#region Notification Management
/**
* Adds a notification for a specific user.
*
* @param user the target user
* @param notification the notification to add
* @throws IllegalArgumentException if either parameter is null
*/
public static void addUserNotification(User user, Notification notification) {
validateNotNull("User", user);
validateNotNull("Notification", notification);
synchronized (userNotifications) {
userNotifications.computeIfAbsent(user, k -> new TreeSet<>()).add(notification);
}
}
/**
* Retrieves all notifications for a user.
*
* @param user the user whose notifications to retrieve
* @return list of notifications, empty list if none found
* @throws IllegalArgumentException if user is null
*/
public static List<Notification> getUserNotifications(User user) {
validateNotNull("User", user);
synchronized (userNotifications) {
TreeSet<Notification> notifications = userNotifications.get(user);
return notifications != null ? new ArrayList<>(notifications) : new ArrayList<>();
}
}
/**
* Gets all pending requests in the system.
*
* @return list of all Request objects
*/
public static List<Request> getRequests() {
synchronized (userNotifications) {
return userNotifications.values().stream()
.flatMap(Collection::stream)
.filter(notification -> notification instanceof Request)
.map(notification -> (Request) notification)
.collect(Collectors.toList());
}
}
/**
* Filters a collection of requests to return only accepted ones.
*
* @param requests collection of requests to filter
* @return list of requests that have been accepted
*/
public static List<Request> getAcceptedRequests(Collection<Request> requests) {
return requests.stream()
.filter(request -> request.getTruster() != null)
.collect(Collectors.toList());
}
/**
* Adds a notification to the delivery queue.
*
* @param notification the notification to queue
* @throws IllegalArgumentException if notification is null
*/
public static void addNotificationToDeliver(Notification notification) {
validateNotNull("Notification", notification);
synchronized (notificationsToDeliver) {
notificationsToDeliver.add(notification);
}
}
/**
* Returns all notifications pending delivery.
*
* @return list of queued notifications
*/
public static List<Notification> getNotificationsToDeliver() {
synchronized (notificationsToDeliver) {
return new ArrayList<>(notificationsToDeliver);
}
}
/**
* Removes a delivered notification from the queue.
*
* @param notification the notification to remove
* @throws IllegalArgumentException if notification is null
*/
public static void removeNotificationDelivered(Notification notification) {
validateNotNull("Notification", notification);
synchronized (notificationsToDeliver) {
notificationsToDeliver.remove(notification);
}
}
//#endregion
//#region Group Management
/**
* Adds a user to a multicast group.
*
* @param group the multicast group address
* @param user the user to add
* @throws IllegalArgumentException if group is invalid or user is null
*/
public static void addUserToGroup(String group, User user) {
validateNotEmpty("Group", group);
validateNotNull("User", user);
validateMulticastAddress(group);
synchronized (groups) {
groups.computeIfAbsent(group, k -> new ArrayList<>()).add(user);
}
}
/**
* Gets all users in a specific group.
*
* @param group the group address
* @return list of users in the group
* @throws IllegalArgumentException if group is null or empty
*/
public static List<User> getUsersFromGroup(String group) {
validateNotEmpty("Group", group);
synchronized (groups) {
List<User> groupUsers = groups.get(group);
return groupUsers != null ? new ArrayList<>(groupUsers) : new ArrayList<>();
}
}
//#endregion
//#region Socket Getters/Setters
/**
* Gets the system's multicast socket.
*
* @return the MulticastSocket instance
*/
public static MulticastSocket getMulticastSocket() {
return multicastSocket;
}
/**
* Sets the system's multicast socket.
*
* @param socket the MulticastSocket to use
*/
public static void setMulticastSocket(MulticastSocket socket) {
multicastSocket = socket;
}
/**
* Gets the system's broadcast socket.
*
* @return the DatagramSocket instance
*/
public static DatagramSocket getBroadcastSocket() {
return broadcastSocket;
}
/**
* Sets the system's broadcast socket.
*
* @param socket the DatagramSocket to use
*/
public static void setBroadcastSocket(DatagramSocket socket) {
broadcastSocket = socket;
}
//#endregion
//#region Data Persistence
/**
* Gets a map of all data structures for persistence.
*
* @return map of structure names to their objects
*/
private static Map<String, Object> getDataStructures() {
Map<String, Object> structures = new HashMap<>();
structures.put("users.bin", users);
structures.put("userNotifications.bin", userNotifications);
structures.put("notificationsToDeliver.bin", notificationsToDeliver);
structures.put("groups.bin", groups);
return structures;
}
/**
* Saves all system state to persistent storage.
*
* @throws IOException if an I/O error occurs during saving
*/
public static void saveData() throws IOException {
for (Map.Entry<String, Object> entry : getDataStructures().entrySet()) {
try (ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("./" + entry.getKey()))) {
out.writeObject(entry.getValue());
}
}
}
/**
* Loads system state from persistent storage.
*
* @throws IOException if an I/O error occurs during loading
* @throws ClassNotFoundException if a serialized class cannot be found
*/
public static void loadData() throws IOException, ClassNotFoundException {
for (Map.Entry<String, Object> entry : getDataStructures().entrySet()) {
try (ObjectInputStream in = new ObjectInputStream(
new FileInputStream(entry.getKey()))) {
Object data = in.readObject();
loadDataStructure(entry.getKey(), data);
}
}
}
/**
* Loads a specific data structure from serialized data.
*
* @param key the identifier for the data structure
* @param data the serialized data to load
*/
private static void loadDataStructure(String key, Object data) {
switch (key) {
case "users.bin":
users.putAll((Map<String, User>) data);
break;
case "userNotifications.bin":
userNotifications.putAll((Map<User, TreeSet<Notification>>) data);
break;
case "notificationsToDeliver.bin":
notificationsToDeliver.addAll((List<Notification>) data);
break;
case "groups.bin":
groups.putAll((Map<String, List<User>>) data);
break;
}
}
//#endregion
//#region Validation Helpers
/**
* Validates that an object is not null.
*
* @param field name of the field being validated
* @param value the value to check
* @throws IllegalArgumentException if value is null
*/
private static void validateNotNull(String field, Object value) {
if (value == null) {
throw new IllegalArgumentException(field + " cannot be null!");
}
}
/**
* Validates that a string is not null or empty.
*
* @param field name of the field being validated
* @param value the string to check
* @throws IllegalArgumentException if value is null or empty
*/
private static void validateNotEmpty(String field, String value) {
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException(field + " cannot be null or empty!");
}
}
/**
* Validates that a string represents a valid multicast address.
*
* @param address the address to validate
* @throws IllegalArgumentException if address format is invalid
*/
private static void validateMulticastAddress(String address) {
if (!address.matches("^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
throw new IllegalArgumentException("Invalid multicast address format!");
}
}
//#endregion
}

View File

@@ -1,28 +0,0 @@
package server.structs.abstractions;
import server.structs.intfaces.Message;
import server.structs.intfaces.User;
/**
* Abstract implementation of Message interface for the emergency communication system.
* Represents the base message type that can be exchanged between users.
*
* @author 0x1eo
* @since 2024-12-13
* @see Message
* @see AbstractNotification
*/
public abstract class AbstractMessage extends AbstractNotification implements Message {
/**
* Creates a new message.
*
* @param sender the user sending the message
* @param receiver the recipient (user, broadcast, or multicast group)
* @param content the message content
* @throws IllegalArgumentException if sender or content is null
*/
protected AbstractMessage(User sender, Object receiver, String content) {
super(sender, receiver, content);
}
}

View File

@@ -1,111 +0,0 @@
package server.structs.abstractions;
import java.time.Instant;
import java.util.Objects;
import server.structs.intfaces.Notification;
import server.structs.intfaces.User;
/**
* Abstract base implementation for notifications in the emergency communication system.
* Provides common functionality for all types of communications including
* messages, requests, and alerts.
*
* Features:
* - Timestamp-based ordering
* - Sender and receiver tracking
* - Message content storage
* - Thread-safe immutable timestamp
*
* @author 0x1eo
* @since 2024-12-13 02:47:23
* @see Notification
*/
public abstract class AbstractNotification implements Notification {
protected User sender;
protected User receiver;
protected String message;
protected final Instant timestamp;
/**
* Creates a new notification.
*
* @param sender the sender's identifier object
* @param receiver the recipient's identifier object
* @param message the notification message content
* @throws IllegalArgumentException if sender, receiver or message is null
*/
protected AbstractNotification(Object sender, Object receiver, String message) {
setSender(sender);
setReceiver(receiver);
setMessage(message);
this.timestamp = Instant.now();
}
@Override
public User getSender() {
return sender;
}
@Override
public void setSender(Object sender) {
if (sender == null) {
throw new IllegalArgumentException("Sender cannot be null");
}
this.sender = (User)sender;
}
@Override
public User getReceiver() { return receiver; }
@Override
public void setReceiver(Object receiver) {
if (receiver == null) {
throw new IllegalArgumentException("Receiver cannot be null");
}
this.receiver = (User)receiver;
}
@Override
public String getMessage() { return message; }
@Override
public void setMessage(String message) {
if (message == null) {
throw new IllegalArgumentException("Message cannot be null");
}
this.message = message;
}
@Override
public Instant getTimestamp() { return timestamp; }
@Override
public void setTimestamp(Instant timestamp) {
throw new UnsupportedOperationException("Timestamp cannot be modified after creation");
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Notification)) return false;
Notification other = (Notification) obj;
return Objects.equals(sender, other.getSender()) &&
Objects.equals(receiver, other.getReceiver()) &&
Objects.equals(message, other.getMessage()) &&
Objects.equals(timestamp, other.getTimestamp());
}
@Override
public int hashCode() { return Objects.hash(sender, receiver, message, timestamp); }
@Override
public String toString() {
return String.format("Notification[from=%s, to=%s, message='%s', time=%s]",
sender,
receiver,
message,
timestamp);
}
}

View File

@@ -1,88 +0,0 @@
package server.structs.abstractions;
import server.structs.intfaces.Request;
import server.structs.intfaces.User;
/**
* Abstract base implementation for requests in the emergency communication system.
* Extends AbstractNotification to provide request-specific functionality including
* request author tracking.
*
* Features:
* - Request author (truster) tracking
* - Inherits notification base features
* - Supports emergency communication protocol
* - Immutable creation timestamp
*
* @author 0x1eo
* @since 2024-12-13
* @see Request
* @see AbstractNotification
* @see User
*/
public abstract class AbstractRequest extends AbstractNotification implements Request {
protected User truster;
/**
* Creates a new request in the emergency system.
*
* @param sender the user initiating the request
* @param receiver the intended recipient
* @param content the request content/message
* @throws IllegalArgumentException if any parameter is null, or if receiver is empty
*/
protected AbstractRequest(User sender, String receiver, String content) {
super(sender.getUsername(), receiver, content);
setTruster(sender); // Initialize truster with the sender
}
/**
* Gets the author (requesting user) of this request.
*
* @return the user who authored this request
*/
@Override
public User getTruster() {
return truster;
}
/**
* Sets the author (requesting user) of this request.
*
* @param author the user who authored this request
* @throws IllegalArgumentException if author is null
*/
@Override
public void setTruster(User author) {
if (author == null) {
throw new IllegalArgumentException("Request author cannot be null");
}
this.truster = author;
}
@Override
public String toString() {
return String.format("Request[from=%s, to=%s, content='%s', author=%s, time=%s]",
getSender(),
getReceiver(),
getMessage(),
truster.getUsername(),
getTimestamp());
}
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) return false;
if (!(obj instanceof Request)) return false;
Request other = (Request) obj;
return truster.equals(other.getTruster());
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + truster.hashCode();
return result;
}
}

View File

@@ -1,125 +0,0 @@
package server.structs.abstractions;
import java.util.Objects;
import server.structs.intfaces.User;
import shared.enums.Hierarchy;
/**
* Abstract base implementation of the User interface that represents
* a user in the emergency communication system with hierarchical privileges.
*
* Features:
* - Username and full name management
* - Secure password storage
* - Hierarchical role-based access
* - Natural ordering based on hierarchy
*
* @author 0x1eo
* @since 2024-12-13
* @see User
* @see Hierarchy
*/
public abstract class AbstractUser implements User {
private String username;
private String name;
private String password;
private Hierarchy hierarchy;
/**
* Creates a new user with the specified credentials and hierarchy level.
*
* @param username unique identifier for the user
* @param name full name of the user
* @param password user's authentication credential
* @param hierarchy user's position in the system hierarchy
* @throws IllegalArgumentException if any parameter is null or empty strings
*/
protected AbstractUser(String username, String name, String password, Hierarchy hierarchy) {
setUsername(username);
setName(name);
setPassword(password);
setHierarchy(hierarchy);
}
@Override
public String getUsername() {
return username;
}
@Override
public void setUsername(String username) {
if (username == null || username.trim().isEmpty()) {
throw new IllegalArgumentException("Username cannot be null or empty");
}
this.username = username.trim();
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("Name cannot be null or empty");
}
this.name = name.trim();
}
@Override
public String getPassword() {
return password;
}
@Override
public void setPassword(String password) {
if (password == null || password.trim().isEmpty()) {
throw new IllegalArgumentException("Password cannot be null or empty");
}
this.password = password;
}
@Override
public Hierarchy getHierarchy() {
return hierarchy;
}
@Override
public void setHierarchy(Hierarchy hierarchy) {
if (hierarchy == null) {
throw new IllegalArgumentException("Hierarchy cannot be null");
}
this.hierarchy = hierarchy;
}
@Override
public int compareTo(User other) {
return this.hierarchy.getValue() - other.getHierarchy().getValue();
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof User)) return false;
User other = (User) obj;
return Objects.equals(username, other.getUsername()) &&
Objects.equals(name, other.getName()) &&
Objects.equals(password, other.getPassword()) &&
Objects.equals(hierarchy, other.getHierarchy());
}
@Override
public int hashCode() {
return Objects.hash(username, name, password, hierarchy);
}
@Override
public String toString() {
return String.format("User[username='%s', name='%s', hierarchy=%s]",
username,
name,
hierarchy.getDisplayName());
}
}

View File

@@ -1,46 +0,0 @@
package server.structs.implementations;
import server.structs.abstractions.AbstractMessage;
import server.structs.intfaces.Message;
import server.structs.intfaces.User;
/**
* Standard message implementation for the emergency communication system.
* Provides a concrete implementation for direct, broadcast, and group messages.
*
* Features:
* - Direct user-to-user messaging
* - Broadcast messaging support
* - Multicast group communication
* - Emergency notifications
* - Timestamp-based ordering
*
* @author 0x1eo
* @since 2024-12-13 03:42:45 UTC
* @see Message
* @see AbstractMessage
* @see User
*/
public class StandardMessage extends AbstractMessage {
/**
* Creates a new message in the emergency system.
*
* @param sender the user initiating the message
* @param receiver the recipient (user, broadcast address, or multicast group)
* @param content the message content
* @throws IllegalArgumentException if sender or content is null
*/
public StandardMessage(User sender, Object receiver, String content) {
super(sender, receiver, content);
}
@Override
public String toString() {
return String.format("Message[from=%s, to=%s, content='%s', time=%s]",
getSender(),
getReceiver(),
getMessage(),
getTimestamp());
}
}

View File

@@ -1,44 +0,0 @@
package server.structs.implementations;
import server.structs.abstractions.AbstractNotification;
import server.structs.intfaces.User;
import server.utils.UserHandler;
/**
* Standard notification implementation for the emergency communication system.
* Provides a concrete implementation of AbstractNotification for general-purpose
* system notifications.
*
* Features:
* - Direct messaging support
* - Broadcast capability
* - Multicast group messaging
* - System alerts and announcements
*
* @author 0x1eo
* @since 2024-12-13 03:41:39 UTC
* @see AbstractNotification
*/
public class StandardNotification extends AbstractNotification {
/**
* Creates a new standard notification.
*
* @param sender the sender's identifier (user, system, or service)
* @param receiver the recipient's identifier (user, broadcast, or multicast group)
* @param message the notification content
* @throws IllegalArgumentException if any parameter is null
*/
public StandardNotification(Object sender, Object receiver, String message) {
super(sender, receiver, message);
}
public String convertToUser(Object obj) {
if (obj instanceof String) {
// Use your user lookup mechanism
return UserHandler.findUser(String.valueOf(obj));
}
throw new IllegalArgumentException("Cannot convert to User: " + obj);
}
}

View File

@@ -1,17 +0,0 @@
package server.structs.implementations;
import server.structs.abstractions.AbstractRequest;
import server.structs.intfaces.User;
/**
* Standard request implementation for the emergency communication system.
*
* @author 0x1eo
* @since 2024-12-13
*/
public class StandardRequest extends AbstractRequest {
public StandardRequest(User sender, String receiver, String content) {
super(sender, receiver, content);
}
}

View File

@@ -1,17 +0,0 @@
package server.structs.implementations;
import server.structs.abstractions.AbstractUser;
import shared.enums.Hierarchy;
/**
* Standard user implementation for the emergency communication system.
*
* @author 0x1eo
* @since 2024-12-13
*/
public class StandardUser extends AbstractUser {
public StandardUser(String username, String name, String password, Hierarchy hierarchy) {
super(username, name, password, hierarchy);
}
}

View File

@@ -1,15 +0,0 @@
package server.structs.intfaces;
/**
* Represents a message in the communication system.
* Extends the base Notification interface to provide message-specific functionality.
* This interface serves as a marker for distinguishing message types from other notifications.
*
* @author 0x1eo
* @since 2024-12-12
* @see Notification
*/
public interface Message extends Notification {
// Marker interface - no additional methods required
// Implementation classes should provide message-specific functionality
}

View File

@@ -1,86 +0,0 @@
package server.structs.intfaces;
import java.io.Serializable;
import java.time.Instant;
/**
* Represents a notification in the communication system.
* Provides methods for managing notification metadata and content.
* Implements Serializable for network transmission and Comparable for ordering.
*
* @author 0x1eo
* @since 2024-12-12
*/
public interface Notification extends Serializable, Comparable<Notification> {
/**
* Gets the sender of the notification.
*
* @return the sender's identifier
*/
User getSender();
/**
* Sets the sender of the notification.
*
* @param sender the sender's identifier
* @throws IllegalArgumentException if sender is null or empty
*/
void setSender(Object sender);
/**
* Gets the receiver of the notification.
*
* @return the receiver's identifier
*/
User getReceiver();
/**
* Sets the receiver of the notification.
*
* @param receiver the receiver's identifier
* @throws IllegalArgumentException if receiver is null or empty
*/
void setReceiver(Object receiver);
/**
* Gets the content of the notification.
*
* @return the notification message content
*/
String getMessage();
/**
* Sets the content of the notification.
*
* @param message the notification message content
* @throws IllegalArgumentException if message is null
*/
void setMessage(String message);
/**
* Gets the timestamp of the notification.
*
* @return the instant when the notification was created
*/
Instant getTimestamp();
/**
* Sets the timestamp of the notification.
*
* @param timestamp the instant when the notification was created
* @throws IllegalArgumentException if timestamp is null
*/
void setTimestamp(Instant timestamp);
/**
* Provides a default natural ordering for notifications based on timestamp.
*
* @param other the notification to compare with
* @return negative if this is earlier, zero if same time, positive if later
*/
@Override
default int compareTo(Notification other) {
return this.getTimestamp().compareTo(other.getTimestamp());
}
}

View File

@@ -1,29 +0,0 @@
package server.structs.intfaces;
/**
* Represents a request in the communication system.
* Extends the Notification interface to add request-specific functionality.
* A request is a special type of notification that includes an author (requesting user).
*
* @author 0x1eo
* @since 2024-12-12
* @see Notification
* @see User
*/
public interface Request extends Notification {
/**
* Gets the author (requesting user) of this request.
*
* @return the user who authored this request
*/
User getTruster();
/**
* Sets the author (requesting user) of this request.
*
* @param author the user who authored this request
* @throws IllegalArgumentException if author is null
*/
void setTruster(User author);
}

View File

@@ -1,101 +0,0 @@
package server.structs.intfaces;
import java.io.Serializable;
import shared.enums.Hierarchy;
/**
* Represents a user in the system with their credentials and permissions.
* Implements Serializable for network transmission and Comparable for user ordering.
*
* @author 0x1eo
* @since 2024-12-12
*/
public interface User extends Serializable, Comparable<User> {
/**
* Gets the user's unique username.
*
* @return the username
*/
String getUsername();
/**
* Sets the user's username.
*
* @param username the username to set
* @throws IllegalArgumentException if username is null or empty
*/
void setUsername(String username); // Fixed method name from setUserName
/**
* Gets the user's password (hashed).
*
* @return the hashed password
*/
String getPassword();
/**
* Sets the user's password.
* Implementation should ensure the password is properly hashed before storage.
*
* @param password the password to set
* @throws IllegalArgumentException if password is null or empty
*/
void setPassword(String password);
/**
* Gets the user's display name.
*
* @return the user's full name
*/
String getName();
/**
* Sets the user's display name.
*
* @param name the name to set
* @throws IllegalArgumentException if name is null or empty
*/
void setName(String name);
/**
* Gets the user's hierarchy level in the system.
*
* @return the user's hierarchy level
*/
Hierarchy getHierarchy();
/**
* Sets the user's hierarchy level.
*
* @param hierarchy the hierarchy level to set
* @throws IllegalArgumentException if hierarchy is null
*/
void setHierarchy(Hierarchy hierarchy);
/**
* Provides a default natural ordering for users based on username.
*
* @param other the user to compare with
* @return negative if this username comes before, zero if equal, positive if after
*/
@Override
default int compareTo(User other) {
return this.getUsername().compareToIgnoreCase(other.getUsername());
}
/**
* Checks if the user has at least the specified hierarchy level.
*
* @param minimumHierarchy the minimum required hierarchy level
* @return true if user's hierarchy is at least the specified level
* @throws IllegalArgumentException if minimumHierarchy is null
*/
default boolean hasMinimumHierarchy(Hierarchy minimumHierarchy) {
if (minimumHierarchy == null) {
throw new IllegalArgumentException("Minimum hierarchy cannot be null");
}
return this.getHierarchy().ordinal() >= minimumHierarchy.ordinal();
}
}

View File

@@ -1,90 +0,0 @@
package server.utils;
import java.net.Socket;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import server.structs.SystemStateManager;
import server.structs.intfaces.User;
import shared.enums.ConnType;
/**
* This class represents the protocol used for processing input in the server.
*/
public class InputCommandRouter {
private static final Logger logger = Logger.getLogger(InputCommandRouter.class.getName());
/**
* Processes the input based on the given connection type, socket packet, and input string.
*
* @param connType The type of connection (DIRECT or INDIRECT).
* @param socketPacket The socket packet object.
* @param input The input string to be processed.
* @return The response as a JSON string.
*/
public static String processInput(ConnType connType, Object socketPacket, String input) {
JSONObject response = new JSONObject();
try {
JSONObject json = new JSONObject(input);
if (!json.has("command")) {
response.put("response", "Invalid command!");
return response.toString();
}
// Register the user's socket if it is not already registered
Socket socket;
if (connType == ConnType.UNICAST) {
if (json.has("username") || json.has("from")) {
User User = null;
if (json.has("username")) {
User = SystemStateManager.getUser(json.getString("username"));
}
if (json.has("from")) {
User = SystemStateManager.getUser(json.getString("from"));
}
if (User != null) {
if (socketPacket instanceof Socket) {
socket = (Socket) socketPacket;
if (socket != SystemStateManager.getUserSocket(User)) {
SystemStateManager.addUserSocket(User, socket);
}
}
}
}
}
// Process the input based on the command
switch (json.getString("command")) {
case "register":
if (connType != ConnType.UNICAST) {
return null;
}
socket = (Socket) socketPacket;
return UserHandler.register(json, socketPacket);
case "login":
if (connType != ConnType.UNICAST) {
return null;
}
socket = (Socket) socketPacket;
return UserHandler.login(json, socketPacket);
case "message":
MessageProtocolHandler.receiveMessage(connType, json);
return null;
case "request":
MessageProtocolHandler.receiveRequest(connType, json, socketPacket);
return null;
case "joinGroup":
UserHandler.joinGroup(json);
return null;
default:
logger.severe("Invalid command received! " + input);
return null;
}
} catch (JSONException e) {
logger.severe("Invalid JSON received! " + e.getMessage());
return null;
}
}
}

View File

@@ -1,260 +0,0 @@
package server.utils;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import server.structs.SystemStateManager;
import server.structs.implementations.StandardMessage;
import server.structs.implementations.StandardRequest;
import server.structs.intfaces.Notification;
import server.structs.intfaces.Message;
import server.structs.intfaces.Request;
import server.structs.intfaces.User;
import server.handlers.AcceptRequestHandler;
import shared.enums.ConnType;
public class MessageProtocolHandler {
private static final Logger logger = Logger.getLogger(MessageProtocolHandler.class.getName());
private static final ExecutorService executorService = Executors.newFixedThreadPool(50);
/**
* Returns the ExecutorService used by the EventsHandler.
*
* @return the ExecutorService used by the EventsHandler
*/
public static ExecutorService getExecutorService() {
return executorService;
}
/**
* Converts an event to a JSON object.
*
* @param notification The event to convert.
* @return The JSON object representation of the event.
*/
public static <N extends Notification> JSONObject notificationToJson(N notification) throws JSONException {
JSONObject json = new JSONObject();
json.put("from", notification.getSender());
Object receiver = notification.getReceiver();
if (receiver instanceof User) {
json.put("to", ((User) receiver).getUsername());
} else if (receiver instanceof String) {
String receiverString = (String) receiver;
if (receiverString.equals("broadcast")) {
json.put("to", "broadcast");
} else if (receiverString.matches(
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
json.put("to", receiverString);
}
} else {
logger.severe("Invalid receiver type!");
}
json.put("content", notification.getMessage());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
json.put("date", sdf.format(notification.getTimestamp()));
if (notification instanceof Message) {
json.put("command", "message");
return json;
} else if (notification instanceof Request) {
json.put("command", "request");
Request request = (Request) notification;
if (request.getTruster() != null) {
json.put("accepter", request.getTruster().getUsername());
} else {
json.put("accepter", "");
}
return json;
}
throw new JSONException("Invalid event type");
}
/**
* Converts a collection of events to a JSON array.
*
* @param notifications The collection of events to convert.
* @return The JSON array representation of the events.
* @throws JSONException If an error occurs while converting the events to JSON.
*/
public static JSONArray notificationsToJson(Collection<? extends Notification> notifications) throws JSONException {
JSONArray jsonArray = new JSONArray();
for (Notification notification : notifications) {
jsonArray.put(notificationToJson(notification));
}
return jsonArray;
}
/**
* Converts a JSON object to a message.
*
* @param json The JSON object to convert.
* @return The message representation of the JSON object.
*/
public static Message messageFromJson(JSONObject json) {
try {
if (!json.has("from") || !json.has("to") || !json.has("content")) {
logger.severe("Invalid message received! (field missing)");
return null;
}
if (json.getString("from").equals("server")) {
return null;
}
User from = SystemStateManager.getUser(json.getString("from"));
if (from == null) {
logger.severe("Invalid message received! (User from)");
return null;
}
String to = json.getString("to");
String content = json.getString("content");
Message message = new StandardMessage(from, null, content);
if (to.equals("broadcast")) {
message.setReceiver(to);
} else if (to.matches(
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
message.setReceiver(to);
} else {
message.setReceiver(SystemStateManager.getUser(to));
}
if (message.getReceiver() == null) {
logger.severe("Invalid message received! (User to)");
return null;
}
return message;
} catch (JSONException ignored) {
logger.severe("Invalid message received! (JSONException)");
return null;
}
}
/**
* Converts a JSON object to a request.
*
* @param json The JSON object to convert.
* @return The request representation of the JSON object.
*/
public static Request requestFromJson(JSONObject json) {
try {
if (!json.has("from") || !json.has("to") || !json.has("content")) {
logger.severe("Invalid request received! (field missing)");
return null;
}
User from = SystemStateManager.getUser(json.getString("from"));
if (from == null) {
logger.severe("Invalid request received! (User from)");
return null;
}
String to = json.getString("to");
String content = json.getString("content");
Request request = new StandardRequest(from, null, content);
if (to.equals("broadcast")) {
request.setReceiver(to);
} else if (to.matches(
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
request.setReceiver(to);
} else {
request.setReceiver(SystemStateManager.getUser(to));
}
if (request.getReceiver() == null) {
logger.severe("Invalid request received! (User to)");
return null;
}
return request;
} catch (JSONException ignored) {
logger.severe("Invalid request received! (JSONException)");
return null;
}
}
/// ! Protocol methods
/**
* Receives a message and processes it based on the connection type and the message content.
*
* @param connType the type of connection (DIRECT or BROADCAST)
* @param json the JSON object containing the message data
* @return always returns null
*/
public static String receiveMessage(ConnType connType, JSONObject json) {
Message message = messageFromJson(json);
if (message != null) {
Object Receiver = message.getReceiver();
if (Receiver instanceof User) {
SystemStateManager.addUserNotification((User) message.getReceiver(), message);
} else if (Receiver instanceof String) {
String receiverString = (String) Receiver;
if (receiverString.equals("broadcast")) {
Collection<User> users = SystemStateManager.getUsers();
for (User user : users) {
SystemStateManager.addUserNotification(user, message);
}
} else if (receiverString.matches(
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
Collection<User> users = SystemStateManager.getUsersFromGroup(receiverString);
for (User user : users) {
SystemStateManager.addUserNotification(user, message);
}
} else {
User user = SystemStateManager.getUser(receiverString);
if (user != null) {
SystemStateManager.addUserNotification(user, message);
}
}
}
SystemStateManager.addUserNotification(message.getSender(), message);
if (connType == ConnType.UNICAST) {
SystemStateManager.addNotificationToDeliver(message);
}
}
return null;
}
/**
* Receives a request and processes it based on the connection type, JSON data, and socket packet.
*
* @param connType The type of connection (DIRECT or INDIRECT).
* @param json The JSON object containing the request data.
* @param socketPacket The socket packet associated with the request.
* @return The response string.
*/
public static String receiveRequest(ConnType connType, JSONObject json, Object socketPacket) {
Request request = requestFromJson(json);
if (request != null) {
Object Receiver = request.getReceiver();
if (Receiver instanceof User) {
SystemStateManager.addUserNotification(request.getReceiver(), request);
} else if (Receiver instanceof String) {
String receiverString = (String) Receiver;
if (receiverString.equals("broadcast")) {
Collection<User> users = SystemStateManager.getUsers();
for (User user : users) {
SystemStateManager.addUserNotification(user, request);
}
} else if (receiverString.matches(
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")) {
Collection<User> users = SystemStateManager.getUsersFromGroup(receiverString);
for (User user : users) {
SystemStateManager.addUserNotification(user, request);
}
} else {
User user = SystemStateManager.getUser(receiverString);
if (user != null) {
SystemStateManager.addUserNotification(user, request);
}
}
}
SystemStateManager.addUserNotification(request.getSender(), request);
if (connType == ConnType.UNICAST) {
SystemStateManager.addNotificationToDeliver(request);
}
executorService.execute(new AcceptRequestHandler(connType, request));
}
return null;
}
}

View File

@@ -1,193 +0,0 @@
package server.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import server.Server;
import server.handlers.MessageHistoryHandler;
import server.structs.SystemStateManager;
import server.structs.intfaces.User;
import server.structs.implementations.StandardUser; // New concrete implementation
import shared.enums.Hierarchy;
/**
* Handles user registration, authentication, and communication in the emergency system.
* Manages user sessions and group memberships.
*
* @author 0x1eo
* @since 2024-12-13
*/
public class UserHandler {
private static final Logger logger = Logger.getLogger(UserHandler.class.getName());
private static final String MULTICAST_GROUP_PATTERN =
"^(22[4-9]|23[0-9]|2[4-9][0-9]|[3-9][0-9]{2}|[12][0-9]{3})"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
// Prevent instantiation
private UserHandler() {}
/**
* Registers a new user in the system.
*
* @param json The registration details
* @param socketPacket The connection socket
* @return JSON response indicating success or failure
* @throws JSONException If registration data is malformed
*/
public static String register(JSONObject json, Object socketPacket) throws JSONException {
try {
String username = json.getString("username");
if (SystemStateManager.getUser(username) != null) {
logger.info("Registration failed: User already exists - " + username);
return createErrorResponse("User already exists!");
}
User user = createUser(json);
Socket socket = (Socket) socketPacket;
SystemStateManager.addUser(user);
SystemStateManager.addUserSocket(user, socket);
return createSuccessResponse();
} catch (IllegalArgumentException e) {
logger.log(Level.WARNING, "Registration failed: Invalid role", e);
return createErrorResponse("Invalid role!");
} catch (Exception e) {
logger.log(Level.SEVERE, "Registration failed: Unexpected error", e);
return e.getMessage();
}
}
/**
* Creates a new user instance from JSON data.
*/
private static User createUser(JSONObject json) throws JSONException {
return new StandardUser(
json.getString("username"),
json.getString("name"),
json.getString("password"),
Hierarchy.valueOf(json.getString("role").toUpperCase())
);
}
/**
* Authenticates a user and establishes their session.
*/
public static String login(JSONObject json, Object socketPacket) throws JSONException {
User user = SystemStateManager.getUser(json.getString("username"));
if (user == null) {
logger.info("Login failed: Invalid username");
return createErrorResponse("Invalid username!");
}
if (!user.getPassword().equals(json.getString("password"))) {
logger.info("Login failed: Invalid password for user " + user.getUsername());
return createErrorResponse("Invalid password!");
}
establishUserSession(user, (Socket) socketPacket);
return createSuccessResponse();
}
/**
* Sets up user session and starts message history handler.
*/
private static void establishUserSession(User user, Socket socket) {
SystemStateManager.addUserSocket(user, socket);
new Thread(new MessageHistoryHandler(user)).start();
}
/**
* Sends data to a user through their socket connection.
*/
public static void sendSomething(User user, String data) throws IOException {
try (Socket newSocket = createUserSocket(user);
PrintWriter out = new PrintWriter(newSocket.getOutputStream(), true)) {
out.println(data);
}
}
/**
* Sends data and waits for a response.
*/
public static String sendAndReceiveSomething(User user, String data) throws IOException {
try (Socket newSocket = createUserSocket(user);
PrintWriter out = new PrintWriter(newSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(newSocket.getInputStream()))) {
out.println(data);
return in.readLine();
}
}
private static Socket createUserSocket(User user) throws IOException {
Socket userSocket = SystemStateManager.getUserSocket(user);
return new Socket(userSocket.getInetAddress(), Server.USER_PORT);
}
/**
* Adds a user to a multicast group.
*/
public static String joinGroup(JSONObject json) throws JSONException {
if (!isValidGroupRequest(json)) {
return null;
}
User user = SystemStateManager.getUser(json.getString("username"));
if (user == null) {
logger.info("Group join failed: Invalid username");
return null;
}
try {
String group = json.getString("group");
SystemStateManager.getMulticastSocket().joinGroup(InetAddress.getByName(group));
SystemStateManager.addUserToGroup(group, user);
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to join multicast group", e);
}
return null;
}
private static boolean isValidGroupRequest(JSONObject json) {
if (!json.has("group") || !json.has("username")) {
logger.info("Group join failed: Missing required fields");
return false;
}
String group = json.getString("group");
if (!group.matches(MULTICAST_GROUP_PATTERN)) {
logger.info("Group join failed: Invalid group address - " + group);
return false;
}
return true;
}
private static String createSuccessResponse() {
return new JSONObject().put("response", "OK").toString();
}
private static String createErrorResponse(String message) {
return new JSONObject().put("response", message).toString();
}
public static String findUser(String username) {
User user = SystemStateManager.getUser(username);
if (user == null) {
return null;
}
return user.getUsername();
}
}

View File

@@ -1,72 +0,0 @@
package shared.enums;
import java.util.Arrays;
import java.util.Optional;
/**
* Represents different types of network connections used in the application.
*
* @author 0x1eo
* @since 2024-12-12
*/
public enum ConnType {
UNICAST("Unicast", "Point-to-point connection between two nodes"),
MULTICAST("Multicast", "One-to-many connection to a specific group of nodes"),
BROADCAST("Broadcast", "One-to-all connection reaching all nodes in the network");
private final String displayName;
private final String description;
ConnType(String displayName, String description) {
this.displayName = displayName;
this.description = description;
}
/**
* Gets the user-friendly display name of the connection type.
*
* @return the display name
*/
public String getDisplayName() {
return displayName;
}
/**
* Gets the description of the connection type.
*
* @return the description
*/
public String getDescription() {
return description;
}
/**
* Safely converts a string to a ConnType.
*
* @param value the string value to convert
* @return an Optional containing the ConnType if valid, empty Optional otherwise
*/
public static Optional<ConnType> fromString(String value) {
if (value == null || value.trim().isEmpty()) {
return Optional.empty();
}
return Arrays.stream(values())
.filter(connType -> connType.name().equalsIgnoreCase(value.trim()))
.findFirst();
}
/**
* Checks if the connection type is suitable for group communication.
*
* @return true if the connection type supports group communication
*/
public boolean isGroupCapable() {
return this == MULTICAST || this == BROADCAST;
}
@Override
public String toString() {
return String.format("%s (%s)", displayName, description);
}
}

View File

@@ -1,108 +0,0 @@
package shared.enums;
import java.util.Arrays;
import java.util.Optional;
/**
* Represents the priority levels in the system's hierarchy.
* Used for tasks and user permissions.
*
* @author 0x1eo
* @since 2024-12-12
*/
public enum Hierarchy {
LOW(0, "Low Priority"),
MEDIUM(1, "Medium Priority"),
HIGH(2, "High Priority");
private final int value;
private final String displayName;
Hierarchy(int value, String displayName) {
this.value = value;
this.displayName = displayName;
}
/**
* Gets the numeric value associated with this hierarchy level.
*
* @return the numeric value of the hierarchy level
*/
public int getValue() {
return value;
}
/**
* Gets the display name of this hierarchy level.
*
* @return the user-friendly name of the hierarchy level
*/
public String getDisplayName() {
return displayName;
}
/**
* Finds a Hierarchy enum by its numeric value.
*
* @param value the numeric value to look up
* @return an Optional containing the Hierarchy if found, empty Optional otherwise
*/
public static Optional<Hierarchy> fromValue(int value) {
return Arrays.stream(values())
.filter(h -> h.value == value)
.findFirst();
}
/**
* Finds a Hierarchy enum by its name (case-insensitive).
*
* @param name the name to look up
* @return an Optional containing the Hierarchy if found, empty Optional otherwise
*/
public static Optional<Hierarchy> fromString(String name) {
if (name == null || name.trim().isEmpty()) {
return Optional.empty();
}
return Arrays.stream(values())
.filter(h -> h.name().equalsIgnoreCase(name.trim()))
.findFirst();
}
/**
* Gets all hierarchy values as strings.
*
* @return array of hierarchy names
*/
public static String[] getAllNames() {
return Arrays.stream(values())
.map(Hierarchy::name)
.toArray(String[]::new);
}
/**
* Gets all hierarchy display names.
*
* @return array of user-friendly hierarchy names
*/
public static String[] getAllDisplayNames() {
return Arrays.stream(values())
.map(Hierarchy::getDisplayName)
.toArray(String[]::new);
}
/**
* Compares this hierarchy level with another.
*
* @param other the hierarchy level to compare with
* @return true if this level is higher than the other
*/
public boolean isHigherThan(Hierarchy other) {
return this.value > other.value;
}
@Override
public String toString() {
return displayName;
}
}

Some files were not shown because too many files have changed in this diff Show More