-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM_server.java
More file actions
157 lines (129 loc) · 3.65 KB
/
SM_server.java
File metadata and controls
157 lines (129 loc) · 3.65 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Nico Feld 1169233
package Topologie;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.AllPermission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import Topologie.MySP.*;
class SM_server {
/*
*
* CLASSES!
*/
static boolean running = true;
static class Client {
static HashMap<Integer,Client> allclients = new HashMap<Integer,Client>();
static ArrayList<Integer> allids = new ArrayList<Integer>();
static Iterator<Integer> iter;
RecieveClientInputThread input;
MySP mysp;
int id;
String name;
public Client(String ip, int port, Socket s) {
mysp = new MySP(s);
input = new RecieveClientInputThread(this);
input.start();
int i = 0;
while (allclients.containsKey(i)){
i++;
}
id = i;
allids.add(id);
allclients.put(id, this);
}
public void disconnect() {
ta.setText(ta.getText()+"\nDisconnected: "
+ this.mysp.socket.getInetAddress()+" aka \""+name+"\"");
input.interrupt();
try {
mysp.objIn.close();
mysp.objOut.close();
mysp.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
allclients.remove(id);
allids.remove((Integer) id);
}
}
static class WaitForNewClientsThread extends Thread {
public void run() {
while (running) {
Socket cs = null;
try {
cs = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
Client newClient = new Client(cs.getInetAddress().toString(),
cs.getPort(), cs);
newClient.mysp.send(new MySP_Msg_Message(-1,"Server: Connected! Your Id is: "+newClient.id));
}
}
}
static class RecieveClientInputThread extends Thread {
Client c;
public RecieveClientInputThread(Client client) {
c = client;
}
public void run() {
while (running) {
if (c.mysp.socket.isClosed())
break;
MySP_Message msg = c.mysp.receive();
if (msg instanceof MySP_Msg_Disconnect) {
c.disconnect();
}
if (msg instanceof MySP_Msg_Name) {
c.name = ((MySP_Msg_Name)msg).s;
ta.setText(ta.getText()+"\nConnected: " + c.mysp.serverIP+" aka \""+c.name+"\"");
}
if (msg instanceof MySP_Msg_GetIds) {
Client.iter = Client.allids.iterator();
String s = "\n";
while (Client.iter.hasNext()){
int tc = Client.iter.next();
s = s+"Id: "+tc+" aka \""+Client.allclients.get((Integer)tc).name+"\"\n";
}
c.mysp.send(new MySP_Msg_Message(-1,s));
}
if(msg instanceof MySP_Msg_Message){
int id = ((MySP_Msg_Message) msg).id;
String s = ((MySP_Msg_Message) msg).msg;
Client cl = Client.allclients.get(id);
cl.mysp.send(new MySP_Msg_Message(-1,"Id "+c.id +" aka \""+c.name +"\" sent: "+s));
ta.setText(ta.getText()+"\nId "+c.id+" sent to Id "+id+": "+s);
}
}
}
}
static ServerSocket ss;
static JFrame wnd;
static JTextArea ta;
public static void main(String[] argv) throws IOException {
wnd = new JFrame("SternServer");
wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.setSize(200, 300);
Container cp = wnd.getContentPane();
//cp.setLayout(new BorderLayout());
ta = new JTextArea(20,30);
ta.setText("Server started!");
cp.add(ta);
wnd.pack();
wnd.setLocationRelativeTo(null);
wnd.setVisible(true);
ss = new ServerSocket(3000);
WaitForNewClientsThread wt = new WaitForNewClientsThread();
wt.start();
}
}