diff --git a/src/main/java/com/pixelgamelibrary/api/Importance.java b/src/main/java/com/pixelgamelibrary/api/Importance.java new file mode 100644 index 0000000..5717cb4 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/Importance.java @@ -0,0 +1,35 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api; + +import lombok.Getter; + +/** + * + * @author robertvokac + */ +public enum Importance { + HIGHEST(5), HIGH(4), MEDIUM(3), LOW(2), LOWEST(1); + @Getter + private final int number; + Importance(int numberIn) { + this.number = numberIn; + } +} diff --git a/src/main/java/com/pixelgamelibrary/api/Version.java b/src/main/java/com/pixelgamelibrary/api/Version.java index 9c3baf0..cecc49a 100644 --- a/src/main/java/com/pixelgamelibrary/api/Version.java +++ b/src/main/java/com/pixelgamelibrary/api/Version.java @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////////////////////// -// Pixel Game Library. -// Copyright (C) 2024 Your Name or Company. +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. // // This program is free software: you can redistribute it and/or // modify it under the terms of the GNU General Public License diff --git a/src/main/java/com/pixelgamelibrary/api/audio/Music.java b/src/main/java/com/pixelgamelibrary/api/audio/Music.java new file mode 100644 index 0000000..bcefc57 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/audio/Music.java @@ -0,0 +1,123 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.audio; + +/** + * The Music interface represents a longer audio track that can be controlled with playback functions. + * It provides methods for playing, pausing, stopping, and controlling playback settings like looping, volume, and panning. + * This interface allows for precise manipulation of the music playback within a game or application. + * + * Implementations of this interface should handle music resources, and the {@link #dispose()} method + * should be called when the music is no longer needed to free up resources. + * + * @author robertvokac + */ +public interface Music { + + /** + * Starts or resumes playback of the music track. + */ + public void play(); + + /** + * Pauses the playback of the music track. + */ + public void pause(); + + /** + * Stops the playback of the music track and resets its position to the beginning. + */ + public void stop(); + + /** + * Checks if the music track is currently playing. + * @return true if the music is playing, false otherwise. + */ + public boolean isPlaying(); + + /** + * Sets whether the music track should loop when it reaches the end. + * @param isLooping true to loop the music, false to play it once. + */ + public void setLooping(boolean isLooping); + + /** + * Checks if the music track is set to loop. + * @return true if looping is enabled, false otherwise. + */ + public boolean isLooping(); + + /** + * Sets the volume of the music playback. + * @param volume the volume level in the range [0, 1]. + */ + public void setVolume(float volume); + + /** + * Gets the current volume of the music playback. + * @return the volume level in the range [0, 1]. + */ + public float getVolume(); + + /** + * Sets the panning and volume for the music playback. + * Panning works in the range -1 (left) to 1 (right), with 0 being the center. + * @param pan panning in the range -1 to 1. + * @param volume the volume level in the range [0, 1]. + */ + public void setPan(float pan, float volume); + + /** + * Sets the playback position of the music track. + * @param position the playback position in seconds. + */ + public void setPosition(float position); + + /** + * Gets the current playback position of the music track. + * @return the playback position in seconds. + */ + public float getPosition(); + + /** + * Releases all resources associated with this music instance. + */ + public void dispose(); + + /** + * Sets a listener that will be notified when the music track finishes playing. + * @param listener the listener to be notified upon completion. + */ + public void setOnCompletionListener(OnCompletionListener listener); + + /** + * The OnCompletionListener interface provides a callback method to notify when the music has finished playing. + */ + public interface OnCompletionListener { + + /** + * Invoked when the music track has finished playing. + * + * @param music the Music instance that has reached the end of the track + */ + public abstract void onCompletion(Music music); + } + +} diff --git a/src/main/java/com/pixelgamelibrary/api/audio/Sound.java b/src/main/java/com/pixelgamelibrary/api/audio/Sound.java new file mode 100644 index 0000000..a24ab98 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/audio/Sound.java @@ -0,0 +1,151 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.audio; + +import com.pixelgamelibrary.api.Disposable; + +/** + * The Sound interface represents a short audio clip that can be played and controlled in various ways. + * It provides methods for playback, looping, volume control, pitch adjustment, and panning. + * + * The sound clip can be played multiple times concurrently and requires manual disposal + * to free up resources once it is no longer needed. + * + * This interface extends Disposable, so the {@link #dispose()} method should be called + * when the sound instance is no longer needed. + * + * @author robertvokac + */ +public interface Sound extends Disposable { + + /** + * Plays the sound. If the sound is already playing, it will be played again concurrently. + * @return the ID of the sound instance if successful, or -1 on failure. + */ + public long play(); + + /** + * Plays the sound with the specified volume. + * @param volume the volume level in the range [0, 1] + * @return the ID of the sound instance if successful, or -1 on failure. + */ + public long play(float volume); + + /** + * Plays the sound with specified volume, pitch, and pan. + * @param volume the volume level in the range [0, 1] + * @param pitch the pitch multiplier, where 1 is the default, >1 is faster, <1 is slower + * @param pan panning in the range -1 (left) to 1 (right), 0 is center + * @return the ID of the sound instance if successful, or -1 on failure. + */ + public long play(float volume, float pitch, float pan); + + /** + * Plays the sound in a loop. The sound will continue playing until stopped manually. + * @return the ID of the sound instance if successful, or -1 on failure. + */ + public long loop(); + + /** + * Plays the sound in a loop with the specified volume. + * @param volume the volume level in the range [0, 1] + * @return the ID of the sound instance if successful, or -1 on failure. + */ + public long loop(float volume); + + /** + * Plays the sound in a loop with specified volume, pitch, and pan. + * @param volume the volume level in the range [0, 1] + * @param pitch the pitch multiplier, where 1 is the default, >1 is faster, <1 is slower + * @param pan panning in the range -1 (left) to 1 (right), 0 is center + * @return the ID of the sound instance if successful, or -1 on failure. + */ + public long loop(float volume, float pitch, float pan); + + /** + * Stops playing all instances of this sound. + */ + public void stop(); + + /** + * Pauses all instances of this sound. + */ + public void pause(); + + /** + * Resumes all paused instances of this sound. + */ + public void resume(); + + /** + * Disposes of the sound and releases any system resources associated with it. + */ + @Override + public void dispose(); + + /** + * Stops the sound instance with the specified ID. + * @param soundId the ID of the sound instance to stop + */ + public void stop(long soundId); + + /** + * Pauses the sound instance with the specified ID. + * @param soundId the ID of the sound instance to pause + */ + public void pause(long soundId); + + /** + * Resumes the sound instance with the specified ID. + * @param soundId the ID of the sound instance to resume + */ + public void resume(long soundId); + + /** + * Sets whether the sound instance with the specified ID should loop. + * @param soundId the ID of the sound instance + * @param looping true to loop the sound, false to stop looping + */ + public void setLooping(long soundId, boolean looping); + + /** + * Sets the pitch for the sound instance with the specified ID. + * @param soundId the ID of the sound instance + * @param pitch the pitch multiplier, where 1 is the default, >1 is faster, <1 is slower + */ + public void setPitch(long soundId, float pitch); + + /** + * Sets the volume for the sound instance with the specified ID. + * @param soundId the ID of the sound instance + * @param volume the volume level in the range [0, 1] + */ + public void setVolume(long soundId, float volume); + + /** + * Sets the panning and volume for the sound instance with the specified ID. + * Panning only works for mono sounds. + * @param soundId the ID of the sound instance + * @param pan panning in the range -1 (left) to 1 (right), 0 is center + * @param volume the volume level in the range [0, 1] + */ + public void setPan(long soundId, float pan, float volume); + +} diff --git a/src/main/java/com/pixelgamelibrary/api/interfaces/Audio.java b/src/main/java/com/pixelgamelibrary/api/interfaces/Audio.java index d575b2d..4d487d9 100644 --- a/src/main/java/com/pixelgamelibrary/api/interfaces/Audio.java +++ b/src/main/java/com/pixelgamelibrary/api/interfaces/Audio.java @@ -19,11 +19,16 @@ /////////////////////////////////////////////////////////////////////////////////////////////// package com.pixelgamelibrary.api.interfaces; +import com.pixelgamelibrary.api.audio.Music; +import com.pixelgamelibrary.api.audio.Sound; +import com.pixelgamelibrary.api.storage.FileHandle; + /** * * @author robertvokac */ public interface Audio { //Add MIDI support - todo - + Sound newSound(FileHandle fileHandle); + Music newMusic(FileHandle fileHandle); } diff --git a/src/main/java/com/pixelgamelibrary/api/interfaces/Net.java b/src/main/java/com/pixelgamelibrary/api/interfaces/Net.java index 3212f31..94d5c75 100644 --- a/src/main/java/com/pixelgamelibrary/api/interfaces/Net.java +++ b/src/main/java/com/pixelgamelibrary/api/interfaces/Net.java @@ -19,10 +19,41 @@ /////////////////////////////////////////////////////////////////////////////////////////////// package com.pixelgamelibrary.api.interfaces; +import com.pixelgamelibrary.api.net.sockets.ClientSocket; +import com.pixelgamelibrary.api.net.sockets.ServerSocket; +import com.pixelgamelibrary.api.net.sockets.ClientSocketHints; +import com.pixelgamelibrary.api.net.http.HttpRequest; +import com.pixelgamelibrary.api.net.http.HttpResponseListener; +import com.pixelgamelibrary.api.net.sockets.ServerSocketHints; + /** * * @author robertvokac */ public interface Net { - + + public void sendHttpRequest(HttpRequest httpRequest, HttpResponseListener httpResponseListener); + + public void cancelHttpRequest(HttpRequest httpRequest); + + public boolean isHttpRequestPending(HttpRequest httpRequest); + + public ServerSocket newServerSocket(String hostNameOrIpAddress, int port, ServerSocketHints hints); + + ServerSocket newServerSocket(int port, ServerSocketHints hints); + + default ServerSocket newServerSocket(String hostNameOrIpAddress, int port) { + return newServerSocket(hostNameOrIpAddress, port, ServerSocketHints.getDefault()); + } + + default ServerSocket newServerSocket(int port) { + return newServerSocket(port, ServerSocketHints.getDefault()); + } + + ClientSocket newClientSocket(String hostNameOrIpAddress, int port, ClientSocketHints hints); + + default ClientSocket newClientSocket(String hostNameOrIpAddress, int port) { + return newClientSocket(hostNameOrIpAddress, port, ClientSocketHints.getDefault()); + } + } diff --git a/src/main/java/com/pixelgamelibrary/api/net/http/HttpMethod.java b/src/main/java/com/pixelgamelibrary/api/net/http/HttpMethod.java new file mode 100644 index 0000000..ca8a3ce --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/http/HttpMethod.java @@ -0,0 +1,62 @@ +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.http; + +/** + * Enum representing the various HTTP methods used for making requests. + * Each method is described below: + *
    + *
  • HEAD: Requests a response identical to a GET request, but without the response body.
  • + *
  • GET: Requests a representation of the specified resource, intended only for data retrieval.
  • + *
  • POST: Submits data to the specified resource, potentially causing changes or side effects on the server.
  • + *
  • PUT: Replaces all current representations of the target resource with the provided payload.
  • + *
  • PATCH: Applies partial modifications to a resource.
  • + *
  • DELETE: Removes the specified resource.
  • + *
