import java.io.*;
import java.net.*;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class Client
{
private Socket socket;
private DataInputStream dis=null;
private DataOutputStream dos=null;
boolean connection;
boolean streamsonline;
Vector<File> caseFiles=null;
private FileInputStream fileinputStream=null;
private boolean connected;
// -----------------------------------------------------------------------------OKERRORDIALOG------------
private void setcaseFiles(Vector<File> caseFiles){this.caseFiles=caseFiles;}
// -----------------------------------------------------------------------------OKERRORDIALOG------------
private boolean connectStream()
{
try
{
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
connected=true;
return true;
}
catch (IOException e)
{
System.err.println("Streams not initialized!!");
e.printStackTrace();
connected =false;
return false;
}
}
// -----------------------------------------------------------------------------OKERRORDIALOG------------
private boolean connect2Server()
{
try
{
socket=new Socket("localhost",4711);
return true;
}
catch (UnknownHostException e)
{
System.err.println("UNKNOWN HOST localhost");
e.printStackTrace();
connected=false;
return false;
}
catch (IOException e)
{
System.err.println("IOException connectClient localhost");
e.printStackTrace();
connected=false;
return false;
}
}
// -----------------------------------------------------------------------------OKERRORDIALOG------------
public void okErrorDialog(String message, String title)
{
JOptionPane.showMessageDialog(null, message, title,JOptionPane.ERROR_MESSAGE);
}
// -----------------------------------------------------------------------------OKERRORDIALOG------------
public boolean sendUTF(String s)
{
try
{
dos.writeUTF(s);
dos.flush();
return true;
}
catch (IOException e) {e.printStackTrace();return false;}
}
// -----------------------------------------------------------------------------OKERRORDIALOG------------
private boolean send(File f, String caseID)
{
if (socket == null)
{
okErrorDialog("AClient is not connected.Please connect and try\nagain.","Connection failed");
return false;
}
if (f == null || !f.exists())
{
okErrorDialog("One or more files for case doesnt exists!.\n caseID "+ caseID, "File not found");
return false;
}
System.out.println("sending File...." + f.getName());
byte[] buffer = new byte[16384];
try
{
fileinputStream = new FileInputStream(f);
while (fileinputStream.available() > 0)
{
dos.write(buffer, 0, fileinputStream.read(buffer));
}
dos.flush();
System.out.println("send!!");
return true;
}
catch (FileNotFoundException e)
{
System.out.println("FILENOTFOUND in sendFile");
e.printStackTrace();
return false;
}
catch (IOException e)
{
System.out.println("IO in sendFile");
e.printStackTrace();
return false;
}
finally
{
try
{
fileinputStream.close();
}
catch (IOException e){e.printStackTrace();}
}
}
//-----------------------------------------------------------------------------OKERRORDIALOG------------
private boolean sendInformation(String FileName, String cmd, String filecnt)
{
try
{
dos.writeUTF(FileName + "," + cmd + "," +filecnt);
System.out.println("Client write information.....end");
dos.flush();
return true;
}
catch (IOException e)
{
System.out.println("Send Information Fehler");
e.printStackTrace();
return false;
}
}
//-----------------------------------------------------------------------------OKERRORDIALOG------------
private synchronized boolean startProcedure()
{
boolean procedureOK=true;
if(connection && streamsonline)
{
for(int i=0;i<caseFiles.size();i++)
{
if(procedureOK==false)
{
try
{
dos.writeUTF("q");
return false;
} catch (IOException e)
{
e.printStackTrace();
}
return false;
}
File actScript=caseFiles.get(i);
if(actScript==null||!actScript.exists())
{
procedureOK=false;
try
{
dos.writeUTF("q");
} catch (IOException e) {e.printStackTrace();}
break;
}
// if(!socket.isConnected()||socket.isClosed())
// {
// System.out.println("Connection closed");
// try
// {
// dis.close();
// dos.close();
// procedureOK=false;
// break;
// }
// catch (IOException e)
// {
// e.printStackTrace();
// procedureOK=false;
// okErrorDialog("Client not connected", "Connection failed");
// break;
// }
// }
System.out.println("**************CLIENT STARTED*******************");
if(!sendInformation(actScript.getName(), "cmd", "filecnt"))
{
procedureOK=false;
try
{
dos.writeUTF("q");
}
catch (IOException e) {e.printStackTrace();}
break;
}
System.out.println("Client send message");
System.out.println("sending File");
if(!send(actScript, "1")){procedureOK=false;break;}
System.out.println("CLIENT WAITING FOR SERVER ANSWER....");
try
{
String received=dis.readUTF();
if(received.equals("1"))procedureOK=true;
else
{
System.out.println("failed Server Res= > "+received);
procedureOK=false;
}
}
catch (IOException e)
{
e.printStackTrace();
procedureOK=false;
break;
}
System.out.println("END OF PROCEDURE FOR SCRIPT ="+actScript.getName());
System.out.println("PROCEDURE IS ="+procedureOK);
}
return procedureOK;
}
else
{
System.out.println("connection or streams not online");
return false;
}
}
//-----------------------------------------------------------------------------OKERRORDIALOG------------
private boolean closeConnection()
{
try
{
if(dos!=null)dos.close();
if(dis!=null)dis.close();
if(socket!=null)socket.close();
System.out.println("Connection end");
return true;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
//-----------------------------------------------------------------------------OKERRORDIALOG------------
public static void main(String args[])
{
JFileChooser choos=new JFileChooser(".");
Vector<File> v=new Vector<File>();
choos.showOpenDialog(null);
v.add(choos.getSelectedFile());
final Client client=new Client();
client.connection= client.connect2Server();
client.streamsonline= client.connectStream();
client.setcaseFiles(v);
// client.startProcedure(v);
Thread t1=new Thread(new Runnable(){
@Override
public void run()
{
for(int i=0;i<10;i++)
{
client.startProcedure();
}
}
});
//dies ist ein test um einen zweiten durchlauf zu testen
t1.start();
try {
Thread.sleep(20000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
//-----------------------------------------------------
//eigentlich soll die connection nur vom benutzter geclosed werden
//solange das programm laeuft soll die funktion main immerwieder neue vectoren ubergeben und diese ausfuhren
System.out.println("ENDE");
}
}