加强版的带进度条的传文件和加表情的聊天室 -4
package networking;2

3
import java.io.*;4

5
/**6
* Class that will send the send file request7
* @author 8
* @version 1.09
*/10
public class RequestFrame implements Serializable11
{12
/**13
* Attributes14
*/15
private static final long serialVersionUID = 1L;16
private String sender;17
private String receiver;18
private long senderKey;19
private long fileSize;20
private double totalSections;21
private double packageSize;22
private String fileName;23
/**24
* Constructor25
*/26
public RequestFrame(String sender, String receiver, long senderKey, long fileSize, double totalSections, double packageSize,String fileName)27
{28
this.sender = sender;29
this.receiver = receiver;30
this.senderKey = senderKey;31
this.fileSize = fileSize;32
this.totalSections = totalSections;33
this.packageSize = packageSize;34
this.fileName=fileName;35
}36
/**37
* Class attribute getter38
* @return the fileSize39
*/40
public long getFileSize()41
{42
return this.fileSize;43
}44
/**45
* Class attribute setter46
* @param fileSize the fileSize to set47
*/48
public void setFileSize(long fileSize)49
{50
this.fileSize = fileSize;51
}52
/**53
* Class attribute getter54
* @return the packageSize55
*/56
public double getPackageSize()57
{58
return this.packageSize;59
}60
/**61
* Class attribute setter62
* @param packageSize the packageSize to set63
*/64
public void setPackageSize(double packageSize)65
{66
this.packageSize = packageSize;67
}68
/**69
* Class attribute getter70
* @return the receiver71
*/72
public String getReceiver()73
{74
return this.receiver;75
}76
/**77
* Class attribute setter78
* @param receiver the receiver to set79
*/80
public void setReceiver(String receiver)81
{82
this.receiver = receiver;83
}84
/**85
* Class attribute getter86
* @return the sender87
*/88
public String getSender()89
{90
return this.sender;91
}92
/**93
* Class attribute setter94
* @param sender the sender to set95
*/96
public void setSender(String sender)97
{98
this.sender = sender;99
}100
/**101
* Class attribute getter102
* @return the senderKey103
*/104
public long getSenderKey()105
{106
return this.senderKey;107
}108
/**109
* Class attribute setter110
* @param senderKey the senderKey to set111
*/112
public void setSenderKey(long senderKey)113
{114
this.senderKey = senderKey;115
}116
/**117
* Class attribute getter118
* @return the totalSections119
*/120
public double getTotalSections()121
{122
return this.totalSections;123
}124
/**125
* Class attribute setter126
* @param totalSections the totalSections to set127
*/128
public void setTotalSections(double totalSections)129
{130
this.totalSections = totalSections;131
}132
/**133
* Class attribute getter134
* @return the fileName135
*/136
public String getFileName()137
{138
return this.fileName;139
}140
/**141
* Class attribute setter142
* @param fileName the fileName to set143
*/144
public void setFileName(String fileName)145
{146
this.fileName = fileName;147
}148
}149

package networking;2

3
import java.io.*;4
import java.net.*;5

6
/**7
* class that has the run method that the thread will use8
* 9
* @author 10
* @version 1.011
*/12
public class ServerThreadHandler implements Runnable13
{14
/**15
* Attributes16
*/17
private Socket socket;18
private ServerGUI serverGUI;19
private static final int TIMEOUT_LENGTH = 1000 * 60 * 60*5;// max timeout20
21
// number,6022
// minutes23
/**24
* Constructor25
*/26
public ServerThreadHandler(Socket socket, ServerGUI serverGUI)27
{28
this.socket = socket;29
try30
{31
this.socket.setSoTimeout(TIMEOUT_LENGTH);32
}33
catch(SocketException se)34
{35
}36
this.serverGUI = serverGUI;37
}38
39
/*40
* (non-Javadoc)41
* 42
* @see java.lang.Runnable#run()43
*/44
public void run()45
{46
try47
{48
ObjectInputStream ois = new ObjectInputStream(socket49
.getInputStream());50
ObjectOutputStream oos = new ObjectOutputStream(socket51
.getOutputStream());52
try53
{54
while(true)55
{56
DataFrame df = (DataFrame)ois.readObject();57
if(df.getType().equals("Login"))58
{59
String name = (String)df.getObject();60
if(Secretary.getSecretary().isExist(name))61
{62
oos.reset();63
oos.writeObject(new DataFrame("Error",64
"Name already exists"));// name exists65
oos.flush();66
}67
else if(Coordinator.getCoordinator().findCaller(socket) != null)68
{69
oos.reset();70
oos.writeObject(new DataFrame("Error",71
"IP and port already exist"));// ip and72
// port73
// exist74
oos.flush();75
}76
else77
{78
Secretary.getSecretary().addRecord(socket, name);79
Coordinator.getCoordinator().notifyOnline(80
new Caller(socket, oos));81
this.serverGUI.addText(name + " logined");// login82
// request83
}84
continue;85
}86
if(Coordinator.getCoordinator().findCaller(socket) == null)87
{88
oos.reset();89
oos.writeObject(new DataFrame("Error",90
"Must login first"));// login validation91
oos.flush();92
}93
else if(df.getType().equals("Room"))94
{95
String nextRoom = (String)df.getObject();96
Chatter chatter = Secretary.getSecretary().findChatter(97
socket);98
String previousRoom = chatter.getRoom();99
chatter.setRoom(nextRoom);100
Coordinator.getCoordinator().updateRooms(previousRoom,101
nextRoom);102
this.serverGUI.addText(chatter.getName()103
+ " changed room to " + chatter.getRoom());// change104
// room105
// request106
}107
else if(df.getType().equals("Public"))108
{109
String message = (String)df.getObject();110
Chatter chatter = Secretary.getSecretary().findChatter(111
socket);112
Coordinator.getCoordinator().sendPublicMessage(chatter,113
message);114
this.serverGUI.addText(chatter.getName()115
+ " sent public message:\n" + message);// send116
// public117
// message118
// request119
}120
else if(df.getType().startsWith("Private"))121
{122
Chatter sender = Secretary.getSecretary().findChatter(123
socket);124
String name = df.getType().replaceFirst("Private", "");125
Chatter receiver = Secretary.getSecretary()126
.findChatter(name);127
String message = (String)df.getObject();128
Coordinator.getCoordinator().sendPrivateMessage(129
message, receiver, sender);130
this.serverGUI.addText(sender.getName()131
+ " sent private message" + message + " to "132
+ receiver.getName() + ":\n");// send private133
// message134
// request135
}136
else if(df.getType().equals("Logout"))137
{138
synchronized(Coordinator.getCoordinator())139
{140
Chatter chatter = Secretary.getSecretary()141
.findChatter(socket);142
Secretary.getSecretary().deleteRecord(socket);143
Coordinator.getCoordinator().notifyOffline(144
Coordinator.getCoordinator().findCaller(145
socket), chatter.getRoom());146
this.serverGUI.addText(chatter.getName()147
+ " loged out");// logout request148
}149
}150
else if(df.getType().equals("SendFileRequest"))151
{152
RequestFrame rf = (RequestFrame)df.getObject();153
Chatter sender = Secretary.getSecretary().findChatter(154
rf.getSender());155
Chatter receiver = Secretary.getSecretary()156
.findChatter(rf.getReceiver());157
Coordinator.getCoordinator().sendPrivateInformation(rf,158
"ReceiveFileRequest", "SendFileError",159
receiver, sender);160
this.serverGUI.addText(sender.getName()161
+ " sent send file request to "162
+ receiver.getName() + ":\n");163
}164
else if(df.getType().equals("SendFileAcknowledgement"))165
{166
AcknowledgementFrame af = (AcknowledgementFrame)df167
.getObject();168
Chatter sender = Secretary.getSecretary().findChatter(169
af.getSender());170
Chatter receiver = Secretary.getSecretary()171
.findChatter(af.getReceiver());172
Coordinator.getCoordinator().sendPrivateInformation(af,173
"ReceiveFileAcknowledgement",174
"ReceiveFileError", sender, receiver);175
this.serverGUI.addText(receiver.getName()176
+ " sent send file acknowledegment to "177
+ sender.getName() + ":\n");178
}179
else if(df.getType().equals("SendFileSection"))180
{181
FileFrame ff = (FileFrame)df.getObject();182
Chatter sender = Secretary.getSecretary().findChatter(183
ff.getSender());184
Chatter receiver = Secretary.getSecretary()185
.findChatter(ff.getReceiver());186
Coordinator.getCoordinator().sendPrivateInformation(ff,187
"ReceiveFileSection", "SendFileSectionError",188
receiver, sender);189
// this.serverGUI.addText(sender.getName()190
// + " sent send file section to "191
// + receiver.getName() + ":\n");192
}193
else if(df.getType().equals("IGotTheRequest"))194
{195
NotifyFrame nf=(NotifyFrame)df.getObject();196
Chatter sender = Secretary.getSecretary().findChatter(197
nf.getSender());198
Chatter receiver = Secretary.getSecretary()199
.findChatter(nf.getReceiver());200
Coordinator.getCoordinator().sendPrivateInformation(nf,201
"SheGotTheRequest",202
"SomethingWrong", sender, receiver);203
this.serverGUI.addText(receiver.getName()204
+ " send receive notification of request to "205
+ sender.getName() + ":\n");206
}207
else if(df.getType().equals("CancelFileSendFromSender"))208
{209
CancelFrame cf = (CancelFrame)df.getObject();210
Chatter sender = Secretary.getSecretary().findChatter(211
cf.getSender());212
Chatter receiver = Secretary.getSecretary()213
.findChatter(cf.getReceiver());214
Coordinator.getCoordinator().sendPrivateInformation(cf,215
"ReceiveCancelFileSendFromSender",216
"CancelFileSendFromSenderError", receiver,217
sender);218
this.serverGUI.addText(sender.getName()219
+ " send cancel file send to "220
+ receiver.getName() + ":\n");221
}222
else if(df.getType().equals("CancelFileSendFromReceiver"))223
{224
CancelFrame cf = (CancelFrame)df.getObject();225
Chatter sender = Secretary.getSecretary().findChatter(226
cf.getSender());227
Chatter receiver = Secretary.getSecretary()228
.findChatter(cf.getReceiver());229
Coordinator.getCoordinator().sendPrivateInformation(cf,230
"ReceiveCancelFileSendFromReceiver",231
"CancelFileSendFromReceiverError", sender,232
receiver);233
this.serverGUI.addText(receiver.getName()234
+ " send cancel file send to "235
+ sender.getName() + ":\n");236
}237
else238
{239
oos.reset();240
oos.writeObject(new DataFrame("Error", df.getType()));241
oos.flush();242
}243
}244
}245
catch(ClassNotFoundException e)246
{247
if(Coordinator.getCoordinator().findCaller(socket) != null)248
{249
synchronized(Coordinator.getCoordinator())250
{251
Chatter chatter = Secretary.getSecretary().findChatter(252
socket);253
Secretary.getSecretary().deleteRecord(socket);254
Coordinator.getCoordinator()255
.notifyOffline(256
Coordinator.getCoordinator()257
.findCaller(socket),258
chatter.getRoom());259
this.serverGUI260
.addText(chatter.getName() + " loged out");// exception261
// that262
// will263
// notify264
// the265
// other266
// chatters267
}268
}269
socket.close();270
}271
catch(InterruptedIOException iioe)272
{273
if(Coordinator.getCoordinator().findCaller(socket) != null)274
{275
synchronized(Coordinator.getCoordinator())276
{277
Chatter chatter = Secretary.getSecretary().findChatter(278
socket);279
Secretary.getSecretary().deleteRecord(socket);280
Coordinator.getCoordinator()281
.notifyOffline(282
Coordinator.getCoordinator()283
.findCaller(socket),284
chatter.getRoom());285
this.serverGUI286
.addText(chatter.getName() + " loged out");// exception287
// that288
// will289
// notify290
// the291
// other292
// users293
}294
}295
socket.close();296
}297
catch(IOException ioe)298
{299
if(Coordinator.getCoordinator().findCaller(socket) != null)300
{301
synchronized(Coordinator.getCoordinator())302
{303
Chatter chatter = Secretary.getSecretary().findChatter(304
socket);305
Secretary.getSecretary().deleteRecord(socket);306
Coordinator.getCoordinator()307
.notifyOffline(308
Coordinator.getCoordinator()309
.findCaller(socket),310
chatter.getRoom());311
this.serverGUI312
.addText(chatter.getName() + " loged out");// exception313
}314
}315
socket.close();316
}317
}318
catch(IOException ioe)319
{320
}321
}322
}323

