ソースを参照

ON PAUSE FOR TYPESCRIPT

Patrick Glavin 6 年 前
コミット
363c514a3b
共有2 個のファイルを変更した57 個の追加2 個の削除を含む
  1. 53
    0
      Client/src/main/java/MessageScraper.java
  2. 4
    2
      Client/src/main/java/SimpleShell.java

+ 53
- 0
Client/src/main/java/MessageScraper.java ファイルの表示

@@ -0,0 +1,53 @@
1
+import com.fasterxml.jackson.databind.ObjectMapper;
2
+
3
+import java.io.IOException;
4
+import java.util.ArrayList;
5
+
6
+public class MessageScraper implements Runnable{
7
+    private ArrayList<Message> messageList;
8
+    private User user;
9
+    private YouAreEll webber;
10
+
11
+    public MessageScraper(User user, YouAreEll webber) {
12
+        this.user = user;
13
+        this.webber = webber;
14
+    }
15
+
16
+    public void run() {
17
+        this.messageList = new ArrayList<Message>();
18
+        ObjectMapper objectMapper = new ObjectMapper();
19
+        Message[] incoming = new Message[0];
20
+        String scrape;
21
+        while (true){
22
+            waitN(4000);
23
+            scrape = webber.get_messages(user);
24
+            waitN(100);
25
+            try {
26
+                incoming = objectMapper.readValue(scrape, Message[].class);
27
+            } catch (IOException e) {
28
+                e.printStackTrace();
29
+            }
30
+            for (Message message:incoming) {
31
+                if (newMessageCheck(message)){
32
+                    System.out.println("New Message!\n" + message.getFromid() + " says: " + message.getMessage());
33
+                    this.messageList.add(message);
34
+                }
35
+            }
36
+        }
37
+    }
38
+
39
+    private void waitN(int i) {
40
+        try {
41
+            Thread.sleep(i);
42
+        } catch (InterruptedException e) {
43
+            e.printStackTrace();
44
+        }
45
+    }
46
+
47
+    private boolean newMessageCheck(Message newMessage){
48
+        for (Message message:this.messageList) {
49
+            if (message.getSequence().equals(newMessage.getSequence())) return false;
50
+        }
51
+        return true;
52
+    }
53
+}

+ 4
- 2
Client/src/main/java/SimpleShell.java ファイルの表示

@@ -68,8 +68,10 @@ public class SimpleShell {
68 68
         String githubID = console.readLine();
69 69
         User user = new User(name, githubID);
70 70
         System.out.println(user.getName() + " " + user.getGithub());
71
-        Thread scrapeThread = new Thread(new DMScraper(user, webber));
72
-        scrapeThread.start();
71
+        Thread dmscrapeThread = new Thread(new DMScraper(user, webber));
72
+        dmscrapeThread.start();
73
+        Thread globalScrapeThread = new Thread(new MessageScraper(user, webber));
74
+        globalScrapeThread.start();
73 75
         //we break out with <ctrl c>
74 76
         while (true) {
75 77
             //read what the user enters