Skip to content

Commit df07e82

Browse files
committed
增加加速器
1 parent 5774989 commit df07e82

File tree

8 files changed

+384
-8
lines changed

8 files changed

+384
-8
lines changed

conf/nbs.db

0 Bytes
Binary file not shown.

src/main/java/io/ipfs/api/ResData.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public ResData(String message) {
2929
this.message = message;
3030
}
3131

32+
/**
33+
* 0 success
34+
* @return
35+
*/
3236
public int getCode() {
3337
return code;
3438
}

src/main/java/io/nbs/client/Launcher.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ public static Launcher getContext() {
297297
}
298298

299299
public IPFS getIpfs() {
300+
if(ipfs ==null){
301+
String apiURL = ConfigurationHelper.getInstance().getIPFSAddress();
302+
try{
303+
ipfs = new IPFS(apiURL);
304+
}catch (RuntimeException e){
305+
logger.error("未能链接上IPFS服务,请检查服务是否已停止.");
306+
}
307+
}
300308
return ipfs;
301309
}
302310

src/main/java/io/nbs/client/ui/panels/manage/MMSearcherPanel.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.nbs.client.ui.components.SearchTextField;
1919
import io.nbs.client.ui.components.common.DownLoadTipIconPanel;
2020
import io.nbs.client.ui.components.common.JCirclePanel;
21+
import io.nbs.client.ui.components.forms.LCFormLabel;
2122
import io.nbs.client.ui.frames.MainFrame;
2223
import io.nbs.client.ui.panels.ParentAvailablePanel;
2324
import io.nbs.client.ui.panels.manage.body.TipResultHashPanel;
@@ -181,16 +182,19 @@ private void searchHash(final String text){
181182
tipResultHashPanel.setHash(multihash.toBase58());
182183
new Thread(()->{
183184
Multihash multihash1= Multihash.fromBase58(text);
185+
long start = System.currentTimeMillis();
184186
try {
185187
Object o =ipfs.object.stat(multihash1);
186188
String json = JSONParser.toString(o);
187189
BlockStat stat = JSON.parseObject(json,BlockStat.class);
188-
tipResultHashPanel.setBlkStat(stat,null);
190+
long usedsecd = System.currentTimeMillis()-start;
191+
tipResultHashPanel.setBlkStat(stat,null,usedsecd);
189192
} catch (IOException e) {
190193
e.printStackTrace();
191194
logger.error(e.getMessage());
192195
String error = "没有在NBS网络世界查到你要的数据["+multihash1.toBase58()+"]";
193-
tipResultHashPanel.setBlkStat(null,error);
196+
long usedsecd = System.currentTimeMillis()-start;
197+
tipResultHashPanel.setBlkStat(null,error,usedsecd);
194198
}
195199
}).start();
196200
}else {
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
package io.nbs.client.ui.panels.manage.body;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import io.ipfs.api.IPFS;
5+
import io.ipfs.api.JSONParser;
6+
import io.ipfs.api.MerkleNode;
7+
import io.ipfs.api.ResData;
8+
import io.ipfs.api.beans.blk.BlockStat;
9+
import io.ipfs.api.beans.bw.BitSwap;
10+
import io.ipfs.api.bitswap.BitSwapService;
11+
import io.ipfs.multihash.Multihash;
12+
import io.nbs.client.Launcher;
13+
import io.nbs.client.cnsts.ColorCnst;
14+
import io.nbs.client.cnsts.FontUtil;
15+
import io.nbs.client.ui.components.LCJlabel;
16+
import io.nbs.client.ui.components.SizeAutoAdjustTextArea;
17+
import io.nbs.client.ui.components.VerticalFlowLayout;
18+
import io.nbs.client.ui.components.forms.LCFormLabel;
19+
import io.nbs.client.ui.frames.MainFrame;
20+
import io.nbs.client.ui.panels.ParentAvailablePanel;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import javax.swing.*;
25+
import java.awt.*;
26+
import java.awt.event.MouseAdapter;
27+
import java.awt.event.MouseEvent;
28+
import java.awt.event.MouseListener;
29+
import java.io.IOException;
30+
import java.util.HashMap;
31+
import java.util.Map;
32+
import java.util.concurrent.ArrayBlockingQueue;
33+
import java.util.concurrent.BlockingQueue;
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.atomic.AtomicLong;
36+
37+
/**
38+
* @Package : io.nbs.client.ui.panels.manage.body
39+
* @Description :
40+
* <p>
41+
* 任务监控
42+
* </p>
43+
* @Author : lambor.c
44+
* @Date : 2018/7/18-20:39
45+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
46+
* All rights reserved.
47+
*/
48+
public class MMMonitPanel extends JPanel {
49+
private static final Logger logger = LoggerFactory.getLogger(MMMonitPanel.class);
50+
private static MMMonitPanel context;
51+
private JLabel statusLabel = new JLabel();
52+
private int seconds = 0;//用时
53+
private Color wantHashColor;
54+
55+
56+
//private SizeAutoAdjustTextArea textArea;
57+
private JPanel wantListPanel;
58+
59+
private static boolean CtrlSign = false;
60+
private long fsize = 0l;
61+
private String rhash;
62+
private IPFS ipfs;
63+
private Map<String,Long> blkmap = new HashMap();
64+
private AtomicLong completeSize = new AtomicLong();
65+
66+
private BitSwapService bitSwapService;
67+
private JPanel middlePanel ;
68+
private LCJlabel timelabel;
69+
/**
70+
*
71+
*/
72+
private BlockingQueue<Multihash> pinedBooster = new ArrayBlockingQueue<>(20);
73+
/**
74+
* construction
75+
*/
76+
public MMMonitPanel() {
77+
context = this;
78+
ipfs = Launcher.getContext().getIpfs();
79+
bitSwapService = BitSwapService.getInstance();
80+
wantHashColor = ColorCnst.MAIN_COLOR_DARKER;
81+
initComponents();
82+
initView();
83+
setListeners();
84+
}
85+
86+
/**
87+
* [initComponents description]
88+
*
89+
* @return {[type]} [description]
90+
*/
91+
private void initComponents() {
92+
93+
// int maxWidth = (int)(MainFrame.getContext().currentWindowWidth*0.375);
94+
// textArea = new SizeAutoAdjustTextArea(maxWidth);
95+
wantListPanel = new JPanel();
96+
middlePanel = new JPanel();
97+
timelabel = new LCJlabel();
98+
99+
}
100+
101+
/**
102+
* [initView description]
103+
*
104+
* @return {[type]} [description]
105+
*/
106+
private void initView() {
107+
this.setLayout(new BorderLayout());
108+
109+
middlePanel.setLayout(new FlowLayout(FlowLayout.RIGHT,5,0));
110+
statusLabel.setForeground(ColorCnst.FONT_ABOUT_TITLE_BLUE);
111+
statusLabel.setFont(FontUtil.getDefaultFont(16));
112+
statusLabel.setHorizontalAlignment(JLabel.LEFT);
113+
114+
timelabel = new LCJlabel(wantHashColor);
115+
timelabel.setHorizontalAlignment(JLabel.LEFT);
116+
middlePanel.add(statusLabel);
117+
middlePanel.add(timelabel);
118+
119+
120+
// textArea.setEditable(false);
121+
wantListPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP,10,0,true,false));
122+
123+
this.add(middlePanel,BorderLayout.NORTH);
124+
this.add(wantListPanel,BorderLayout.CENTER);
125+
126+
127+
this.setVisible(false);
128+
}
129+
130+
private void setListeners() {
131+
/* MouseListener[] listeners = textArea.getMouseListeners();
132+
for(MouseListener mouseListener : listeners){
133+
textArea.removeMouseListener(mouseListener);
134+
}*/
135+
136+
}
137+
138+
/**
139+
* [getContext description]
140+
*
141+
* @return {[type]} [description]
142+
*/
143+
public static MMMonitPanel getContext() {
144+
return context;
145+
}
146+
147+
public static void setCtrlSign(boolean ctrlSign) {
148+
CtrlSign = ctrlSign;
149+
}
150+
151+
/**
152+
* 启动浏览
153+
* @param rhash
154+
* @param stat
155+
*/
156+
public void startMonitor(String rhash,BlockStat stat){
157+
this.rhash = rhash;
158+
this.fsize = stat.getCumulativeSize();
159+
Multihash multihash = Multihash.fromBase58(stat.getHash());
160+
statusLabel.setText("浏览器打开,正在加载数据...");
161+
162+
new Thread(()->{
163+
while (completeSize.get()<fsize){
164+
seconds += seconds;
165+
try {
166+
TimeUnit.SECONDS.sleep(1);
167+
timelabel.setText(seconds+"s");
168+
} catch (InterruptedException e) {
169+
e.printStackTrace();
170+
}
171+
}
172+
}).start();
173+
174+
175+
blkmap.clear();
176+
//start
177+
new Thread(()->{
178+
generateHash(multihash);
179+
}).start();
180+
181+
boosterPined();
182+
monitorList();
183+
this.setVisible(true);
184+
}
185+
186+
187+
private void generateHash(Multihash multihash){
188+
try {
189+
MerkleNode node = ipfs.object.links(multihash);
190+
if(node!=null){
191+
if(node.links==null||node.links.size()==0){
192+
pinedBooster.put(node.hash);
193+
}else {
194+
for(MerkleNode n : node.links){
195+
generateHash(n.hash);
196+
}
197+
}
198+
}
199+
} catch (IOException e) {
200+
e.printStackTrace();
201+
} catch (InterruptedException e) {
202+
e.printStackTrace();
203+
}
204+
}
205+
206+
/**
207+
*
208+
*/
209+
private void boosterPined(){
210+
new Thread(()->{
211+
while (completeSize.get()<fsize){
212+
try {
213+
Multihash multihash = pinedBooster.take();
214+
215+
Object o =ipfs.object.stat(multihash);
216+
String json = JSONParser.toString(o);
217+
BlockStat stat = JSON.parseObject(json,BlockStat.class);
218+
blkmap.put(multihash.toBase58(),stat.getCumulativeSize());
219+
long nl = completeSize.longValue()+stat.getCumulativeSize();
220+
completeSize.addAndGet(nl);
221+
ipfs.pin.add(multihash);
222+
Map blkMap = ipfs.block.stat(multihash);
223+
logger.info("{}>>block :{}",multihash.toBase58(),JSON.toJSON(blkMap));
224+
} catch (InterruptedException e) {
225+
e.printStackTrace();
226+
} catch (IOException e) {
227+
e.printStackTrace();
228+
}
229+
}
230+
}).start();
231+
}
232+
233+
private void monitorList(){
234+
new Thread(()->{
235+
while (completeSize.get()<fsize){
236+
ResData<BitSwap> resData = bitSwapService.getBitSwapStat();
237+
if(resData.getCode()==0){
238+
BitSwap bitSwap = resData.getData();
239+
if(bitSwap!=null&&bitSwap.getWantlist().size()>0){
240+
wantListPanel.removeAll();
241+
for(String hash58 : bitSwap.getWantlist()){
242+
wantListPanel.add(new LCJlabel(hash58,wantHashColor){
243+
@Override
244+
public void setToolTipText(String text) {
245+
super.setToolTipText(hash58);
246+
}
247+
@Override
248+
public synchronized void addMouseListener(MouseListener l) {
249+
MouseAdapter adapter = new MouseAdapter() {
250+
@Override
251+
public void mouseEntered(MouseEvent e) {
252+
e.getComponent().setCursor(MainFrame.handCursor);
253+
}
254+
};
255+
super.addMouseListener(adapter);
256+
}
257+
});
258+
wantListPanel.updateUI();
259+
}
260+
261+
try {
262+
TimeUnit.SECONDS.sleep(2);
263+
} catch (InterruptedException e) {
264+
e.printStackTrace();
265+
}
266+
}
267+
}
268+
}
269+
}).start();
270+
271+
}
272+
}

0 commit comments

Comments
 (0)