diff --git a/.idea/encodings.xml b/.idea/encodings.xml index aa00ffa..3ef4afe 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -1,6 +1,8 @@ + + diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 0000000..e6da414 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6c5c2d6..eaa4059 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,6 +8,7 @@ diff --git a/.idea/runConfigurations/Main.xml b/.idea/runConfigurations/Main.xml deleted file mode 100644 index e4f3fdc..0000000 --- a/.idea/runConfigurations/Main.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/runConfigurations/io_github_naveenb2004_SampleProject_Main.xml b/.idea/runConfigurations/io_github_naveenb2004_SampleProject_Main.xml new file mode 100644 index 0000000..e74bf6e --- /dev/null +++ b/.idea/runConfigurations/io_github_naveenb2004_SampleProject_Main.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/Docs/SampleProject/pom.xml b/Docs/SampleProject/pom.xml new file mode 100644 index 0000000..705cad5 --- /dev/null +++ b/Docs/SampleProject/pom.xml @@ -0,0 +1,76 @@ + + + + + 4.0.0 + + io.github.naveenb2004 + SampleProject + 1.1.0 + + SampleProject + Sample codes for Socket Data Handler! + https://github.com/NaveenB2004/SocketDataHandler + 2024 + + + 11 + ${java.version} + ${java.version} + UTF-8 + + 1.18.30 + 1.1.0 + + + + + org.projectlombok + lombok + ${ver.lombok} + + + io.github.naveenb2004 + SocketDataHandler + 1.1.0 + + + + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + + NaveenB2004 + Naveen Balasooriya + naveennbalasooriya2004@gmail.com + https://github.com/NaveenB2004 + UTC+05:30 + + + https://t.me/NaveenB2004 + + + + \ No newline at end of file diff --git a/src/test/java/Client/Client.java b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Client/Client.java similarity index 93% rename from src/test/java/Client/Client.java rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Client/Client.java index 45b22bf..4b91e7b 100644 --- a/src/test/java/Client/Client.java +++ b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Client/Client.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package Client; +package io.github.naveenb2004.SampleProject.Client; import java.io.IOException; import java.net.Socket; public class Client { - public void connect(int port) { + public static void connect(int port) { try { // connect to the server Socket socket = new Socket("localhost", port); diff --git a/src/test/java/Client/ClientHelper.java b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Client/ClientHelper.java similarity index 92% rename from src/test/java/Client/ClientHelper.java rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Client/ClientHelper.java index c879c24..5884bdd 100644 --- a/src/test/java/Client/ClientHelper.java +++ b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Client/ClientHelper.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package Client; +package io.github.naveenb2004.SampleProject.Client; -import Common.SampleObject; +import io.github.naveenb2004.SampleProject.Common.SampleObject; import io.github.naveenb2004.SocketDataHandler.DataHandler; import io.github.naveenb2004.SocketDataHandler.DataProcessor; import io.github.naveenb2004.SocketDataHandler.SocketDataHandler; @@ -67,7 +67,8 @@ protected void sendText() { // send sample file protected void sendFile() { System.out.println("Client : Sending File..."); - File file = new File("src/test/java/Common/SampleFile.jpg"); + File file = new File( + "Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Common/SampleFile.jpg"); DataHandler dataHandler = new DataHandler("/SendFile", file); try { diff --git a/src/test/java/Common/SampleFile.jpg b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Common/SampleFile.jpg similarity index 100% rename from src/test/java/Common/SampleFile.jpg rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Common/SampleFile.jpg diff --git a/src/test/java/Common/SampleObject.java b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Common/SampleObject.java similarity index 94% rename from src/test/java/Common/SampleObject.java rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Common/SampleObject.java index a572a09..aadecec 100644 --- a/src/test/java/Common/SampleObject.java +++ b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Common/SampleObject.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package Common; +package io.github.naveenb2004.SampleProject.Common; import lombok.Builder; import lombok.Getter; diff --git a/src/test/java/Main.java b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Main.java similarity index 57% rename from src/test/java/Main.java rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Main.java index 9894dfc..db33166 100644 --- a/src/test/java/Main.java +++ b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Main.java @@ -14,6 +14,24 @@ * limitations under the License. */ +package io.github.naveenb2004.SampleProject;/* + * Copyright 2024 Naveen Balasooriya + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import io.github.naveenb2004.SampleProject.Client.Client; +import io.github.naveenb2004.SampleProject.Server.Server; import io.github.naveenb2004.SocketDataHandler.SocketDataHandler; import lombok.SneakyThrows; @@ -30,9 +48,9 @@ public static void main(String[] args) { // set default temporary folder for files downloading SocketDataHandler.setTempFolder(new File("Temp")); - new Server.Server().connect(port); + Server.connect(port); Thread.sleep(3000); - new Client.Client().connect(port); + Client.connect(port); } } diff --git a/src/test/java/Server/Server.java b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Server/Server.java similarity index 94% rename from src/test/java/Server/Server.java rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Server/Server.java index 891604f..adfd47c 100644 --- a/src/test/java/Server/Server.java +++ b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Server/Server.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package Server; +package io.github.naveenb2004.SampleProject.Server; import lombok.Cleanup; @@ -23,7 +23,7 @@ import java.net.Socket; public class Server { - public void connect(int port) { + public static void connect(int port) { new Thread(() -> { try { // create new server socket diff --git a/src/test/java/Server/ServerHelper.java b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Server/ServerHelper.java similarity index 97% rename from src/test/java/Server/ServerHelper.java rename to Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Server/ServerHelper.java index c192fea..52605f6 100644 --- a/src/test/java/Server/ServerHelper.java +++ b/Docs/SampleProject/src/main/java/io/github/naveenb2004/SampleProject/Server/ServerHelper.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package Server; +package io.github.naveenb2004.SampleProject.Server; -import Common.SampleObject; +import io.github.naveenb2004.SampleProject.Common.SampleObject; import io.github.naveenb2004.SocketDataHandler.DataHandler; import io.github.naveenb2004.SocketDataHandler.DataProcessor; import io.github.naveenb2004.SocketDataHandler.PreUpdateHandler.PreDataHandler; diff --git a/Docs/User/README.md b/Docs/User/README.md index e69de29..f90562e 100644 --- a/Docs/User/README.md +++ b/Docs/User/README.md @@ -0,0 +1,148 @@ +# User guids for Socket Data Handler + +- [User guids for Socket Data Handler](#user-guids-for-socket-data-handler) + - [Import into the project](#import-into-the-project) + - [Common library settings](#common-library-settings) + - [Setting up the receiver](#setting-up-the-receiver) + - [Connect socket and library](#connect-socket-and-library) + - [Receiving pre-updates](#receiving-pre-updates) + - [Send data](#send-data) + - [Close socket](#close-socket) + +## Import into the project + +[![Central Repository](https://img.shields.io/maven-central/v/io.github.naveenb2004/SocketDataHandler +)](https://central.sonatype.com/artifact/io.github.naveenb2004/SocketDataHandler) +[![Minimum JDK version](https://img.shields.io/badge/Minumum_JDK-v11-green)](#)\ +If you are using project managers like `Maven`, `Gradle` etc., please take a look at +the [Central repository page for this project](https://central.sonatype.com/artifact/io.github.naveenb2004/SocketDataHandler). +You can find most of the import snippets for project managers from there. For `Maven`: + +```xml + + + io.github.naveenb2004 + SocketDataHandler + + +``` + +## Common library settings + +In this library, you can set up maximum IO buffer size for data chunks (in `bytes`) and default location for work +with `Files`. To do that place this code before any initialization of the `SocketDataHandler`. + +```java +// set default maximum buffer size for chunks +SocketDataHandler.setDefaultBufferSize(1024); + +// set default temporary folder for files downloading +SocketDataHandler.setTempFolder(new File("Temp")); +``` + +## Setting up the receiver + +Now you're good to code your receiver file. To do that, make a class that `extends` the `SocketDataHandler` and +implement the required constructors and methods in that class. + +> MySocket.java + +```java +public class MySocket extends SocketDataHandler { + public MySocket(Socket SOCKET) { + super(SOCKET); + } + + @Override + public void onUpdateReceived(DataHandler update) { + + } + + @Override + public void onDisconnected(DataProcessor.DisconnectStatus status, Exception exception) { + + } +} +``` + +All completed updates are pushed to the `onUpdateReceived(DataHandler update)` method and if socket disconnected (either +end) it will push to `onDisconnected(DataProcessor.DisconnectStatus status, Exception exception)` method. + +## Connect socket and library + +`SocketDataHandler` is just a library to manage socket IO operations. So you need to work on your network operations. + +```java +// connect to the server +Socket socket = new Socket("localhost", port); + +// connect socket with library +MySocket clientSocket = new MySocket(socket); +``` + +## Receiving pre-updates + +`Pre-update` means that uncompleted, processing (receiving or sending) update. You can only get pre-updates +for `Serializable Objects` and `Files`. To do that you need to implement a `pre-updates watcher` class and register it +with the `receiver` class that `extends` the `SocketDataHandler`. + +> MyPreUpdateWatcher.java + +```java +public class MyPreUpdateReceiver implements PreUpdateWatcher { + @Override + public void onPreUpdateSeen(PreDataHandler preUpdate) { + + } +} +``` + +> MySocket.java + +```java +public class MySocket extends SocketDataHandler { + public MySocket(Socket SOCKET) { + super(SOCKET); + // register the watcher + PreUpdateHandler watcher = getPRE_UPDATE_HANDLER(); + watcher.addWatcher(new MyPreUpdateReceiver()); + } + + @Override + public void onUpdateReceived(DataHandler update) { + + } + + @Override + public void onDisconnected(DataProcessor.DisconnectStatus status, Exception exception) { + + } +} +``` + +## Send data + +You can use `DataHandler` object to store your request and use `send` method to send it. Note that the `send` method is +part of your receiver class that `extends` with the `SocketDataHandler`. + +```java +DataHandler dataHandler = new DataHandler("/SendText"); +try { + clientSocket.send(dataHandler); +} catch (IOException e) { + throw new RuntimeException(e); +} +``` + +## Close socket + +You can either direct close the socket or use `close` method of your receiver class that `extends` with +the `SocketDataHandler`. On successful close, you will receive an update for `onDisconneted`. + +```java +try { + clientSocket.close(); +} catch (IOException e) { + throw new RuntimeException(e); +} +``` \ No newline at end of file diff --git a/README.md b/README.md index e405d03..c4a3206 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ You can find most of the import snippets for project managers from there. For `M ## Usage - Please refer to the [user guids](Docs/User/README.md) to know how this library is going to make your work easy 😎. If - you feel too lazy to read docs (not recommended), here are [some example codes](src/test/java) for you! + you feel too lazy to read docs (not recommended), here are [some example codes](Docs/SampleProject) for you! - Please refer to the [developer guids](Docs/Dev/README.md) to know how developers are going to smile (or maybe laugh at me) 😁.