import java.net.*;
import java.io.*;
public class registramiFTP extends Thread
{
String lista_file[]=new String[60];
ListaFile serverReg=new ListaFile();
String nome_coordinatore=new String();
static final
int Porta_com=1188;
static final
int Porta_dati=1189;
InetAddress local;
private static
String workDir="c:\\Ftpdir";
File workingPath=new File(workDir);
int porta_file=1200;
public registramiFTP(String co,String ls[])
{
nome_coordinatore=co;
lista_file=ls;
}
public void
run()
{
ListaFile temp=new ListaFile();
String messaggio=new String();
try
{
//socket di ascolto
ServerSocket
sock=new
ServerSocket(Porta_dati);
Socket
sock_dati;
try
{
//socket per la registrazione
Socket sock_com=new Socket(nome_coordinatore,Porta_com);
System.out.println("registrazione
partita");
ObjectInputStream
ois=new
ObjectInputStream(sock_com.getInputStream());
ObjectOutputStream
oos=new
ObjectOutputStream(sock_com.getOutputStream());
oos.writeObject("registrazione");
oos.flush();
//invio la lista dei file solamente
oos.writeObject(lista_file);
oos.flush();
messaggio=(String)ois.readObject();
System.out.println(messaggio);
//leggo la struttura completa
temp=(ListaFile)ois.readObject();
synchronized(serverReg)
{
serverReg=temp.copia();
}
oos.close();
ois.close();
sock_com.close();
}//try per la registrazione
catch(Exception ex)
{
System.out.println("impossibile
eseguire la registrazione");
//System.out.println(ex);
}
//in ascolto per
verifica stato e aggiornamenti
while(true)
{
try
{
sock_dati=sock.accept();
sock_dati.setSoLinger(true,180);
sock_dati.setSoTimeout(180000);
ObjectInputStream ois2=new
ObjectInputStream(sock_dati.getInputStream());
ObjectOutputStream oos2=new
ObjectOutputStream(sock_dati.getOutputStream());
messaggio=(String) ois2.readObject();
//System.out.println(messaggio);
if(messaggio.equals("ancora
attivo?"))
{
try
{
oos2.writeObject("si");
oos2.flush();
ois2.close();
oos2.close();
System.out.println("Verifica
attivita\'");
}
catch(Exception ex)
{
System.out.println("errore durante la verifica");
}
}
else if(messaggio.equals("aggiornamento
lista"))
{
try
{
temp =(ListaFile) ois2.readObject();
oos2.writeObject("lista ricevuta");
oos2.flush();
System.out.println("ricevuto
aggiornamento lista file");
ois2.close();
oos2.close();
synchronized(serverReg)
{
serverReg.cancella();
serverReg.copia(temp);
}
}
catch(Exception ex)
{
System.out.println("errore durante
la ricezione di un aggiornamento");
}
}//if: aggiornamento
lista
else if(messaggio.equals("richiesta
file"))
{
try
{
File workingPath=new File(workDir);
String mess=new String();
messaggio=(String) ois2.readObject();
messaggio=messaggio.trim();
System.out.println("richiesto il
file "+messaggio+" da parte di:
"+sock_dati.getInetAddress().getHostName());
File f=new
File(workingPath,messaggio);
if(f.exists())
{
oos2.writeObject("trasferimento
accordato");
oos2.flush();
oos2.writeObject(new Integer(porta_file));
oos2.flush();
mess=(String)ois2.readObject();
//System.out.println(mess);
(new Thread(new
inviaFileFTP(sock_dati.getInetAddress().getHostName(),porta_file,messaggio))).start();
}
else
{
oos2.writeObject("file non
trovato");
System.out.println("richiesto file
non presente");
oos2.flush();
ois2.close();
oos2.close();
}
}
catch(Exception ex)
{
System.out.println("errore durante
lo scambio di un file con un server");
System.out.println(ex);
}
porta_file++;
}//richiesta file
else if(messaggio.equals("richiesta
registrazione"))
{
try
{
String tp[]=new String[60];
tp=workingPath.list();
oos2.writeObject("effetuo la
registrazione");
oos2.flush();
oos2.writeObject(tp);
oos2.flush();
temp=(ListaFile)ois2.readObject();
synchronized(serverReg)
{
serverReg.cancella();
serverReg.copia(temp);
}
oos2.close();
ois2.close();
System.out.println("server
registrato");
}
catch(Exception ex)
{
System.out.println("errore durante una richiesta di
registrazione");
}
}
}
catch(Exception ee)
{
System.out.println(ee);
}
}//while
}
catch(Exception e)
{
System.out.println("errore durante
la registrazione");
System.out.println("il server viene
eseguito senza coordinamento");
//System.out.println(e);
}
}
}