Skip to content

Commit

Permalink
Merge pull request #160 from floralvikings/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
floralvikings committed Feb 27, 2014
2 parents c340cba + 20948ac commit 8dc11f8
Show file tree
Hide file tree
Showing 121 changed files with 2,952 additions and 1,584 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ This database must contain a table called "users" laid out in the following way:
**users**


| username | password | salt | loggedin | xCoord | yCoord | zoneID |
| ------------- | ----------------------------------------------------------------- | ------------------------------------------------ | ---------- | ------ | ------ | ------ |
| TestAccount01 | 8b678bbcf5cf2a60c6dc631b01d6b3c77d142d05eb521a62f73014cc987e0156 | 66db065da6853ec1dafb45933c77b3fdac9ce354a391e8d3 | 0 | 0 | 0 | 0 |
| TestAccount02 | 650f00f552d4df0147d236e240ccfc490444f4b358c4ff1d79f5fd90f57243bd | e3c42b85a183d3f654a3d2bb3bc5ea607d0fb529d9b890d3 | 0 | 0 | 0 | 0 |
| username | password | salt | loggedin | xCoord | yCoord | zoneID |
| ---------------------- | ----------------------------------------------------------------- | ------------------------------------------------ | ---------- | ------ | ------ | ------ |
| TestAccount1 | 8b678bbcf5cf2a60c6dc631b01d6b3c77d142d05eb521a62f73014cc987e0156 | 66db065da6853ec1dafb45933c77b3fdac9ce354a391e8d3 | 0 | 0 | 0 | 0 |
| TestAccount2 | 650f00f552d4df0147d236e240ccfc490444f4b358c4ff1d79f5fd90f57243bd | e3c42b85a183d3f654a3d2bb3bc5ea607d0fb529d9b890d3 | 0 | 0 | 0 | 0 |
| TestAccount...(to 99) | 650f00f552d4df0147d236e240ccfc490444f4b358c4ff1d79f5fd90f57243bd | e3c42b85a183d3f654a3d2bb3bc5ea607d0fb529d9b890d3 | 0 | 0 | 0 | 0 |

Any tests that utilize the login functionality will fail without this table.

Expand All @@ -51,9 +52,7 @@ fields in the MySQL database.

The Jenjin core architecture uses the following unmodified libraries:

