Handled errors with notification popups

This commit is contained in:
Lorenzo Iovino 2017-06-23 01:35:58 +02:00
parent c54393c2b9
commit 124b672aeb
16 changed files with 1203 additions and 626 deletions

View file

@ -4,13 +4,14 @@ import com.texttwist.client.pages.AuthService;
import com.texttwist.client.pages.Home;
import com.texttwist.client.pages.MatchService;
import com.texttwist.client.pages.SessionService;
import com.texttwist.client.services.ClientNotification;
import com.texttwist.client.services.NotificationClient;
import constants.Config;
import interfaces.INotificationClient;
import interfaces.INotificationServer;
import utilities.Logger;
import javax.swing.*;
import javax.swing.text.Position;
import java.awt.*;
import java.io.File;
import java.io.IOException;
@ -28,6 +29,7 @@ public class App extends JFrame {
public static AuthService authService;
public static SessionService sessionService;
public static MatchService matchService;
public static JFrame app;
public App() throws IOException {
setPreferredSize( new Dimension( 640, 480 ));
@ -47,7 +49,7 @@ public class App extends JFrame {
/* si registra per la callback */
System.out.println("Registering for callback");
INotificationClient callbackObj = new ClientNotification();
INotificationClient callbackObj = new NotificationClient();
INotificationClient stub = (INotificationClient) UnicastRemoteObject.exportObject(callbackObj, 0);
server.registerForCallback(stub);
@ -61,7 +63,12 @@ public class App extends JFrame {
authService = new AuthService();
sessionService = new SessionService();
matchService = new MatchService();
app = this;
Home home = new Home(this);
}
public static Point getWindowsPosition(){
return new Point(app.getX(), app.getY());
}
}

View file

@ -1,10 +1,13 @@
package com.texttwist.client.pages;
import com.texttwist.client.App;
import constants.Palette;
import com.texttwist.client.ui.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.concurrent.Callable;
@ -20,18 +23,6 @@ public class MatchRequests extends Page{
window.setVisible(true);
}
//TODO Spostare i metodi di fetches nella classe MatchRequestService per separare MVC
private DefaultListModel fetchMatches(){
DefaultListModel<String> matchsList = new DefaultListModel<String>();
matchsList.addElement("Pippo ti ha sfidato ------- Accetta/Declina");
matchsList.addElement("Paperino ti ha sfidato ------- Accetta/Declina");
matchsList.addElement("Minnie ti ha sfidato ------- Accetta/Declina");
matchsList.addElement("Luca ti ha sfidato ------- Accetta/Declina");
matchsList.addElement("Gino ti ha sfidato ------- Accetta/Declina");
matchsList.addElement("Filippo ti ha sfidato ------- Accetta/Declina");
matchsList.addElement("Yuri ti ha sfidato ------- Accetta/Declina");
return matchsList;
}
@Override
public void createUIComponents() throws IOException {
@ -51,11 +42,24 @@ public class MatchRequests extends Page{
null,
matchsContainer);
TTScrollList highscoreList = new TTScrollList(
TTScrollList pendingMatches = new TTScrollList(
new Point(20, 60),
new Dimension(520, 142),
fetchMatches(),
App.matchService.pendingList,
matchsContainer);
pendingMatches.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
super.mouseClicked(evt);
JList thisList = (JList)evt.getSource();
if (evt.getClickCount() == 2) {
// Double-click detected
int index = thisList.locationToIndex(evt.getPoint());
App.matchService.joinMatch(App.matchService.pendingList.get(index));
}
}
});
addFooter(root);
addBack(footer,

View file

@ -1,6 +1,7 @@
package com.texttwist.client.pages;
import com.texttwist.client.App;
import com.texttwist.client.ui.TTDialog;
import constants.Config;
import models.Message;
import models.Response;
@ -11,37 +12,109 @@ import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.concurrent.Callable;
/**
* Created by loke on 18/06/2017.
*/
public class MatchService {
public DefaultListModel<String> pendingList = new DefaultListModel<String>();
ByteBuffer buffer = ByteBuffer.allocate(1024);
public MatchService(){
}
public Response play(DefaultListModel<String> userNames) throws IOException {
public void newMatch(String userName) {
//Aggiungi alla lista di inviti
try {
this.addToPendingList(userName);
} catch (IOException e) {
e.printStackTrace();
}
//Visualizza popup
new TTDialog("success", "New invitation from: " + userName + "!",
new Callable() {
@Override
public Object call() throws Exception {
App.matchService.joinMatch(userName);
return null;
}
},
new Callable() {
@Override
public Object call() throws Exception {
return new Menu(Page.window);
}
});
}
public void joinMatch(String userName) {
//Svuota la lista dei match pendenti e joina il match selezionato
this.pendingList.clear();
try {
//Invia tcp req a server per dirgli che sto joinando
new Game(Page.window);
} catch (IOException e) {
e.printStackTrace();
}
}
public Object play(DefaultListModel<String> userNames) throws IOException {
InetSocketAddress socketAddress = new InetSocketAddress(Config.GameServerURI, Config.GameServerPort);
SocketChannel clientSocket = SocketChannel.open(socketAddress);
clientSocket.configureBlocking(false);
Message message = new Message("START_GAME", App.sessionService.account.userName, App.sessionService.account.token, userNames);
byte[] byteMessage = new String(message.toString()).getBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteMessage);
buffer = ByteBuffer.wrap(byteMessage);
clientSocket.write(buffer);
while (true) {
if (clientSocket.read(buffer) != -1) {
buffer.clear();
//Risposta dal server
/*JsonObject data = new JsonObject();
data.put("obj", out);
data.put("unavailableUsers", out);*/
clientSocket.close();
String line = new String(buffer.array(), buffer.position(), buffer.remaining());
System.out.println(line);
if (line.startsWith("MESSAGE")) {
Message msg = Message.toMessage(line);
Response res = new Response("Player unavailable!",400, new JsonObject());
return res;
if (msg.message.equals("USER_NOT_ONLINE")) {
clientSocket.close();
new TTDialog("alert", "Users not online!",
new Callable() {
@Override
public Object call() throws Exception {
return null;
}
}, null);
return null;
}
if (msg.message.equals("INVITES_ALL_SENDED")) {
clientSocket.close();
new TTDialog("success", "Invite all sended!",
new Callable() {
@Override
public Object call() throws Exception {
//In attesa dei giocatori
return new Game(Page.window);
}
}, null);
return null;
}
}
}
}
}
public void addToPendingList(String username) throws IOException {
pendingList.addElement(username);
}
}

View file

@ -80,19 +80,7 @@ public class MatchSetup extends Page{
@Override
public Object call() throws Exception {
//If server response ok, start play, else error
Response res = matchSetupController.play(searchUserBar.list);
if (res.code == 200){
//OK, go to next page and show popup
return new Game(Page.window);
} else {
return new TTDialog("alert", res.message,
new Callable() {
@Override
public Object call() throws Exception {
return null;
}
},null);
}
return matchSetupController.play(searchUserBar.list);
}
});

View file

@ -15,7 +15,7 @@ import java.rmi.RemoteException;
public class MatchSetupController {
public MatchSetupController(){}
public Response play(DefaultListModel<String> userNames) throws RemoteException, NotBoundException, MalformedURLException {
public Object play(DefaultListModel<String> userNames) {
try {
return App.matchService.play(userNames);
} catch (IOException e) {

View file

@ -1,39 +0,0 @@
package com.texttwist.client.services;
import com.texttwist.client.App;
import interfaces.INotificationClient;
import models.Response;
import utilities.Logger;
import javax.swing.*;
import java.rmi.RemoteException;
/**
* Created by loke on 15/06/2017.
*/
public class ClientNotification implements INotificationClient {
public ClientNotification() throws RemoteException {
}
@Override
public Response sendInvite(String userName, DefaultListModel<String> users) throws RemoteException {
Logger.write("Invoked invitation with username=" + userName + "|" + users.toString() );
if(users.contains(App.sessionService.account.userName)){
Logger.write(userName+" ti ha sfidato!");
}
//Aggiungi alla lista di inviti
/* if ((userName != null && !userName.isEmpty()) && (password != null && !password.equals(""))) {
if(AccountsManager.getInstance().register(userName, password)){
Logger.write("Registration successfull");
return new Response("Registration successfull", 200, null);
} else {
Logger.write("Registration unsuccessfull");
return new Response("Registration unsuccessfull: Username exist!", 400, null);
}
}*/
return new Response("Invitation received!", 200, null);
}
}

View file

@ -0,0 +1,36 @@
package com.texttwist.client.services;
import com.texttwist.client.App;
import com.texttwist.client.pages.Game;
import com.texttwist.client.pages.Home;
import com.texttwist.client.pages.Menu;
import com.texttwist.client.pages.Page;
import com.texttwist.client.ui.TTDialog;
import interfaces.INotificationClient;
import models.Response;
import utilities.Logger;
import javax.swing.*;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.concurrent.Callable;
/**
* Created by loke on 15/06/2017.
*/
public class NotificationClient implements INotificationClient {
public NotificationClient() throws RemoteException {
}
@Override
public Response sendInvite(String userName, DefaultListModel<String> users) throws RemoteException {
Logger.write("Invoked invitation with username=" + userName + "|" + users.toString() );
if(users.contains(App.sessionService.account.userName)){
Logger.write(userName+" ti ha sfidato!");
App.matchService.newMatch(userName);
}
return null;
}
}

View file

@ -1,5 +1,6 @@
package com.texttwist.client.ui;
import com.texttwist.client.App;
import constants.Palette;
import javax.swing.*;
@ -16,7 +17,7 @@ public class TTDialog extends JFrame {
public TTDialog(String type, String message, Callable okHandler, Callable cancelHandler) {
setPreferredSize( new Dimension( 450, 200 ));
setSize(new Dimension(450,200));
setLocation(200,300);
setLocation(App.getWindowsPosition().x+100,App.getWindowsPosition().y+150);
setResizable(false);
setAlwaysOnTop(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);