Projekt_SIK/klient/src/main/java/Projekt/App.java

201 lines
9.5 KiB
Java
Raw Normal View History

2019-10-31 11:17:48 +01:00
package Projekt;
2019-11-07 21:22:20 +01:00
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
2019-11-22 14:51:44 +01:00
import javax.swing.JScrollPane;
2019-11-07 21:22:20 +01:00
import javax.swing.JTextField;
2019-11-22 14:51:44 +01:00
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
2019-11-10 13:45:49 +01:00
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
2019-11-22 14:51:44 +01:00
import java.io.File;
2019-11-10 13:45:49 +01:00
import java.net.InetAddress;
import java.net.Socket;
2019-11-22 14:51:44 +01:00
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
2019-10-31 11:17:48 +01:00
2019-11-10 13:45:49 +01:00
/*
Lepiej lapac exception!!!
2019-11-22 14:51:44 +01:00
CZY po wyjsciu z aplikacji zamkaja sie tez inne procesy? chyba tak
2019-11-10 13:45:49 +01:00
*/
public class App {
2019-11-22 14:51:44 +01:00
static JTextField ipText, portText, messageT, myUserT; // przy evencie musi byc static albo w klasie musi byc final
2019-11-10 13:45:49 +01:00
static JFrame frame = new JFrame("Messages");
2019-11-22 14:51:44 +01:00
static JButton mSendB, connectB;
2019-11-10 13:45:49 +01:00
static JList<String> messagesL;
static DefaultListModel<String> messages = new DefaultListModel<>();
2019-11-22 14:51:44 +01:00
static JScrollPane sPane;
static Thread receiveThread = new Thread() { //watek w ktorym bedziemy tylko odbierac wiadomosci
public void run() { //uwazac zeby nie dochodzic jednoczesnie do tej samej zmiennej (synchronize?)
Socket rSocket;
try {
InetAddress rAddr = InetAddress.getByName(ipText.getText());
int rPort = Integer.parseInt(portText.getText());
rSocket = new Socket(rAddr, rPort + 1); // laczymy
DataInputStream rDis = new DataInputStream(rSocket.getInputStream()); // tu bedziemy przyjmowac wiadomosci
addMessage("LISTEN CONNECTION OK");
while (true) { //czytamy wiadomosc
byte[] rec = new byte[1024];
if (rDis.read(rec) > 0){
String s = new String(rec);
addMessage(s);
}
}
} catch (Exception ae) {
ae.printStackTrace();
addMessage("REC CANT CONNECT");
}
return;
}
};
2019-11-07 21:22:20 +01:00
2019-11-10 13:45:49 +01:00
public static void main(String[] args) {
2019-11-07 21:22:20 +01:00
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2019-11-10 13:45:49 +01:00
frame.setSize(320, 400);
2019-11-11 18:37:06 +01:00
ipText = new JTextField("0.0.0.0");
2019-11-10 13:45:49 +01:00
ipText.setBounds(10, 10, 200, 30);
2019-11-11 18:37:06 +01:00
portText = new JTextField("44444");
2019-11-07 21:22:20 +01:00
portText.setBounds(210, 10, 100, 30);
2019-11-11 18:37:06 +01:00
connectB = new JButton("Connect");
connectB.setBounds(110, 50, 100, 30);
connectB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
//zapobiegac podwojnemu kliknieciu w connect!
2019-11-20 10:35:02 +01:00
receiveThread.start();
2019-11-11 18:37:06 +01:00
}
});
2019-11-22 14:51:44 +01:00
2019-11-10 13:45:49 +01:00
myUserT = new JTextField("Nick");
myUserT.setBounds(10, 50, 100, 30);
2019-11-07 21:22:20 +01:00
messagesL = new JList<String>(messages);
2019-11-22 14:51:44 +01:00
sPane = new JScrollPane(messagesL);
2019-11-23 19:22:51 +01:00
sPane.setBounds(10, 100, 300, 180);
2019-11-07 21:22:20 +01:00
messageT = new JTextField("Message");
messageT.setBounds(10, 290, 300, 30);
mSendB = new JButton("Send");
2019-11-23 19:22:51 +01:00
mSendB.setBounds(110, 330, 100, 30);
2019-11-10 13:45:49 +01:00
mSendB.addActionListener(new ActionListener() {
2019-11-11 18:37:06 +01:00
public void actionPerformed(ActionEvent e) { //wysylanie wiadomosci
2019-11-10 13:45:49 +01:00
try {
2019-11-22 14:51:44 +01:00
InetAddress tAddr = InetAddress.getByName(ipText.getText()); //ustanawiamy polaczenie na wysylanie wiadomosci
2019-11-20 10:35:02 +01:00
int tPort = Integer.parseInt(portText.getText());
2019-11-22 14:51:44 +01:00
String messString = messageT.getText();
2019-11-23 19:22:51 +01:00
DataOutputStream tDos, fDos;
DataInputStream fDis;
Socket tSocket, fSocket;
2019-11-22 14:51:44 +01:00
String toCheckString = messString + "xdddddddd"; //sprawdzany string zawsze bedzie mial min 9 znakow
if (toCheckString.substring(0, 5).equals("/FILE")){ //CHYBA OK? wysylanie pliku
String fileName = messString.substring(6, messString.length()); //uzyskujemy sciezke do pliku
System.out.println(fileName);
File sendFile = new File(fileName);
if (!sendFile.exists() || !sendFile.isFile()) { //zly plik
addMessage("ZLY PLIK!");
} else {
System.out.println(sendFile.length()); //wielkosc pliku
tSocket = new Socket(tAddr, tPort); // laczymy
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
String[] parsed = messString.split("/"); //chcemy pobrac nazwe pliku
2019-11-23 19:22:51 +01:00
tDos.write(convert(myUserT.getText() + "> /FILE " + parsed[parsed.length-1] + " " + sendFile.length())); //wysylamy wielkosc pliku
2019-11-22 14:51:44 +01:00
byte[] fileContent = Files.readAllBytes(sendFile.toPath()); //plik do bitow
2019-11-23 19:22:51 +01:00
tSocket.close();
fSocket = new Socket(tAddr, tPort + 2);
fDos = new DataOutputStream(fSocket.getOutputStream()); //polaczenie dla pliku
2019-11-22 14:51:44 +01:00
int wyslano = 0;
while (wyslano < sendFile.length()){ //wysylamy po 1024
2019-11-23 19:22:51 +01:00
int tobyte = 1024; //standardowa ilosc bajtow
if (wyslano + tobyte > fileContent.length){
tobyte = fileContent.length - wyslano;
}
2019-11-27 12:13:10 +01:00
byte[] bufor = new byte[tobyte];
2019-11-23 19:22:51 +01:00
System.arraycopy(fileContent, wyslano, bufor, 0, tobyte);
fDos.write(bufor); //uwaga dodac sprawdzanie czy na pewno wyslalismy dobra liczbe bajtow
2019-11-27 12:26:45 +01:00
wyslano += tobyte;
2019-11-22 14:51:44 +01:00
addMessage("WYSLANO: " + wyslano);
}
messageT.setText("");
2019-11-23 19:22:51 +01:00
fSocket.close(); //czy to tez trzeba?
2019-11-22 14:51:44 +01:00
}
} else if(toCheckString.substring(0, 9).equals("/DOWNLOAD")) { //pobieranie pliku
tSocket = new Socket(tAddr, tPort); // laczymy
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
2019-11-23 19:22:51 +01:00
tDos.write(convert(myUserT.getText() + "> /DOWNLOAD " + messString.substring(10, messString.length()))); //wysylamy prosbe
fSocket = new Socket(tAddr, tPort + 2);
fDis = new DataInputStream(fSocket.getInputStream());
2019-11-22 14:51:44 +01:00
byte[] rec = new byte[1024];
String s = new String();
2019-11-23 19:22:51 +01:00
if (fDis.read(rec) > 0){
2019-11-22 14:51:44 +01:00
s = new String(rec); //otrzymana wiadomosc o wielkosci pliku
2019-11-23 19:22:51 +01:00
System.out.println(s);
2019-11-22 14:51:44 +01:00
}
int received = 0;
List<byte[]> fileBList = new ArrayList<byte[]>(); //plik
2019-11-23 19:22:51 +01:00
while (received < Integer.parseInt(s.trim())){ //pobieramy zawartosc pliku
2019-11-22 14:51:44 +01:00
rec = new byte[1024]; //czyscimy tablice
2019-11-23 19:22:51 +01:00
if (fDis.read(rec) > 0){
2019-11-22 14:51:44 +01:00
fileBList.add(rec); //dodajemy do listy
}
received += 1024;
addMessage("POBRANO: " + received);
}
byte[] fileB = fileBList.get(0);
for(int i=1; i<fileBList.size(); i++){
fileB = ArrayUtils.addAll(fileB, fileBList.get(i));
}
FileUtils.writeByteArrayToFile(new File(messString.substring(10, messString.length())), fileB); //zapis pliku
addMessage("ZAPISANO PLIK");
} else {
tSocket = new Socket(tAddr, tPort); // laczymy
tDos = new DataOutputStream(tSocket.getOutputStream()); // tu wysylamy wiadomosci
tDos.write(convert(myUserT.getText() + "> " + messString)); //wysylamy
messageT.setText("");
tSocket.close(); //czy to tez trzeba?
}
2019-11-20 10:35:02 +01:00
} catch (Exception ae){
ae.printStackTrace();
2019-11-22 14:51:44 +01:00
addMessage("CANT SEND");
2019-11-10 13:45:49 +01:00
}
}
});
2019-11-07 21:22:20 +01:00
frame.add(ipText);
frame.add(portText);
2019-11-22 14:51:44 +01:00
frame.add(sPane);
2019-11-07 21:22:20 +01:00
frame.add(messageT);
frame.add(mSendB);
2019-11-10 13:45:49 +01:00
frame.add(myUserT);
2019-11-11 18:37:06 +01:00
frame.add(connectB);
2019-11-07 21:22:20 +01:00
frame.setLayout(null);
frame.setVisible(true);
2019-11-11 18:37:06 +01:00
}
2019-11-07 21:22:20 +01:00
2019-11-11 18:37:06 +01:00
synchronized static byte[] convert(String wiad){
2019-11-22 14:51:44 +01:00
byte[] mess = new byte[1024]; //UWAGA pewnie dluzsze wiadomosci trzeba jakos rozdzielac
2019-11-11 18:37:06 +01:00
mess = wiad.getBytes(); // konwertujemy na bajty
return mess;
2019-10-31 11:17:48 +01:00
}
2019-11-11 18:37:06 +01:00
2019-11-22 14:51:44 +01:00
synchronized static void addMessage(String mess){ //funkcja do wsadzania nowych wiadomosci
messages.addElement(mess);
messagesL.setModel(messages);
//messagesL.ensureIndexIsVisible(messages.getSize() - 1);
}
2019-10-31 11:17:48 +01:00
}
2019-11-22 14:51:44 +01:00
//byte[] combined = new byte[1024*(i+1)]; //bufor do laczenia
//System.arraycopy(fileB, 0, combined, 0, fileB.length); //kopiowanie
//System.arraycopy(fileBList.get(i), 0, combined, fileB.length, fileBList.get(i).length);
//fileB = new byte[1024*(i+1)];
//System.arraycopy(combined, 0, fileB, 0, combined.length); //kopiowanie 2