-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainNewConversationThread.java
More file actions
74 lines (63 loc) · 2.06 KB
/
Copy pathMainNewConversationThread.java
File metadata and controls
74 lines (63 loc) · 2.06 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
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 gui.*;
public class MainNewConversationThread extends Thread {
private final static int NEW_CONVERSATION_PORT = 10001;
private Vector<Conversation> _convs;
MainNewConversationThread(Vector<Conversation> c){ _convs = c; }
@Override
public void run(){
ExecutorService newConversationPool = Executors.newFixedThreadPool(50);
try(ServerSocket newConversationSSocket = new ServerSocket(NEW_CONVERSATION_PORT)){
while(true){
Socket connection = newConversationSSocket.accept();
Callable<Void> task = new NewConversationTask(connection, _convs);
newConversationPool.submit(task);
}
} catch(IOException ex){
//_logger.log(Level.SEVERE, "Can't open _loginSSocket.", ex);
}
}
private static class NewConversationTask implements Callable<Void> {
private Socket _connection;
private Vector<Conversation> _convs;
NewConversationTask(Socket connection, Vector<Conversation> convs){
_connection = connection;
_convs = convs;
}
@Override
public Void call(){
try{
// Fetch input.
//boolean public;
//SocketChannel client = _connection.getChannel();
// Construct a new Conversation.
ServerSocket test = new ServerSocket(0);
int newPort = test.getLocalPort();
test.close();
Conversation conversation = new Conversation(true, _convs.size(), newPort);
Thread t = new Thread(conversation);
t.start();
_convs.addElement(conversation);
//Write protocol of new conversation to members.
//Writer out = new OutputStreamWriter(_connection.getOutputStream());
System.out.println("New Conversation at port "+newPort);
//out.write("new conversation at port "+newPort);
//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;
}
}
}