-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebSocketSimProc.java
191 lines (152 loc) · 4.98 KB
/
WebSocketSimProc.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
/*
* Copyright (C) J.P. Morrison, Enterprises, Ltd. 2009, 2012 All Rights Reserved.
*/
package com.jpaulmorrison.fbp.examples.components;
//import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import com.jpaulmorrison.fbp.core.engine.*;
/**
* Sample web socket request processor
*
* This is a non-looper processing one substream per activation
*
* Expected input is a substream, consisting of - open bracket - packet
* containing socket reference - Java Class WebSocket - packet containing data
* reference - should be a string, colon, blank, 'namelist' - close bracket
*
* Generated output is a substream, conforming to requirements of
* WebSocketRespond - in the case of this component, this is - open bracket -
* packet containing socket reference - Java Class WebSocket - 0 or more packets
* containing data string references - close bracket
*
*/
@ComponentDescription("Simple request processing")
@OutPort("OUT")
@InPort("IN")
public class WebSocketSimProc extends Component {
static final String copyright = "Copyright 2007, 2014, J. Paul Morrison. At your option, you may copy, "
+ "distribute, or make derivative works under the terms of the Clarified Artistic License, "
+ "based on the Everything Development Company's Artistic License. A document describing "
+ "this License may be found at http://www.jpaulmorrison.com/fbp/artistic2.htm. "
+ "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";
private InputPort inport;
private OutputPort outport;
/*
* Make sure that the substream comes out of a single port of a single
* process, all together...
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
protected void execute() {
// Packet q = jarport.receive();
// String jarfilename = (String) q.getContent();
// drop(q);
// jarport.close();
LinkedList l = new LinkedList();
Packet lbr = inport.receive(); // open bracket
Packet p1 = inport.receive(); // contains Connection
Packet p2 = inport.receive();
while (p2.getType() != Packet.CLOSE) {
String message = (String) p2.getContent();
message = message.substring(0, message.length() - 1); // drop squiggly bracket
l.add(message);
drop(p2);
p2 = inport.receive();
}
drop(p2);
String s = (String) l.get(0);
int i = s.indexOf(":");
String t = s.substring(0, i);
if (s.endsWith("namelist")) {
/*
* try { sleep(j * 500); } catch (InterruptedException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
*/
outport.send(lbr);
outport.send(p1); // contains Connection
outport.send(create(t + " Joe Fresh"));
outport.send(create(t + " Aunt Jemima"));
outport.send(create(t + " Frankie Tomatto's"));
outport.send(create(Packet.CLOSE, ""));
} else
if (s.endsWith("complist")) {
outport.send(lbr);
outport.send(p1); // contains Connection
s = (String) l.get(1);
i = s.indexOf(":");
String jarfilename = s.substring(i + 1);
jarfilename = jarfilename.trim();
Enumeration<?> entries;
DefaultMutableTreeNode top = new DefaultMutableTreeNode();
DefaultMutableTreeNode next;
try {
JarFile jarFile = new JarFile(jarfilename);
entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = (JarEntry) entries.nextElement();
// System.out.println(entry);
outport.send(create(t + " " + entry));
if (!(entry.isDirectory())) {
s = entry.getName();
if (s.toLowerCase().endsWith(".class")) {
next = top;
DefaultMutableTreeNode child;
while (true) {
i = s.indexOf("/");
if (i == -1) {
child = new DefaultMutableTreeNode(s);
next.add(child);
break;
} else {
String u = s.substring(0, i);
if (null == (child = findChild(next, u))) {
child = new DefaultMutableTreeNode(u);
next.add(child);
}
s = s.substring(i + 1);
next = child;
}
}
}
}
}
jarFile.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
outport.send(create(Packet.CLOSE, ""));
} else {
outport.send(lbr);
outport.send(p1);
outport.send(create("Unknown keyword: " + s));
outport.send(create(Packet.CLOSE, ""));
}
}
private DefaultMutableTreeNode findChild(DefaultMutableTreeNode current,
String t) {
if (current == null)
return null;
@SuppressWarnings("unchecked")
Enumeration<TreeNode> e = current.children();
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement();
Object obj = node.getUserObject();
if (t.equals((String) obj))
return node;
}
return null;
}
@Override
protected void openPorts() {
inport = openInput("IN");
// jarport = openInput("JARFILE");
outport = openOutput("OUT");
}
}