-
Notifications
You must be signed in to change notification settings - Fork 3
/
Link.java
278 lines (262 loc) · 7.42 KB
/
Link.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package DRAKO.table;
import DRAKO.unit.*;
import DRAKO.Listener.*;
import DRAKO.talk.*;
import DRAKO.img.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class Link extends ImgJPanel implements ActionListener
{
private static final long serialVersionUID=1L;
static String DEFAULT_IP="230.0.0.1";
static int DEFAULT_PORT=6000;
static int PORT_1=6001,PORT_2=6002;
String roomname,player1,player2,name,rname,rip;
JLabel la;
JLabel lb;
JLabel lc;
JTextField ta;
ImgJButton submit,rescan,connection;
public ImgJButton cancel;
public ImgJButton start;
JScrollPane sp;
DefaultListModel<String> list;
JList<String> roomlist;
ActionListener mainl;
DocumentListener textl;
ListSelectionListener text2;
StartGameListener startgame;
Thread scanr=null,wait_for_start=null;;
Boolean tag=false;
MulticastSocket socket=null;
IpList head;
ServerSocket waitl;
Socket rival_1=null,rival_2=null;
StreamData _talk,_chat;
ImgLoader imgloader;
public Link(StartGameListener startgame,ImgLoader imgloader){
super(imgloader.get_bg(5));
this.startgame=startgame;
this.imgloader=imgloader;
reset();
}
public void reset(){
removeAll();
repaint();
la=new JLabel("房間名稱:");
lb=new JLabel("暱稱:");
ta=new JTextField("PlayerL");
submit=new ImgJButton("submit",imgloader.get_btn(100,30),24);
start=new ImgJButton("start",imgloader.get_btn(100,30),24);
connection=new ImgJButton("連線",imgloader.get_btn(100,30),24);
rescan=new ImgJButton("刷新",imgloader.get_btn(100,30),24);
cancel=new ImgJButton("cancel",imgloader.get_btn(100,30),24);
setLayout(null);
removeAll();
textl=new DocumentListener(){
public void changedUpdate(DocumentEvent e){}
public void insertUpdate(DocumentEvent e){
submit.setEnabled(ta.getText().length()!=0);
}
public void removeUpdate(DocumentEvent e){
submit.setEnabled(ta.getText().length()!=0);
}
};
la.setText("暱稱:");
la.setBounds(230,200,100,30);
la.setForeground(Color.YELLOW);
la.setFont(new Font("helvetica",Font.BOLD,16));
add(la);
ta.setText("PlayerL");
ta.setBounds(300,200,200,30);
ta.getDocument().addDocumentListener(textl);
add(ta);
submit.setBounds(270,320,100,30);
submit.addActionListener(this);
submit.setEnabled(true);
add(submit);
cancel.setBounds(400,320,100,30);
cancel.addActionListener(mainl);
add(cancel);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==submit){
name=ta.getText();
setList();
}
if(e.getSource()==rescan){
runscan();
}
if(e.getSource()==connection){
try{
rname=roomlist.getSelectedValue().toString();
int id=roomlist.getAnchorSelectionIndex();
rip=head.getIp(id+1);
setLink(id+1);
}catch(NullPointerException f){
System.out.println("請選取房間!!");
}
}
}
void setList(){
removeAll();
repaint();
text2=new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
connection.setEnabled(true);
}
};
sp=new JScrollPane();
list=new DefaultListModel<String>();
roomlist=new JList<String>(list);
roomlist.setForeground(Color.YELLOW);
roomlist.setFont(new Font("helvetica",Font.BOLD,16));
roomlist.setOpaque(false);
sp.setBounds(100,80,400,300);
sp.setOpaque(false);
sp.setViewportView(roomlist);
add(sp);
roomlist.addListSelectionListener(text2);
connection.setBounds(550,250,100,30);
connection.addActionListener(this);
add(connection);
rescan.setBounds(550,290,100,30);
rescan.addActionListener(this);
add(rescan);
cancel.setBounds(550,330,100,30);
add(cancel);
runscan();
}
void runscan(){
scanr=new Thread(new Runnable(){
public void run(){
try{
scanRoom();
}catch(IOException e){}
}
});
tag=true;
scanr.start();
try{
java.lang.Thread.sleep(1000);
}catch(InterruptedException e){}
tag=false;
socket.close();
}
void scanRoom() throws IOException{
head=new IpList("head","","",0);
list.clear();
connection.setEnabled(false);
socket=new MulticastSocket(DEFAULT_PORT);
InetAddress address=InetAddress.getByName(DEFAULT_IP);
socket.joinGroup(address);
DatagramPacket get,go;
byte[] buf=new byte[1024];
String te=new String("$f$,");
buf=te.getBytes();
go=new DatagramPacket(buf,buf.length,address,6000);
socket.send(go);
while(tag){
byte[] bug=new byte[1024];
get=new DatagramPacket(bug,bug.length);
socket.receive(get);
InetAddress sendIP = get.getAddress();
System.out.println("IP : "+sendIP.getHostAddress());
String rnm = new String(bug,0,get.getLength());
String[] getString=rnm.split(",");
if(!getString[0].equals(new String("$f$"))&&head.add(sendIP.getHostAddress(),getString[0],getString[1])){
String blank=" ";
getString[0]=(getString[0]+blank+blank+blank).substring(0,50-getString[0].getBytes().length);
list.addElement(getString[0]+"| "+getString[1]);
}
}
}
public void setLink(int id){
roomname=head.getRoom(id);
player1=head.getName(id);
player2=name;
removeAll();
repaint();
la.setText("房間名稱: "+roomname);
la.setBounds(200,200,300,30);
add(la);
lb.setText("玩家1: "+player1);
lb.setBounds(220,250,300,30);
lb.setForeground(Color.YELLOW);
lb.setFont(new Font("helvetica",Font.BOLD,16));
add(lb);
lc=new JLabel("玩家2: "+player2);
lc.setBounds(220,300,200,30);
lc.setForeground(Color.YELLOW);
lc.setFont(new Font("helvetica",Font.BOLD,16));
add(lc);
start.setText("waiting");
start.setBounds(280,340,100,30);
start.setEnabled(false);
add(start);
cancel.setBounds(400,340,100,30);
add(cancel);
tag=true;
wait_for_start=new Thread(new Runnable(){
public void run(){
try{
waitl=new ServerSocket(PORT_2);
rival_1=new Socket(rip,PORT_1);
rival_2=waitl.accept();
waitl.close();
_talk=new StreamData(rival_1,true);
_chat=new StreamData(rival_2,true);
System.out.println("talk to room!");
_talk.write(name+",");
while(!_talk.read().get(0).equals(new String("start"))){}
startgame.BuildGame(_talk,_chat);
}catch(IOException f){System.out.println("fuck");}
}
});
wait_for_start.start();
}
public void addMainL(ActionListener mainl){
this.mainl=mainl;
}
public void stopAll(){
tag=false;
if(socket!=null)socket.close();
socket=null;
}
}
class IpList
{
String _ip,_name,_room;
int _id;
IpList _next=null;
IpList(String ip,String room,String name,int id){
_ip=ip;
_name=name;
_room=room;
_id=id;
}
public boolean add(String ip,String room,String name){
if(_ip.equals(ip))return false;
if(_next!=null)return _next.add(ip,room,name);
_next=new IpList(ip,room,name,_id+1);
return true;
}
public String getIp(int id){
if(_id==id)return _ip;
return _next.getIp(id);
}
public String getRoom(int id){
if(_id==id)return _room;
return _next.getRoom(id);
}
public String getName(int id){
if(_id==id)return _name;
return _next.getName(id);
}
}