Skip to content

Commit

Permalink
Event system added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hashes committed Feb 26, 2013
1 parent 6294bc3 commit aa7f014
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 101 deletions.
Binary file added lib/netty-3.6.2.Final.jar
Binary file not shown.
8 changes: 0 additions & 8 deletions src/org/thematics/io/Stream.java

This file was deleted.

78 changes: 0 additions & 78 deletions src/org/thematics/io/networking/Channel.java

This file was deleted.

41 changes: 41 additions & 0 deletions src/org/thematics/io/networking/Handler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.thematics.io.networking;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

public class Handler extends SimpleChannelHandler {

@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) {

}

@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {

}

@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

}

@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) {

}

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent ee) throws Exception {

}

}
49 changes: 49 additions & 0 deletions src/org/thematics/io/networking/ServerChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.thematics.io.networking;

import java.net.InetSocketAddress;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelException;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.thematics.utility.Log;
import org.thematics.utility.Log.Level;

public class ServerChannel {

private static final int PORT= 43594;
private static ServerBootstrap bootstrap;

public ServerChannel() {

}

public void init() {
ChannelFactory factory = new NioServerSocketChannelFactory();

bootstrap = new ServerBootstrap(factory);

Handler handler = new Handler();

ChannelPipeline pipeline = bootstrap.getPipeline();
pipeline.addLast("handler", handler);

bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);

try {
bootstrap.bind(new InetSocketAddress(PORT));
} catch (ChannelException e) {
Log.log("Could not bind the server to port " + PORT + ", shutting down..." , Level.FATAL);
shutdown();
System.exit(0);
}
Log.log("Successfully bound on port " + PORT + ".", Level.INFO);
}

public static final void shutdown() {
bootstrap.shutdown();
bootstrap.releaseExternalResources();
}
}
5 changes: 5 additions & 0 deletions src/org/thematics/server/Executing.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

import org.thematics.server.world.WorldExecuting;

/**
* The concurrency threads used for server tasks.
* @author Guillaume
*
*/
public final class Executing {

/**
Expand Down
11 changes: 7 additions & 4 deletions src/org/thematics/server/Server.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.thematics.server;

import org.thematics.io.FilesManager;
import org.thematics.io.networking.Channel;
import org.thematics.io.networking.ServerChannel;
import org.thematics.server.player.Player;
import org.thematics.server.world.World;
import org.thematics.utility.Log;
Expand All @@ -10,23 +10,26 @@
public class Server {

/**
* Starts the server with the threads,
* the world and everything needed to be
* Starts the server with the threads, the world and everything needed to be
* launched on server launching.
*/
public static void start() {
Log.log("Creating the world...", Level.INFO);
World.getWorld().initWorld();
Log.log("Finished creating the world.", Level.INFO);
Log.log("Initiating network handler...", Level.INFO);
Channel channel = new Channel();
ServerChannel channel = new ServerChannel();
channel.init();
Executing.initExecutors();
}

public static void stopServer() {
Log.log("Closing the server...", Level.WARNING);
for (Player player : World.getWorld().getPlayers())
FilesManager.savePlayer(player);
ServerChannel.shutdown();
Executing.shutdown();
Log.log("Server closed.", Level.INFO);
System.exit(0);
}
}
60 changes: 60 additions & 0 deletions src/org/thematics/server/event/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.thematics.server.event;

import java.util.ArrayList;
import java.util.List;

/**
* Class containing the specifications for the events.
* @author Guillaume
*
*/
public abstract class Event {

/**
* The delay between each event process.
*/
private int delay;

/**
* The events to be processed.
*/
private List<Event> events = new ArrayList<Event>();

/**
* An empty constructor with delay initialized with super() call.
*/
public Event() {
}

/**
* The processed event instance to initialize the delay.
* @param delay
*/
protected Event(int delay) {
this.delay = delay;
}

/**
* Returns the events to be processed.
* @return events
*/
public List<Event> getEvents() {
return events;
}

/**
* The delay between each event process.
* @return delay
*/
public int getDelay() {
return delay;
}

/**
* The event processed every x amount of time
* depending on the delay value.
* @param event
* the event processed.
*/
public abstract void processEvent(Event event);
}
39 changes: 39 additions & 0 deletions src/org/thematics/server/event/EventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.thematics.server.event;

import java.util.concurrent.TimeUnit;

import org.thematics.server.Executing;

public class EventHandler {

/**
* The event instance to be processed.
*/
private Event event;

/**
* Adds an event in the list to be processed and
* initializes the event instance.
* @param event
*/
public void startEvent(Event event) {
event.getEvents().add(event);
this.event = event;
}

/**
* Processes all the events from the list with a delay
* determined with the constructor of the class.
*/
public void process() {
Executing.bigTasks.scheduleAtFixedRate(new Runnable() {

@Override
public void run() {
for (Event events : event.getEvents())
events.processEvent(events);
}

}, 0, event.getDelay(), TimeUnit.MILLISECONDS);
}
}
19 changes: 19 additions & 0 deletions src/org/thematics/server/event/WorldEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.thematics.server.event;


/**
* All the events processed every 2000 ms.
* @author Guillaume
*
*/
public class WorldEvents extends Event {

public WorldEvents() {
super(2000);
}

@Override
public void processEvent(Event event) {

}
}
6 changes: 5 additions & 1 deletion src/org/thematics/server/npc/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public Npc(CombatTypes combatTypes, int hitpoints) {
*/
public Npc() {
this(CombatTypes.MELEE, 10);
}
}

@Override
public void process() {
System.out.println("Npcs process.");
}
}
5 changes: 5 additions & 0 deletions src/org/thematics/server/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.regex.Pattern;

import org.thematics.server.Server;
import org.thematics.server.entity.Combat.CombatTypes;
import org.thematics.server.entity.Entity;
import org.thematics.server.player.skills.Crafting;
Expand Down Expand Up @@ -158,6 +159,10 @@ public void setCharacterName(String characterName) {
this.characterName = characterName;
}

/**
* Returns the skills handler instance.
* @return skillsHandler
*/
public SkillsHandler getSkillsHandler() {
return skillsHandler;
}
Expand Down
7 changes: 0 additions & 7 deletions src/org/thematics/server/tasks/Tasks.java

This file was deleted.

1 change: 0 additions & 1 deletion src/org/thematics/server/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ public void initWorld() {
private World() {

}

}
Loading

0 comments on commit aa7f014

Please sign in to comment.