Skip to content

Commit

Permalink
lavoro sul progetto
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Valsecchi committed Jul 25, 2012
1 parent 1930797 commit 2a654e4
Show file tree
Hide file tree
Showing 9 changed files with 299 additions and 44 deletions.
4 changes: 3 additions & 1 deletion src/it/valsecchi/polypserver/PolypServer.java
Expand Up @@ -5,6 +5,7 @@
import java.net.Socket;

import it.valsecchi.polypserver.connection.SessionsManager;
import it.valsecchi.polypserver.data.FilesManager;
import it.valsecchi.polypserver.data.UsersManager;
import static it.valsecchi.polypserver.Utility.Log;

Expand All @@ -27,14 +28,15 @@ public class PolypServer {
public String polyp_path;
private boolean run_polyp_server = true;

public PolypServer(String polyp_name, int port, int max_clients,String password) {
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;
// inizializzo il server
try {
polyp_server = new ServerSocket(this.port, this.max_clients);
Expand Down
174 changes: 161 additions & 13 deletions src/it/valsecchi/polypserver/connection/Session.java
Expand Up @@ -3,6 +3,9 @@
import it.valsecchi.polypserver.PolypServer;
import it.valsecchi.polypserver.Utility;
import it.valsecchi.polypserver.data.User;
import it.valsecchi.polypserver.exception.GenericException;
import it.valsecchi.polypserver.exception.StreamException;
import it.valsecchi.polypserver.exception.UserAlreadyConnectedException;
import it.valsecchi.polypserver.exception.WrongPasswordException;
import static it.valsecchi.polypserver.Utility.Log;
import java.io.BufferedInputStream;
Expand All @@ -11,6 +14,8 @@
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 All @@ -24,9 +29,11 @@ public class Session implements Runnable {
private Socket socket;
private User session_user;
private String session_id;
private String user_id;
private ObjectInputStream input;
private ObjectOutputStream output;
private PolypServer server;
private boolean session_active = false;

public Session(Socket socket, PolypServer server) {
// si memorizza il socket della sessione
Expand All @@ -40,10 +47,51 @@ public void run() {
// si avvia il thread della sessione
// si acquisiscono gli stream
this.getStreams();
// la sessione è attiva
this.session_active = true;
// ora si richiedono le informazioni dell'utente da confrontare con il
// UsersManager
this.getUserInfo();
try {
this.getUserInfo();
} catch (StreamException e) {
Log.error("errore stream");
this.close();
return;
} catch (WrongPasswordException e) {
this.close();
return;
} catch (UserAlreadyConnectedException e) {
this.close();
return;
}
// ora si richiede la lista file e si invia quella del server
try {
this.manageFileList();
} catch (StreamException e) {
Log.error("errore stream");
this.close();
return;
} catch (GenericException e) {
this.close();
return;
}
// ora si sta in attesa di richieste.
this.listenForRequests();
}

private void listenForRequests() {
try {
while (this.session_active) {
String cmd = this.readString();
switch(cmd){

}


}
} catch (StreamException e) {

}
}

/** Apre gli stream */
Expand All @@ -57,15 +105,17 @@ private void getStreams() {
}
}

/** Recupera le informazioni dell'utente e lo registra */
public void getUserInfo() {
/**
* Recupera le informazioni dell'utente e lo registra
*
* @throws StreamException
* @throws WrongPasswordException
* @throws UserAlreadyConnectedException
*/
public void getUserInfo() throws StreamException, WrongPasswordException,
UserAlreadyConnectedException {
String user_info = "";
try {
user_info = (String) input.readObject();
} catch (ClassNotFoundException | IOException e) {
Log.error("errore ricezione informazioni utente. sessione: "
+ this.session_id);
}
user_info = this.readString();
StringTokenizer tok = new StringTokenizer(user_info, "@");
String userid, username, password;
userid = tok.nextToken();
Expand All @@ -77,26 +127,124 @@ public void getUserInfo() {
} catch (WrongPasswordException e) {
// password errata
Log.error("User: " + userid + ", password errata!");
// si chiude
this.close();
// si invia la risposta
this.sendConnectionAnswer("WRONG_PASSWORD");
throw e;
}
// si attiva l'utente
server.users_manager.activateUser(userid);

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");
throw new UserAlreadyConnectedException();
}
// si indica all'utente l'avvenuta connessione
this.sendConnectionAnswer("CONNECTED");
this.user_id = userid;
}

/** Si chiude la sessione */
public void close() {
try {
Log.info("chiusura sessione: " + this.session_id);
output.flush();
output.close();
input.close();
socket.close();
// si disattiva l'utente
server.users_manager.deactivateUser(this.user_id);
// si elimina la sessione
server.sessions_manager.removeSession(this);
// la sessione non è più attiva
session_active = false;
} catch (IOException e) {
Log.error("errore chiusura sessione: " + this.session_id);
}
}

/**
* Metodo che gestisasce lo scambio della lista file
*
* @throws StreamException
* @throws GenericException
*/
private void manageFileList() 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");
throw e;
}
// si invia l'ok
this.sendAnswer("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")) {
// Si chiude la connessione
this.close();
Log.error("errore comunicazione lista file");
throw new GenericException();
}
}

/** Metodo che legge le stringhe inviate dal client */
private String readString() throws StreamException {
try {
return (String) input.readObject();
} catch (ClassNotFoundException | IOException e) {
Log.error("errore lettura stringa");
throw new StreamException("errore lettura stringa");
}
}

private List<String> readFileList() throws StreamException {
try {
return (List<String>) input.readObject();
} catch (ClassNotFoundException | IOException e) {
Log.error("errore lettura lista file");
throw new StreamException("errore lettura lista file");
}
}

public void sendFileList() throws StreamException {
try {
output.writeObject(server.files_manager.getFileList());
} catch (IOException e) {
Log.error("errore invio lista file");
throw new StreamException("errore invio lista file");
}
}

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

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

public Socket getSocket() {
return socket;
}
Expand Down
4 changes: 4 additions & 0 deletions src/it/valsecchi/polypserver/connection/SessionsManager.java
Expand Up @@ -35,4 +35,8 @@ public void addSession(Socket socket) {
Session session = new Session(socket,server);
this.addSession(session);
}

public void removeSession(Session session){
sessionsList.remove(session);
}
}
91 changes: 89 additions & 2 deletions src/it/valsecchi/polypserver/data/FilesManager.java
@@ -1,8 +1,23 @@
package it.valsecchi.polypserver.data;

