//routine di invio del risultato finale allo Slave richiedente via TCP

import java.io.*;
import java.net.*;
import java.util.*;

public class sendSlave{
	public static int sendRisultati(String risp, String rich){
				String richiesta=rich;
				String ambito;
				String mail;
				int ttl;
				String ipSlave;
				int portaSlave;
	
				BufferedReader br = null; 
				PrintWriter pw =null;

				String[]  inLine = new String[4];

				try {
					
					
					br = new BufferedReader(new StringReader(richiesta));
					
					int i=0;
					while(!br.ready()){}
					while(i<4){
						inLine[i] = br.readLine();
						i++;
					}//leggo per forza 3 linee
					
					mail= inLine[0].trim();
					richiesta=inLine[1].trim();
					ambito = inLine[2].trim();
					ttl= (new Integer(inLine[3].trim())).intValue();
					int indice1 = mail.indexOf(':');
					ipSlave = mail.substring(0,indice1);
					portaSlave = Integer.parseInt(mail.substring(indice1+1));
					System.out.println("ricevuta risposta/file da mandare allo slave: "+ipSlave+"-"+portaSlave+": "+risp);
					byte[] data;
					byte[] buffer;
					DatagramSocket ds;
					DatagramPacket  input;
					DatagramPacket  output;
					buffer = new byte[65507];
					ds = new DatagramSocket();
					int UDPTCPport = ds.getLocalPort();
					input=new DatagramPacket(buffer, buffer.length);
					if (risp.equals("errore")){
						data = "errore".getBytes();
						output = new DatagramPacket(data, data.length, InetAddress.getByName(ipSlave), portaSlave);
						System.out.println("dico che ho finito allo slave su : " + InetAddress.getByName(ipSlave) + "-" + portaSlave);
						ds.send(output);
					}
					else{
						data = (""+UDPTCPport).getBytes();
						output = new DatagramPacket(data, data.length, InetAddress.getByName(ipSlave), portaSlave);
						ds.send(output);
						ds.setSoTimeout(Constants.attesaRec);
						ds.receive(input);
						String s=new String(input.getData(), 0,input.getLength());

						if (!s.equals("errore")){
							System.out.println("creo socket tcp sulla stessa porta udp mia e faccio accept(sono server)!!! Porta TCPUDP: " + UDPTCPport);
							
							ServerSocket ss = new ServerSocket(UDPTCPport);
							Socket sendFile=ss.accept();
							System.out.println("spedisco il file");
							br = new BufferedReader(new InputStreamReader(sendFile.getInputStream()));
							pw = new PrintWriter(new BufferedOutputStream(sendFile.getOutputStream()), false);
							String conf=br.readLine();
							System.out.println("ho ricevuto " + conf + "; posso dare il file");
							downloadFile(risp, pw);
						}
					}
		//risp==errore-->UDP:dare errore
		//risp==file-->UDP:dare ok, TCP:dare file
		return 0;
	}//fine try

	catch (Exception e){
		System.out.println("eccezione: "+e);
		return -1;
	}

	}

	private static int downloadFile(String nomeFile, PrintWriter pw){
		
		try {
			System.out.println("sono dentro a download file: "+nomeFile + " su " + pw);
			FileReader fis = new FileReader(nomeFile);
			BufferedReader inp = new BufferedReader(fis);
			String inStr;
			while (       (inStr = inp.readLine())     != null){
				System.out.println("sendSlave-"+ nomeFile+"-"+inStr);
				pw.println(inStr);
			}
			pw.flush();
			pw.close();
			fis.close();
			System.out.println("sendSlave-"+ nomeFile+"-"+"ok, dati scritti su: " + pw + "fine flusso");	
			return 0;
		} 
		
		catch ( Exception e){
			System.out.println("sendSlave-"+ nomeFile+"-"+"errore download file");
			pw.println("errore download file-"+nomeFile+"-"+e);
			pw.flush();
			pw.close();
			return -1;
		}
	}//fine download file
	
	
}//fine class