blob: 32f5a113cc8e4394e2b08e1626725f9ae351c43c (
plain)
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
|
package tesseract.objects.remote;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.UUID;
import javax.vecmath.Vector3f;
import tesseract.objects.PhysicalObject;
/**
* Parent class of network controlled objects.
*
* @author jesse
*/
public abstract class RemoteObject extends PhysicalObject {
/**
* Serial UID.
*/
private static final long serialVersionUID = -6966379446377480998L;
/**
* Privately used by the key controller.
*/
protected static final float STEP = 0.01f;
/**
* Unique object id.
*/
private UUID myId;
/**
* The home address.
*/
private SocketAddress myHome;
/**
* The local computer address.
*/
transient private boolean isLocal;
/**
* The socket.
*/
transient private RemoteObjectReciever myListener;
/**
*
* @param thePosition
* @param mass
*/
public RemoteObject(Vector3f thePosition, float mass) {
super(thePosition, mass);
myId = UUID.randomUUID();
isLocal = true;
}
public void setHome(SocketAddress home) {
myHome = home;
myListener = new RemoteObjectReciever();
new Thread(myListener).start();
}
/**
* This method is called when a key event is received.
*
* @param event The KeyEvent recieved
*/
protected void keyEventReceived(final KeyInfo event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_W:
velocity.z -= STEP;
break;
case KeyEvent.VK_S:
velocity.z += STEP;
break;
case KeyEvent.VK_A:
velocity.x -= STEP;
break;
case KeyEvent.VK_D:
velocity.x += STEP;
break;
}
}
/**
* Get the name of the object for the menu.
*
* @return The object's name for the menu.
*/
public abstract String getName();
public UUID getId() {
return myId;
}
/**
* Send a KeyEvent to this remote object.
*
* @param keyEvent The key event
*/
public void sendKeyEvent(final KeyInfo keyEvent) {
keyEventReceived(keyEvent);
updateTranformGroup();
}
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
// Start Socket Thread
myListener = new RemoteObjectReciever();
new Thread(myListener).start();
}
private void writeObject(ObjectOutputStream out)
throws IOException {
out.defaultWriteObject();
myListener.stop();
}
private class RemoteObjectReciever implements Runnable {
private Socket mySocket;
public void stop() {
try {
mySocket.close();
} catch (IOException e) {
}
}
public void run() {
mySocket = new Socket();
try {
System.out.println("Connecting to " + myHome);
mySocket.connect(myHome);
// Send id
DataOutputStream out = new DataOutputStream(mySocket.getOutputStream());
out.writeLong(myId.getMostSignificantBits());
out.writeLong(myId.getLeastSignificantBits());
out.flush();
// Wait for data
DataInputStream in = new DataInputStream(mySocket.getInputStream());
while (true) {
try {
int key = in.readInt();
KeyInfo event = new KeyInfo(key);
sendKeyEvent(event);
} catch (Exception e) {
System.err.println("Could not read KeyEvent: " + e);
break;
}
}
} catch (IOException e) {
System.err.println(e);
}
}
}
}
|