-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
142 lines (109 loc) · 3.36 KB
/
Server.java
File metadata and controls
142 lines (109 loc) · 3.36 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
package chatapp;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.List;
import java.io.IOException;
import java.util.Objects;
public class Server extends JFrame {
private JPanel contentPane;
List list = new List();
Peer_chat_server server;
/**
* Launch the application.
*/
public void update_list_of_users()
{
list.removeAll();
for(int i=0;i<server.user_List.size();i++)
{
if(server.user_List.get(i).is_online == true)
{
list.addItem(server.user_List.get(i).username+" (on)");
}
else {
list.addItem(server.user_List.get(i).username+" (off)");
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @param sv
*/
public Server(Peer_chat_server sv) {
this.server = sv ;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnBack = new JButton("Logout");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
Login login=new Login();
login.back();
}
});
btnBack.setBounds(10, 11, 89, 23);
contentPane.add(btnBack);
list.setBounds(153, 44, 133, 154);
contentPane.add(list);
JButton btnKickOff = new JButton("Kick off");
btnKickOff.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name_state = list.getSelectedItem();
String name = name_state.substring(0, name_state.indexOf(" "));
String state=name_state.substring(name_state.indexOf(" ")+3,name_state.length()-1);
if(Objects.equals(state, new String("n"))){
for(int i=0;i<server.user_List.size();i++)
{
if(server.user_List.get(i).username.equalsIgnoreCase(name) )
{
System.out.println("I kicked"+ name);
server.user_List.get(i).is_online = false;
try {
server.socket_map.get(server.user_List.get(i)).dis.close();
server.socket_map.get(server.user_List.get(i)).dos.close();
server.socket_map.get(server.user_List.get(i)).user.close();
server.socket_map.get(server.user_List.get(i)).interrupt();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
else JOptionPane.showMessageDialog(contentPane, "this user is already offline");
update_list_of_users();
}
});
btnKickOff.setBounds(174, 217, 97, 25);
contentPane.add(btnKickOff);
JButton btnR = new JButton("R");
btnR.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
update_list_of_users();
}
});
btnR.setBounds(10, 217, 41, 25);
contentPane.add(btnR);
}
}