Commit ae79939c authored by Jonathan Sudibya's avatar Jonathan Sudibya
Browse files

Message Controller fixed; Saving JSON Object

parent 97f096dc
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2"> <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group> <group>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/TCPClient.java</file> <file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/TCPClient.java</file>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/Connection/Sender.java</file>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/Connection/Listener.java</file>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/client/TCPClient/src/Connection/ConnectionHandler.java</file>
</group> </group>
</open-files> </open-files>
</project-private> </project-private>
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
<group> <group>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/Controller/ConnectionHandler.java</file> <file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/Controller/ConnectionHandler.java</file>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/TCPServer.java</file> <file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/TCPServer.java</file>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/Model/MessageProcessor.java</file>
<file>file:/C:/Users/Jonathan/Documents/NetBeansProjects/GrandQuest/src/server/TCPServer/src/Model/Client.java</file>
</group> </group>
</open-files> </open-files>
</project-private> </project-private>
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Controller;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
*
* @author Jonathan
*/
public class MemoryCached implements Runnable {
private Vector<JSONObject> currentData;
private Vector<JSONObject> dumpData;
private Thread t;
public MemoryCached(){
currentData = new Vector<>();
dumpData = new Vector<>();
}
@Override
public void run() {
FileReader read;
FileWriter write;
String line = null;
JSONParser parser = new JSONParser();
JSONObject data = null;
JSONArray array = null;
try {
read = new FileReader("UserData.JSON"); //open dump and try to read data
if (read != null){ // file is present
BufferedReader bufferReader = new BufferedReader(read);
while ((line = bufferReader.readLine()) != null)
{
}
bufferReader.close();
if (line != null) // data is present
{
Object obj = parser.parse(line);
if (obj != null){
data = (JSONObject) obj;
System.out.println("Message :" + data.toJSONString());
array = (JSONArray) data.get("user");
while (!array.isEmpty()){
dumpData.add((JSONObject)array.remove(0));
}
}
}
}
if (dumpData.isEmpty()) // if data is empty
{
dumpData = currentData;
currentData.clear();
}
else // if data is present update data
{
int i =0;
int j =0;
boolean found = false;
while (!currentData.isEmpty())
{
if (i >= dumpData.size()) // if the data is not is not in the file
{
while (!currentData.isEmpty()){
dumpData.add(currentData.remove(0));
}
}
else{ // checking data with the current dumpData.
j = 0;
found = false;
while(j < currentData.size() && !found)
{
if (dumpData.get(i).get("id") == currentData.get(j).get("id"))
{
dumpData.get(i).put("inventory", currentData.get(i).get("inventory"));
dumpData.get(i).put("location", currentData.get(i).get("location"));
found = true;
currentData.remove(j);
}
else
{
j++;
}
}
i++;
}
}
}
array.clear();
while (!dumpData.isEmpty())
{
array.add(dumpData.remove(0));
}
write = new FileWriter("UserData.JSON");
data.put("user", array);
write.write(data.toJSONString());
write.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(MemoryCached.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MemoryCached.class.getName()).log(Level.SEVERE, null, ex);
} catch (ParseException ex) {
Logger.getLogger(MemoryCached.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void start(){
if (t == null)
{
t = new Thread("DB-Sync");
t.start();
}
else{
System.err.println("Error opening or creating File!\nProcess will terminate!");
}
}
/**
* Add an entire vector of JSONObject. WARNING : It will override the entire vector.
* @param data
*/
public void addData(Vector<JSONObject> data)
{
currentData = data;
}
/**
* Add or update (if exist) a data to the vector.
* @param data
*/
public void addData(JSONObject data)
{
int i = 0;
boolean found = false;
while(i < currentData.size() && !found) //if found update new data
{
if (currentData.get(i).get("id") == data.get("id"))
{
found = true;
currentData.get(i).put("inventory", data.get("inventory"));
currentData.get(i).put("location", data.get("location"));
}
else
{
i++;
}
}
if (!found) //add new data
{
currentData.add(data);
}
}
}
package Model; package Model;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
...@@ -10,7 +8,6 @@ import java.net.Socket; ...@@ -10,7 +8,6 @@ import java.net.Socket;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
...@@ -48,22 +45,17 @@ public class Client implements Runnable { ...@@ -48,22 +45,17 @@ public class Client implements Runnable {
while(active) while(active)
{ {
DataOutputStream outToClient = null; DataOutputStream outToClient = null;
//BufferedReader inFromClient = null;
InputStreamReader inFromClient = null; InputStreamReader inFromClient = null;
String clientSentence = ""; String clientSentence = "";
int buf; int buf;
try { try {
System.out.println("Client ip "+threadName+ " :" +connectionSocket.getRemoteSocketAddress()); System.out.println("Client ip "+threadName+ " :" +connectionSocket.getRemoteSocketAddress());
//connectionSocket.setSoTimeout(3000); connectionSocket.setSoTimeout(3000);
//inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
//clientSentence = inFromClient.readLine();
inFromClient = new InputStreamReader(connectionSocket.getInputStream()); inFromClient = new InputStreamReader(connectionSocket.getInputStream());
while ((buf=inFromClient.read()) != -1){ while ((buf=inFromClient.read()) != -1){
char c = (char)buf; char c = (char)buf; //converting to char
clientSentence += Character.toString(c); clientSentence += Character.toString(c); //added to the sentence and convert char to String.
//System.out.print(c);
} }
//System.out.println();
System.out.println("Received From "+connectionSocket.getInetAddress().getHostAddress()+ ": " + clientSentence); System.out.println("Received From "+connectionSocket.getInetAddress().getHostAddress()+ ": " + clientSentence);
if (!clientSentence.equals("")){ if (!clientSentence.equals("")){
outToClient = new DataOutputStream(connectionSocket.getOutputStream()); outToClient = new DataOutputStream(connectionSocket.getOutputStream());
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import java.util.Vector;
import org.json.simple.JSONObject;
/**
*
* @author Jonathan
*/
public final class MessageProcessor {
public static JSONObject getReply(JSONObject data)
{
JSONObject ret = new JSONObject();
return ret;
}
private JSONObject statusOK()
{
JSONObject ret = new JSONObject();
ret.put("status", "ok");
return ret;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment