This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
IncomingLink.java
118 lines (104 loc) · 4.15 KB
/
IncomingLink.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
import java.io.*;
import java.net.*;
/**
* @author Angelo DiNardi ([email protected])
*/
class IncomingLink extends Thread {
Socket socket = null;
public IncomingLink( Socket socket ) {
this.socket = socket;
}
public void run() {
listen();
}
private void listen() {
InputStream in = null;
try {
in = socket.getInputStream();
}catch( Exception e ) {
e.printStackTrace();
System.out.println( "Cannot get input stream");
}
int opcode = 0;
String data = null;
String fullLine = null;
//int[] odata;
//int temp;
//String datamsg = null;
//Long opcodemsg;
int lo = 0; //length opcode
int ld = 0; //length data
boolean active = true;
BufferedReader read = new BufferedReader( new InputStreamReader( in ) );
while( active ) {
try {
fullLine = read.readLine();
System.out.println( "Raw Crap: " + fullLine );
}catch( SocketException e ) {
//active = false;
//e.printStackTrace();
System.out.println( "Lost Connection To Server" );
ConfigMgr.getInstance().setConnected( false );
CommLink.getInstance().Reconnect();
break;
}catch( IOException e ) {
System.out.println( "Lost Connection To Server" );
ConfigMgr.getInstance().setConnected( false );
CommLink.getInstance().Reconnect();
break;
}catch( Exception e ) {
e.printStackTrace();
System.out.println( "Error reading Incoming Data.");
}
if( fullLine == null ) {
active = false;
System.out.println( "Lost Connection To Server" );
ConfigMgr.getInstance().setConnected( false );
CommLink.getInstance().Reconnect();
break;
}
//System.out.println( read.readLine() );
opcode = Integer.parseInt( fullLine.substring(0,1) );
data = fullLine.substring(1, fullLine.length() );
switch( opcode ) {
case -1:
//Um, no?
break;
case 1:
//Login ACK
System.out.println( "Login ACK!" );
CommLink.getInstance().getOutgoingLink().sendSlotInfo( OneWireCommands.getInstance().getEmptyInfo() );
break;
case 2:
//Login NACK
System.out.println( "Login NACK!" );
break;
case 3:
//drop slot
System.out.println( "Drop Slot!" );
//OneWireLightShow.getInstance().setStop( true );
this.setPriority( Thread.MAX_PRIORITY );
int stat = OneWireCommands.getInstance().drop( Integer.parseInt(data) );
this.setPriority( Thread.NORM_PRIORITY );
System.out.println("About to send drop ack... " + stat);
if( stat == 200 ) {
CommLink.getInstance().getOutgoingLink().sendDropACK();
}else{
CommLink.getInstance().getOutgoingLink().sendDropNACK();
}
// Send our entire slot status!
CommLink.getInstance().getOutgoingLink().sendSlotInfo( OneWireCommands.getInstance().getEmptyInfo() );
//OneWireLightShow.getInstance().setStop( false );
break;
case 6:
//Slot Status Req
System.out.println( "Slot Status Req!" );
//OneWireLightShow.getInstance().setStop( true );
CommLink.getInstance().getOutgoingLink().sendSlotInfo( OneWireCommands.getInstance().getEmptyInfo() );
//OneWireLightShow.getInstance().setStop( false );
break;
}
opcode = -1;
}
}
}