Skip to content

Commit

Permalink
Merged JSON Read changes with Krager-Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMkrage committed Jun 18, 2017
2 parents 84ca683 + aa849d6 commit 6a10aca
Show file tree
Hide file tree
Showing 14 changed files with 635 additions and 9 deletions.
5 changes: 4 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
'src' : [ 'src', 'test' ],
'libraries' : [
'third_party/junit4-4.11.jar',
'third_party/hamcrest-core-1.3.jar'
'third_party/hamcrest-core-1.3.jar',
'third_party/jackson-core-2.9.0.pr3.jar',
'third_party/jackson-databind-2.9.0.pr3.jar',
'third_party/jackson-annotations-2.9.0.pr3.jar'
],
'separators' : {
'nt' : ';',
Expand Down
42 changes: 38 additions & 4 deletions src/codeu/chat/common/ConversationHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down Expand Up @@ -46,10 +47,10 @@ public void write(OutputStream out, ConversationHeader value) throws IOException
public ConversationHeader read(InputStream in) throws IOException {

return new ConversationHeader(
Uuid.SERIALIZER.read(in),
Uuid.SERIALIZER.read(in),
Time.SERIALIZER.read(in),
Serializers.STRING.read(in)
Uuid.SERIALIZER.read(in),
Uuid.SERIALIZER.read(in),
Time.SERIALIZER.read(in),
Serializers.STRING.read(in)
);

}
Expand Down Expand Up @@ -85,4 +86,37 @@ public String getOwnerUUID() {
public String getCreationTime() {
return creation.toString();
}

@Override
public boolean equals(Object toCompare)
{
if(!(toCompare instanceof ConversationHeader)) {
return false;
}
ConversationHeader toCompareConv = (ConversationHeader)(toCompare);
if(!this.id.equals(toCompareConv.id)) {
return false;
}
if(!this.owner.equals(toCompareConv.owner)) {
return false;
}
if(!this.creation.equals(toCompareConv.creation)) {
return false;
}
if(!this.title.equals(toCompareConv.title)) {
return false;
}
return true;
}

public int hashCode() { return hash(this); }

private static int hash(ConversationHeader conv) {
int hash = 0;
hash+=conv.id.hashCode();
hash+=conv.owner.hashCode();
hash+=conv.creation.hashCode();
hash+=conv.title.hashCode();
return hash;
}
}
44 changes: 44 additions & 0 deletions src/codeu/chat/common/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,48 @@ public String getAuthorUUID() {
public String getCreationTime() {
return creation.toString();
}

@Override
public boolean equals(Object toCompare)
{
if(!(toCompare instanceof Message))
{
return false;
}
Message toCompareMessage = (Message)(toCompare);
if(!this.id.equals(toCompareMessage.id)) {
return false;
}
if(!this.next.equals(toCompareMessage.next)) {
return false;
}
if(!this.previous.equals(toCompareMessage.previous)) {
return false;
}
if(!this.creation.equals(toCompareMessage.creation)) {
return false;
}
if(!this.author.equals(toCompareMessage.author)) {
return false;
}
if(!this.content.equals(toCompareMessage.content)) {
return false;
}
return true;
}

public int hashCode() { return hash(this); }

private static int hash(Message mess) {
int hash = 0;
hash+=mess.id.hashCode();
hash+=mess.next.hashCode();
hash+=mess.previous.hashCode();
hash+=mess.creation.hashCode();
hash+=mess.author.hashCode();
hash+=mess.content.hashCode();

return hash;
}

}
29 changes: 29 additions & 0 deletions src/codeu/chat/common/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,33 @@ public String getUUID() {
public String getCreationTime() {
return creation.toString();
}

@Override
public boolean equals (Object toCompare)
{
if(!(toCompare instanceof User)) {
return false;
}
User toCompareUser = (User)(toCompare);
if(!this.id.equals(toCompareUser.id)) {
return false;
}
if(!this.name.equals(toCompareUser.name)) {
return false;
}
if(!this.creation.equals(toCompareUser.creation)) {
return false;
}
return true;
}

public int hashCode() { return hash(this); }

private static int hash(User user) {
int hash = 0;
hash+=user.id.hashCode();
hash+=user.name.hashCode();
hash+=user.creation.hashCode();
return hash;
}
}
196 changes: 195 additions & 1 deletion src/codeu/chat/server/JSON.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codeu.chat.server;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import codeu.chat.common.ConversationHeader;
Expand All @@ -16,10 +17,20 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import codeu.chat.util.Time;
import codeu.chat.util.Uuid;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

/**
* JSON class used to write and read from the given save file.
*
*
* This file is to be used to allow the server to get a log of all actions completed
* This class saves all inputted commands
* It creates a new command so that the user can then access the log
* This log can be used by the server to re-load all commands from previous
* uses of the server, and to save all users and conversations on the server
*/
public final class JSON {

Expand Down Expand Up @@ -125,4 +136,187 @@ public void save(ConversationHeader conversation) {
// save the object conversation into the array "conversations"
save(conversation, "conversations");
}

public Model createModelForServer()
{
// This function is to be used by the Server class
// If the file, input.json, which holds the transactions
// and commands used into the server at a previous time,
// exists, this means that the server had been used before.
// This will then call readFromFile using that previous
// log, allowing the sever to access all previous transactions
// If the server hasn't been used before, then there have
// been no transactions and therefore there has not yet been a
// file created, causing the IOException to be thrown.
// Other times when this exception can be thrown
// are when the transactions haven't been converted to JSON
// properly. In any of these cases, a new Model instance will be
// created, essentially starting the server without any memory of
// previous transactions/commands.
try
{
// data.json is the predetermined folder in which
// the transaction log will go, located in the Server directory
// The path may need to be updated based on the way the local project is set up
return readFromFile("data.json");
}
catch (IOException e)
{
return new Model();
}
}

public Model readFromFile (String file) throws IOException
{
Model model = new Model();
JsonFactory jsonF = new JsonFactory();
JsonParser jp = null;
try
{
jp = jsonF.createParser(new FileReader(file));
}
catch(IOException e)
{
// If the file is invalid or can't be parsed correctly
// a version of Model without any transaction logs in
// will be used and returned.
System.out.println("Invalid file");
}
if (jp!=null) {
// If the file exists, then the file will be parsed
// in this set of code
jp.nextToken();
while (jp.nextToken() != JsonToken.END_OBJECT)
// This ensures that we haven't reached the end of the
// file because the data is being parsed in such a way
// that this END_OBJECT specifically relates to the
// very last } in the json file
{
if(jp.getText().equals("users")) {
// If the object type represents a User, then
// specific steps will take place to create
// a User variable
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
String name = "";
Uuid id = null;
Time time = null;
jp.nextToken();
if (jp.getText().equals("name")) {
jp.nextToken();
name = jp.getText();
}
jp.nextToken();
if (jp.getText().equals("uuid")) {
jp.nextToken();
try {
id = Uuid.parse(jp.getText());
} catch (IOException e) {
System.out.println("Invalid Uuid");
}
}
jp.nextToken();
if (jp.getText().equals("creationTime")) {
jp.nextToken();
time = Time.fromMs(jp.getLongValue());
}
jp.nextToken();
if (!(name.equals("")) && time != null && id != null)
model.add(new User(id, name, time));
}
continue;
}
if(jp.getText().equals("conversations"))
{
// If the object type represents a Conversation, then
// specific steps will take place to create
// a ConversationHeader variable
jp.nextToken();
while(jp.nextToken()!=JsonToken.END_ARRAY) {
Uuid id = null;
Uuid owner = null;
Time creation = null;
String title = "";
jp.nextToken();
if (jp.getText().equals("title")) {
jp.nextToken();
title = jp.getText();
}
jp.nextToken();
if (jp.getText().equals("uuid")) {
jp.nextToken();
try {
id = Uuid.parse(jp.getText());
} catch (IOException e) {
System.out.println("Invalid Uuid");
}
}
jp.nextToken();
if (jp.getText().equals("ownerUUID")) {
jp.nextToken();
try {
owner = Uuid.parse(jp.getText());
} catch (IOException e) {
System.out.println("Invalid Uuid");
}
}
jp.nextToken();
if (jp.getText().equals("creationTime")) {
jp.nextToken();
creation = Time.fromMs(jp.getLongValue());
}
jp.nextToken();
if (!(title.equals("")) && id != null && owner != null && creation != null)
model.add(new ConversationHeader(id, owner, creation, title));
}
continue;
}
if(jp.getText().equals("messages")) {
// If the object type represents a Message, then
// specific steps will take place to create
// a Message variable
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
Uuid id = null;
Uuid owner = null;
Time creation = null;
String body = "";
jp.nextToken();
if (jp.getText().equals("content")) {
jp.nextToken();
body = jp.getText();
}
jp.nextToken();
if (jp.getText().equals("uuid")) {
jp.nextToken();
try {
id = Uuid.parse(jp.getText());
} catch (IOException e) {
System.out.println("Invalid Uuid");
}
}
jp.nextToken();
if (jp.getText().equals("authorUUID")) {
jp.nextToken();
try {
owner = Uuid.parse(jp.getText());
} catch (IOException e) {
System.out.println("Invalid Uuid");
}
}
jp.nextToken();
if (jp.getText().equals("creationTime")) {
jp.nextToken();
creation = Time.fromMs(jp.getLongValue());
}
jp.nextToken();
if (!(body.equals("")) && id != null && owner != null && creation != null)
model.add(new Message(id, Uuid.NULL, Uuid.NULL, creation, owner, body));
}
}
}
jp.close();
}
return model;
}
}
10 changes: 8 additions & 2 deletions src/codeu/chat/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import codeu.chat.util.Timeline;
import codeu.chat.util.Uuid;
import codeu.chat.util.connections.Connection;
import codeu.chat.common.ServerInfo;
import codeu.chat.common.ServerInfo;

public final class Server {

Expand All @@ -58,7 +58,13 @@ private interface Command {
private final Uuid id;
private final Secret secret;
private ServerInfo info = null;
private final Model model = new Model();
// Changed model initialization
// If there were previous transactions, the createModelForServer
// function will reload the past transactions into this model
// If there haven't, and the server is being used for the first time
// then it will simply create a new variable of type Model
// without any "log" or data pre-loaded into it
private final Model model = new JSON().createModelForServer();
private final View view = new View(model);
private final Controller controller;
private final Relay relay;
Expand Down
Loading

0 comments on commit 6a10aca

Please sign in to comment.