* [JUnit](https://github.com/junit-team/junit)
* License: EPL (Commercial Friendly)
* [Hamcrest](https://github.com/hamcrest/JavaHamcrest)
* License: BSD (Commercial Friendly)
* [TestNG](http://testng.org/doc/index.html)
* License: Apache 2.0
* [Drizzle](https://github.com/krummas/DrizzleJDBC)
* License: BSD (Commercial Friendly)
* License: BSD
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ allprojects {


group = 'com.jenjinstudios'
version = '0.4.0-alpha'
version = '0.5.0-alpha'

}

Expand All @@ -22,11 +22,19 @@ subprojects {
}

dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.testng:testng:6.8.7'
}

test {
useTestNG()
maxParallelForks = 8;
workingDir = project.rootDir
beforeTest { descriptor ->
logger.lifecycle("Running test: " + descriptor)
}
afterTest { descriptor ->
logger.lifecycle("Completed test: " + descriptor)
}
}

tasks.withType(Compile) {
Expand Down
6 changes: 0 additions & 6 deletions jenjin-client-world/build.gradle

This file was deleted.

5 changes: 0 additions & 5 deletions jenjin-client/build.gradle

This file was deleted.

5 changes: 5 additions & 0 deletions jenjin-core-client/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
description = ''

dependencies {
compile project(':jenjin-core')
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.jenjinstudios.io.ExecutableMessage;
import com.jenjinstudios.io.Message;
import com.jenjinstudios.io.MessageRegistry;
import com.jenjinstudios.io.MessageType;
import com.jenjinstudios.net.Client;

Expand Down Expand Up @@ -30,7 +29,7 @@ public abstract class ClientExecutableMessage extends ExecutableMessage
protected ClientExecutableMessage(Client client, Message message) {
super(message);

if (!getClass().isAssignableFrom(MessageRegistry.getMessageType(message.getID()).clientExecutableMessageClass))
if (!getClass().isAssignableFrom(client.getMessageRegistry().getMessageType(message.getID()).clientExecutableMessageClass))
throw new IllegalArgumentException("Message supplied to " + getClass().getName() + "is invalid.");

this.client = client;
Expand All @@ -46,7 +45,7 @@ protected ClientExecutableMessage(Client client, Message message) {
public static ExecutableMessage getClientExecutableMessageFor(Client client, Message message) {
ExecutableMessage r = null;

MessageType messageType = MessageRegistry.getMessageType(message.getID());
MessageType messageType = client.getMessageRegistry().getMessageType(message.getID());
Class<? extends ExecutableMessage> execClass = messageType.clientExecutableMessageClass;

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ExecutablePingResponse(Client client, Message message) {
public void runSynced() {
long requestTime = (long) getMessage().getArgument("requestTimeNanos");
long updateTime = getClient().getPeriod() * 1000000;
getClient().addPingTime(System.nanoTime() - requestTime - updateTime);
getClient().addPingTime((System.nanoTime() - requestTime - updateTime) / 1000000);
}

/** Run asynchronous portion of this message. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.jenjinstudios.net;

import com.jenjinstudios.io.Message;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -37,8 +40,11 @@ public class AuthClient extends Client
* @param username The username that will be used by this client.
* @param password The password that will be used by this client.
* @throws java.security.NoSuchAlgorithmException If there is an error generating encryption keys.
* @throws java.io.IOException If there is an IO exception when reading XML files.
* @throws javax.xml.parsers.ParserConfigurationException If there is an error parsing XML files.
* @throws org.xml.sax.SAXException If there is an error parsing XML files.
*/
public AuthClient(String address, int port, String username, String password) throws NoSuchAlgorithmException {
public AuthClient(String address, int port, String username, String password) throws NoSuchAlgorithmException, IOException, SAXException, ParserConfigurationException {
super(address, port);
this.username = username;
this.password = password;
Expand All @@ -51,8 +57,8 @@ public AuthClient(String address, int port, String username, String password) th
public boolean sendBlockingLoginRequest() {
sendLoginRequest();
long startTime = System.currentTimeMillis();
long timepast = System.currentTimeMillis() - startTime;
while (isWaitingForLoginResponse() && (timepast < TIMEOUT_MILLIS))
long timePast = System.currentTimeMillis() - startTime;
while (isWaitingForLoginResponse() && (timePast < TIMEOUT_MILLIS))
{
try
{
Expand All @@ -61,7 +67,7 @@ public boolean sendBlockingLoginRequest() {
{
LOGGER.log(Level.WARNING, "Interrupted while waiting for login response.", e);
}
timepast = System.currentTimeMillis() - startTime;
timePast = System.currentTimeMillis() - startTime;
}
return isLoggedIn();
}
Expand Down Expand Up @@ -100,7 +106,7 @@ private void sendLoginRequest() {
* @return The LoginRequest message.
*/
private Message generateLoginRequest() {// Create the login request.
Message loginRequest = new Message("LoginRequest");
Message loginRequest = new Message(this, "LoginRequest");
loginRequest.setArgument("username", username);
loginRequest.setArgument("password", password);
return loginRequest;
Expand Down Expand Up @@ -129,8 +135,8 @@ public boolean isWaitingForLoginResponse() {
public boolean sendBlockingLogoutRequest() {
sendLogoutRequest();
long startTime = System.currentTimeMillis();
long timepast = System.currentTimeMillis() - startTime;
while (isWaitingForLogoutResponse() && (timepast < TIMEOUT_MILLIS))
long timePast = System.currentTimeMillis() - startTime;
while (isWaitingForLogoutResponse() && (timePast < TIMEOUT_MILLIS))
{
try
{
Expand All @@ -139,14 +145,14 @@ public boolean sendBlockingLogoutRequest() {
{
LOGGER.log(Level.WARNING, "Interrupted while waiting for login response.", e);
}
timepast = System.currentTimeMillis() - startTime;
timePast = System.currentTimeMillis() - startTime;
}
return !isLoggedIn();
}

/** Send a logout request to the server. */
protected void sendLogoutRequest() {
Message logoutRequest = new Message("LogoutRequest");
Message logoutRequest = new Message(this, "LogoutRequest");

// Send the request, continue when response is received.
setWaitingForLogoutResponse(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.jenjinstudios.net;

import com.jenjinstudios.message.ClientExecutableMessage;
import com.jenjinstudios.io.Message;
import com.jenjinstudios.io.MessageRegistry;
import com.jenjinstudios.message.ClientExecutableMessage;

import java.io.IOException;
import java.net.Socket;
Expand All @@ -19,6 +20,8 @@ public class Client extends Connection
{
/** The logger associated with this class. */
public static final Logger LOGGER = Logger.getLogger(Client.class.getName());
/** The number of milliseconds before a blocking method should time out. */
public static long TIMEOUT_MILLIS = 30000;
/** The port over which the client communicates with the server. */
private final int PORT;
/** The address of the server to which this client will connect. */
Expand All @@ -33,20 +36,17 @@ public class Client extends Connection
private PublicKey publicKey;
/** The private key sent to the server. */
private PrivateKey privateKey;
/** The number of milliseconds before a blocking method should time out. */
public static long TIMEOUT_MILLIS = 30000;

/**
* Construct a new client and attempt to connect to the server over the specified port.
* @param address The address of the server to which to connect
* @param port The port over which to connect to the server.
* @throws java.security.NoSuchAlgorithmException If there is an error generating encryption keys.
*/
protected Client(String address, int port) throws NoSuchAlgorithmException {
protected Client(String address, int port) {
ADDRESS = address;
PORT = port;
repeatedSyncedTasks = new LinkedList<>();

setMessageRegistry(new MessageRegistry(false));
try
{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
Expand All @@ -57,7 +57,6 @@ protected Client(String address, int port) throws NoSuchAlgorithmException {
} catch (NoSuchAlgorithmException e)
{
LOGGER.log(Level.SEVERE, "Unable to create RSA key pair!", e);
throw e;
}
}

Expand Down Expand Up @@ -95,14 +94,49 @@ public boolean blockingStart() throws InterruptedException {
@Override
public final void run() {
if (!isConnected()) connect();
// The ClientLoop is used to send messages in the outgoing queue and do syncrhonized executables.

if (!isConnected())
return;
// The ClientLoop is used to send messages in the outgoing queue and do synchronized actions.
sendMessagesTimer = new Timer("Client Update Loop", false);
sendMessagesTimer.scheduleAtFixedRate(new ClientLoop(this), 0, period);

super.run();
}

/** Tell the client threads to stop running. */
public void shutdown() {
super.shutdown();
sendMessagesTimer.cancel();
closeLink();
}

/**
* Add a task to the list of repeated synchronized tasks.
* @param r The task to add.
*/
public void addRepeatedSyncedTask(Runnable r) {
synchronized (repeatedSyncedTasks)
{
repeatedSyncedTasks.add(r);
}
}

/**
* Get the private key.
* @return The private key.
*/
public PrivateKey getPrivateKey() {
return privateKey;
}

/**
* Get the update period of this client.
* @return The update period of this client.
*/
public int getPeriod() {
return period;
}

/**
* Attempt to connect to the server at {@code ADDRESS} over {@code PORT} This method must be called <i>before</i> the
* client thread is started.
Expand All @@ -113,8 +147,7 @@ private void connect() {
try
{
super.setSocket(new Socket(ADDRESS, PORT));
doPostConnectInit();
super.setConnected(true);
super.setConnected(doPostConnectInit());
} catch (IOException ex)
{
LOGGER.log(Level.SEVERE, "Unable to connect to server.", ex);
Expand All @@ -124,28 +157,27 @@ private void connect() {
/**
* Take care of all the necessary initialization messages between client and server. These include things like RSA key
* exchanges and latency checks.
* @return Whether the init was successful.
* @throws IOException If there's an IOException when attempting to communicate with the server.
*/
private void doPostConnectInit() throws IOException {
private boolean doPostConnectInit() throws IOException {
// First, get and process the required FirstConnectResponse message from the server.
Message firstConnectResponse = getInputStream().readMessage();
if (firstConnectResponse == null)
{
return false;
}
int ups = (int) firstConnectResponse.getArgument("ups");
period = 1000 / ups;

// Next, queue up the PublicKeyMessage used to exchange the encrypted AES key used for encryption.
Message publicKeyMessage = new Message("PublicKeyMessage");
Message publicKeyMessage = new Message(this, "PublicKeyMessage");
publicKeyMessage.setArgument("key", publicKey.getEncoded());
queueMessage(publicKeyMessage);

// Finally, send a ping request to establish latency.
sendPing();
}

/** Tell the client threads to stop running. */
public void shutdown() {
super.shutdown();
sendMessagesTimer.cancel();
closeLink();
return true;
}

/**
Expand All @@ -158,27 +190,12 @@ protected ClientExecutableMessage getExecutableMessage(Message message) {
return (ClientExecutableMessage) ClientExecutableMessage.getClientExecutableMessageFor(this, message);
}

/**
* Get the list of repeating tasks.
* @return The list of repeating tasks.
*/
public LinkedList<Runnable> getRepeatedSyncedTasks() {
return repeatedSyncedTasks;
}

/**
* Get the private key.
* @return The private key.
*/
public PrivateKey getPrivateKey() {
return privateKey;
}

/**
* Get the update period of this client.
* @return The update period of this client.
*/
public int getPeriod() {
return period;
/** Run the repeated synchronized tasks. */
void runRepeatedSyncedTasks() {
synchronized (repeatedSyncedTasks)
{
for (Runnable r : repeatedSyncedTasks)
r.run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ public ClientLoop(Client client) {

@Override
public void run() {
for (Runnable r : client.getRepeatedSyncedTasks())
r.run();
for (Runnable r : client.getSyncedTasks())
r.run();

client.runRepeatedSyncedTasks();
client.runSyncedTasks();
client.sendAllMessages();

}

}
Loading

0 comments on commit 8dc11f8

Please sign in to comment.