66import UI .panel .im .IMPanel ;
77import UI .panel .setting .SettingPanel ;
88import com .alibaba .fastjson .JSON ;
9+ import com .nbs .entity .ContactsItem ;
910import com .nbs .entity .PeerBoradcastInfo ;
1011import com .nbs .ipfs .IPFSHelper ;
12+ import com .nbs .ipfs .entity .IpfsMessage ;
1113import com .nbs .tools .ConfigHelper ;
1214import com .nbs .utils .Base64CodecUtil ;
1315import io .ipfs .api .IPFS ;
16+ import io .ipfs .api .JSONParser ;
1417import io .ipfs .api .MerkleNode ;
1518import io .ipfs .api .NamedStreamable ;
19+ import io .ipfs .multihash .Multihash ;
1620import org .apache .commons .lang3 .StringUtils ;
1721import org .slf4j .Logger ;
1822import org .slf4j .LoggerFactory ;
1923
2024import javax .swing .*;
2125import java .awt .*;
2226import java .io .File ;
27+ import java .io .FileOutputStream ;
2328import java .io .IOException ;
29+ import java .util .HashMap ;
30+ import java .util .List ;
2431import java .util .Map ;
2532import java .util .Properties ;
33+ import java .util .concurrent .TimeUnit ;
34+ import java .util .stream .Collectors ;
35+ import java .util .stream .Stream ;
2636
2737/**
2838 * @Package : UI
@@ -43,6 +53,8 @@ public class AppMainWindow {
4353 */
4454 private static final Logger logger = LoggerFactory .getLogger (AppMainWindow .class );
4555
56+ private static boolean boradcastSuccess = false ;
57+
4658 public static JFrame frame ;
4759 /**
4860 * 主窗口
@@ -73,16 +85,8 @@ public class AppMainWindow {
7385 */
7486 public static IPFS ipfs = IPFSHelper .getInstance ().getIpfs ();
7587
76-
7788 public static String PROFILE_NICKNAME = "" ;
7889
79- /**
80- *
81- */
82- public static String NBS_FILES_ROOT_PATH ;
83-
84- public static String NBS_FILES_IPFS_ROOT ;
85-
8690 /**
8791 *
8892 */
@@ -91,6 +95,10 @@ public class AppMainWindow {
9195 * 关于面板
9296 */
9397 public static AboutPanel aboutPanel ;
98+ /**
99+ * 联系人缓存
100+ */
101+ public static Map <String ,ContactsItem > peerItems = new HashMap <>();
94102
95103 public static void main (String [] args ){
96104
@@ -123,6 +131,7 @@ public void run() {
123131 public AppMainWindow (){
124132 initialize ();
125133 broadcastOnline ();
134+ subSelf (400 );
126135 }
127136 /**
128137 * 初始化frame内容
@@ -195,9 +204,11 @@ private void loadEnv(){
195204 logger .info (k +"=" +v );
196205 }
197206 logger .info ("ENV ============================<<" );
198- NBS_FILES_ROOT_PATH = ConfigHelper .CURRENT_DIR +File .separator + ConfigHelper .getNbsFilesRoot ()+File .separator ;
199- NBS_FILES_IPFS_ROOT = NBS_FILES_ROOT_PATH +"ipfs" + File .separator ;
200- File ipfsDir = new File (NBS_FILES_IPFS_ROOT );
207+ /**
208+ *
209+ */
210+
211+ File ipfsDir = new File (ConfigHelper .NBS_FILES_IPFS_ROOT );
201212 if (ipfsDir .isDirectory ()&& !ipfsDir .exists ()){
202213 ipfsDir .mkdirs ();
203214 }
@@ -286,18 +297,131 @@ public void initAvatar(){
286297 }
287298
288299 /**
289- *
300+ * 上线广播
290301 */
291302 private void broadcastOnline (){
292- if (self ==null )return ;
293- String ctrlMsg = Base64CodecUtil .encodeCtrlMsg (self ,Base64CodecUtil .CtrlTypes .online );
303+ if (boradcastSuccess )return ;
304+ new Thread (new Runnable () {
305+ @ Override
306+ public void run () {
307+ try {
308+ if (self ==null ||self .getNick ()==null ){
309+ TimeUnit .SECONDS .sleep (30 );
310+ broadcastOnline ();
311+ }else {
312+ String ctrlMsg = Base64CodecUtil .encodeCtrlMsg (self ,Base64CodecUtil .CtrlTypes .online );
313+ logger .info ("Send CTRL MSG : " +ctrlMsg );
314+ ipfs .pubsub .pub (IPFSHelper .NBSWORLD_CTRL_TOPIC ,ctrlMsg );
315+ logger .info (IPFSHelper .NBSWORLD_CTRL_TOPIC +"Send CTRL MSG : " +ctrlMsg );
316+ boradcastSuccess = true ;
317+ }
318+ } catch (InterruptedException e ) {
319+ broadcastOnline ();
320+ } catch (Exception e ) {
321+ logger .info (e .getMessage ());
322+ broadcastOnline ();
323+ }
324+ }
325+ }).start ();
326+ }
294327
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 ();
328+ /**
329+ * See yourself
330+ */
331+ private void subSelf (long sleepTimes ){
332+ new Thread (new Runnable () {
333+ @ Override
334+ public void run () {
335+ try {
336+ if (StringUtils .isBlank (PEER_ID )){
337+ TimeUnit .SECONDS .sleep (30 );
338+ }
339+ if (sleepTimes >0l ){ TimeUnit .MILLISECONDS .sleep (sleepTimes ); }
340+ logger .info ("SUB SEFL :" +self .getId ());
341+ Stream <Map <String ,Object >> sub = ipfs .pubsub .sub (self .getId ());
342+ List <Map > lst = sub .limit (1 ).collect (Collectors .toList ());
343+ String json = JSONParser .toString (lst .get (0 ));
344+ logger .info (System .currentTimeMillis ()+"-revc : " +json );
345+ IpfsMessage message = JSON .parseObject (json ,IpfsMessage .class );
346+ //TODO 处理
347+ updateContactsCache (message );
348+ subSelf (sleepTimes );
349+ } catch (Exception e ) {
350+ e .printStackTrace ();
351+ logger .info (e .getMessage ());
352+ subSelf (sleepTimes );
353+ }
354+ }
355+ }).start ();
356+ }
357+
358+ public void updateContactsCache (IpfsMessage m ){
359+ if (m ==null )return ;
360+ m = Base64CodecUtil .parseIpmsMessageCtrlType (m );
361+ switch (m .getTypes ()){
362+ case online :
363+
364+ //更新缓存
365+ updatePeerItem (m );
366+ break ;
367+ default :
368+ break ;
369+ }
370+ }
371+
372+ public void updatePeerItem (IpfsMessage m ){
373+ logger .info (m .getContents ());
374+ PeerBoradcastInfo info = JSON .parseObject (m .getContents (),PeerBoradcastInfo .class );
375+ ContactsItem item = null ;
376+ if (info .getId ().equals (self .getId ())){
377+ //自己不处理
378+ return ;
379+ }
380+ if (peerItems .containsKey (info .getId ())){
381+ item = peerItems .get (info .getId ());
382+ item .setFormid (m .getFrom ());
383+ if (info .getAvatarHash ()!=null )item .setAvatar (info .getAvatarHash ());
384+ if (info .getAvatarSuffix ()!=null )item .setAvatarSuffix (info .getAvatarSuffix ());
385+ item .setName (info .getNick ());
386+ }else {
387+ item = new ContactsItem (info .getId (),info .getNick (),m .getFrom ());
388+ item .setAvatar (info .getAvatarHash ());
389+ item .setAvatarSuffix (info .getAvatarSuffix ());
390+ peerItems .put (info .getId (),item );
301391 }
392+ safeAndFreshIM (item );
393+ }
394+
395+ /**
396+ * 启用新线程下载头像,并存储
397+ * @param item
398+ */
399+ private void safeAndFreshIM (ContactsItem item ){
400+ //TODO 通知
401+ new Thread (new Runnable () {
402+ @ Override
403+ public void run () {
404+ //TODO save sqlite
405+ String headRootPath = ConfigHelper .NBS_CACHE_AVATAR_ROOT_PATH ;
406+ try {
407+ if (StringUtils .isNotBlank (item .getAvatar ())){
408+ FileOutputStream fos ;
409+ File headImage = new File (headRootPath ,item .getAvatar ()+item .getAvatarSuffix ());
410+ if (headImage .exists ()){
411+ headImage .delete ();
412+ }
413+ headImage .createNewFile ();
414+ Multihash multihash = Multihash .fromBase58 (item .getAvatar ());
415+ byte [] bytes = ipfs .get (multihash );
416+ fos = new FileOutputStream (headImage );
417+ fos .write (bytes );
418+ fos .flush ();
419+ fos .close ();
420+ }
421+ } catch (IOException e ) {
422+ e .printStackTrace ();
423+ }
424+ }
425+ }).start ();
302426 }
303427}
0 commit comments