+ */ +public enum HttpMethod { + + /** The HEAD method requests a response identical to a GET request without the response body. */ + HEAD("HEAD"), + + /** The GET method requests a representation of the specified resource, intended solely for data retrieval. */ + GET("GET"), + + /** The POST method submits an entity to the specified resource, potentially causing changes on the server. */ + POST("POST"), + + /** The PUT method replaces all current representations of the target resource with the given payload. */ + PUT("PUT"), + + /** The PATCH method is used to apply partial modifications to a resource. */ + PATCH("PATCH"), + + /** The DELETE method removes the specified resource. */ + DELETE("DELETE"); + + private final String method; + + HttpMethod(String method) { + this.method = method; + } + + public String getMethod() { + return method; + } +} diff --git a/src/main/java/com/pixelgamelibrary/api/net/http/HttpRequest.java b/src/main/java/com/pixelgamelibrary/api/net/http/HttpRequest.java new file mode 100644 index 0000000..60df18c --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/http/HttpRequest.java @@ -0,0 +1,241 @@ +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.http; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * Represents an HTTP request, containing methods to configure various parameters: + *
    + *
  • httpMethod: Common methods include GET and POST. Utilize {@link HttpMethod} for static references.
  • + *
  • url: The endpoint for the request.
  • + *
  • headers: A map of headers; the setter can be called multiple times.
  • + *
  • timeout: Duration to attempt a connection before timing out.
  • + *
  • content: A string that holds the data for processing the HTTP request.
  • + *
