This is an automated email from the ASF dual-hosted git repository.

aradzinski pushed a commit to branch NLPCRAFT-108
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git


The following commit(s) were added to refs/heads/NLPCRAFT-108 by this push:
     new 3727d36  Update NCCli.scala
3727d36 is described below

commit 3727d367d7eb306c97e75d8f24902b29f9f283b8
Author: Aaron Radzinski <[email protected]>
AuthorDate: Sat Oct 3 01:06:38 2020 -0700

    Update NCCli.scala
---
 .../nlpcraft/model/tools/cmdline/NCCli.scala       | 54 ++++++++++++++++------
 1 file changed, 40 insertions(+), 14 deletions(-)

diff --git 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
index 93641fa..f657518 100644
--- 
a/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
+++ 
b/nlpcraft/src/main/scala/org/apache/nlpcraft/model/tools/cmdline/NCCli.scala
@@ -83,6 +83,13 @@ object NCCli extends App {
 
     private val gson = new GsonBuilder().setPrettyPrinting().create
 
+    case class State(
+        var isServer: Boolean,
+        var accessToken: Option[String]
+    )
+
+    private val state = State(isServer = false, None)
+
     // Single CLI command.
     case class Command(
         name: String,
@@ -595,23 +602,24 @@ object NCCli extends App {
     }
 
     /**
-     * Loads server beacon file and return its data and its corresponding 
process handle.
+     * Loads and returns server beacon file.
      *
      * @return
      */
-    private def loadServerBeacon(): Option[NCCliServerBeacon] =
-        try {
-            val rawObj = managed(
-                new ObjectInputStream(
-                    new FileInputStream(
-                        new File(SystemUtils.getUserHome, SRV_BEACON_PATH)
+    private def loadServerBeacon(): Option[NCCliServerBeacon] = {
+        val beacon = try {
+            val beacon = (
+                managed(
+                    new ObjectInputStream(
+                        new FileInputStream(
+                            new File(SystemUtils.getUserHome, SRV_BEACON_PATH)
+                        )
                     )
-                )
-            ) acquireAndGet {
-                _.readObject()
-            }
-
-            val beacon = rawObj.asInstanceOf[NCCliServerBeacon]
+                ) acquireAndGet {
+                    _.readObject()
+                }
+            )
+            .asInstanceOf[NCCliServerBeacon]
 
             ProcessHandle.of(beacon.pid).asScala match {
                 case Some(ph) ⇒
@@ -628,6 +636,12 @@ object NCCli extends App {
         catch {
             case _: Exception ⇒ None
         }
+
+        state.isServer = beacon.isDefined
+
+        beacon
+    }
+
     /**
      * @param cmd Command descriptor.
      * @param args Arguments, if any, for this command.
@@ -930,10 +944,20 @@ object NCCli extends App {
 
         var exit = false
 
+        val pinger = U.mkThread("repl-server-pinger") { _ ⇒
+            loadServerBeacon()
+
+            Thread.sleep(10.secs)
+        }
+
+        pinger.start()
+
         while (!exit) {
+            val prompt = if (state.isServer) s"${y("\u26a1 ")}${g("\u25b6")} " 
else s"${g("\u25b6")} "
+
             val rawLine =
                 try
-                    reader.readLine(s"${g("\u25b6")} ")
+                    reader.readLine(prompt)
                 catch {
                     case _: PatternSyntaxException ⇒ "" // Guard against JLine 
hiccups.
                     case _: UserInterruptException ⇒ "" // Ignore.
@@ -962,6 +986,8 @@ object NCCli extends App {
             }
         }
 
+        U.stopThread(pinger)
+
         // Save command history.
         ignoring(classOf[IOException]) {
             reader.getHistory.save()

Reply via email to