implementazione persistenza
This commit is contained in:
parent
406a5647b8
commit
eac6e43420
33 changed files with 1876 additions and 736 deletions
|
|
@ -11,5 +11,7 @@
|
|||
<orderEntry type="library" name="com.github.cliftonlabs:json-simple:2.1.2" level="project" />
|
||||
<orderEntry type="module" module-name="Commons" />
|
||||
<orderEntry type="module" module-name="Client" />
|
||||
<orderEntry type="library" name="jedis-2.1.0-sources" level="application" />
|
||||
<orderEntry type="library" name="redis.clients:jedis:2.9.0" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
|
|
@ -5,6 +5,9 @@ import com.texttwist.server.components.GameServer;
|
|||
import com.texttwist.server.components.NotificationServer;
|
||||
import constants.Config;
|
||||
import interfaces.INotificationServer;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import utilities.Logger;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -19,9 +22,11 @@ import java.rmi.server.UnicastRemoteObject;
|
|||
*/
|
||||
public class Server {
|
||||
public static NotificationServer notificationServer;
|
||||
public static JedisPool jedisPool;
|
||||
|
||||
public Server() throws IOException {
|
||||
//Start services
|
||||
Logger logger = new Logger(new File("./server.log"), "Server");
|
||||
Logger logger = new Logger(new File("./notificationServer.log"), "Server");
|
||||
|
||||
Logger.write("Server starting ...");
|
||||
try {
|
||||
|
|
@ -30,6 +35,9 @@ public class Server {
|
|||
Registry authRegistry = LocateRegistry.createRegistry(auth.serverPort);
|
||||
authRegistry.bind("auth", auth);
|
||||
|
||||
//Connecting to Redis server on localhost
|
||||
jedisPool = new JedisPool(new JedisPoolConfig(), "localhost");
|
||||
|
||||
GameServer gameServer = new GameServer(Config.GameServerPort);
|
||||
new Thread(gameServer).start();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import models.User;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by loke on 18/06/2017.
|
||||
|
|
@ -28,49 +26,55 @@ public class AccountsManager {
|
|||
users.add(new User("c","c",0));
|
||||
users.add(new User("d","d",0));
|
||||
|
||||
List<Serializable> l = JedisService.get("users");
|
||||
for(int i=0; i<l.size(); i++) {
|
||||
users.add((User) l.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
public boolean register(String userName, String password) {
|
||||
if(!exists(userName)){
|
||||
return users.add(new User(userName, password,0));
|
||||
User newUser = new User(userName, password,0);
|
||||
Boolean res = users.add(newUser);
|
||||
JedisService.add("users", newUser);
|
||||
return res;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean exists(String userName) {
|
||||
Iterator<User> i = users.iterator();
|
||||
while (i.hasNext()) {
|
||||
if (i.next().userName.equals(userName)) {
|
||||
return true;
|
||||
}
|
||||
Iterator<User> i = users.iterator();
|
||||
while (i.hasNext()) {
|
||||
if (i.next().userName.equals(userName)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public boolean checkPassword(String userName, String password) {
|
||||
Iterator<User> i = users.iterator();
|
||||
while (i.hasNext()) {
|
||||
User account = i.next();
|
||||
if (account.userName.equals(userName) && account.password.equals(password)) {
|
||||
return true;
|
||||
}
|
||||
Iterator<User> i = users.iterator();
|
||||
while (i.hasNext()) {
|
||||
User account = i.next();
|
||||
if (account.userName.equals(userName) && account.password.equals(password)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
public User findUser(String userName){
|
||||
Iterator<User> i = users.iterator();
|
||||
while (i.hasNext()) {
|
||||
User u = i.next();
|
||||
if (u.userName.equals(userName)) {
|
||||
return u;
|
||||
}
|
||||
Iterator<User> i = users.iterator();
|
||||
while (i.hasNext()) {
|
||||
User u = i.next();
|
||||
if (u.userName.equals(userName)) {
|
||||
return u;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int size(){
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ public class GameServer implements Runnable{
|
|||
datagramChannel = DatagramChannel.open();
|
||||
datagramChannel.configureBlocking(true);
|
||||
datagramChannel.connect(address);
|
||||
Logger.write("GamePage Service is running at "+this.serverPort+" port...");
|
||||
Logger.write("Game Service is running at "+this.serverPort+" port...");
|
||||
|
||||
wordsReceiver = new ReceiveWords(datagramChannel, bufferWords, bufferMessages, client);
|
||||
threadPool.submit(wordsReceiver);
|
||||
|
|
|
|||
96
Server/src/com/texttwist/server/components/JedisService.java
Normal file
96
Server/src/com/texttwist/server/components/JedisService.java
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import models.User;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static com.texttwist.server.Server.jedisPool;
|
||||
|
||||
/**
|
||||
* Created by loke on 11/07/2017.
|
||||
*/
|
||||
public class JedisService {
|
||||
|
||||
public JedisService(){
|
||||
|
||||
}
|
||||
|
||||
/** Read the object from Base64 string. */
|
||||
public static Object fromString(String s) throws IOException, ClassNotFoundException {
|
||||
byte [] data = Base64.getDecoder().decode(s);
|
||||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
|
||||
Object o = ois.readObject();
|
||||
ois.close();
|
||||
return o;
|
||||
}
|
||||
|
||||
/** Write the object to a Base64 string. */
|
||||
public static String toString(Serializable o) throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(o);
|
||||
oos.close();
|
||||
return Base64.getEncoder().encodeToString(baos.toByteArray());
|
||||
}
|
||||
|
||||
public static Void add(String key, Serializable o){
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
jedis.append(key, JedisService.toString(o)+"\n");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<Serializable> get(String key){
|
||||
Jedis jedis = null;
|
||||
List<Serializable> l = new ArrayList<>();
|
||||
try {
|
||||
System.out.println("USER ss");
|
||||
|
||||
jedis = jedisPool.getResource();
|
||||
String usersString = jedis.get(key);
|
||||
System.out.println("USER "+usersString);
|
||||
if(usersString!=null) {
|
||||
String[] lines = usersString.split("\n");
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
l.add((User) JedisService.fromString(lines[i]));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public static Void removeAll(String key){
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
jedis = jedisPool.getResource();
|
||||
jedis.del(key);
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.components.AccountsManager;
|
||||
import com.texttwist.server.components.JedisService;
|
||||
import models.User;
|
||||
|
||||
import javax.swing.*;
|
||||
|
|
@ -26,8 +27,10 @@ public class ComputeHighscores implements Callable<DefaultListModel<String>> {
|
|||
return o2.score.compareTo(o1.score);
|
||||
}
|
||||
});
|
||||
JedisService.removeAll("users");
|
||||
for(int i =0; i< AccountsManager.getInstance().users.size(); i++){
|
||||
l.addElement(AccountsManager.getInstance().users.get(i).userName+":"+AccountsManager.getInstance().users.get(i).score);
|
||||
JedisService.add("users",AccountsManager.getInstance().users.get(i));
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,20 @@
|
|||
package com.texttwist.server.tasks;
|
||||
import com.texttwist.client.App;
|
||||
import com.texttwist.server.components.AccountsManager;
|
||||
import com.texttwist.server.components.JedisService;
|
||||
import com.texttwist.server.models.Dictionary;
|
||||
import com.texttwist.server.models.Match;
|
||||
import constants.Config;
|
||||
import models.Message;
|
||||
import models.User;
|
||||
import redis.clients.jedis.Jedis;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.Serializable;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import static com.texttwist.server.components.GameServer.activeMatches;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue