Skip to content

Commit fbf5fb9

Browse files
committed
增加启动登录广播
1 parent 9c5592a commit fbf5fb9

File tree

3 files changed

+146
-21
lines changed

3 files changed

+146
-21
lines changed

src/main/java/UI/AppMainWindow.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private void initialize(){
168168
aboutPanel = new AboutPanel();
169169
settingPanel = new SettingPanel(true);
170170
imPanel = new IMPanel(true);
171+
imPanel.receiverRun(200);
171172
filePanel = new FilePanel(true);
172173

173174
//TODO other panel init
@@ -424,4 +425,20 @@ public void run() {
424425
}
425426
}).start();
426427
}
428+
429+
/**
430+
* 在缓存中查找
431+
* @param from
432+
* @return
433+
*/
434+
public static ContactsItem findContactsItemByFrom(String from){
435+
ContactsItem item = null;
436+
for(Map.Entry<String,ContactsItem> entry : peerItems.entrySet()){
437+
item = entry.getValue();
438+
if(item.getFormid().equals(from)){
439+
return item;
440+
}
441+
}
442+
return null;
443+
}
427444
}

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

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
import UI.common.NBSAbstractPanel;
77
import UI.common.ToolbarStatsPanel;
88
import UI.templete.WihteBackJPanel;
9+
import com.alibaba.fastjson.JSON;
910
import com.nbs.entity.ContactsItem;
1011
import com.nbs.entity.PeerInfoBase;
12+
import com.nbs.ipfs.IPFSHelper;
13+
import com.nbs.ipfs.entity.IpfsMessage;
1114
import com.nbs.tools.DateHelper;
1215
import com.nbs.ui.components.ColorCnst;
1316
import com.nbs.utils.Base64CodecUtil;
1417
import io.ipfs.api.IPFS;
18+
import io.ipfs.api.JSONParser;
1519
import org.apache.commons.lang3.StringUtils;
1620

1721
import javax.swing.*;
1822
import java.awt.*;
1923
import java.awt.event.ActionEvent;
2024
import java.io.File;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
2130

