|
| 1 | +// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +package net.ladenthin.llama.server; |
| 6 | + |
| 7 | +import java.util.Objects; |
| 8 | +import lombok.ToString; |
| 9 | + |
| 10 | +/** |
| 11 | + * Scaffold for the <em>native</em> HTTP server bridge — the planned counterpart to |
| 12 | + * {@link OpenAiCompatServer}. |
| 13 | + * |
| 14 | + * <p>{@link OpenAiCompatServer} implements the HTTP transport in Java (on the JDK's |
| 15 | + * {@code com.sun.net.httpserver}) and drives the native llama.cpp server <em>core</em> over JNI. This |
| 16 | + * class is instead the entry point for the upstream <em>native</em> HTTP transport that is already |
| 17 | + * compiled into {@code libjllama} (llama.cpp's {@code server-http.cpp} plus its {@code cpp-httplib} |
| 18 | + * backend). That native transport is the only component able to serve the embedded llama.cpp |
| 19 | + * <strong>WebUI</strong> (the {@code ui.cpp}/{@code ui.h} asset table compiled in behind |
| 20 | + * {@code LLAMA_UI_HAS_ASSETS}).</p> |
| 21 | + * |
| 22 | + * <p><strong>Status: scaffold only.</strong> The route registration that upstream performs in |
| 23 | + * {@code server.cpp} (deliberately excluded from this build) is not yet wired to a JNI entry point, so |
| 24 | + * {@link #start()} throws {@link UnsupportedOperationException} for now. This class only fixes the |
| 25 | + * package structure and the public API shape; the native {@code startServer}/{@code stopServer} |
| 26 | + * methods, their C++ implementation, the server lifecycle/threading and WebUI serving are a separate, |
| 27 | + * detailed step (see {@code CLAUDE.md}, "WebUI (llama.cpp Svelte UI) embedding").</p> |
| 28 | + * |
| 29 | + * <p>It is {@link AutoCloseable} so that, once implemented, callers can drive it with |
| 30 | + * try-with-resources exactly like {@link OpenAiCompatServer}.</p> |
| 31 | + */ |
| 32 | +@ToString |
| 33 | +public final class NativeServer implements AutoCloseable { |
| 34 | + |
| 35 | + /** Message thrown by {@link #start()} until the native route-wiring lands. */ |
| 36 | + static final String NOT_WIRED_MESSAGE = |
| 37 | + "NativeServer is a scaffold: the upstream native HTTP routes (server-http.cpp) are " |
| 38 | + + "not yet wired to JNI. Use OpenAiCompatServer for now; the native server and " |
| 39 | + + "embedded WebUI are a planned step."; |
| 40 | + |
| 41 | + /** Immutable server configuration (bind host, port, ...) shared with {@link OpenAiCompatServer}. */ |
| 42 | + private final OpenAiServerConfig config; |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a native-server bridge for the given configuration. |
| 46 | + * |
| 47 | + * <p>Construction performs no native work and binds no socket; it only captures the configuration. |
| 48 | + * Call {@link #start()} to launch the server (not implemented yet).</p> |
| 49 | + * |
| 50 | + * @param config the server configuration (host, port, ...); must not be {@code null} |
| 51 | + */ |
| 52 | + public NativeServer(OpenAiServerConfig config) { |
| 53 | + this.config = Objects.requireNonNull(config, "config"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Starts the native HTTP server and begins serving the embedded WebUI. |
| 58 | + * |
| 59 | + * <p><strong>Not implemented yet</strong> — this is a scaffold. The native route registration and |
| 60 | + * its JNI binding are a planned step, so this method always throws until then.</p> |
| 61 | + * |
| 62 | + * @return this server instance (for fluent / try-with-resources use), once implemented |
| 63 | + * @throws UnsupportedOperationException always, until the native routes are wired to JNI |
| 64 | + */ |
| 65 | + // Scaffold: start() intentionally always throws for now, but must stay callable (not @DoNotCall) |
| 66 | + // so the real implementation and its callers/tests keep the same signature. |
| 67 | + @SuppressWarnings("DoNotCallSuggester") |
| 68 | + public NativeServer start() { |
| 69 | + throw new UnsupportedOperationException(NOT_WIRED_MESSAGE); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Reports whether the native server is currently running. |
| 74 | + * |
| 75 | + * @return {@code false} — the scaffold never starts a server yet |
| 76 | + */ |
| 77 | + public boolean isRunning() { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns the host the server is configured to bind to. |
| 83 | + * |
| 84 | + * @return the configured bind host |
| 85 | + */ |
| 86 | + public String getHost() { |
| 87 | + return config.getHost(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the port the server is configured to bind to. |
| 92 | + * |
| 93 | + * @return the configured port |
| 94 | + */ |
| 95 | + public int getPort() { |
| 96 | + return config.getPort(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Stops the native server if it is running. |
| 101 | + * |
| 102 | + * <p>No-op in the scaffold (nothing is ever started), so it is always safe to call, including from |
| 103 | + * try-with-resources. Real lifecycle teardown is part of the planned native-server implementation.</p> |
| 104 | + */ |
| 105 | + @Override |
| 106 | + public void close() { |
| 107 | + // Nothing is started yet, so there is nothing to release. |
| 108 | + } |
| 109 | +} |
0 commit comments