package networking;2

3
import java.awt.*;4
import java.awt.event.*;5
import javax.swing.*;6

7
/**8
* server gui class that will display the updated information9
* 10
* @author 11
* @version 1.012
*/13
public class ServerGUI extends JFrame14
{15
/**16
* Attributes17
*/18
private static final long serialVersionUID = 1L;19
private JPanel jContentPane = null;20
private JDesktopPane jDesktopPane = null;21
private JScrollPane jScrollPane = null;22
private JTextArea jTextArea = null;23
24
/**25
* This is the default constructor26
*/27
public ServerGUI()28
{29
super();30
initialize();31
this.addWindowListener(new WindowAdapter()32
{33
public void windowClosing(WindowEvent e)34
{35
System.exit(0);// close the application,it will cause the36
// socket to be closed37
}38
});39
}40
41
/**42
* This method initializes this43
* 44
* @return void45
*/46
private void initialize()47
{48
this.setSize(579, 356);49
this.setContentPane(getJContentPane());50
this.setTitle("JFrame");51
}52
53
/**54
* This method initializes jContentPane55
* 56
* @return javax.swing.JPanel57
*/58
private JPanel getJContentPane()59
{60
if(jContentPane == null)61
{62
jContentPane = new JPanel();63
jContentPane.setLayout(new BorderLayout());64
jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);65
}66
return jContentPane;67
}68
69
/**70
* This method initializes jDesktopPane71
* 72
* @return javax.swing.JDesktopPane73
*/74
private JDesktopPane getJDesktopPane()75
{76
if(jDesktopPane == null)77
{78
jDesktopPane = new JDesktopPane();79
jDesktopPane.add(getJScrollPane(), null);80
}81
return jDesktopPane;82
}83
84
/**85
* This method initializes jScrollPane86
* 87
* @return javax.swing.JScrollPane88
*/89
private JScrollPane getJScrollPane()90
{91
if(jScrollPane == null)92
{93
jScrollPane = new JScrollPane();94
jScrollPane.setBounds(new Rectangle(96, 14, 391, 266));95
jScrollPane.setViewportView(getJTextArea());96
}97
return jScrollPane;98
}99
100
/**101
* This method initializes jTextArea102
* 103
* @return javax.swing.JTextArea104
*/105
private JTextArea getJTextArea()106
{107
if(jTextArea == null)108
{109
jTextArea = new JTextArea();110
}111
return jTextArea;112
}113
114
/**115
* method to add the text to the text field in the gui116
* 117
* @param text118
*/119
public void addText(String text)120
{121
if(this.jTextArea.getText().length() > 20000)122
{123
this.jTextArea.setText("");124
}125
else126
{127
if(this.jTextArea.getText().equals(""))128
{129
this.jTextArea.setText(text);130
}131
else132
{133
this.jTextArea.setText(this.jTextArea.getText() + "\n" + text);134
}135
}136
}137
}138

package networking;2

3
import java.io.*;4
import java.net.*;5

6
/**7
* method that will start the server,and begin to listen to the connection8
* requests and create new threads to deal with those requests9
* 10
* @author 11
* @version 1.012
*/13
public class Server14
{15
/**16
* Attributes17
*/18
private ServerSocket ss;19
private static final int BIND_PORT = 2008;20
private static final int MAX_CONNECTIONS = 1000;21
private static ThreadGroup connections = new ThreadGroup("connections");22
private ServerGUI serverGUI;23
24
/**25
* Constructor26
*/27
public Server(ServerGUI serverGUI)28
{29
this.serverGUI = serverGUI;30
}31
32
/**33
* method to start the server34
*/35
public void run()36
{37
try38
{39
ss = new ServerSocket(BIND_PORT);40
while(true)41
{42
try43
{44
Socket socket = ss.accept();45
if(connections.activeCount() >= MAX_CONNECTIONS)// if the46
// server47
// has48
// reached49
// the max50
// connections,then51
// drop the52
// request53
{54
socket.close();55
}56
else57
{58
ServerThreadHandler sth = new ServerThreadHandler(59
socket, serverGUI);60
Thread thread = new Thread(connections, sth);61
thread.start();// create new thread to deal with the62
// new connection63
}64
}65
catch(IOException e)66
{67
continue;68
}69
}70
}71
catch(IOException e)72
{73
}74
}75
76
/**77
* main method to start the application78
* 79
* @param args80
*/81
public static void main(String[] args)82
{83
ServerGUI serverGUI = new ServerGUI();84
serverGUI.setVisible(true);85
Server server = new Server(serverGUI);86
server.run();87
}88
}89

package networking;2

3
import java.net.*;4
import java.util.*;5

6



