Skip to content

Commit

Permalink
[ #194 ] use Sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
zxj5470 committed Dec 17, 2018
1 parent 45f0f37 commit 4f69791
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
7 changes: 5 additions & 2 deletions res/org/ice1000/julia/lang/action/IntelliJ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ IntelliJ.jl:

if VERSION >= v"0.7.0"
using Pkg
using Sockets
end

println("Initialize julia-intellij REPL environment...")
Expand Down Expand Up @@ -55,8 +56,10 @@ function _intellij_x_to_bytes(x)
end

function _intellij_send_to(rows)
open("$(tempdir())/tempJuliaVarInfo.tsv","w") do f
write(f,json(rows))
if "JULIA_INTELLIJ_DATA_PORT" in keys(ENV)
port = parse(Int, ENV["JULIA_INTELLIJ_DATA_PORT"])
f = connect(getipaddr(), port)
write(f,json(rows) * "\n")
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/org/ice1000/julia/lang/julia-constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.annotations.NonNls
@NonNls const val JULIA_REPL_EMPTY_ACTION_ID = "Julia.Tools.EmptyAction"
@NonNls const val JULIA_SCI_VIEW_ID = "JuliaSciView"
@NonNls const val JULIA_INTELLIJ_PLOT_PORT = "JULIA_INTELLIJ_PLOT_PORT"
@NonNls const val JULIA_INTELLIJ_DATA_PORT = "JULIA_INTELLIJ_DATA_PORT"
@NonNls const val JULIA_PLUGIN_ID = "org.ice1000.julia"
@NonNls @Language("RegExp") const val JULIA_CHAR_SINGLE_UNICODE_X_REGEX = "\\\\x([A-Fa-f0-9]){2}"
@NonNls @Language("RegExp") const val JULIA_CHAR_NOT_UX_REGEX = "\\\\([^uxUX])"
Expand Down Expand Up @@ -61,6 +62,7 @@ Pkg.build("DocumentFormat")"""
@NonNls const val DOCFMT_EXTENSION = "julia-config"
@NonNls const val DOCFMT_LANGUAGE_NAME = "DocumentFormat"
val JULIA_SCI_PORT_KEY = Key<String>("JuliaSciPortKey")
val JULIA_DATA_PORT_KEY = Key<String>("JuliaSciDataPortKey")
val JULIA_SCI_DATA_KEY = Key<JuliaVariablesView>("JuliaVariablesViewKey")
val JULIA_VAR_LIST_KEY = Key<List<JuliaDebugValue>>("JuliaVarListKey")
val JULIA_REPL_RUNNER_KEY = Key<JuliaReplRunner>("JuliaReplRunnerKey")
Expand Down
46 changes: 38 additions & 8 deletions src/org/ice1000/julia/lang/module/julia-components.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ice1000.julia.lang.module

import com.google.gson.JsonParser
import com.intellij.ide.plugins.PluginManager
import com.intellij.notification.*
import com.intellij.openapi.application.ApplicationManager
Expand All @@ -12,16 +13,18 @@ import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.util.PlatformUtils
import org.ice1000.julia.lang.*
import org.jetbrains.annotations.Nls
import java.io.DataInputStream
import java.io.IOException
import java.io.*
import java.net.ServerSocket

class JuliaProjectComponent(private val project: Project) : ProjectComponent {
var isNightlyNotificationShown = false
lateinit var socket: ServerSocket
lateinit var plotSocket: ServerSocket
lateinit var dataSocket: ServerSocket
@Volatile
private var hold = true

private val json = JsonParser()

override fun getComponentName() = "JuliaProjectComponent"
override fun projectOpened() {
super.projectOpened()
Expand All @@ -43,19 +46,46 @@ class JuliaProjectComponent(private val project: Project) : ProjectComponent {
syncJuliaLibrary()
val useSciView = true
if (useSciView) {
socket = ServerSocket(0)
project.putUserData(JULIA_SCI_PORT_KEY, socket.localPort.toString())
plotSocket = ServerSocket(0)
dataSocket = ServerSocket(0)
project.putUserData(JULIA_SCI_PORT_KEY, plotSocket.localPort.toString())
project.putUserData(JULIA_DATA_PORT_KEY, dataSocket.localPort.toString())
ApplicationManager.getApplication().executeOnPooledThread {
while (this.hold) {
waitAndHandlePlots()
}
}
ApplicationManager.getApplication().executeOnPooledThread {
while (this.hold) {
waitAndHandle()
waitAndHandleDataView()
}
}
}
}
}

private fun waitAndHandle() {
val socketAccept = socket.accept()
private fun waitAndHandleDataView() {
while (true) {
val connectionSocket = dataSocket.accept()
val inFromClient = BufferedReader(InputStreamReader(connectionSocket.getInputStream()))
val text = inFromClient.readLine()
val root = json.parse(text).asJsonArray
val list = root.map {
val (name, bytes, value, typeSummary) = it.asJsonArray.map { ele -> ele.asJsonPrimitive.asString }
val container = when {
typeSummary.contains("EnvDict") -> true
typeSummary.contains("Array{") -> true
else -> false
}
JuliaDebugValue(name, typeSummary, value, container)
}
project.putUserData(JULIA_VAR_LIST_KEY, list)
project.getUserData(JULIA_SCI_DATA_KEY)?.rebuildView()
}
}

private fun waitAndHandlePlots() {
val socketAccept = plotSocket.accept()
try {
val inputStream = socketAccept.getInputStream()
val data = DataInputStream(inputStream)
Expand Down

0 comments on commit 4f69791

Please sign in to comment.