|
3 | 3 | import com.github.elebras1.flecs.collection.EcsLongList; |
4 | 4 | import com.github.elebras1.flecs.util.internal.FlecsLoader; |
5 | 5 |
|
6 | | -import java.lang.foreign.Arena; |
7 | | -import java.lang.foreign.MemoryLayout; |
8 | | -import java.lang.foreign.MemorySegment; |
9 | | -import java.lang.foreign.ValueLayout; |
| 6 | +import java.lang.foreign.*; |
10 | 7 | import java.nio.charset.StandardCharsets; |
11 | 8 | import java.util.Map; |
12 | 9 | import java.util.concurrent.ConcurrentHashMap; |
@@ -728,73 +725,42 @@ public void fromJson(String json) { |
728 | 725 | } |
729 | 726 | } |
730 | 727 |
|
731 | | - public FlecsServer httpServer(short port) { |
| 728 | + public void enableRest(short port) { |
732 | 729 | this.checkClosed(); |
733 | 730 |
|
734 | | - try (Arena tempArena = Arena.ofConfined()) { |
735 | | - MemorySegment desc = ecs_http_server_desc_t.allocate(tempArena); |
736 | | - ecs_http_server_desc_t.port(desc, port); |
737 | | - MemorySegment server = flecs_h.ecs_http_server_init(desc); |
738 | | - if (server.equals(MemorySegment.NULL)) { |
739 | | - throw new IllegalStateException("Failed to start HTTP server on port " + port); |
740 | | - } |
741 | | - |
742 | | - return new FlecsServer(server); |
743 | | - } |
744 | | - } |
745 | | - |
746 | | - public void httpServerStop(FlecsServer server) { |
747 | | - this.checkClosed(); |
748 | | - if (server != null && !server.getServerSegment().equals(MemorySegment.NULL)) { |
749 | | - flecs_h.ecs_http_server_fini(server.getServerSegment()); |
750 | | - } |
751 | | - } |
752 | | - |
753 | | - public void processHttp(FlecsServer server, float deltaTime) { |
754 | | - this.checkClosed(); |
755 | | - if (server != null && !server.getServerSegment().equals(MemorySegment.NULL)) { |
756 | | - flecs_h.ecs_http_server_dequeue(server.getServerSegment(), deltaTime); |
757 | | - } |
758 | | - } |
759 | | - |
760 | | - public FlecsServer restServer(short port) { |
761 | | - this.checkClosed(); |
762 | 731 | flecs_h.FlecsDocImport(this.nativeWorld); |
| 732 | + flecs_h.FlecsRestImport(this.nativeWorld); |
| 733 | + flecs_h.FlecsAlertsImport(this.nativeWorld); |
763 | 734 | flecs_h.FlecsStatsImport(this.nativeWorld); |
764 | 735 | flecs_h.FlecsMetricsImport(this.nativeWorld); |
765 | | - flecs_h.FlecsWorldMonitorImport(this.nativeWorld); |
766 | | - flecs_h.FlecsSystemMonitorImport(this.nativeWorld); |
767 | | - flecs_h.FlecsPipelineMonitorImport(this.nativeWorld); |
768 | | - flecs_h.FlecsAlertsImport(this.nativeWorld); |
769 | | - flecs_h.FlecsRestImport(this.nativeWorld); |
770 | 736 |
|
771 | | - flecs_h.ecs_set_entity_range(this.nativeWorld, 0, 0); |
| 737 | + try (Arena arena = Arena.ofConfined()) { |
| 738 | + MemorySegment restCompName = arena.allocateFrom("flecs.rest.Rest"); |
| 739 | + long restCompId = flecs_h.ecs_lookup(this.nativeWorld, restCompName); |
772 | 740 |
|
773 | | - MemorySegment desc = ecs_http_server_desc_t.allocate(this.arena); |
774 | | - desc.fill((byte) 0); |
775 | | - ecs_http_server_desc_t.port(desc, port); |
| 741 | + if (restCompId == 0) { |
| 742 | + throw new IllegalStateException("Failed to find flecs.rest.Rest component. Make sure FlecsRest module is imported."); |
| 743 | + } |
776 | 744 |
|
777 | | - MemorySegment server = flecs_h.ecs_rest_server_init(this.nativeWorld, desc); |
778 | | - if (server == null || server.equals(MemorySegment.NULL)) { |
779 | | - throw new IllegalStateException("Failed to initialize REST server"); |
780 | | - } |
| 745 | + MemorySegment restData = arena.allocate(2); |
| 746 | + restData.set(ValueLayout.JAVA_SHORT, 0, port); |
781 | 747 |
|
782 | | - int result = flecs_h.ecs_http_server_start(server); |
783 | | - if (result != 0) { |
784 | | - flecs_h.ecs_http_server_fini(server); |
785 | | - throw new IllegalStateException("Failed to start REST server on port " + port); |
| 748 | + flecs_h.ecs_set_id(this.nativeWorld, restCompId, restCompId, 2, restData); |
786 | 749 | } |
787 | | - |
788 | | - System.out.println("REST server started on http://localhost:" + port); |
789 | | - |
790 | | - return new FlecsServer(server); |
791 | 750 | } |
792 | 751 |
|
793 | | - public void restServerStop(FlecsServer server) { |
| 752 | + public void disableRest() { |
794 | 753 | this.checkClosed(); |
795 | | - if (server != null && !server.getServerSegment().equals(MemorySegment.NULL)) { |
796 | | - flecs_h.ecs_http_server_stop(server.getServerSegment()); |
797 | | - flecs_h.ecs_rest_server_fini(server.getServerSegment()); |
| 754 | + |
| 755 | + try (Arena arena = Arena.ofConfined()) { |
| 756 | + MemorySegment restCompName = arena.allocateFrom("flecs.rest.Rest"); |
| 757 | + long restCompId = flecs_h.ecs_lookup(this.nativeWorld, restCompName); |
| 758 | + |
| 759 | + if( restCompId == 0) { |
| 760 | + throw new IllegalStateException("Failed to find flecs.rest.Rest component. Make sure FlecsRest module is imported."); |
| 761 | + } |
| 762 | + |
| 763 | + flecs_h.ecs_remove_id(this.nativeWorld, restCompId, restCompId); |
798 | 764 | } |
799 | 765 | } |
800 | 766 |
|
|
0 commit comments