Skip to content

Commit 159cae3

Browse files
committed
增加启动登录广播
1 parent d992405 commit 159cae3

File tree

12 files changed

+347
-55
lines changed

12 files changed

+347
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ hs_err_pid*
2727
*.impl
2828
*.iml
2929
.idea/
30+
nbs/cache

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
<artifactId>sqlite-jdbc</artifactId>
5858
<version>3.20.1</version>
5959
</dependency>
60+
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
61+
<dependency>
62+
<groupId>commons-codec</groupId>
63+
<artifactId>commons-codec</artifactId>
64+
<version>1.11</version>
65+
</dependency>
6066

6167

6268
</dependencies>

profile/nbs.png

6.68 KB
Loading

src/main/java/UI/AppMainWindow.java

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import UI.panel.about.AboutPanel;
55
import UI.panel.fm.FilePanel;
66
import UI.panel.im.IMPanel;
7-
import UI.panel.monitor.ConsolePanel;
87
import UI.panel.setting.SettingPanel;
98
import com.alibaba.fastjson.JSON;
9+
import com.nbs.entity.PeerBoradcastInfo;
1010
import com.nbs.ipfs.IPFSHelper;
1111
import com.nbs.tools.ConfigHelper;
12+
import com.nbs.utils.Base64CodecUtil;
1213
import io.ipfs.api.IPFS;
14+
import io.ipfs.api.MerkleNode;
15+
import io.ipfs.api.NamedStreamable;
1316
import org.apache.commons.lang3.StringUtils;
1417
import org.slf4j.Logger;
1518
import org.slf4j.LoggerFactory;
@@ -60,6 +63,8 @@ public class AppMainWindow {
6063

6164
public static String PEER_ID = "";
6265

66+
public static PeerBoradcastInfo self = null;
67+
6368
public static boolean SERVER_STAT = false;
6469

6570
protected static IPFSHelper ipfsHelper = IPFSHelper.getInstance();
@@ -117,7 +122,7 @@ public void run() {
117122
*/
118123
public AppMainWindow(){
119124
initialize();
120-
125+
broadcastOnline();
121126
}
122127
/**
123128
* 初始化frame内容
@@ -209,7 +214,10 @@ private void loadEnv(){
209214

210215
if(map.get("ID")!=null)PEER_ID = (String)map.get("ID");
211216
logger.info(">>>>>>>>>>>>>."+map.get("ID"));
212-
initNick();
217+
String n = initNick();
218+
self = new PeerBoradcastInfo((String)map.get("ID"),n);
219+
//avatar must below init nick
220+
initAvatar();
213221
}catch (Exception e){
214222
logger.error("ipfs Server is dead.",e.getCause());
215223
ipfs = new IPFS(ConfigHelper.getIpfsAddress());
@@ -218,26 +226,78 @@ private void loadEnv(){
218226
}
219227

220228

221-
public void initNick(){
222-
if(StringUtils.isNotBlank(PROFILE_NICKNAME))return;
229+
public String initNick(){
230+
if(StringUtils.isNotBlank(PROFILE_NICKNAME))return PROFILE_NICKNAME;
223231
String nick = "";
232+
224233
try {
225234
Map cfgMap = ipfs.config.show();
226-
if(cfgMap.containsKey(IPFSHelper.JSON_NICKNAME_KEY)){
227-
nick = (String)cfgMap.get(IPFSHelper.JSON_NICKNAME_KEY);
235+
if(cfgMap.containsKey(ConfigHelper.JSON_NICKNAME_KEY)){
236+
nick = (String)cfgMap.get(ConfigHelper.JSON_NICKNAME_KEY);
228237
}
229238
} catch (IOException e) {
230239
e.printStackTrace();
231240
nick = ipfsHelper.generateNickName();
232241
try {
233-
ipfs.config.set(IPFSHelper.JSON_NICKNAME_KEY,nick);
242+
ipfs.config.set(ConfigHelper.JSON_NICKNAME_KEY,nick);
234243
String s=JSON.toJSONString(ipfs.config.show(),true);
235244
logger.info(s);
236245
} catch (IOException e1) {
237246
e1.printStackTrace();
238247
}
239248
}
240249
PROFILE_NICKNAME = nick;
250+
return PROFILE_NICKNAME;
251+
}
252+
253+
/**
254+
* 初始化头像
255+
*/
256+
public void initAvatar(){
257+
try {
258+
String avatarHash,suffix,fileName;
259+
Map cfgMap = ipfs.config.show();
260+
if(cfgMap.containsKey(ConfigHelper.JSON_AVATAR_KEY)&&cfgMap.containsKey(ConfigHelper.JSON_AVATAR_SUFFIX_KEY)){
261+
//
262+
}else {
263+
File defaultAvatarImage = new File(ConfigHelper.PROFILE_ROOT+"nbs.png");
264+
if(!defaultAvatarImage.exists()||defaultAvatarImage.isDirectory())return;
265+
fileName = defaultAvatarImage.getName();
266+
suffix = fileName.substring(fileName.lastIndexOf("."));
267+
logger.info(suffix);
268+
NamedStreamable.FileWrapper file = new NamedStreamable.FileWrapper(defaultAvatarImage);
269+
java.util.List<MerkleNode> merkleNodes= ipfs.add(file);
270+
avatarHash = merkleNodes.get(0).hash.toBase58();
271+
272+
//存入config
273+
try{
274+
ipfs.config.set(ConfigHelper.JSON_AVATAR_KEY,avatarHash);
275+
ipfs.config.set(ConfigHelper.JSON_AVATAR_SUFFIX_KEY,suffix);
276+
ipfs.config.set(ConfigHelper.JSON_AVATAR_NAME_KEY,fileName);
277+
}catch (Exception ioe){
278+
logger.error(ioe.getMessage());
279+
}
280+
self.setAvatarHash(avatarHash);
281+
self.setAvatarSuffix(suffix);
282+
}
283+
} catch (IOException e) {
284+
e.printStackTrace();
285+
}
241286
}
242287

288+
/**
289+
*
290+
*/
291+
private void broadcastOnline(){
292+
if(self==null)return;
293+
String ctrlMsg = Base64CodecUtil.encodeCtrlMsg(self,Base64CodecUtil.CtrlTypes.online);
294+
295+
try {
296+
logger.info("Send CTRL MSG : "+ctrlMsg);
297+
ipfs.pubsub.pub(IPFSHelper.NBSWORLD_CTRL_TOPIC,ctrlMsg);
298+
logger.info(IPFSHelper.NBSWORLD_CTRL_TOPIC+"Send CTRL MSG : "+ctrlMsg);
299+
} catch (Exception e) {
300+
e.printStackTrace();
301+
}
302+
}
243303
}

src/main/java/UI/common/ToolbarStatsPanel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
public class ToolbarStatsPanel extends JPanel {
2020

2121
private final JLabel titleLabel = new JLabel();
22-
private final JLabel contactsToLabel = new JLabel();
22+
private final JTextPane contactsToLabel = new JTextPane();
2323
public ToolbarStatsPanel(String key){
2424
titleLabel.setFont(ConstantsUI.FONT_TITLE);
2525
titleLabel.setForeground(ConstantsUI.PANEL_TITILE_COLOR);
2626
contactsToLabel.setFont(ConstantsUI.FONT_LABEL);
2727
contactsToLabel.setForeground(ConstantsUI.PANEL_TITILE_COLOR);
28-
contactsToLabel.setHorizontalAlignment(JLabel.HORIZONTAL);
28+
// contactsToLabel.set
2929
if(StringUtils.isNoneBlank(key)&& null != PropertyUtil.getProperty(key)){
3030
titleLabel.setText(PropertyUtil.getProperty(key));
3131
}

src/main/java/UI/panel/fm/FilePanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void actionPerformed(ActionEvent e) {
185185
private void addFile2Ipfs(File file) {
186186
if(file==null||AppMainWindow.ipfs==null)return;
187187
String name = file.getName();
188-
NamedStreamable namedStreamable;
188+
189189
List<MerkleNode> nodes;
190190
try {
191191
if(file.isDirectory()){

src/main/java/UI/panel/im/IMPanel.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.nbs.entity.PeerInfoBase;
1111
import com.nbs.tools.DateHelper;
1212
import com.nbs.ui.components.ColorCnst;
13+
import com.nbs.utils.Base64CodecUtil;
1314
import io.ipfs.api.IPFS;
1415
import org.apache.commons.lang3.StringUtils;
1516

@@ -133,21 +134,6 @@ public void load() {
133134

134135
}
135136

136-
private void refreshPeers(){
137-
138-
}
139-
140-
/**
141-
* 加载在线节点
142-
* @return
143-
*/
144-
public boolean loadOnlinePeers(){
145-
if(!AppMainWindow.SERVER_STAT)return false;
146-
IPFS ipfs = AppMainWindow.ipfs;
147-
148-
return true;
149-
}
150-
151137
@Override
152138
protected void addListener() {
153139
sendButton.addActionListener(new AbstractAction() {
@@ -158,9 +144,7 @@ public void actionPerformed(ActionEvent e) {
158144
});
159145
}
160146

161-
private void buildLeftPeerContainer(){
162147

163-
}
164148

165149
/**
166150
* 上下结构
@@ -248,22 +232,40 @@ private void testText(int rows){
248232
private void sendMsg(){
249233
String sendContent = inputArea.getText();
250234
if(StringUtils.isBlank(sendContent))return;
235+
if(CURRENT_TO_CONTACTS_ITEM == null){
236+
JOptionPane.showMessageDialog(AppMainWindow.frame,"请选中联系人");
237+
return;
238+
}
239+
String topic = CURRENT_TO_CONTACTS_ITEM.getId();
251240

252241
try {
253242
StringBuffer sb = new StringBuffer();
254243
sb.append(AppMainWindow.PROFILE_NICKNAME).append(" ").append(DateHelper.currentTime());
255244
sb.append(ConstantsUI.ENTER_CHARACTER);
256245
sb.append(ConstantsUI.WSPACE_CHARACTER4).append(sendContent);
246+
257247
//send pub
258-
AppMainWindow.ipfs.pubsub.pub("nbs",sendContent);
248+
AppMainWindow.ipfs.pubsub.pub(topic,Base64CodecUtil.encodeIPFSMsg(sendContent));
259249
inputArea.setText("");
260250
sb.append(ConstantsUI.ENTER_CHARACTER);
261251
imMSGShow.append(sb.toString());
262252
} catch (Exception e) {
263253
e.printStackTrace();
254+
logger.error(e.getMessage());
255+
String error = ConstantsUI.WSPACE_CHARACTER4+"发送失败....."+ConstantsUI.ENTER_CHARACTER;
256+
imMSGShow.append(error);
264257
}
265258
}
266259

260+
/**
261+
*
262+
* @param content
263+
* @param item
264+
*/
265+
public void recvMsg(String content,ContactsItem item){
266+
267+
}
268+
267269
public static void setCurrentToPeer(ContactsItem item){
268270
CURRENT_TO_CONTACTS_ITEM = item;
269271
}
@@ -276,7 +278,6 @@ public void contactsItemChanged(ContactsItem item){
276278
CURRENT_TO_CONTACTS_ITEM = item;
277279
if(item==null)return;
278280
toolbarStatsPanel.resetContacts(item.getName());
279-
280281
}
281282

282283
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.nbs.entity;
2+
3+
/**
4+
* @Package : com.nbs.entity
5+
* @Description : <p></p>
6+
* @Author : lambor.c
7+
* @Date : 2018/6/24-15:30
8+
* Copyright (c) 2018, NBS , lambor.c<[email protected]>.
9+
* All rights reserved.
10+
*/
11+
public class PeerBoradcastInfo {
12+
13+
private String id;
14+
private String nick;
15+
private String avatarHash;
16+
private String avatarSuffix;
17+
18+
public PeerBoradcastInfo(String id, String nick, String avatarHash, String avatarSuffix) {
19+
this.id = id;
20+
this.nick = nick;
21+
this.avatarHash = avatarHash;
22+
this.avatarSuffix = avatarSuffix;
23+
}
24+
25+
public PeerBoradcastInfo(String id, String nick) {
26+
this.id = id;
27+
this.nick = nick;
28+
}
29+
30+
public String getId() {
31+
return id;
32+
}
33+
34+
public void setId(String id) {
35+
this.id = id;
36+
}
37+
38+
public String getNick() {
39+
return nick;
40+
}
41+
42+
public void setNick(String nick) {
43+
this.nick = nick;
44+
}
45+
46+
public String getAvatarHash() {
47+
return avatarHash;
48+
}
49+
50+
public void setAvatarHash(String avatarHash) {
51+
this.avatarHash = avatarHash;
52+
}
53+
54+
public String getAvatarSuffix() {
55+
return avatarSuffix;
56+
}
57+
58+
public void setAvatarSuffix(String avatarSuffix) {
59+
this.avatarSuffix = avatarSuffix;
60+
}
61+
62+
}

src/main/java/com/nbs/ipfs/IPFSHelper.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.nbs.ipfs;
22

33
import com.nbs.tools.ConfigHelper;
4+
import com.nbs.utils.Base64CodecUtil;
45
import io.ipfs.api.IPFS;
56
import org.apache.commons.lang3.StringUtils;
67
import org.slf4j.Logger;
@@ -26,16 +27,15 @@ public class IPFSHelper {
2627
*/
2728
private ConcurrentHashMap secMap = new ConcurrentHashMap();
2829
public static String CLIENT_PEERID;
29-
public static String JSON_NICKNAME_KEY = "nickname";
3030

3131
private IPFS ipfs;
3232

3333
/**
3434
*
3535
*/
36-
private static final String NBSWORLD_CTRL_TOPIC = "$nbs.ctrl|broadcast$";
37-
private static final String NBS_IM_TOPIC_PREFFIX = "$nbs.im|";
38-
private static final String NBS_TOPIC_SEPRATOR = "$";
36+
public static final String NBSWORLD_CTRL_TOPIC = Base64CodecUtil.encode("$nbs.ctrl|broadcast$");
37+
public static final String NBS_IM_TOPIC_PREFFIX = "$nbs.im|";
38+
public static final String NBS_TOPIC_SEPRATOR = "$";
3939

4040
public IPFSHelper() {
4141
ipfs = new IPFS(ConfigHelper.getIpfsAddress());
@@ -92,21 +92,6 @@ private int getRadom(int len){
9292
return (int)Math.round(Math.random()*len);
9393
}
9494

95-
/**
96-
*
97-
* @return
98-
* @throws IOException
99-
*/
100-
public String getNickName() throws IOException {
101-
102-
String nick = ipfs.config.get(JSON_NICKNAME_KEY);
103-
if(StringUtils.isBlank(nick)){
104-
nick = generateNickName();
105-
ipfs.config.set(JSON_NICKNAME_KEY,nick);
106-
}
107-
return nick;
108-
}
109-
11095
/**
11196
*
11297
* @param nick
@@ -116,12 +101,10 @@ public String getNickName() throws IOException {
116101
public String updateNick(String nick) throws IOException {
117102
if(ipfs==null||StringUtils.isBlank(nick))return null;
118103
nick = nick.trim();
119-
ipfs.config.set(JSON_NICKNAME_KEY,nick);
104+
ipfs.config.set(ConfigHelper.JSON_NICKNAME_KEY,nick);
120105
return nick;
121106
}
122107

123108

124-
public void getIMPeers(){
125109

126-
}
127110
}

0 commit comments

Comments
 (0)