/**
7
* class that will hold all the current connected chatter's information
8
*
9
* @author
10
* @version 1.0
11*/ 12publicclass Secretary
13{

































































14/** 15 * Attributes
16*/ 17private ArrayList<Chatter> register;
18privatestatic Secretary secretary;
19 20/** 21 * Constructor
22*/ 23private Secretary()
24{
25 register =new ArrayList<Chatter>();
26 } 27 28/** 29 * singleton pattern that will only hold one instance of this class
30 *
31 * @return Secretary
32*/ 33publicstatic Secretary getSecretary()
34{
35if(secretary ==null)
36{
37 secretary =new Secretary();
38 } 39return secretary;
40 } 41 42/** 43 * method that will add the record to the list
44 *
45 * @param socket
46 * @param name
47*/ 48publicvoid addRecord(Socket socket, String name)
49{
50 Chatter chatter =new Chatter(socket, name);
51synchronized(this.register)
52{
53this.register.add(chatter);
54 } 55 } 56 57/** 58 * method that will delete the record in the list
59 *
60 * @param socket
61*/ 62publicvoid deleteRecord(Socket socket)
63{
64synchronized(this.register)
65{
66 Chatter targetChatter =null;
67for(Chatter chatter: this.register)
68{
69if(chatter.getIP().equals(socket.getInetAddress().toString()))
70{
71if(chatter.getPort() == socket.getPort())
72{
73 targetChatter = chatter;
74 } 75 } 76 } 77if(targetChatter !=null)
78{
79this.register.remove(targetChatter);
80 } 81 } 82 } 83 84/** 85 * method that will find the chatter through his name
86 *
87 * @param name
88 * @return Chatter
89*/ 90public Chatter findChatter(String name)
91{
92synchronized(this.register)
93{
94for(Chatter chatter: this.register)
95{
96if(chatter.getName().equals(name))
97{
98return chatter;
99 }100 }101 }102returnnull;
103 }104105/**106 * method that will find the chatter through his ip and port
107 *
108 * @param socket
109 * @return Chatter
110*/111public Chatter findChatter(Socket socket)
112{
113synchronized(this.register)
114{
115for(Chatter chatter: this.register)
116{
117if(chatter.getIP().equals(socket.getInetAddress().toString()))
118{
119if(chatter.getPort() == socket.getPort())
120{
121return chatter;
122 }123 }124 }125 }126returnnull;
127 }128129/**130 * method that will check to see if the name has existed or not
131 *
132 * @param name
133 * @return boolean
134*/135publicboolean isExist(String name)
136{
137synchronized(this.register)
138{
139for(Chatter chatter: this.register)
140{
141if(chatter.getName().equals(name))
142{
143returntrue;
144 }145 }146 }147returnfalse;
148 }149150/**151 * method that will return the list of all the chatters
152 *
153 * @return ArrayList<Chatter>
154*/155public ArrayList<Chatter> getRegister()
156{
157returnthis.register;
158 }159160/**161 * method that will return the list of all the chatters in the particular
162 * room
163 *
164 * @param room
165 * @return ArrayList<Chatter>
166*/167public ArrayList<Chatter> getRegister(String room)
168{
169 ArrayList<Chatter> roomRegister =new ArrayList<Chatter>();
170synchronized(this.register)
171{
172if(room.equals("Hall"))
173{
174returnthis.register;
175 }176else177{
178for(Chatter chatter: this.register)
179{
180if(chatter.getRoom().equals(room))
181{
182 roomRegister.add(chatter);
183 }184 }185 }186 }187return roomRegister;
188 }189}190
package networking;2

3
import java.io.*;4

5
/**6
* @author 7
* @version 1.08
*/9
public class NotifyFrame implements Serializable10
{11
/**12
* Attributes13
*/14
private static final long serialVersionUID = 1L;15
private String sender;16
private String receiver;17
private long senderKey;18
private long receiverKey;19
/**20
* Constructor21
*/22
public NotifyFrame(String sender, String receiver, long senderKey,long receiverKey)23
{24
this.sender = sender;25
this.receiver = receiver;26
this.senderKey = senderKey;27
this.receiverKey=receiverKey;28
}29
/**30
* Class attribute getter31
* @return the receiverKey32
*/33
public long getReceiverKey()34
{35
return this.receiverKey;36
}37
/**38
* Class attribute setter39
* @param receiverKey the receiverKey to set40
*/41
public void setReceiverKey(long receiverKey)42
{43
this.receiverKey = receiverKey;44
}45
/**46
* Class attribute getter47
* @return the receiver48
*/49
public String getReceiver()50
{51
return this.receiver;52
}53
/**54
* Class attribute setter55
* @param receiver the receiver to set56
*/57
public void setReceiver(String receiver)58
{59
this.receiver = receiver;60
}61
/**62
* Class attribute getter63
* @return the sender64
*/65
public String getSender()66
{67
return this.sender;68
}69
/**70
* Class attribute setter71
* @param sender the sender to set72
*/73
public void setSender(String sender)74
{75
this.sender = sender;76
}77
/**78
* Class attribute getter79
* @return the senderKey80
*/81
public long getSenderKey()82
{83
return this.senderKey;84
}85
/**86
* Class attribute setter87
* @param senderKey the senderKey to set88
*/89
public void setSenderKey(long senderKey)90
{91
this.senderKey = senderKey;92
}93
}94




浙公网安备 33010602011771号