2231
/**
2332
* @Package : UI.panel.im
@@ -245,7 +254,7 @@ private void sendMsg(){
245254
sb.append(ConstantsUI.WSPACE_CHARACTER4).append(sendContent);
246255

247256
//send pub
248-
AppMainWindow.ipfs.pubsub.pub(topic,Base64CodecUtil.encodeIPFSMsg(sendContent));
257+
AppMainWindow.ipfs.pubsub.pub(topic,Base64CodecUtil.encodeByCtrlType(sendContent,Base64CodecUtil.CtrlTypes.normal));
249258
inputArea.setText("");
250259
sb.append(ConstantsUI.ENTER_CHARACTER);
251260
imMSGShow.append(sb.toString());
@@ -259,11 +268,61 @@ private void sendMsg(){
259268

260269
/**
261270
*
262-
* @param content
263-
* @param item
271+
* @param
272+
* @param
264273
*/
265-
public void recvMsg(String content,ContactsItem item){
274+
public void receiverRun(long sleepSec){
275+
new Thread(new Runnable() {
276+
@Override
277+
public void run() {
278+
try {
279+
if(sleepSec>=1){
280+
TimeUnit.MILLISECONDS.sleep(sleepSec);
281+
}
282+
if(AppMainWindow.self==null){
283+
TimeUnit.SECONDS.sleep(30);
284+
receiverRun(sleepSec);
285+
return;
286+
}
287+
Stream<Map<String,Object>> sub = IPFSHelper.getInstance().getIpfs().pubsub.sub(AppMainWindow.self.getId());
288+
289+
List<Map> lst = sub.limit(1).collect(Collectors.toList());
290+
String json = JSONParser.toString(lst.get(0));
291+
logger.info(System.currentTimeMillis()+"-revc : "+json);
292+
appenRevcMsg(json);
293+
receiverRun(10);
294+
} catch (Exception e) {
295+
e.printStackTrace();
296+
try {
297+
TimeUnit.SECONDS.sleep(2);
298+
} catch (InterruptedException e1) {
299+
e1.printStackTrace();
300+
}
301+
receiverRun(2000);
302+
}
303+
}
304+
}).start();
305+
}
266306

307+
/**
308+
*
309+
* @param json
310+
*/
311+
private void appenRevcMsg(String json){
312+
IpfsMessage message = JSON.parseObject(json,IpfsMessage.class);
313+
ContactsItem item = AppMainWindow.findContactsItemByFrom(message.getFrom());
314+
String nick = (item==null||item.getName()==null) ? message.getFrom() : item.getName();
315+
message = Base64CodecUtil.parseIpmsMessageCtrlType(message);
316+
if(message==null||message.getTypes()!=Base64CodecUtil.CtrlTypes.normal){
317+
logger.info("message is null");
318+
return;
319+
}
320+
String msg = message.getContents();
321+
StringBuffer sb = new StringBuffer();
322+
sb.append(nick).append(" ").append(DateHelper.currentTime()).append(ConstantsUI.ENTER_CHARACTER);
323+
sb.append(ConstantsUI.WSPACE_CHARACTER4).append(msg).append(ConstantsUI.ENTER_CHARACTER);
324+
imMSGShow.append(sb.toString());
325+
//TODO save SQLite
267326
}
268327

269328
public static void setCurrentToPeer(ContactsItem item){

src/main/java/com/nbs/utils/Base64CodecUtil.java

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
import io.ipfs.multihash.Multihash;
66
import org.apache.commons.codec.CharEncoding;
77
import org.apache.commons.codec.binary.Base64;
8-
import org.apache.commons.codec.binary.StringUtils;
8+
import org.apache.commons.lang3.StringUtils;
99

1010
import java.io.UnsupportedEncodingException;
11-
import java.sql.Struct;
1211
import java.util.regex.Matcher;
1312
import java.util.regex.Pattern;
1413

@@ -22,10 +21,10 @@
2221
*/
2322
public class Base64CodecUtil {
2423
private static final Base64 base64 = new Base64();
25-
public static final String REGX_BASE64_MSG = "^\\$BASE64\\|\\w+\\|$";
24+
public static final String REGX_BASE64_MSG = "^\\$BASE64\\$\\w+\\$$";
2625

27-
public static final String BASE64_START = "$BASE64|";
28-
public static final String BASE64_END = "|";
26+
public static final String BASE64_START = "$BASE64$";
27+
public static final String BASE64_END = "$";
2928
/**
3029
*
3130
* @param origin
@@ -111,21 +110,70 @@ public static String encodeCtrlMsg(Object o,CtrlTypes type){
111110
return mSb.toString();
112111
}
113112

113+
/**
114+
*
115+
* @param message
116+
* @param types
117+
* @return
118+
*/
119+
public static String encodeByCtrlType(Object message,CtrlTypes types){
120+
if(message==null)return null;
121+
StringBuffer sb = new StringBuffer();
122+
if(types==null)types = CtrlTypes.normal;
123+
switch (types){
124+
case online:
125+
sb.append(types.sperator).append(types.starter).append(types.sperator);
126+
sb.append(encode(JSON.toJSONString(message)));
127+
sb.append(types.sperator+"");
128+
return sb.toString();
129+
case normal:
130+
sb.append(types.sperator).append(types.starter).append(types.sperator);
131+
sb.append(encode(message.toString()));
132+
sb.append(types.sperator+"");
133+
return sb.toString();
134+
default:
135+
sb.append(types.sperator).append(types.starter).append(types.sperator);
136+
sb.append(encode(message.toString()));
137+
sb.append(types.sperator+"");
138+
return sb.toString();
139+
}
140+
}
141+
142+
/**
143+
*
144+
* @param m
145+
* @return
146+
*/
114147
public static IpfsMessage parseIpmsMessageCtrlType(IpfsMessage m){
115148
if(m==null||m.getData()==null||m.getData().length()<1)return null;
116149
String decode64 = decode(m.getData());
117-
if(decode64
118-
.startsWith(CtrlTypes.online.sperator+CtrlTypes.online.starter+CtrlTypes.online.sperator)
119-
&& decode64.endsWith(CtrlTypes.online.sperator+"")){
120-
int len = decode64.length();
121-
m.setContents(decode(decode64.substring(CtrlTypes.online.starter.length()+2,len-2)));
122-
m.setTypes(CtrlTypes.online);
123-
return m;
124-
}else {
125-
m.setContents(decode64);
126-
m.setTypes(CtrlTypes.normal);
127-
return m;
150+
CtrlTypes t = null;
151+
int len = decode64.length();
152+
for(CtrlTypes types : CtrlTypes.values()){
153+
if(decode64
154+
.startsWith(types.sperator + types.starter + types.sperator)
155+
&& decode64.endsWith(types.sperator+"")){
156+
t = types;
157+
m.setContents(decode64.substring(types.starter.length()+2,len-2));
158+
m.setTypes(t);
159+
return m;
160+
}
128161
}
162+
if(t==null){
163+
//无效消息
164+
return null;
165+
}
166+
//处理content
167+
switch (t){
168+
case online:
169+
//json base64
170+
m.setContents(decode(m.getContents()));
171+
break;
172+
case normal:
173+
break;
174+
}
175+
176+
return m;
129177
}
130178

131179

@@ -156,8 +204,9 @@ public static enum CtrlTypes{
156204
online("ON.B64.J",'$'),
157205
/**
158206
* 正常消息
207+
* $IM.B64.S$xxx$
159208
*/
160-
normal("",'$');
209+
normal("IM.B64.S",'$');
161210

162211
private String starter;
163212
private char sperator;

0 commit comments

Comments
 (0)