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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
package NET.worlds.scape;
import NET.worlds.console.Console;
import NET.worlds.console.Main;
import NET.worlds.console.MainCallback;
import NET.worlds.core.Std;
import NET.worlds.network.InfiniteWaitException;
import NET.worlds.network.ObjID;
import NET.worlds.network.PacketTooLargeException;
import NET.worlds.network.PropertyList;
import NET.worlds.network.PropertySetCmd;
import NET.worlds.network.WorldServer;
import NET.worlds.network.net2Property;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Vector;
public class Sharer extends SuperRoot implements MainCallback {
private Vector<Attribute> attributes;
private ByteArrayOutputStream bs = new ByteArrayOutputStream();
private boolean shared = false;
public static final int FIRST_TIME = 0;
public static final int STATIC = 2;
public static final int FORWARDED_STATIC = 3;
public static final int DYNAMIC = 4;
public static final int FORWARDED_DYNAMIC = 5;
public static final int FORWARDED = 1;
private boolean createdFromNet;
Vector<Attribute> noteQ;
Vector<Attribute> prepQ;
Vector<Attribute> sendQ;
private DynamicForwardAttribute dfa;
int lastSendTime;
private static Object classCookie = new Object();
private Vector<Object> restoredAttributes = null;
public Sharer() {
this.attributes = new Vector<Attribute>(256);
this.attributes.setSize(256);
}
public boolean isShared() {
return this.shared;
}
private int getMaybeDefaultMode() {
return ((WObject)this.getOwner()).getSharerMode();
}
public int getMode() {
int mode = this.getMaybeDefaultMode();
if (mode == 0) {
this.setMode(mode);
}
return mode;
}
public void setMode(int mode) {
WObject w = (WObject)this.getOwner();
if (w instanceof Room) {
mode = 2;
} else if (this.createdFromNet) {
mode = 4;
} else if (mode == 2 || mode == 4) {
mode = 0;
}
if (mode == 0) {
mode = 3;
}
int oldMode = this.getMaybeDefaultMode();
if (oldMode != mode) {
this.unshare();
w.setSharerMode(mode);
}
this.share();
}
public void createDynamicForwardedFromNet(DynamicForwardAttribute dfa) {
assert !((WObject)this.getOwner()).isActive();
this.dfa = dfa;
((WObject)this.getOwner()).setSharerMode(5);
}
public void createDynamicFromNet() {
assert !((WObject)this.getOwner()).isActive() || this.createdFromNet;
this.createdFromNet = true;
((WObject)this.getOwner()).setSharerMode(4);
}
public void adjustShare() {
this.share();
}
private void share() {
if (!this.shared) {
int mode = this.getMaybeDefaultMode();
switch (mode) {
case 0:
this.setMode(0);
case 1:
case 2:
default:
break;
case 3:
this.flushQueue();
Enumeration<Attribute> e = this.getAttributes();
while (e.hasMoreElements()) {
e.nextElement().addForwarding();
}
break;
case 4:
assert this.createdFromNet;
break;
case 5:
if (this.dfa == null) {
this.flushQueue();
WObject w = (WObject)this.getOwner();
WObject terminal = w.getServed();
if (w.getSourceURL() != null && terminal != null) {
this.dfa = new DynamicForwardAttribute(-1);
if (terminal.getSharer().addAttribute(this.dfa) == -1) {
this.dfa = null;
} else {
this.dfa.connect(w);
}
}
}
}
this.shared = true;
}
}
private void unshare() {
if (this.shared) {
int mode = this.getMaybeDefaultMode();
switch (mode) {
case 2:
case 4:
default:
break;
case 3:
Enumeration<Attribute> e = this.getAttributes();
while (e.hasMoreElements()) {
e.nextElement().unforward();
}
break;
case 5:
if (this.dfa != null) {
this.dfa.unconnect();
this.dfa = null;
}
}
this.shared = false;
}
}
public int addAttribute(Attribute a) {
this.setMode(this.getMaybeDefaultMode());
a._attrID = this.getFreeAttrIDFor(a, a._attrID);
if (a._attrID == -1) {
return a._attrID;
} else {
try {
this.add(a);
} catch (ClassCastException var3) {
Console.println(Console.message("Cant-attach") + var3);
a._attrID = -1;
return a._attrID;
}
this.attributes.setElementAt(a, a._attrID);
if (this.getMaybeDefaultMode() == 3) {
a.addForwarding();
}
return a._attrID;
}
}
public boolean attrIDAvailable(int id) {
return this.attributes.elementAt(id) == null;
}
private int getFreeAttrID(int id, int low, int high) {
if (id >= 20 && id <= 255 && this.attrIDAvailable(id)) {
return id;
} else {
int freeCount = this.countFree(low, high);
if (freeCount == 0) {
return -1;
} else {
int i;
label42:
while (true) {
int pos = (int)(Math.random() * freeCount);
i = low;
while (true) {
if (this.attributes.elementAt(i) == null) {
if (--pos < 0) {
assert i <= high;
if (i != 150 && i != 155 && i != 160) {
break label42;
}
break;
}
}
i++;
}
}
return i;
}
}
}
public int getFreeAttrIDFor(Attribute a, int id) {
return a instanceof DynamicForwardAttribute ? this.getFreeAttrID(id, 210, 249) : this.getFreeAttrID(id, 100, 249);
}
public void removeAttribute(Attribute a) {
a.detach();
if (a.getOwner() == null) {
this.attributes.setElementAt(null, a._attrID);
}
}
public int resetAttributeID(Attribute a, int newID) {
newID = this.getFreeAttrIDFor(a, newID);
if (newID == -1) {
return newID;
} else {
if (this.attributes.elementAt(newID) == null) {
int oldID = a._attrID;
this.attributes.setElementAt(a, newID);
this.attributes.setElementAt(null, oldID);
a._attrID = newID;
}
return newID;
}
}
public void moveToSlot(WObject child, int newID) {
if (child.isDynamic()) {
Sharer s = child.getSharer();
if (s.dfa != null) {
this.resetAttributeID(s.dfa, newID);
}
}
}
public Enumeration<Attribute> getAttributes() {
return this.getAttributesList().elements();
}
public Vector<Attribute> getAttributesList() {
Vector<Attribute> v = new Vector<Attribute>(this.attributes.size());
this.fillAttributesList(v);
return v;
}
public void fillAttributesList(Vector<Attribute> v) {
int end = this.attributes.size();
for (int i = 0; i < end; i++) {
Attribute a = this.attributes.elementAt(i);
if (a != null) {
v.addElement(a);
}
}
}
public Attribute getAttribute(int id) {
try {
return this.attributes.elementAt(id);
} catch (ArrayIndexOutOfBoundsException var3) {
return null;
}
}
public boolean isEmpty() {
return this.countFree(0, 255) == 256;
}
public int countFree(int low, int high) {
int count = 0;
for (int i = low; i <= high; i++) {
if (this.attributes.elementAt(i) == null) {
count++;
}
}
return count;
}
@Override
public void getChildren(DeepEnumeration d) {
d.addChildVectorWithNulls(this.attributes);
}
public void noteChange(Attribute a, boolean sendTriggers) {
assert Main.isMainThread();
if (this.prepQ == null) {
this.noteQ = new Vector<Attribute>();
this.prepQ = new Vector<Attribute>();
this.sendQ = new Vector<Attribute>();
}
if (sendTriggers && !this.noteQ.contains(a)) {
if (this.noteQ.isEmpty() && this.prepQ.isEmpty() && this.sendQ.isEmpty()) {
Main.register(this);
}
this.noteQ.addElement(a);
}
Attribute fa;
if (this.getMode() == 3 && (fa = a.getForwardAttribute()) != null) {
fa.noteChange();
} else if (this.dfa != null) {
this.dfa.noteChange();
} else if (!this.prepQ.contains(a)) {
if (this.noteQ.isEmpty() && this.prepQ.isEmpty() && this.sendQ.isEmpty()) {
Main.register(this);
}
this.prepQ.addElement(a);
}
}
private void prepQueue() {
if (!this.noteQ.isEmpty()) {
int end = this.noteQ.size();
for (int i = 0; i < end; i++) {
Attribute a = this.noteQ.elementAt(i);
ValueEvent e = new ValueEvent(Std.getFastTime(), this.getOwner(), (WObject)this.getOwner(), a);
a.trigger(e);
}
this.noteQ.removeAllElements();
}
int end = this.prepQ.size();
for (int i = 0; i < end; i++) {
Attribute a = this.prepQ.elementAt(i);
this.bs.reset();
try {
a.generateNetData(new DataOutputStream(this.bs));
} catch (IOException var5) {
System.err.println(var5);
throw new Error("Fatal in generateNetData");
}
a._outgoing = this.bs.toByteArray();
if (Std.byteArraysEqual(a._outgoing, a._serverData)) {
a._outgoing = null;
} else if (!this.sendQ.contains(a)) {
this.sendQ.addElement(a);
}
}
this.prepQ.removeAllElements();
}
@Override
public void mainCallback() {
if (this.noteQ.isEmpty() && this.prepQ.isEmpty() && this.sendQ.isEmpty()) {
Main.unregister(this);
} else {
int frameTime = Std.getFastTime();
if (frameTime > this.lastSendTime + 250) {
this.sendQueue();
Main.unregister(this);
this.lastSendTime = frameTime;
}
}
}
private void flushQueue() {
if (this.prepQ != null) {
this.prepQueue();
if (!this.sendQ.isEmpty()) {
int i = this.sendQ.size();
while (--i >= 0) {
Attribute a = this.sendQ.elementAt(i);
a._waitingForFeedback = false;
a._serverData = null;
a._outgoing = null;
}
this.sendQ.removeAllElements();
}
}
}
private void sendQueue() {
this.prepQueue();
WorldServer rs = ((WObject)this.getOwner()).getServer();
if (rs != null) {
int end = this.sendQ.size();
for (int i = 0; i < end; i++) {
Attribute a = this.sendQ.elementAt(i);
if (a._outgoing != null) {
this.share(rs, a);
a._serverData = a._outgoing;
a._outgoing = null;
a._waitingForFeedback = true;
}
}
}
this.sendQ.removeAllElements();
}
private void share(WorldServer ws, Attribute a) {
SuperRoot w = this.getOwner();
ObjID objID = null;
if (w instanceof Room) {
Pilot pilot = Pilot.getActive();
if (pilot != null && pilot.getLastServedRoom() != w) {
objID = new ObjID(((Room)w).getNetworkRoom().getLongID());
} else {
objID = new ObjID(253);
}
} else if (w instanceof Pilot) {
objID = new ObjID(1);
} else {
objID = new ObjID(w.getName());
}
try {
PropertyList propList = new PropertyList();
propList.addProperty(new net2Property(a._attrID, a.getFlags(), a.getAccessFlags(), a._outgoing));
ws.sendNetworkMsg(new PropertySetCmd(objID, propList));
} catch (InfiniteWaitException var6) {
Console.println(Console.message("Net-shutdown") + var6.toString());
} catch (PacketTooLargeException var7) {
assert false;
}
}
public void setFromNetData(int attrID, byte[] data) {
if (this.prepQ != null) {
this.prepQueue();
}
Attribute a = this.getAttribute(attrID);
if (a == null) {
if ((this.getMode() & 1) != 0) {
Object[] arguments = new Object[]{new String("" + attrID), new String(this.getOwner().getName())};
Console.println(MessageFormat.format(Console.message("Unknown-attr"), arguments));
return;
}
a = new DynamicForwardAttribute(attrID);
this.addAttribute(a);
}
this.setData(a, data);
if (!a.loaded) {
a.loaded = true;
if (a.callbacks != null && a.callbacks.size() > 0) {
Enumeration<LoadedAttribute> e = a.callbacks.elements();
while (e.hasMoreElements()) {
LoadedAttribute la = e.nextElement();
la.loadedAttribute(a, null);
}
a.callbacks.removeAllElements();
}
}
}
private void setData(Attribute a, byte[] data) {
boolean matchesLastSent = Std.byteArraysEqual(data, a._serverData);
if (a._waitingForFeedback) {
if (!matchesLastSent) {
return;
}
a._waitingForFeedback = false;
if (data.length != 0 || a._outgoing != null) {
return;
}
} else if (matchesLastSent) {
return;
}
a._serverData = data;
if (Std.byteArraysEqual(data, a._outgoing)) {
a._outgoing = null;
} else if (a._outgoing == null) {
try {
a.setFromNetData(new DataInputStream(new ByteArrayInputStream(data)), data.length);
} catch (IOException var5) {
var5.printStackTrace();
Console.println(Console.message("Unrec-format") + a.getName());
}
ValueEvent e = new ValueEvent(Std.getFastTime(), this, (WObject)this.getOwner(), a);
a.trigger(e);
}
}
@Override
public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
throw new NoSuchPropertyException();
}
@Override
public void saveState(Saver s) throws IOException {
s.saveVersion(2, classCookie);
super.saveState(s);
s.saveVector(this.getAttributesList());
}
@Override
public void restoreState(Restorer r) throws IOException, TooNewException {
switch (r.restoreVersion(classCookie)) {
case 0:
this.setName(r.restoreString());
int nSharedAtts = r.restoreInt();
this.restoredAttributes = new Vector<Object>(nSharedAtts);
for (int i = 0; i < nSharedAtts; i++) {
this.restoredAttributes.addElement(r.restoreString());
}
break;
case 1:
this.setName(r.restoreString());
this.restoredAttributes = r.restoreVector();
break;
case 2:
super.restoreState(r);
this.restoredAttributes = r.restoreVector();
break;
default:
throw new TooNewException();
}
}
public void ownerPostRestore() {
int desiredMode = this.getMaybeDefaultMode();
((WObject)this.getOwner()).setSharerMode(0);
this.setMode(desiredMode);
Enumeration<Object> e = this.restoredAttributes.elements();
while (e.hasMoreElements()) {
Attribute a = (Attribute)e.nextElement();
this.addAttribute(a);
}
this.restoredAttributes = null;
}
public void releaseAuxilaryData() {
for (int i = 0; i < 256; i++) {
Attribute a = this.attributes.elementAt(i);
if (a != null) {
a.releaseAuxilaryData();
}
}
}
}
|