import static it.valsecchi.polypserver.Utility.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import it.valsecchi.polypserver.PolypServer;

/**
Expand All @@ -12,12 +27,84 @@
*
*/
public class FilesManager {
private Map<String,List<PolypFile>> filesMap;

private Map<String, List<String>> filesMap;
private PolypServer server;

public FilesManager(PolypServer server) {
this.server = server;
//si caricano i dati
this.loadFiles();
}

/** Metodo che legge i file dal server del server da file files_data */
private void loadFiles() {
// si legge il file Users
Document doc = new Document();
if (Files.exists(Paths.get(server.polyp_path
+ "\\files\\files_data.xml"))) {
// solo se esiste si legge se no si lascia così
SAXBuilder build = new SAXBuilder();
try {
doc = build
.build(server.polyp_path + "\\files\\files_data.xml");
} catch (JDOMException | IOException e) {
Log.error("errore lettura dati file");
}
}
// ora si leggono i dati e si creano gli user
for (Element e : doc.getRootElement().getChildren("user_id")) {
for (Element e2 : e.getChildren("file")) {
this.addFile(e.getAttributeValue("id"), e2.getText());
}
}
}

/** Metodo che scrive i dati degli files nel file files_data */
public void writeUsers() {
// si crea un document e lo si scrive
Document doc = new Document();
doc.setRootElement(new Element("files_data"));
for (String u:filesMap.keySet()){
Element e = new Element("user_id");
e.setAttribute(new Attribute("id",u));
for(String f: filesMap.get(u)){
Element fi = new Element("file").setText(f);
e.addContent(fi);
}
doc.getRootElement().addContent(e);
}
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat());
FileOutputStream output;
try {
output = new FileOutputStream(server.polyp_path
+ "\\files\\files_data.xml");
outputter.output(doc, output);
} catch (IOException e) {
Log.error("errore scrittura dati files");
}
}

public void addFile(String user_id, String file) {
if (filesMap.containsKey(user_id)) {
filesMap.get(user_id).add(file);
} else {
filesMap.put(user_id, new ArrayList<String>());
}
}

public void setFileList(String user_id, List<String> files){
if(filesMap.containsKey(user_id)){
filesMap.get(user_id).clear();
filesMap.get(user_id).addAll(files);
}else{
filesMap.put(user_id, files);
}
}

public Map<String, List<String>> getFileList(){
return filesMap;
}

}

0 comments on commit 2a654e4

Please sign in to comment.