+ * + * Example usage: + * + *
+ * Map<String, String> parameters = new HashMap<>();
+ * parameters.put("user", "myuser");
+ * 
+ * HttpRequest httpGet = new HttpRequest(HttpMethod.GET);
+ * httpGet.setUrl("http://example.com");
+ * httpGet.setContent(HttpParametersUtils.convertHttpParameters(parameters));
+ * ...
+ * NetworkUtils.sendHttpRequest(httpGet, new HttpResponseListener() {
+ *     public void handleHttpResponse(HttpResponse response) {
+ *         String status = response.getResultAsString();
+ *         // Process response here
+ *     }
+ * 
+ *     public void failed(Throwable t) {
+ *         String status = "failed";
+ *         // Handle failure here
+ *     }
+ * });
+ * 
+ */ +public class HttpRequest { + + private String httpMethod; + private String url; + private Map headers; + private int timeout; + + private String content; + private InputStream contentStream; + private long contentLength; + + private boolean followRedirects = true; + private boolean includeCredentials = false; + + public HttpRequest() { + this.headers = new HashMap<>(); + this.timeout = 0; // Default timeout value + } + + /** + * Constructs a new HTTP request with the specified method. + * @param httpMethod The HTTP method for the request, e.g., GET or POST. + */ + public HttpRequest(String httpMethod) { + this(); + this.httpMethod = httpMethod; + } + + /** + * Sets the URL for the HTTP request. + * @param url The URL to set. + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * Adds a header to the HTTP request. + * @param name The name of the header. + * @param value The value of the header. + */ + public void setHeader(String name, String value) { + headers.put(name, value); + } + + /** + * Sets the content to be sent with the HTTP request. + * @param content A string representing the data to be sent. + */ + public void setContent(String content) { + this.content = content; + } + + /** + * Sets the content as an input stream for transmission, useful for larger data. + * @param contentStream The stream containing the content data. + * @param contentLength The length of the content. + */ + public void setContent(InputStream contentStream, long contentLength) { + this.contentStream = contentStream; + this.contentLength = contentLength; + } + + /** + * Sets the timeout duration for the HTTP request. + * @param timeout The number of milliseconds to wait before timing out. + */ + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + /** + * Specifies whether to follow redirects for HTTP requests. + * @param followRedirects True to follow redirects, false otherwise. + */ + public void setFollowRedirects(boolean followRedirects) { + this.followRedirects = followRedirects; + } + + /** + * Indicates if credentials should be included in cross-origin requests. + * @param includeCredentials True to include credentials, false otherwise. + */ + public void setIncludeCredentials(boolean includeCredentials) { + this.includeCredentials = includeCredentials; + } + + /** + * Sets the HTTP method of the request. + * @param httpMethod The HTTP method to set. + */ + public void setMethod(String httpMethod) { + this.httpMethod = httpMethod; + } + + /** + * Gets the timeout value for the HTTP request. + * @return The timeout in milliseconds. + */ + public int getTimeout() { + return timeout; + } + + /** + * Gets the HTTP method of the request. + * @return The HTTP method. + */ + public String getMethod() { + return httpMethod; + } + + /** + * Gets the URL of the HTTP request. + * @return The URL. + */ + public String getUrl() { + return url; + } + + /** + * Gets the content string for the HTTP request. + * @return The content string. + */ + public String getContent() { + return content; + } + + /** + * Gets the input stream containing the content. + * @return The content stream. + */ + public InputStream getContentStream() { + return contentStream; + } + + /** + * Gets the length of the content if it is provided as a stream. + * @return The length of the content. + */ + public long getContentLength() { + return contentLength; + } + + /** + * Gets the headers of the HTTP request. + * @return A map of headers. + */ + public Map getHeaders() { + return headers; + } + + /** + * Indicates if redirects should be followed. + * @return True if redirects are followed, false otherwise. + */ + public boolean getFollowRedirects() { + return followRedirects; + } + + /** + * Indicates if credentials are included in cross-origin requests. + * @return True if credentials are included, false otherwise. + */ + public boolean getIncludeCredentials() { + return includeCredentials; + } + + /** + * Resets the request to its initial state. + */ + public void reset() { + httpMethod = null; + url = null; + headers.clear(); + timeout = 0; + + content = null; + contentStream = null; + contentLength = 0; + + followRedirects = true; + includeCredentials = false; + } +} diff --git a/src/main/java/com/pixelgamelibrary/api/net/http/HttpResponse.java b/src/main/java/com/pixelgamelibrary/api/net/http/HttpResponse.java new file mode 100644 index 0000000..538ff56 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/http/HttpResponse.java @@ -0,0 +1,43 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.http; + +import com.pixelgamelibrary.api.Disposable; +import java.io.InputStream; +import java.util.List; +import java.util.Map; + +/** + * @author robertvokac + */ +public interface HttpResponse extends Disposable { + + byte[] getResult(); + + String getResultAsString(); + + InputStream getResultAsStream(); + + HttpStatusCode getStatus(); + + String getHeader(String name); + + Map> getHeaders(); +} diff --git a/src/main/java/com/pixelgamelibrary/api/net/http/HttpResponseListener.java b/src/main/java/com/pixelgamelibrary/api/net/http/HttpResponseListener.java new file mode 100644 index 0000000..f0c4c74 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/http/HttpResponseListener.java @@ -0,0 +1,35 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.http; + +import com.pixelgamelibrary.api.net.sockets.Socket; + +/** + * @author robertvokac + */ +public interface HttpResponseListener extends Socket { + + void handleHttpResponse(HttpResponse httpResponse); + + void failed(Throwable t); + + void cancelled(); + +} diff --git a/src/main/java/com/pixelgamelibrary/api/net/http/HttpStatusCode.java b/src/main/java/com/pixelgamelibrary/api/net/http/HttpStatusCode.java new file mode 100644 index 0000000..5634956 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/http/HttpStatusCode.java @@ -0,0 +1,290 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.http; + +import com.pixelgamelibrary.api.PixelException; +import java.util.Arrays; +import java.util.function.Supplier; +import lombok.Getter; + +/** + * Enum representing various HTTP status codes utilized in responses for + * {@link HttpRequest}. Each constant is associated with its respective integer + * value, and they are categorized based on the type of response they represent. + */ +public enum HttpStatusCode { + + // --- 1xx Informational Responses --- + /** + * Indicates that the initial part of a request has been received and the + * client should continue with the request. + */ + CONTINUE(100), + /** + * Indicates that the server is switching protocols as requested by the + * client, as per the client's request. + */ + SWITCHING_PROTOCOLS(101), + /** + * Indicates that the server has received and is processing the request, but + * no response is available yet. + */ + PROCESSING(102), + // --- 2xx Successful Responses --- + /** + * Indicates that the request was successful and the server has returned the + * requested resource. + */ + OK(200), + /** + * Indicates that a new resource has been created in response to a POST + * request. + */ + CREATED(201), + /** + * Indicates that the request has been accepted for processing, but the + * processing is not complete. + */ + ACCEPTED(202), + /** + * Indicates that the server successfully processed the request, but is + * returning information that may differ from the original request. + */ + NON_AUTHORITATIVE_INFORMATION(203), + /** + * Indicates that the server has successfully processed the request, but is + * not returning any content. + */ + NO_CONTENT(204), + /** + * Indicates that the server has successfully processed the request and + * wants the client to reset the view. + */ + RESET_CONTENT(205), + /** + * Indicates that the server is delivering only part of the resource due to + * a range header sent by the client. + */ + PARTIAL_CONTENT(206), + /** + * Indicates multiple status responses for WebDAV. + */ + MULTI_STATUS(207), + // --- 3xx Redirection Responses --- + /** + * Indicates multiple options for the resource that the client can choose. + */ + MULTIPLE_CHOICES(300), + /** + * Indicates that the requested resource has been permanently moved to a new + * URI. + */ + MOVED_PERMANENTLY(301), + /** + * Indicates that the requested resource has been temporarily moved to a + * different URI. + */ + MOVED_TEMPORARILY(302), + /** + * Indicates that the client should perform a GET request to a different URI + * to retrieve the resource. + */ + SEE_OTHER(303), + /** + * Indicates that the resource has not been modified since the last request, + * so the client can use the cached version. + */ + NOT_MODIFIED(304), + /** + * Indicates that the requested resource must be accessed through a proxy. + */ + USE_PROXY(305), + /** + * Indicates that the client must perform a temporary redirect to a + * different URI. + */ + TEMPORARY_REDIRECT(307), + // --- 4xx Client Error Responses --- + /** + * Indicates that the server cannot process the request due to a client + * error (e.g., malformed request). + */ + BAD_REQUEST(400), + /** + * Indicates that the request requires user authentication and the client + * has not provided it. + */ + UNAUTHORIZED(401), + /** + * Indicates that the request was valid, but the server is refusing to + * process it. + */ + PAYMENT_REQUIRED(402), + /** + * Indicates that the server understands the request but refuses to + * authorize it. + */ + FORBIDDEN(403), + /** + * Indicates that the requested resource could not be found on the server. + */ + NOT_FOUND(404), + /** + * Indicates that the method specified in the request is not allowed for the + * resource. + */ + METHOD_NOT_ALLOWED(405), + /** + * Indicates that the requested resource is not acceptable for the client + * according to the Accept headers. + */ + NOT_ACCEPTABLE(406), + /** + * Indicates that the client must authenticate with the proxy before the + * requested resource can be retrieved. + */ + PROXY_AUTHENTICATION_REQUIRED(407), + /** + * Indicates that the server timed out waiting for the request. + */ + REQUEST_TIMEOUT(408), + /** + * Indicates that the request could not be completed due to a conflict with + * the current state of the resource. + */ + CONFLICT(409), + /** + * Indicates that the requested resource is no longer available. + */ + GONE(410), + /** + * Indicates that the request requires a valid Content-Length header. + */ + LENGTH_REQUIRED(411), + /** + * Indicates that one or more conditions in the request header fields + * evaluated to false. + */ + PRECONDITION_FAILED(412), + /** + * Indicates that the server is refusing to process the request because the + * request payload is too large. + */ + REQUEST_TOO_LONG(413), + /** + * Indicates that the URI provided was too long for the server to process. + */ + REQUEST_URI_TOO_LONG(414), + /** + * Indicates that the server refuses to accept the request because the + * payload format is not supported. + */ + UNSUPPORTED_MEDIA_TYPE(415), + /** + * Indicates that the server cannot serve the requested range for the + * resource. + */ + REQUESTED_RANGE_NOT_SATISFIABLE(416), + /** + * Indicates that the server cannot meet the requirements of the Expect + * request-header field. + */ + EXPECTATION_FAILED(417), + // --- 5xx Server Error Responses --- + /** + * Indicates that the server encountered an unexpected condition that + * prevented it from fulfilling the request. + */ + INTERNAL_SERVER_ERROR(500), + /** + * Indicates that the server does not support the functionality required to + * fulfill the request. + */ + NOT_IMPLEMENTED(501), + /** + * Indicates that the server, while acting as a gateway, received an invalid + * response from the upstream server. + */ + BAD_GATEWAY(502), + /** + * Indicates that the server is currently unable to handle the request due + * to temporary overloading or maintenance of the server. + */ + SERVICE_UNAVAILABLE(503), + /** + * Indicates that the server, while acting as a gateway, did not receive a + * timely response from the upstream server. + */ + GATEWAY_TIMEOUT(504), + /** + * Indicates that the server does not support the HTTP protocol version that + * was used in the request. + */ + HTTP_VERSION_NOT_SUPPORTED(505), + /** + * Indicates insufficient storage space on the server to complete the + * request. + */ + INSUFFICIENT_STORAGE(507), + // --- Special HTTP Codes --- + /** + * Indicates that the server has a variant configuration that is not + * appropriate for the request. + */ + VARIANT_ALSO_NEGOTIATES(506), + /** + * Indicates that the client needs to authenticate to gain network access. + */ + NETWORK_AUTHENTICATION_REQUIRED(511); + + @Getter + private final int statusCode; + + /** + * Private constructor to initialize the HTTP status code. + * + * @param statusCode the integer value representing the HTTP status code. + */ + HttpStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + /** + * Retrieves the integer value associated with this HTTP status code. + * + * @return the HTTP status code as an integer. + */ + public int getStatusCode() { + return statusCode; + } + + public static HttpStatusCode ofCode(int statusCode) { + return Arrays + .asList(HttpStatusCode.values()) + .stream() + .filter(e -> e.getStatusCode() == statusCode) + .findFirst() + .orElseThrow(() -> new PixelException("There is no such status code: " + statusCode)); + } + public int getFirstDigit() { + return Integer.parseInt(String.valueOf(statusCode).substring(0, 1)); +} + +} diff --git a/src/main/java/com/pixelgamelibrary/api/net/sockets/ClientSocket.java b/src/main/java/com/pixelgamelibrary/api/net/sockets/ClientSocket.java new file mode 100644 index 0000000..df917ca --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/sockets/ClientSocket.java @@ -0,0 +1,36 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.sockets; + +import java.io.InputStream; +import java.io.OutputStream; + +/** + * @author robertvokac + */ +public interface ClientSocket extends Socket { + + boolean isConnected(); + + InputStream getInputStream(); + + OutputStream getOutputStream(); + +} diff --git a/src/main/java/com/pixelgamelibrary/api/net/sockets/ClientSocketHints.java b/src/main/java/com/pixelgamelibrary/api/net/sockets/ClientSocketHints.java new file mode 100644 index 0000000..913096f --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/sockets/ClientSocketHints.java @@ -0,0 +1,41 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.sockets; + +/** + * @author robertvokac + */ +public class ClientSocketHints { + public static final ClientSocketHints getDefault() { + return new ClientSocketHints(); + } + + public int connectionMsTimeout = 5000; + public SocketPerformancePreferences performancePreferences = new SocketPerformancePreferences(); + public int trafficClass = 0x14; + + public boolean keepAlive = true; + public boolean tcpNoDelay = true; + public int sendBufferSize = 4096; + public int receiveBufferSize = 4096; + public boolean linger = false; + public int lingerDuration = 0; + public int socketTimeout = 0; +} diff --git a/src/main/java/com/pixelgamelibrary/api/audio/AudioDevice.java b/src/main/java/com/pixelgamelibrary/api/net/sockets/ServerSocket.java similarity index 84% rename from src/main/java/com/pixelgamelibrary/api/audio/AudioDevice.java rename to src/main/java/com/pixelgamelibrary/api/net/sockets/ServerSocket.java index 3a10c86..c146b48 100644 --- a/src/main/java/com/pixelgamelibrary/api/audio/AudioDevice.java +++ b/src/main/java/com/pixelgamelibrary/api/net/sockets/ServerSocket.java @@ -13,17 +13,16 @@ // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License -// along with this program. If not, see +// along with this program. If not, see // or write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. /////////////////////////////////////////////////////////////////////////////////////////////// - -package com.pixelgamelibrary.api.audio; +package com.pixelgamelibrary.api.net.sockets; /** - * * @author robertvokac */ -public interface AudioDevice { - +public interface ServerSocket extends Socket { + + ClientSocket accept(ClientSocketHints hints); } diff --git a/src/main/java/com/pixelgamelibrary/api/net/sockets/ServerSocketHints.java b/src/main/java/com/pixelgamelibrary/api/net/sockets/ServerSocketHints.java new file mode 100644 index 0000000..dec7e53 --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/sockets/ServerSocketHints.java @@ -0,0 +1,36 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.sockets; + +/** + * @author robertvokac + */ +public class ServerSocketHints { + + public static final ServerSocketHints getDefault() { + return new ServerSocketHints(); + } + + public int backlog = 16; + public SocketPerformancePreferences performancePreferences = new SocketPerformancePreferences(); + public boolean reuseAddress = true; + public int acceptTimeout = 5000; + public int receiveBufferSize = 4096; +} diff --git a/src/main/java/com/pixelgamelibrary/api/audio/AudioRecorder.java b/src/main/java/com/pixelgamelibrary/api/net/sockets/Socket.java similarity index 81% rename from src/main/java/com/pixelgamelibrary/api/audio/AudioRecorder.java rename to src/main/java/com/pixelgamelibrary/api/net/sockets/Socket.java index 2da200b..8214380 100644 --- a/src/main/java/com/pixelgamelibrary/api/audio/AudioRecorder.java +++ b/src/main/java/com/pixelgamelibrary/api/net/sockets/Socket.java @@ -13,17 +13,20 @@ // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License -// along with this program. If not, see +// along with this program. If not, see // or write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. /////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.sockets; -package com.pixelgamelibrary.api.audio; +import com.pixelgamelibrary.api.Disposable; /** - * * @author robertvokac */ -public interface AudioRecorder { - +public interface Socket extends Disposable { + + String getRemoteHostNameOrIpAddress(); + + int getPort(); } diff --git a/src/main/java/com/pixelgamelibrary/api/net/sockets/SocketPerformancePreferences.java b/src/main/java/com/pixelgamelibrary/api/net/sockets/SocketPerformancePreferences.java new file mode 100644 index 0000000..0d697db --- /dev/null +++ b/src/main/java/com/pixelgamelibrary/api/net/sockets/SocketPerformancePreferences.java @@ -0,0 +1,33 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// Pixel: Game library. +// Copyright (C) 2024 the original author or authors. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation, either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// or write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +/////////////////////////////////////////////////////////////////////////////////////////////// +package com.pixelgamelibrary.api.net.sockets; + +import com.pixelgamelibrary.api.Importance; + +/** + * @author robertvokac + */ +public class SocketPerformancePreferences { + + public Importance connectionTime = Importance.MEDIUM; + public Importance latency = Importance.HIGH; + public Importance bandwidth = Importance.MEDIUM; + +}