RMI callback for manage the invitations
This commit is contained in:
parent
cedaf8f009
commit
c54393c2b9
43 changed files with 2060 additions and 556 deletions
|
|
@ -2,20 +2,23 @@ package com.texttwist.server;
|
|||
|
||||
import com.texttwist.server.components.Auth;
|
||||
import com.texttwist.server.components.GameServer;
|
||||
import com.texttwist.server.components.NotificationServer;
|
||||
import constants.Config;
|
||||
import interfaces.INotificationServer;
|
||||
import utilities.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.rmi.AlreadyBoundException;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
|
||||
/**
|
||||
* Created by loke on 15/06/2017.
|
||||
*/
|
||||
public class Server {
|
||||
|
||||
public static NotificationServer notificationServer;
|
||||
public Server() throws IOException {
|
||||
//Start services
|
||||
Logger logger = new Logger(new File("./server.log"), "Server");
|
||||
|
|
@ -23,23 +26,30 @@ public class Server {
|
|||
Logger.write("Server starting ...");
|
||||
try {
|
||||
//Definitions of registry for auth
|
||||
Auth auth = new Auth(9999);
|
||||
Registry registry = LocateRegistry.createRegistry(auth.serverPort);
|
||||
registry.bind("auth", auth);
|
||||
Auth auth = new Auth(Config.AuthServerPort);
|
||||
Registry authRegistry = LocateRegistry.createRegistry(auth.serverPort);
|
||||
authRegistry.bind("auth", auth);
|
||||
|
||||
GameServer server = new GameServer(10000);
|
||||
new Thread(server).start();
|
||||
GameServer gameServer = new GameServer(Config.GameServerPort);
|
||||
new Thread(gameServer).start();
|
||||
|
||||
try {
|
||||
/*registrazione presso il registry */
|
||||
notificationServer = new NotificationServer();
|
||||
INotificationServer stub = (INotificationServer) UnicastRemoteObject.exportObject(notificationServer, Config.NotificationServerPort);
|
||||
LocateRegistry.createRegistry(Config.NotificationServerStubPort);
|
||||
Registry notificationRegistry = LocateRegistry.getRegistry(Config.NotificationServerStubPort);
|
||||
notificationRegistry.bind(Config.NotificationServerName, stub);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Eccezione" + e);
|
||||
}
|
||||
Logger.write("Server started");
|
||||
|
||||
} catch (RemoteException e) {
|
||||
System.out.println(e.getMessage());
|
||||
e.printStackTrace();
|
||||
} catch (AlreadyBoundException e) {
|
||||
System.out.println(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,6 @@ public class AccountsManager {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean checkPassword(String userName, String password) {
|
||||
synchronized(accounts) {
|
||||
Iterator<Account> i = accounts.iterator();
|
||||
|
|
|
|||
|
|
@ -62,10 +62,11 @@ public class Auth extends UnicastRemoteObject implements IAuth {
|
|||
public Response logout(String userName, String token) throws RemoteException {
|
||||
Logger.write("Invoked logout with username=" + userName + " AND " + " token=" + token);
|
||||
if ((userName != null && !userName.isEmpty()) && (token != null && !token.isEmpty())) {
|
||||
SessionsManager.getInstance().remove(userName);
|
||||
Logger.write("Logout successfull");
|
||||
boolean res = SessionsManager.getInstance().remove(userName);
|
||||
if(res) {
|
||||
Logger.write("Logout successfull");
|
||||
|
||||
return new Response("Logout successfull", 200, null);
|
||||
}
|
||||
}
|
||||
|
||||
SessionsManager.getInstance().remove(userName);
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ import java.nio.channels.Selector;
|
|||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Iterator;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static java.nio.channels.SelectionKey.OP_ACCEPT;
|
||||
import static java.nio.channels.SelectionKey.OP_READ;
|
||||
|
|
@ -29,7 +28,6 @@ public class GameServer implements Runnable{
|
|||
protected ServerSocketChannel serverSocketChannel = null;
|
||||
protected ThreadProxy proxy;
|
||||
protected Selector selector = null;
|
||||
protected JsonObject msg = null;
|
||||
|
||||
public GameServer(int port){
|
||||
this.serverPort = port;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import interfaces.INotificationClient;
|
||||
import interfaces.INotificationServer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by loke on 19/06/2017.
|
||||
*/
|
||||
public class NotificationServer implements INotificationServer {
|
||||
|
||||
private List<INotificationClient> clients;
|
||||
public NotificationServer() throws RemoteException {
|
||||
super();
|
||||
clients = new ArrayList<INotificationClient>();
|
||||
}
|
||||
|
||||
public synchronized void registerForCallback(INotificationClient clientInterface) throws RemoteException {
|
||||
if(!clients.contains(clientInterface)){
|
||||
clients.add(clientInterface);
|
||||
System.out.print("New client registered");
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void unregisterForCallback(INotificationClient client) throws RemoteException {
|
||||
if(clients.remove(client)) {
|
||||
System.out.print("Client unregistered");
|
||||
} else {
|
||||
System.out.print("Unable to unregister client");
|
||||
}
|
||||
}
|
||||
|
||||
public void update(String username, DefaultListModel<String> users) throws RemoteException {
|
||||
doCallbacks(username, users);
|
||||
}
|
||||
|
||||
private synchronized void doCallbacks(String username, DefaultListModel<String> users) throws RemoteException{
|
||||
System.out.print("Starting callbacks");
|
||||
Iterator i = clients.iterator();
|
||||
while(i.hasNext()){
|
||||
INotificationClient client = (INotificationClient) i.next();
|
||||
|
||||
//Calcola i valori da inviare e inviali
|
||||
client.sendInvite(username, users);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -30,10 +30,17 @@ public class SessionsManager {
|
|||
}
|
||||
|
||||
public boolean remove(String userName){
|
||||
return sessions.remove(exists(userName));
|
||||
if(exists(userName)) {
|
||||
Session s = getSession(userName);
|
||||
if(s != null) {
|
||||
sessions.remove(s);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Session exists(String userName) {
|
||||
public Session getSession(String userName) {
|
||||
synchronized(sessions) {
|
||||
Iterator<Session> i = sessions.iterator();
|
||||
while (i.hasNext()) {
|
||||
|
|
@ -46,6 +53,19 @@ public class SessionsManager {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean exists(String userName) {
|
||||
synchronized(sessions) {
|
||||
Iterator<Session> i = sessions.iterator();
|
||||
while (i.hasNext()) {
|
||||
Session elem = i.next();
|
||||
if (elem.userName.equals(userName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isValidToken(String token) {
|
||||
synchronized(sessions) {
|
||||
|
|
@ -60,6 +80,11 @@ public class SessionsManager {
|
|||
}
|
||||
|
||||
|
||||
public void printSessions() {
|
||||
for(int i = 0; i<sessions.size(); i++){
|
||||
System.out.println(sessions.get(i).toString());
|
||||
}
|
||||
}
|
||||
public int size(){
|
||||
return sessions.size();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,9 @@
|
|||
package com.texttwist.server.components;
|
||||
|
||||
import com.texttwist.server.tasks.CheckOnlineUsers;
|
||||
import com.texttwist.server.tasks.SendInvitations;
|
||||
import models.Message;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
|
|
@ -28,20 +24,41 @@ public class ThreadProxy implements Runnable {
|
|||
public void run() {
|
||||
System.out.println("Selecting right task for new thread");
|
||||
|
||||
System.out.println(request.token);
|
||||
System.out.println(request.sender);
|
||||
System.out.println(request.message);
|
||||
System.out.println(request.data);
|
||||
|
||||
|
||||
|
||||
if(isValidToken(request.token)){
|
||||
switch(request.message){
|
||||
case "START_GAME":
|
||||
Future<Boolean> newTask = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||
Boolean returnedValue = null;
|
||||
Future<Boolean> onlineUsers = threadPool.submit(new CheckOnlineUsers(request.data));
|
||||
Boolean res = null;
|
||||
try {
|
||||
returnedValue = newTask.get();
|
||||
System.out.println(returnedValue);
|
||||
res = onlineUsers.get();
|
||||
SessionsManager.getInstance().printSessions();
|
||||
if(res){
|
||||
Future<Boolean> sendInvitations = threadPool.submit(new SendInvitations(request.sender, request.data));
|
||||
try {
|
||||
res = sendInvitations.get();
|
||||
System.out.println(res);
|
||||
if (res) {
|
||||
System.out.println("SJSJSJSJSJ");
|
||||
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println(returnedValue);
|
||||
|
||||
default:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
package com.texttwist.server.components;
|
||||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.components.SessionsManager;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
|
@ -16,9 +18,10 @@ public class CheckOnlineUsers implements Callable<Boolean> {
|
|||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
System.out.print("Check If users are online!");
|
||||
System.out.println(users);
|
||||
for(int i = 0; i < 1; i++){
|
||||
Thread.sleep(2000);
|
||||
for(int i = 0; i < users.size(); i++){
|
||||
if(!(SessionsManager.getInstance().exists(users.get(i)))){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
38
Server/src/com/texttwist/server/tasks/SendInvitations.java
Normal file
38
Server/src/com/texttwist/server/tasks/SendInvitations.java
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package com.texttwist.server.tasks;
|
||||
|
||||
import com.texttwist.server.Server;
|
||||
import com.texttwist.server.components.NotificationServer;
|
||||
import constants.Config;
|
||||
import interfaces.INotificationServer;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.rmi.registry.LocateRegistry;
|
||||
import java.rmi.registry.Registry;
|
||||
import java.rmi.server.UnicastRemoteObject;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* Created by loke on 19/06/2017.
|
||||
*/
|
||||
public class SendInvitations implements Callable<Boolean> {
|
||||
private final DefaultListModel<String> users;
|
||||
private String sender;
|
||||
|
||||
public SendInvitations( String sender, DefaultListModel<String> users) {
|
||||
this.users = users;
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
System.out.print("SendInvitations!");
|
||||
try {
|
||||
Server.notificationServer.update(sender, users);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Eccezione" + e);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue