Skip to content

Commit

Permalink
Started BungeeCord channels implementation and generic handler list
Browse files Browse the repository at this point in the history
  • Loading branch information
KarmaDeb committed Dec 16, 2023
1 parent 44d3f40 commit dd1efc8
Show file tree
Hide file tree
Showing 15 changed files with 767 additions and 160 deletions.
2 changes: 2 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package es.karmadev.locklogin.api.plugin.service.network;

import es.karmadev.locklogin.api.network.communication.packet.NetworkChannel;
import es.karmadev.locklogin.api.plugin.service.PluginService;
import es.karmadev.locklogin.api.task.FutureTask;

import java.util.Collection;

/**
* Represents a channel provider service. This
* service is responsible for creating {@link es.karmadev.locklogin.api.network.communication.packet.NetworkChannel channels},
* those channels are the ones who manage the
* network communication.
*/
public abstract class ChannelProviderService implements PluginService {

/**
* Get the service name
*
* @return the service name
*/
public final String name() {
return "plugin-messaging";
}

/**
* Get a collection of the registered
* channels from this provider.
*
* @return the provider created channels
*/
public abstract Collection<? extends NetworkChannel> getChannels();

/**
* Get a channel
*
* @param channelName the channel name
* @return the channel. Implementations should
* return an existing channel if there was one
* created with the same name previously. As that
* what the plugin internally expects from the
* channel
*/
public abstract FutureTask<? extends NetworkChannel> getChannel(final String channelName);

/**
* Destroy a channel
*
* @param channelName the channel name to destroy
* @return if the channel was destroyed
*/
public abstract boolean destroyChannel(final String channelName);

/**
* Implementations may relay on 3rd party services
* such as redis to create channels. This method verifies
* that the provider is connected to that service
*
* @return if the provider is connected to all the
* third party services it depends on
*/
public abstract boolean isConnected();
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,15 @@ public boolean isDone() {
* @throws CancellationException if the computation was cancelled
* @throws InterruptedException if the current thread was interrupted
* while waiting
* @throws ExecutionException if the completed task result is an error
*/
@Override
public T get() throws InterruptedException, ExecutionException {
public T get() throws CancellationException, InterruptedException, ExecutionException {
if (result != null) return result;

Thread thread = Thread.currentThread();
synchronized (thread) {
while (result == null) {
while (result == null && error == null) {
if (cancelled) {
throw new CancellationException();
}
Expand All @@ -209,6 +210,7 @@ public T get() throws InterruptedException, ExecutionException {
}
}

if (error != null) throw new ExecutionException(error);
return result;
}

Expand Down Expand Up @@ -243,4 +245,66 @@ public T get(long timeout, @NotNull TimeUnit unit) throws InterruptedException,

return result;
}

/**
* Create and get a completed future for
* the instance
*
* @param element the instance
* @return the completed future
* @param <T> the future type
*/
public static <T> FutureTask<T> completedFuture(final T element) {
FutureTask<T> task = new FutureTask<>();
task.result = element;
task.error = null;

return task;
}

/**
* Create and get a completed future for
* the error
*
* @param error the error
* @return the completed future
* @param <T> the future type
*/
public static <T> FutureTask<T> completedFuture(final Throwable error) {
FutureTask<T> task = new FutureTask<>();
task.result = null;
task.error = error;

return task;
}

/**
* Create and get a completed future
* for the element and error
*
* @param element the element
* @param error the error
* @return the completed future
* @param <T> the future type
*/
public static <T> FutureTask<T> completedFuture(final T element, final Throwable error) {
FutureTask<T> task = new FutureTask<>();
task.result = element;
task.error = error;

return task;
}

/**
* Creates and get a cancelled future
*
* @return the cancelled future
* @param <T> the future type
*/
public static <T> FutureTask<T> cancelledFuture() {
FutureTask<T> task = new FutureTask<>();
task.cancelled = true;

return task;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package es.karmadev.locklogin.bungee.network;

import es.karmadev.locklogin.api.extension.module.Module;
import es.karmadev.locklogin.api.network.communication.packet.NetworkChannel;
import es.karmadev.locklogin.api.network.communication.packet.listener.NetworkListener;
import es.karmadev.locklogin.api.network.communication.packet.listener.event.NetworkEvent;
import es.karmadev.locklogin.api.network.server.packet.NetworkChannelQue;
import es.karmadev.locklogin.common.util.generic.GenericHandlerList;
import lombok.RequiredArgsConstructor;

/**
* Represents a BungeeCord channel. The BungeeCord channel
* is the default implementation which allows communication
* between plugin instances. The implementation is potentially
* compatible with multi-bungee instances, but it's recommended
* to use Redis or Channels instead.
*/
@RequiredArgsConstructor
public class BungeeChannel implements NetworkChannel {

private final String channel;
private final BungeeMessagingQue que = new BungeeMessagingQue();

private final GenericHandlerList handlerList = new GenericHandlerList();

/**
* Get the channel name this
* network channel works at
*
* @return the channel name
*/
@Override
public String getChannel() {
return channel;
}

/**
* Get the channel que
*
* @return the channel que
*/
@Override
public NetworkChannelQue getProcessingQue() {
return que;
}

/**
* Handle a network event
*
* @param event the event to handle
*/
@Override
public void handle(final NetworkEvent event) {
handlerList.handle(event);
}

/**
* Add a listener
*
* @param module the module listener owner
* @param listener the listener
*/
@Override
public void addListener(final Module module, final NetworkListener listener) {
handlerList.addListeners(module, listener);
}

/**
* Remove a single listener
*
* @param listener the listener to remove
*/
@Override
public void removeListener(final NetworkListener listener) {
handlerList.removeListener(listener);
}

/**
* Remove all the listeners registered
* on the module
*
* @param module the module to remove listeners
* from
*/
@Override
public void removeListeners(final Module module) {
handlerList.removeListeners(module);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package es.karmadev.locklogin.bungee.network;

import es.karmadev.locklogin.api.network.server.packet.NetworkChannelQue;
import es.karmadev.locklogin.api.network.server.packet.NetworkPacket;

/**
* Represents the BungeeCord messaging que
*/
public class BungeeMessagingQue implements NetworkChannelQue {

/**
* Append a packet
*
* @param packet the packet to append
* @throws SecurityException if there's no module trying to send the packet
*/
@Override
public void appendPacket(final NetworkPacket packet) throws SecurityException {

}

/**
* Get the next packet
*
* @return the next packet
*/
@Override
public NetworkPacket nextPacket() {
return null;
}

/**
* Get the previous packet
*
* @return the previous packet
*/
@Override
public NetworkPacket previousPacket() {
return null;
}

/**
* Get if the queue is processing a packet
*
* @return if the queue is occupied
*/
@Override
public boolean processing() {
return false;
}

/**
* Cancel the current packet processing, and
* move it to the latest in the queue
*/
@Override
public void shiftPacket() {

}

/**
* Consume the current packet
*/
@Override
public void consumePacket() {

}

/**
* Cancel the current packet processing, and
* do nothing to it
*/
@Override
public void cancelPacket() {

}
}
Loading

0 comments on commit dd1efc8

Please sign in to comment.