package socketHandle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import Frame.ICQChatWindow;
import Frame.ICQClientFrame;
public class ICQChatServer implements Runnable{
private final int port=0;
private ServerSocket ss;
private String username;
private String passwd;
private ExecutorService executorService;
private final int POOL_SIZE=4;
private static ArrayList<ICQChatWindow> list;
private ICQClientFrame icf;
public ICQChatServer(String username,ICQClientFrame icf){
this.username=username;
this.icf=icf;
list=new ArrayList<ICQChatWindow>();
executorService= Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors() * POOL_SIZE);
try {
ss=new ServerSocket(port);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
register();
}
private void register() {
String host="localhost";
try {
Socket socket=new Socket(host,8000);
Message m=new Message(username);
m.setState(Message.MSGSERVER);
m.setPort(ss.getLocalPort());
System.out.println(ss.getLocalPort());
ObjectOutputStream outport=new ObjectOutputStream(socket.getOutputStream());
outport.writeObject(m);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void add(ICQChatWindow icw){
list.add(icw);
}
@Override
public void run() {
while(true){
try {
Socket socket=ss.accept();
executorService.execute(new Handler(socket));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public int getPort(){
return ss.getLocalPort();
}
public void close(){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class Handler implements Runnable{
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
private Message msg;
private ICQConnect ic;
private ArrayList<User> userList;
public Handler(Socket socket){
this.socket=socket;
try {
out=new ObjectOutputStream(socket.getOutputStream());
in=new ObjectInputStream(socket.getInputStream());
msg=(Message)in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
protected void freshFirends() {
try {
ic=new ICQConnect(username,passwd,Message.FRESH);
while(true){
Message m=null;
if((m=(Message)ic.listen())!=null){
userList = m.getUserList();
return;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
if(msg.getState()==Message.SENDMESSAGE){
for(ICQChatWindow icw:list){
if(icw.getUsername().equals(msg.getUsername())){
icw.setVisible(true);
icw.changArea(msg.getMsg());
return;
}
}
freshFirends();
for(User user:userList){
if(user.getUsername().equals(msg.getUsername())){
ICQChatWindow icw=new ICQChatWindow(msg.getUsername(),msg.getHost(),user.getPort(),username);
icw.setVisible(true);
list.add(icw);
icw.changArea(msg.getMsg());
}
}
}
if(msg.getState()==Message.SENDFILE){
int result=JOptionPane.showConfirmDialog(null, "好友"+msg.getUsername()+
"想向您发送文件"+msg.getMsg()+"是否接受","confirm",JOptionPane.YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION){
Message mm=new Message();
mm.setFileaccept(true);
try {
out.writeObject(mm);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JFileChooser jfc=new JFileChooser();
jfc.showSaveDialog(icf);
File f=jfc.getSelectedFile();
try {
FileOutputStream fout=new FileOutputStream(f);
InputStream fin=socket.getInputStream();
int data;
while((data=fin.read())!=-1){
fout.write(data);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "文件传输完毕");
return;
}
else{
Message mm=new Message();
mm.setFileaccept(false);
try {
out.writeObject(mm);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}