-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM_client.java
More file actions
143 lines (125 loc) · 3.12 KB
/
SM_client.java
File metadata and controls
143 lines (125 loc) · 3.12 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
package Topologie;
// Nico Feld 1169233
import javax.swing.*;
import Topologie.MySP.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.StringTokenizer;
class SM_client {
static boolean running = true;
static boolean connected = false;
static String name;
static MySP mysp;
static Socket socket = null;
static JFrame wnd;
static JTextArea ta;
static JTextField tf;
public static void main(String[] argv) {
System.out.println("Client started");
wnd = new JFrame("SternClient");
wnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
wnd.setSize(200, 300);
Container cp = wnd.getContentPane();
cp.setLayout(new BorderLayout());
tf = new JTextField();
tf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringTokenizer t = new StringTokenizer(tf.getText(), " ");
switch (t.nextToken()) {
case "name": {
if (connected) {
ta.setText(ta.getText() + "\nDisconnect first!");
} else {
name = t.nextToken();
ta.setText(ta.getText()+"\nYour name is set to \""+name+"\"");
}
tf.setText("");
return;
}
case "send": {
if (connected) {
String ip = t.nextToken();
String msg = t.nextToken();
while (t.hasMoreTokens()) {
msg = msg + " " + t.nextToken();
}
ta.setText(ta.getText() + "\nSent \"" + msg + "\" to "
+ ip);
mysp.send(new MySP_Msg_Message(Integer.parseInt(ip),
msg));
} else {
ta.setText(ta.getText()
+ "\nConnect to a Server first!");
}
tf.setText("");
return;
}
case "connect": {
if (name == null) {
ta.setText(ta.getText()
+ "\nNo Name set yet. Set name with \"name {Name}\"");
tf.setText("");
return;
}
if (connected)
disconnect();
try {
socket = new Socket(t.nextToken(), 3000);
} catch (IOException ec) {
ec.printStackTrace();
ta.setText(ta.getText() + "\nCould not connect!");
}
mysp = new MySP(socket);
mysp.send(new MySP_Msg_Name(name));
connected = true;
tf.setText("");
return;
}
case "disconnect": {
disconnect();
tf.setText("");
return;
}
case "quit": {
if (connected)
disconnect();
System.exit(0);
}
case "getAllIds": {
mysp.send(new MySP_Msg_GetIds());
tf.setText("");
return;
}
}
tf.setText("");
}
});
cp.add(tf, BorderLayout.NORTH);
ta = new JTextArea(20, 30);
ta.setText("Client started!");
cp.add(ta, BorderLayout.SOUTH);
wnd.pack();
wnd.setLocationRelativeTo(null);
wnd.setVisible(true);
while (running) {
System.out.print("");
if (connected) {
MySP_Message msg = mysp.receive();
if (msg instanceof MySP_Msg_Message) {
ta.setText(ta.getText() + "\n"
+ ((MySP_Msg_Message) msg).msg);
}
}
}
}
static void disconnect() {
ta.setText(ta.getText() + "\nDisconnected!");
connected = false;
mysp.send(new MySP_Msg_Disconnect());
}
}