Skip to content

Commit

Permalink
varie aggiunte a Session e creazione sistema di scambio messaggi
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Valsecchi committed Jul 27, 2012
1 parent 19328ef commit aa294ee
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 67 deletions.
72 changes: 56 additions & 16 deletions src/it/valsecchi/polypserver/PolypServer.java
Expand Up @@ -15,7 +15,7 @@
* @author Davide
*
*/
public class PolypServer {
public class PolypServer implements Runnable {

public UsersManager users_manager;
public SessionsManager sessions_manager;
Expand All @@ -26,17 +26,20 @@ public class PolypServer {
private int max_clients;
public String polyp_password;
public String polyp_path;
private boolean run_polyp_server = true;
/** Variabile che controlla la continuazione dell'esecuzione del server */
private boolean run_polyp_server;
private Thread polyp_thread;

public PolypServer(String polyp_name, int port, int max_clients,String password, String path) {
public PolypServer(String polyp_name, int port, int max_clients,
String password, String path) {
users_manager = new UsersManager(this);
sessions_manager = new SessionsManager(this);
files_manager = new FilesManager(this);
this.polyp_name = polyp_name;
this.port = port;
this.max_clients = max_clients;
this.polyp_password= password;
this.polyp_path= path;
this.polyp_password = password;
this.polyp_path = path;
// inizializzo il server
try {
polyp_server = new ServerSocket(this.port, this.max_clients);
Expand All @@ -47,30 +50,67 @@ public PolypServer(String polyp_name, int port, int max_clients,String password,

/**
* Metodo che avvia il server
*
* @throws Exception
*/
public void runPolyp() {
public void startPolyp() throws Exception {
// inizializzo il server
try {
polyp_server = new ServerSocket(this.port, this.max_clients);
} catch (IOException e) {
Log.error("errore creazione polyp_server: " + this.polyp_name);
this.run_polyp_server = false;
throw new Exception("errore creazione polyp_server: "
+ this.polyp_name);
}
this.run_polyp_server = true;
// si avvia su thread separato la ricezione client del server
this.polyp_thread = new Thread(this,
"Thread principale del PolypServer");
this.polyp_thread.start();
// si prosegue consentendo l'interazione con l'utente.
this.startServerShell();
}

/**
* Metodo che ferma il server
*/
public void stopPolyp() {
// si salvano tutti i dati
this.users_manager.writeUsers();
this.files_manager.writeFiles();
// si ferma il ciclo
this.run_polyp_server = false;
//TODO controllare chiusura server
}

@Override
/**
* Metodo che avvia il processo base del server per ricevere richieste client su un thread separato.
*/
public void run() {
// attesa connessioni
while (this.run_polyp_server) {
Log.info("server in attesa di connessioni...");
try {
Socket socket = polyp_server.accept();
Log.info("connessione stabilita con: " +socket.getInetAddress());
//si crea e aggiunge la sessione
Log.info("connessione stabilita con: "
+ socket.getInetAddress());
//si esce se non si può continuare a utilizzare il server
if (this.run_polyp_server == false) {
socket.close();
return;
}
// si crea e aggiunge la sessione
sessions_manager.addSession(socket);
} catch (IOException e) {
Log.error("errore nella connessione al server...");
}
}
}

/**
* Metodo che ferma il server
*/
public void stopPolyp(){
//si salvano tutti i dati
this.users_manager.writeUsers();
//si ferma il ciclo
this.run_polyp_server= false;
private void startServerShell(){

}

}
30 changes: 30 additions & 0 deletions src/it/valsecchi/polypserver/connection/PMessage.java
@@ -0,0 +1,30 @@
package it.valsecchi.polypserver.connection;

/**
* Enumeratore che rappresenta i messaggi che si scambiano PolypServer e Client;
* @author Davide Valsecchi
*
*/
public enum PMessage {

/** Comunicazione andata a buon fine*/
OK,
/** Errore generico*/
ERROR,
/** Errore trasmissione lista file*/
ERRORE_LISTA_FILE,
/** Password scorretta*/
WRONG_PASSWORD,
/** Utente già connesso*/
ALREADY_CONNECTED,
/** Utente correttamente connesso*/
CONNECTED,
/** Utente sincronizzato*/
SYNCHRONIZED;






}
115 changes: 83 additions & 32 deletions src/it/valsecchi/polypserver/connection/Session.java
Expand Up @@ -8,14 +8,11 @@
import it.valsecchi.polypserver.exception.UserAlreadyConnectedException;
import it.valsecchi.polypserver.exception.WrongPasswordException;
import static it.valsecchi.polypserver.Utility.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
Expand Down Expand Up @@ -66,7 +63,7 @@ public void run() {
}
// ora si richiede la lista file e si invia quella del server
try {
this.manageFileList();
this.synchronizeData();
} catch (StreamException e) {
Log.error("errore stream");
this.close();
Expand All @@ -82,12 +79,8 @@ public void run() {
private void listenForRequests() {
try {
while (this.session_active) {
String cmd = this.readString();
switch(cmd){

}


PMessage cmd = this.readMessage();

}
} catch (StreamException e) {

Expand Down Expand Up @@ -128,19 +121,19 @@ public void getUserInfo() throws StreamException, WrongPasswordException,
// password errata
Log.error("User: " + userid + ", password errata!");
// si invia la risposta
this.sendConnectionAnswer("WRONG_PASSWORD");
this.sendMessage(PMessage.WRONG_PASSWORD);
throw e;
}
// si attiva l'utente
boolean connected = server.users_manager.activateUser(userid);
if (connected == false) {
// si chiude perchè è già connesso
Log.error("user già connesso: " + userid);
this.sendConnectionAnswer("ALREADY_CONNECTED");
this.sendMessage(PMessage.ALREADY_CONNECTED);
throw new UserAlreadyConnectedException();
}
// si indica all'utente l'avvenuta connessione
this.sendConnectionAnswer("CONNECTED");
this.sendMessage(PMessage.CONNECTED);
this.user_id = userid;
}

Expand Down Expand Up @@ -169,39 +162,72 @@ public void close() {
* @throws StreamException
* @throws GenericException
*/
private void manageFileList() throws StreamException, GenericException {
private void synchronizeData() throws StreamException, GenericException {
List<String> file_list = null;
try {
file_list = this.readFileList();
} catch (StreamException e) {
// si risponde che c'è stato un'errore
this.sendAnswer("ERRORE_LISTA");
this.sendMessage(PMessage.ERRORE_LISTA_FILE);
throw e;
}
// si invia l'ok
this.sendAnswer("OK");
this.sendMessage(PMessage.OK);
// si invia al file manager per aggiornarla
server.files_manager.setFileList(this.user_id, file_list);
// ora si invia la lista file completa
this.sendFileList();
// si attende la risposta
String ans;
ans = this.readString();
if (ans.equals("OK")) {
// ok la connessione è effettuata
} else if (ans.equals("ERROR")) {
PMessage m = this.readMessage();
if (m == PMessage.OK) {
// ok si prosegue
} else if (m == PMessage.ERROR) {
// Si chiude la connessione
this.close();
Log.error("errore comunicazione lista file");
throw new GenericException();
}
// si invia la lista utenti
this.sendAvaibleUserList();
// si attende la risposta
PMessage m2 = this.readMessage();
if (m2 == PMessage.OK) {
// ok la connessione è effettuata
this.sendMessage(PMessage.SYNCHRONIZED);
//la connessione è effettuata
} else if (m2 == PMessage.ERROR) {
// Si chiude la connessione
this.close();
Log.error("errore comunicazione lista utenti attivi");
throw new GenericException();
}
}

/** Metodo che legge le stringhe inviate dal client */
/** Metodo che legge i messaggi inviati dal client */
private PMessage readMessage() throws StreamException {
try {
return (PMessage) input.readObject();
} catch (ClassNotFoundException e) {
Log.error("errore lettura messaggio");
throw new StreamException("errore lettura messaggio");
} catch (IOException e1) {
Log.error("errore lettura stringa");
throw new StreamException("errore lettura messaggio");
}
}

/**
* Metodo che legge le stringhe inviate dal client
*
* @throws StreamException
*/
private String readString() throws StreamException {
try {
return (String) input.readObject();
} catch (ClassNotFoundException | IOException e) {
} catch (ClassNotFoundException e) {
Log.error("errore lettura stringa");
throw new StreamException("errore lettura stringa");
} catch (IOException e1) {
Log.error("errore lettura stringa");
throw new StreamException("errore lettura stringa");
}
Expand All @@ -210,7 +236,10 @@ private String readString() throws StreamException {
private List<String> readFileList() throws StreamException {
try {
return (List<String>) input.readObject();
} catch (ClassNotFoundException | IOException e) {
} catch (ClassNotFoundException e) {
Log.error("errore lettura lista file");
throw new StreamException("errore lettura lista file");
} catch (IOException e1) {
Log.error("errore lettura lista file");
throw new StreamException("errore lettura lista file");
}
Expand All @@ -225,23 +254,45 @@ public void sendFileList() throws StreamException {
}
}

public void sendConnectionAnswer(String ans) throws StreamException {
public void sendAvaibleUserList() throws StreamException {
try {
output.writeObject(ans);
output.flush();
output.writeObject(server.users_manager.getListAvaibleUser());
} catch (IOException e) {
Log.error("errore invio risposta connessione");
throw new StreamException("errore invio risposta connessione");
Log.error("errore invio lista utenti attivi");
throw new StreamException("errore invio lista file");
}
}

public void sendAnswer(String ans) throws StreamException {
public void sendMessage(PMessage ans) throws StreamException {
try {
output.writeObject(ans);
output.flush();
} catch (IOException e) {
Log.error("errore invio risposta");
throw new StreamException("errore invio risposta");
Log.error("errore invio messaggio");
throw new StreamException("errore invio messaggio");
}
}

protected byte[] readNBytes(int n) throws StreamException{
byte[] buf = new byte[n];
//si leggono
try {
input.read(buf, 0, n);
} catch (IOException e) {
Log.error("errore lettura trasmissione dati");
throw new StreamException("errore lettura trasmissione dati");
}
return buf;
}

protected void writeNBytes(byte[] buf) throws StreamException{
//si scrivono
try {
output.write(buf);
output.flush();
} catch (IOException e) {
Log.error("errore scrittura trasmissione dati");
throw new StreamException("errore scrittura trasmissione dati");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/it/valsecchi/polypserver/connection/SessionsManager.java
Expand Up @@ -4,7 +4,6 @@

import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
Expand All @@ -19,7 +18,7 @@ public class SessionsManager {

public SessionsManager(PolypServer server) {
this.server = server;
sessionsList = new ArrayList<>();
sessionsList = new ArrayList<Session>();
}

/** Metodo che aggiunge alla lista una sessione e la avvia */
Expand Down

0 comments on commit aa294ee

Please sign in to comment.