Skip to content

Commit

Permalink
inizio creazione processo di traferimento file
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Valsecchi committed Jul 27, 2012
1 parent aa294ee commit 9a54a02
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 27 deletions.
12 changes: 11 additions & 1 deletion src/it/valsecchi/polypserver/connection/PMessage.java
Expand Up @@ -9,6 +9,8 @@ public enum PMessage {

/** Comunicazione andata a buon fine*/
OK,
/** Rifiuto*/
NO,
/** Errore generico*/
ERROR,
/** Errore trasmissione lista file*/
Expand All @@ -20,7 +22,15 @@ public enum PMessage {
/** Utente correttamente connesso*/
CONNECTED,
/** Utente sincronizzato*/
SYNCHRONIZED;
SYNCHRONIZED,
/** Richiesta file*/
FILE_REQUEST,
/** File non disponibile*/
FILE_NOT_AVAIABLE,
/** Utente occupato*/
USER_BUSY,
/** Utente non connesso*/
USER_NOT_CONNECTED;



Expand Down
84 changes: 73 additions & 11 deletions src/it/valsecchi/polypserver/connection/Session.java
Expand Up @@ -31,6 +31,8 @@ public class Session implements Runnable {
private ObjectOutputStream output;
private PolypServer server;
private boolean session_active = false;
private boolean session_busy = false;
private boolean file_request_active = false;

public Session(Socket socket, PolypServer server) {
// si memorizza il socket della sessione
Expand All @@ -43,6 +45,7 @@ public Session(Socket socket, PolypServer server) {
public void run() {
// si avvia il thread della sessione
// si acquisiscono gli stream
busy();
this.getStreams();
// la sessione è attiva
this.session_active = true;
Expand Down Expand Up @@ -72,15 +75,40 @@ public void run() {
this.close();
return;
}
free();
// ora si sta in attesa di richieste.
this.listenForRequests();
}

private void listenForRequests() {
try {
while (this.session_active) {
if(this.isSessionBusy()==true){
//si attente
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
continue;
}
PMessage cmd = this.readMessage();

switch (cmd) {
case FILE_REQUEST:
busy();
String file = this.readString();
// si controlla che il file sia disponibile
if (server.files_manager.isFileAvaiable(file) == true) {
// si avvia il processo di richiesta file
PMessage result = server.sessions_manager.performFileRequest(file, this);


} else {
// si risponde che non è disponibile
this.sendMessage(PMessage.FILE_NOT_AVAIABLE);
free();
continue;
}
}
}
} catch (StreamException e) {

Expand Down Expand Up @@ -140,6 +168,7 @@ public void getUserInfo() throws StreamException, WrongPasswordException,
/** Si chiude la sessione */
public void close() {
try {
free();
Log.info("chiusura sessione: " + this.session_id);
output.flush();
output.close();
Expand Down Expand Up @@ -194,7 +223,7 @@ private void synchronizeData() throws StreamException, GenericException {
if (m2 == PMessage.OK) {
// ok la connessione è effettuata
this.sendMessage(PMessage.SYNCHRONIZED);
//la connessione è effettuata
// la connessione è effettuata
} else if (m2 == PMessage.ERROR) {
// Si chiude la connessione
this.close();
Expand All @@ -204,7 +233,7 @@ private void synchronizeData() throws StreamException, GenericException {
}

/** Metodo che legge i messaggi inviati dal client */
private PMessage readMessage() throws StreamException {
protected PMessage readMessage() throws StreamException {
try {
return (PMessage) input.readObject();
} catch (ClassNotFoundException e) {
Expand All @@ -221,7 +250,7 @@ private PMessage readMessage() throws StreamException {
*
* @throws StreamException
*/
private String readString() throws StreamException {
protected String readString() throws StreamException {
try {
return (String) input.readObject();
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -256,14 +285,14 @@ public void sendFileList() throws StreamException {

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

public void sendMessage(PMessage ans) throws StreamException {
protected void sendMessage(PMessage ans) throws StreamException {
try {
output.writeObject(ans);
output.flush();
Expand All @@ -273,9 +302,19 @@ public void sendMessage(PMessage ans) throws StreamException {
}
}

protected byte[] readNBytes(int n) throws StreamException{
protected void sendString(String str) throws StreamException{
try {
output.writeObject(str);
output.flush();
} catch (IOException e) {
Log.error("errore invio stringa");
throw new StreamException("errore invio stringa");
}
}

protected byte[] readNBytes(int n) throws StreamException {
byte[] buf = new byte[n];
//si leggono
// si leggono
try {
input.read(buf, 0, n);
} catch (IOException e) {
Expand All @@ -284,9 +323,9 @@ protected byte[] readNBytes(int n) throws StreamException{
}
return buf;
}
protected void writeNBytes(byte[] buf) throws StreamException{
//si scrivono

protected void writeNBytes(byte[] buf) throws StreamException {
// si scrivono
try {
output.write(buf);
output.flush();
Expand All @@ -296,6 +335,25 @@ protected void writeNBytes(byte[] buf) throws StreamException{
}
}

/** rende occupata la sessione */
protected void busy() {
this.session_busy = true;
}

/** rende libera la sessione */
protected void free() {
this.session_busy = false;
}

/** metodo utilizzato dall'esterno per capire se la sessione è occupata */
public boolean isSessionBusy() {
return this.session_busy;
}

public void activateFileRequest() {
this.file_request_active = true;
}

public Socket getSocket() {
return socket;
}
Expand All @@ -304,6 +362,10 @@ public User getSession_user() {
return session_user;
}

public String getUser_id() {
return user_id;
}

public String getSession_id() {
return session_id;
}
Expand Down
36 changes: 36 additions & 0 deletions src/it/valsecchi/polypserver/connection/SessionsManager.java
@@ -1,6 +1,7 @@
package it.valsecchi.polypserver.connection;

import it.valsecchi.polypserver.PolypServer;
import it.valsecchi.polypserver.exception.StreamException;

import java.net.Socket;
import java.util.ArrayList;
Expand Down Expand Up @@ -38,4 +39,39 @@ public void addSession(Socket socket) {
public void removeSession(Session session){
sessionsList.remove(session);
}

public PMessage performFileRequest(String filename, Session richiedente) throws StreamException{
//si trova l'id dell'user a cui bisogna chiedere il file
String userid = server.files_manager.getUserIDFromFile(filename);
//si cerca la sessione giusta
Session fonte= null;
for(Session s:sessionsList){
if(s.getUser_id().equals(userid)){
fonte = s;
}
}
if(fonte==null){
return PMessage.FILE_NOT_AVAIABLE;
}else{
//si invia la richiesta alla sessione se non è busy
if(fonte.isSessionBusy()==true){
return PMessage.USER_BUSY;
}else{
//si invia la richiesta
fonte.busy();
fonte.sendMessage(PMessage.FILE_REQUEST);
//si invia il nomefile
fonte.sendString(filename);
//ora si attende una risposta
PMessage result = fonte.readMessage();
if(result == PMessage.OK){
//Si avvia la trasmissione
}
}
}
}

private PMessage performFileTransfer(String filename,Session richiedente, Session fonte){

}
}
63 changes: 49 additions & 14 deletions src/it/valsecchi/polypserver/data/FilesManager.java
Expand Up @@ -32,16 +32,15 @@ public class FilesManager {

public FilesManager(PolypServer server) {
this.server = server;
//si caricano i dati
// 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();
File file = new File(server.polyp_path
+ "\\files\\files_data.xml");
File file = new File(server.polyp_path + "\\files\\files_data.xml");
if (file.exists()) {
// solo se esiste si legge se no si lascia così
SAXBuilder build = new SAXBuilder();
Expand All @@ -50,7 +49,7 @@ private void loadFiles() {
.build(server.polyp_path + "\\files\\files_data.xml");
} catch (JDOMException e) {
Log.error("errore lettura dati file");
}catch( IOException e1){
} catch (IOException e1) {
Log.error("errore lettura dati file");
}
}
Expand All @@ -61,16 +60,16 @@ private void loadFiles() {
}
}
}

/** Metodo che scrive i dati degli files nel file files_data */
public void writeFiles() {
// si crea un document e lo si scrive
Document doc = new Document();
doc.setRootElement(new Element("files_data"));
for (String u:filesMap.keySet()){
for (String u : filesMap.keySet()) {
Element e = new Element("user_id");
e.setAttribute(new Attribute("id",u));
for(String f: filesMap.get(u)){
e.setAttribute(new Attribute("id", u));
for (String f : filesMap.get(u)) {
Element fi = new Element("file").setText(f);
e.addContent(fi);
}
Expand All @@ -95,18 +94,54 @@ public void addFile(String user_id, String file) {
filesMap.put(user_id, new ArrayList<String>());
}
}
public void setFileList(String user_id, List<String> files){
if(filesMap.containsKey(user_id)){

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{
} else {
filesMap.put(user_id, files);
}
}

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


/** Metodo che restituisce la lista di file disponibili */
public List<String> getAvaiableFiles() {
List<String> files = new ArrayList<String>();
for (String user : filesMap.keySet()) {
if (server.users_manager.isUserAvaiable(user) == true) {
files.addAll(filesMap.get(user));
}
}
return files;
}

/** Metodo che controlla se un file è disponibile */
public boolean isFileAvaiable(String filename) {
for (String f : this.getAvaiableFiles()) {
if (f.equals(filename)) {
return true;
}
}
return false;
}

/**
* Metodo che restituisce l'id dell'user a cui appartiene il filename. Se
* non viene trovato restituisce null
*/
public String getUserIDFromFile(String fileName) {
for (String user : filesMap.keySet()) {
for (String file : filesMap.get(user)) {
if (file.equals(fileName)) {
return user;
}
}
}
return null;
}

}
2 changes: 1 addition & 1 deletion src/it/valsecchi/polypserver/data/UsersManager.java
Expand Up @@ -122,7 +122,7 @@ public boolean isUserAvaiable(String userid) {
}
}

public List<String> getListAvaibleUser(){
public List<String> getAvaibleUser(){
List<String > us = new ArrayList<String>();
for(User u: usersMap.values()){
if(u.isOnline()== true){
Expand Down

0 comments on commit 9a54a02

Please sign in to comment.