-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyServer.java
More file actions
165 lines (154 loc) · 5.74 KB
/
MyServer.java
File metadata and controls
165 lines (154 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package BACKUPER;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;
public class MyServer extends JFrame implements ActionListener{
private static OutputStream outputStream;
private static PrintWriter printWriter;
private static Random random;
public static int transferPort;
private JLabel label;
private JButton disconnectButton;
private JButton changePathButton;
protected static DefaultListModel clientsList;
private JList usersList;
private File pathContainer;
public static String PATH;
private static String pathData="E:\\BackuperData";
static Thread transferThread;
public MyServer() {
setSize(500,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setTitle("Server");
label = new JLabel("Lista klientów:");
disconnectButton = new JButton("Rozłącz");
changePathButton = new JButton("Zmień ścieżkę");
clientsList = new DefaultListModel();
usersList = new JList(clientsList);
usersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
label.setBounds(200,20,150,25);
disconnectButton.setBounds(70,300,150,50);
changePathButton.setBounds(250,300,150,50);
usersList.setBounds(20,50,440,220);
add(label);
add(changePathButton);
add(usersList);
add(disconnectButton);
changePathButton.addActionListener(this);
disconnectButton.addActionListener(this);
setVisible(true);
try {
if (Files.notExists(Paths.get(pathData)))
Files.createDirectory(Paths.get(pathData));
if(Files.notExists(Paths.get(pathData+"\\pathContainer.txt")))
Files.createFile(Paths.get(pathData+"\\pathContainer.txt"));
pathContainer=new File(pathData+"\\pathContainer.txt");
FileInputStream pathInput=new FileInputStream(pathContainer);
BufferedReader pathReader=new BufferedReader(new InputStreamReader(pathInput));
PATH=pathReader.readLine();
if(PATH==null)
PATH="C:\\BackuperKopie";
System.out.println(PATH);
pathReader.close();
pathInput.close();
}
catch (IOException e)
{
System.err.println(e);
}
}
public static void main(String[] args) {
new MyServer();
while (true) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(12129);
} catch (Exception e){
System.err.println("Create new meta socket error: " +e);
}
while(true) {
try {
Socket setterSocket = serverSocket.accept();
outputStream = setterSocket.getOutputStream();
printWriter = new PrintWriter(outputStream, true);
random = new Random();
transferPort = random.nextInt(65535);
while(isAvailable(transferPort) == false) {
transferPort = random.nextInt(65535);
}
printWriter.println(transferPort);
setterSocket.close();
Runnable runnable = new MyThread(transferPort);
transferThread = new Thread(runnable);
transferThread.start();
} catch (Exception e) {
System.out.println("Creating meta socket problem: " + e);
}
}
}
}
public static boolean isAvailable(int port) {
ServerSocket ss = null;
DatagramSocket ds = null;
try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
ds = new DatagramSocket(port);
ds.setReuseAddress(true);
return true;
} catch (IOException e) {
} finally {
if (ds != null) {
ds.close();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
}
}
}
return false;
}
@Override
public void actionPerformed(ActionEvent e) {
Object click = e.getSource();
if (click == changePathButton) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
PATH=file.getPath();
try {
PrintWriter pathWriter = new PrintWriter(new FileOutputStream(pathContainer),true);
pathWriter.write(PATH);
pathWriter.close();
}
catch (IOException e1){
System.err.println(e1);
}
JOptionPane.showMessageDialog(null, file);
}
}
else if (click == disconnectButton) {
printWriter.println(MyProtocol.LOGOUT);
String TMP = usersList.getSelectedValue().toString();
MyServer.clientsList.removeElement(TMP);
try{
MyThread.socket.close();
//MyThread.transferSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}