Skip to content

Commit

Permalink
Added multi-cast and more API comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphfrk committed Apr 25, 2011
1 parent bc340ec commit 84c8e03
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 50 deletions.
Binary file modified EventLink.jar
Binary file not shown.
Binary file added old_jar_releases/Release_7/EventLink.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions src/com/raphfrk/bukkit/eventlink/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public void run() {
if(endIn) {
p.log("Closing connection to " + serverName);
connectionManager.activeConnections.remove(serverName);
p.routingTableManager.clearRoutesThrough(serverName);
}
}
}
Expand Down Expand Up @@ -276,6 +277,7 @@ public void run() {
if(endOut) {
p.log("Closing connection to " + serverName);
connectionManager.activeConnections.remove(serverName);
p.routingTableManager.clearRoutesThrough(serverName);
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/com/raphfrk/bukkit/eventlink/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -85,7 +86,7 @@ boolean sendPacket(EventLinkPacket eventLinkPacket) {

boolean sent = false;

String[] destinationBackup = eventLinkPacket.destinationServer;
String[] destinationBackup = eventLinkPacket.destinationServers;
final int length = destinationBackup.length;

for(int cnt1=0;cnt1<length;cnt1++) {
Expand Down Expand Up @@ -130,7 +131,7 @@ boolean sendPacket(EventLinkPacket eventLinkPacket) {
for(int cnt2=0;cnt2<length2;cnt2++) {
temp[cnt2] = targets.get(cnt2);
}

EventLinkPacket newPacket = new EventLinkPacket(eventLinkPacket, temp);

synchronized(activeConnections) {
Expand Down Expand Up @@ -229,6 +230,7 @@ boolean addConnection(String serverName, Socket s, ObjectInputStream in, ObjectO
}
if(activeConnections.containsKey(serverName) && !activeConnections.get(serverName).getAlive()) {
activeConnections.remove(serverName);
p.routingTableManager.clearRoutesThrough(serverName);
}
if(activeConnections.containsKey(serverName) && activeConnections.get(serverName).getAlive()) {
p.log(serverName + " already has a connection, closing");
Expand Down Expand Up @@ -455,8 +457,18 @@ public void run() {
}

void processPacket(EventLinkPacket eventLinkPacket) {

if(eventLinkPacket == null) {
return;
}

Object payload = eventLinkPacket.payload;
if(eventLinkPacket.destinationServer.length != 1 || (!eventLinkPacket.destinationServer[0].equals(p.serverName))) {

if(eventLinkPacket.destinationServers == null || eventLinkPacket.destinationServers.length == 0) {
return;
} else if(eventLinkPacket.destinationServers.length == 1 && eventLinkPacket.destinationServers[0] == null ) {
return;
} else if(eventLinkPacket.destinationServers.length != 1 || (!eventLinkPacket.destinationServers[0].equals(p.serverName))) {
sendPacket(eventLinkPacket);
} else if(payload instanceof Ping) {
processEvent(eventLinkPacket, (Ping)eventLinkPacket.payload);
Expand Down
110 changes: 103 additions & 7 deletions src/com/raphfrk/bukkit/eventlink/EventLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void addOnlinePlayers() {
routingTableManager.addEntry("players", player.getName());
}
}

private void addWorlds() {
List<World> worlds = getServer().getWorlds();
for(World world : worlds) {
Expand Down Expand Up @@ -209,10 +209,6 @@ private boolean readConfig() {

}

public boolean isAdmin(Player player) {
return player.isOp() || admins.contains(player.getName().toLowerCase());
}

@Override
public boolean onCommand(CommandSender commandSender, Command command, String commandLabel, String[] args) {

Expand Down Expand Up @@ -348,28 +344,128 @@ private void createCertFiles() {
public void log(String message) {
logger.log(message);
}


/**
* Tests if a player is listed on the admin_list for the eventlink.txt file
*
* @param player Player to test
* @return true if the player is on the admin list or is an Op
*/

public boolean isAdmin(Player player) {
return player.isOp() || admins.contains(player.getName().toLowerCase());
}

/**
* Sends an event to a single server servers.
*
* NOTE: The packet may be lost en-route even if the function returns true
*
* @param target destination servers.
* @return true if a route exists to the target
*/

public boolean sendEvent(String target, Event event) {
if(connectionManager==null) {
return false;
}
return connectionManager.sendObject(target, event);
}

/**
* Sends an event to multiple servers. This will only send the
* packet once to each directly connected server. This has the
* potential to improve broadcast bandwidth usage.
*
* NOTE: The packet may be lost en-route even if the function returns true
*
* @param target Array of destination servers.
* @return true if a route exists to the target
*/

public boolean sendEvent(String[] target, Event event) {
if(connectionManager==null) {
return false;
}
return connectionManager.sendObject(target, event);
}

/**
* Route entries are markers that can be seen (and routed to) by
* all other servers.
*
* There are 3 reserved table
*
* "servers": The servers that are online
* "players": The players that are online
* "worlds": The worlds for all connected servers
*
* NOTE: If there is a collision, then the closest entry will be routed to.
*
* This means that unique names should be used for servers and worlds.
*
* This tables are auto-updated and shouldn't be modified by other plugins
*
* NOTE: The packet may be lost en-route even if the function returns true
*
*
*/

/**
* Adds a route entry for the local server.
*
* This will be visible by all the other servers in the cluster
*
* @param table This is the table name/type of route entry
* @param name This is the specific name of the entry
* @return true if the entry was added locally
*/

public boolean addRouteEntry(String table, String name) {
return routingTableManager.addEntry(table, name);
}

/**
* Deletes a route entry for the local server.
*
* This will be deleted for all the other servers in the cluster
*
* @param table This is the table name/type of route entry
* @param name This is the specific name of the entry
* @return true if the entry was deleted
*/

public boolean deleteRouteEntry(String table, String name) {
return routingTableManager.deleteEntry(table, name);
}

public String getKeyLocation(String table, String name) {
/**
* Gets the current location of a routing entry
*
* If there is more than one match, the closest entry will be returned
*
* @param table This is the table name/type of route entry
* @param name This is the specific name of the entry
* @return the name of the server where the entry is located
*/

public String getEntryLocation(String table, String name) {
return routingTableManager.getLocation(table, name);

}

public Set<String> copyKeys(String table) {
/**
* Copies the names of all entries in a particular table
*
* The name only appears once, even if there are multiple entries with
* the same name
*
* @param table This is the table name/type of route entry
* @return A set containing all the entry names for the table
*/

public Set<String> copyEntries(String table) {
return routingTableManager.copyKeySet(table);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ String getMessage() {
String getTarget() {
return target;
}

}
83 changes: 66 additions & 17 deletions src/com/raphfrk/bukkit/eventlink/EventLinkPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,97 @@

import java.io.Serializable;
import java.util.Arrays;
import java.util.Random;

public class EventLinkPacket implements Serializable {

final static private int defaultHops = 10;

static Random random = new Random();

private static final long serialVersionUID = 1L;

EventLinkPacket(String sourceServer, String destinationServer, Object payload) {
this(sourceServer, destinationServer, payload, 10);
this(sourceServer, destinationServer, payload, defaultHops);
}

EventLinkPacket(String sourceServer, String[] destinationServers, Object payload) {
this(sourceServer, destinationServers, payload, 10);
this(sourceServer, destinationServers, payload, defaultHops);
}

EventLinkPacket(String sourceServer, String destinationServer, Object payload, int timeToLive) {
this.sourceServer = sourceServer;
this.destinationServer = new String[1];
this.destinationServer[0] = destinationServer;
this.payload = payload;
this.timeToLive = timeToLive;
this(sourceServer, new String[] {destinationServer}, payload, timeToLive, false, false);
}

EventLinkPacket(String sourceServer, String[] destinationServer, Object payload, int timeToLive) {
this(sourceServer, destinationServer, payload, timeToLive, false, false);
}

EventLinkPacket(String sourceServer, String destinationServer, Object payload, int timeToLive, boolean requestConfirm, boolean confirmationPacket) {
this(sourceServer, new String[] {destinationServer}, payload, timeToLive, requestConfirm, confirmationPacket);
}

EventLinkPacket(EventLinkPacket eventLinkPacket, String[] destinationServers) {
this(eventLinkPacket.sourceServer, destinationServers, eventLinkPacket.payload, eventLinkPacket.timeToLive, eventLinkPacket.requestConfirm, eventLinkPacket.confirmationPacket);
}

EventLinkPacket(String sourceServer, String[] destinationServers, Object payload, int timeToLive, boolean requestConfirm, boolean confirmationPacket) {
this.sourceServer = sourceServer;
this.destinationServer = destinationServer;
this.destinationServers = destinationServers;
this.payload = payload;
this.timeToLive = timeToLive;
}

EventLinkPacket(EventLinkPacket eventLinkPacket, String[] destinationServers) {
this.sourceServer = eventLinkPacket.sourceServer;
this.destinationServer = destinationServers;
this.payload = eventLinkPacket.payload;
this.timeToLive = eventLinkPacket.timeToLive;
this.requestConfirm = requestConfirm;
this.confirmationPacket = confirmationPacket;
synchronized(random) {
this.idNum = random.nextLong();
}

if(destinationServers[0] == null) {
try {
throw new RuntimeException();
} catch (Exception e) {
e.printStackTrace();
}
}

}

final public String sourceServer;
final public String[] destinationServer;
final public String[] destinationServers;
final public Object payload;
int timeToLive = 10;

private final boolean requestConfirm;
private boolean confirmationPacket;
private final long idNum;
private long timeStamp = -1;

public String toString() {
return sourceServer + "->" + Arrays.toString(destinationServer) + " [" + payload + "]";
return sourceServer + "->" + Arrays.toString(destinationServers) + " [" + payload + "]";
}

boolean isConfirmRequired() {
return requestConfirm;
}

boolean isConfirmationPacket() {
return confirmationPacket;
}

void setConfirmationPacket(boolean confirmationPacket) {
this.confirmationPacket = confirmationPacket;
}

public long getIdNum() {
return idNum;
}

public long getTimeStamp() {
return timeStamp;
}

public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}

//NOTE: need to reset connection due to object cache
Expand Down
Loading

0 comments on commit 84c8e03

Please sign in to comment.