-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainStorageThread.java
More file actions
69 lines (61 loc) · 1.93 KB
/
Copy pathMainStorageThread.java
File metadata and controls
69 lines (61 loc) · 1.93 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
package archimedesServer;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
//import gui.*;
public class MainStorageThread extends Thread {
private final static int STORAGE_PORT = 10002;
MainStorageThread(){}
@Override
public void run(){
ExecutorService storagePool = Executors.newFixedThreadPool(50);
try(ServerSocket _storageSSocket = new ServerSocket(STORAGE_PORT)){
while(true){
Socket connection = _storageSSocket.accept();
Callable<Void> task = new StorageTask(connection);
storagePool.submit(task);
}
} catch(IOException ex){
//_logger.log(Level.SEVERE, "Can't open _loginSSocket.", ex);
}
}
private static class StorageTask implements Callable<Void> {
private Socket _connection;
private final static String _dataFolderPathPrefix = "C:/Users/nmlab/ChatServerData";
StorageTask(Socket connection){ _connection = connection; }
@Override
public Void call(){
System.out.println("\nStorage port accepted connection from "+_connection);
try{
//ByteBuffer bus = ByteBuffer.allocate(1024*25);
//SocketChannel client = _connection.getChannel();
Reader in = new BufferedReader(
new InputStreamReader(_connection.getInputStream(), "UTF-8")
);
StringBuilder sb = new StringBuilder();
char c;
while( (c = (char)in.read()) > 0)
sb.append(c);
Path targetPath = Paths.get(_dataFolderPathPrefix, "conv_"+ sb.toString());
byte[] targetData = Files.readAllBytes(targetPath);
OutputStream out = new BufferedOutputStream(
_connection.getOutputStream()
);
out.write(targetData);
out.flush();
} catch(IOException ex){
//_logger.log(Level.SEVERE, "Can't write to connection.", ex);
} finally{
try{
_connection.close();
} catch(IOException e){ /*ignore*/ }
}
return null;
}
}
}