-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.java
244 lines (219 loc) · 8.95 KB
/
Core.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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
public class Core
{
ArrayList<View> stack;
Boolean requestInProgres = false;
boolean reseted;
boolean emptyView = true;
public View back()
{
return this.stack.remove(this.stack.size() - 1);
}
public void reset()
{
if (this.requestInProgres)
{
this.reseted = true;
this.requestInProgres = false;
}
}
public View getCurrentView()
{
return this.stack.get(this.stack.size() - 1);
}
public void request(MenuLine menuLine)
{
this.requestInProgres = true;
Socket socket = null;
BufferedReader response = null;
try
{
socket = new Socket(menuLine.host, menuLine.port);
String selectorLine = menuLine.selector + "\r\n";
socket.getOutputStream().write(selectorLine.getBytes());
response = new BufferedReader( new InputStreamReader(socket.getInputStream()));
String line;
if (menuLine.type == ItemType.MENU)
{
Menu menu = new Menu(menuLine.userDisplayString, menuLine.selector, menuLine.host, menuLine.port);
while ((line = response.readLine()) != null)
{
if (line == ".")
{
response.close();
socket.close();
break;
}
if (line.length() == 0)
{
menu.error = true;
menu.errorText = "There is line with zero length";
if (this.reseted)
{
return;
}
else
{
this.requestInProgres = false;
this.stack.add(menu);
return;
}
}
switch (line.charAt(0))
{
case 'i':
{
// Strip type annotation
line = line.substring(1, line.length());
if (line.indexOf(9) != -1) // Unicode 9 is TAB character
{
line = line.substring(0, line.indexOf(9));
}
MenuLine newMenuLine = new MenuLine(ItemType.INFO , line, "", "", -1);
menu.addItem(newMenuLine);
break;
}
case '0':
{
// This is textfile
// Strip type annotation
line = line.substring(1, line.length());
String[] parts = line.split("\t");
MenuLine newMenuLine = new MenuLine(ItemType.TEXTFILE,
parts[0],
parts.length > 1 ? parts[1] : "",
parts.length > 2 ? parts[2] : "",
parts.length > 3 ? Integer.parseInt(parts[3]) : 70);
// TODO: Think waht to do when len parts < 4
menu.addItem(newMenuLine);
break;
}
case '1':
{
// Strip type annotation
line = line.substring(1, line.length());
String[] parts = line.split("\t");
MenuLine newMenuLine = new MenuLine(ItemType.MENU,
parts[0],
parts.length > 1 ? parts[1] : "",
parts.length > 2 ? parts[2] : "",
parts.length > 3 ? Integer.parseInt(parts[3]) : 70);
// TODO: Think waht to do when len parts < 4
menu.addItem(newMenuLine);
break;
}
case '2':
{
// CSO phonebook
MenuLine newMenuLine = new MenuLine(ItemType.CSO, line, "", "", -1);
break;
}
case '3':
{
// error
break;
}
case '4':
{
// BinHexed Macintosh file
MenuLine newMenuLine = new MenuLine(ItemType.MAC, line, "", "", -1);
break;
}
case '5':
{
// DOS binary archive of some sort
MenuLine newMenuLine = new MenuLine(ItemType.DOSARCHIVE, line, "", "", -1);
break;
}
case '6':
{
// UNIX uuencoded file
MenuLine newMenuLine = new MenuLine(ItemType.UNIXUUENC, line, "", "", -1);
break;
}
case '7':
{
// index-search server
MenuLine newMenuLine = new MenuLine(ItemType.SEARCH, line, "", "", -1);
break;
}
case '8':
{
// points to a text-based telnet session
MenuLine newMenuLine = new MenuLine(ItemType.TELNET, line, "", "", -1);
break;
}
case '9':
{
// binary file
MenuLine newMenuLine = new MenuLine(ItemType.BIN, line, "", "", -1);
break;
}
case '+':
{
// redundant server
MenuLine newMenuLine = new MenuLine(ItemType.REDUNDANT, line, "", "", -1);
break;
}
case 'T':
{
// ext-based tn3270 session
MenuLine newMenuLine = new MenuLine(ItemType.TN3270, line, "", "", -1);
break;
}
case 'g':
{
// gif
MenuLine newMenuLine = new MenuLine(ItemType.GIF, line, "", "", -1);
break;
}
case 'I':
{
// other image
MenuLine newMenuLine = new MenuLine(ItemType.IMAGE, line, "", "", -1);
break;
}
}
}
if (!this.reseted)
{
this.requestInProgres = false;
this.stack.add(menu);
}
}
if (menuLine.type == ItemType.TEXTFILE)
{
StringBuilder text = new StringBuilder();
while ((line = response.readLine()) != null)
{
if (line == ".")
{
response.close();
socket.close();
break;
}
text.append(line);
text.append('\n');
}
TextFile doc = new TextFile(text.toString(),
menuLine.userDisplayString,
menuLine.selector,
menuLine.host,
menuLine.port);
if (!this.reseted)
{
this.requestInProgres = false;
this.stack.add(doc);
}
}
}
catch(Exception e)
{
this.requestInProgres = false;
System.out.print(e);
}
}
}