/
ChannelNodePanel.java
280 lines (226 loc) · 7.69 KB
/
ChannelNodePanel.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
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
package snowblossom.channels.iceleaf;
import duckutil.ConfigMem;
import duckutil.PeriodicThread;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.TreeMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import snowblossom.channels.BlockGenUtils;
import snowblossom.channels.ChannelContext;
import snowblossom.channels.ChannelID;
import snowblossom.channels.ChannelNode;
import snowblossom.channels.ChannelGlobals;
import snowblossom.channels.ChunkMapUtils;
import snowblossom.channels.MiscUtils;
import snowblossom.iceleaf.BasePanel;
import snowblossom.iceleaf.IceLeaf;
import snowblossom.lib.HexUtil;
import snowblossom.lib.SystemUtil;
import snowblossom.lib.AddressUtil;
public class ChannelNodePanel extends BasePanel
{
protected volatile ChannelNode node;
protected JProgressBar progress;
protected boolean start_attempt;
protected JTextField sub_chan_field;
protected JButton sub_button;
protected JTextField create_chan_field;
protected JButton create_button;
protected ChannelComboBox channel_import_box;
protected JButton import_button;
public ChannelNodePanel(IceLeaf ice_leaf)
{
super(ice_leaf);
}
@Override
public void setupPanel()
{
GridBagConstraints c = new GridBagConstraints();
c.weightx = 0.0;
c.weighty= 0.0;
c.gridheight = 1;
c.anchor = GridBagConstraints.WEST;
c.gridwidth = GridBagConstraints.REMAINDER;
panel.add(new JLabel("Starting local node"), c);
new NodeUpdateThread().start();
progress = new JProgressBar(0,0);
panel.add(progress, c);
c.gridwidth = 1;
panel.add(new JLabel("Subscribe to channel: "), c);
sub_chan_field = new JTextField();
sub_chan_field.setColumns(50);
panel.add(sub_chan_field, c);
c.gridwidth = GridBagConstraints.REMAINDER;
sub_button = new JButton("Subscribe");
sub_button.addActionListener( new SubscribeAction());
panel.add(sub_button, c);
c.gridwidth = 1;
panel.add(new JLabel("Create channel: "), c);
create_chan_field = new JTextField();
create_chan_field.setColumns(50);
panel.add(create_chan_field, c);
c.gridwidth = GridBagConstraints.REMAINDER;
create_button = new JButton("Create");
create_button.addActionListener( new CreateAction());
panel.add(create_button, c);
c.gridwidth = 1;
panel.add(new JLabel("Import files to channel: "), c);
channel_import_box = new ChannelComboBox(this);
panel.add(channel_import_box, c);
c.gridwidth = GridBagConstraints.REMAINDER;
import_button = new JButton("Import");
import_button.addActionListener( new ImportAction());
panel.add(import_button, c);
}
// May very well be null on startup
public ChannelNode getNode()
{
return node;
}
public class NodeUpdateThread extends PeriodicThread
{
public NodeUpdateThread()
{
super(1000);
}
public void runPass() throws Exception
{
try
{
if (!SystemUtil.isJvm64Bit())
{
setStatusBox("Node requires a 64-bit JVM");
setMessageBox("Snowblososm node uses rocksdb, which requires a 64-bit JVM to run.\n"
+ "See https://wiki.snowblossom.org/index.php/Download to download 64-bit JVM");
return;
}
if (!start_attempt)
{
startNode();
}
if (node == null) return;
StringBuilder sb=new StringBuilder();
sb.append("Local node ID: " + AddressUtil.getAddressString( ChannelGlobals.NODE_ADDRESS_STRING, node.getNodeID()));
sb.append("\n");
sb.append("DHT peers: " + node.getPeerManager().getPeersWithReason("DHT").size());
sb.append("\n");
sb.append("Channels: " + node.getChannelSubscriber().getChannelSet().size());
sb.append("\n");
for(ChannelID cid : node.getChannelSubscriber().getChannelSet())
{
sb.append(" ");
sb.append(cid);
sb.append(" ");
ChannelContext ctx = node.getChannelSubscriber().getContext(cid);
if (ctx != null)
{
if (ctx.block_ingestor.getHead() != null)
{
sb.append("{");
sb.append( HexUtil.getSafeString(ctx.block_ingestor.getHead().getEffectiveSettings().getDisplayName()));
sb.append("}");
sb.append(String.format(" blocks:%d ", ctx.block_ingestor.getHead().getHeader().getBlockHeight()));
}
sb.append(String.format("peers:%d ", ctx.getLinks().size()));
sb.append(String.format("missing_chunks:%d", ChunkMapUtils.getWantList(ctx).size()));
}
sb.append("\n");
}
setStatusBox(sb.toString().trim());
}
catch(Exception e)
{
String text = MiscUtils.printStackTrace(e);
setMessageBox(text);
e.printStackTrace();
}
}
}
public void setProgressBar(int curr, int net)
{
int enet = Math.max(net, curr);
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
progress.setMaximum(enet);
progress.setValue(curr);
}
});
}
private void startNode()
throws Exception
{
//There are too many side effects to try this more than once
start_attempt=true;
TreeMap<String, String> config_map = new TreeMap();
config_map.put("port", ice_leaf_prefs.get("channel_service_port", null));
config_map.put("web_port", ice_leaf_prefs.get("channel_web_port", null));
config_map.put("db_path", ice_leaf_prefs.get("channel_db_path", null));
config_map.put("wallet_path", ice_leaf_prefs.get("channel_wallet_path", null));
config_map.put("db_separate","true");
config_map.put("key_count", "1");
setMessageBox(config_map.toString());
ConfigMem config = new ConfigMem(config_map);
node = new ChannelNode(config, ice_leaf.getStubHolder());
setStatusBox("Node started");
setMessageBox("");
}
public class CreateAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
ChannelID cid = BlockGenUtils.createChannel(node, node.getWalletDB(), create_chan_field.getText().trim());
String base_upload = ice_leaf_prefs.get("channel_upload_path", null);
File channel_upload_path = new File(base_upload, cid.asStringWithoutColon());
channel_upload_path.mkdirs();
setMessageBox("Channel created: " + cid);
}
catch(Throwable t)
{
setMessageBox(MiscUtils.printStackTrace(t));
}
}
}
public class ImportAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
ChannelID cid = ChannelID.fromString((String)channel_import_box.getSelectedItem());
String base_upload = ice_leaf_prefs.get("channel_upload_path", null);
File channel_upload_path = new File(base_upload, cid.asStringWithoutColon());
BlockGenUtils.createBlockForFiles( node.getChannelSubscriber().openChannel(cid), channel_upload_path, node.getWalletDB());
setMessageBox("Channel files imported: " + cid);
}
catch(Throwable t)
{
setMessageBox(MiscUtils.printStackTrace(t));
}
}
}
public class SubscribeAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
ChannelID cid = ChannelID.fromString(sub_chan_field.getText().trim());
node.getChannelSubscriber().openChannel(cid);
setMessageBox("Channel added: " + cid);
}
catch(Throwable t)
{
setMessageBox(MiscUtils.printStackTrace(t));
}
}
}
}