Skip to content

Commit f13b7c5

Browse files
authored
code refactoring (#133)
Signed-off-by: Artem Zatsarynnyi <[email protected]>
1 parent 5a241a9 commit f13b7c5

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

src/main/kotlin/com/redhat/devtools/gateway/DevSpacesConnection.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ package com.redhat.devtools.gateway
1313

1414
import com.redhat.devtools.gateway.openshift.DevWorkspaces
1515
import com.redhat.devtools.gateway.openshift.Pods
16-
import com.redhat.devtools.gateway.server.RemoteServer
16+
import com.redhat.devtools.gateway.server.RemoteIDEServer
1717
import com.jetbrains.gateway.thinClientLink.LinkedClientManager
1818
import com.jetbrains.gateway.thinClientLink.ThinClientHandle
1919
import com.jetbrains.rd.util.lifetime.Lifetime
@@ -50,25 +50,25 @@ class DevSpacesConnection(private val devSpacesContext: DevSpacesContext) {
5050
): ThinClientHandle {
5151
startAndWaitDevWorkspace()
5252

53-
val remoteServer = RemoteServer(devSpacesContext).also { it.waitProjectsReady() }
54-
val projectStatus = remoteServer.getProjectStatus()
53+
val remoteIdeServer = RemoteIDEServer(devSpacesContext).also { it.waitServerReady() }
54+
val remoteIdeServerStatus = remoteIdeServer.getStatus()
5555

5656
val client = LinkedClientManager
5757
.getInstance()
5858
.startNewClient(
5959
Lifetime.Eternal,
60-
URI(projectStatus.joinLink),
60+
URI(remoteIdeServerStatus.joinLink),
6161
"",
6262
onConnected,
6363
false
6464
)
6565

66-
val forwarder = Pods(devSpacesContext.client).forward(remoteServer.pod, 5990, 5990)
66+
val forwarder = Pods(devSpacesContext.client).forward(remoteIdeServer.pod, 5990, 5990)
6767

6868
client.run {
6969
lifetime.onTermination { forwarder.close() }
7070
lifetime.onTermination {
71-
if (remoteServer.waitProjectsTerminated())
71+
if (remoteIdeServer.waitServerTerminated())
7272
DevWorkspaces(devSpacesContext.client)
7373
.stop(
7474
devSpacesContext.devWorkspace.metadata.namespace,

src/main/kotlin/com/redhat/devtools/gateway/server/RemoteServer.kt renamed to src/main/kotlin/com/redhat/devtools/gateway/server/RemoteIDEServer.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import java.util.concurrent.Executors
2525
import java.util.concurrent.TimeUnit
2626
import java.util.concurrent.atomic.AtomicBoolean
2727

28-
29-
class RemoteServer(private val devSpacesContext: DevSpacesContext) {
28+
/**
29+
* Represent an IDE server running in a CDE.
30+
*/
31+
class RemoteIDEServer(private val devSpacesContext: DevSpacesContext) {
3032
var pod: V1Pod
3133
private var container: V1Container
3234
private var readyTimeout: Long = 60
@@ -37,7 +39,10 @@ class RemoteServer(private val devSpacesContext: DevSpacesContext) {
3739
container = findContainer()
3840
}
3941

40-
fun getProjectStatus(): ProjectStatus {
42+
/**
43+
* Asks the CDE for the remote IDE server status.
44+
*/
45+
fun getStatus(): RemoteIDEServerStatus {
4146
Pods(devSpacesContext.client)
4247
.exec(
4348
pod,
@@ -56,44 +61,44 @@ class RemoteServer(private val devSpacesContext: DevSpacesContext) {
5661
)
5762
.trim()
5863
.also {
59-
return if (Strings.isNullOrEmpty(it)) ProjectStatus.empty()
60-
else Gson().fromJson(it, ProjectStatus::class.java)
64+
return if (Strings.isNullOrEmpty(it)) RemoteIDEServerStatus.empty()
65+
else Gson().fromJson(it, RemoteIDEServerStatus::class.java)
6166
}
6267
}
6368

6469
@Throws(IOException::class)
65-
fun waitProjectsReady() {
66-
doWaitProjectsState(true, readyTimeout)
70+
fun waitServerReady() {
71+
doWaitServerState(true, readyTimeout)
6772
.also {
6873
if (!it) throw IOException(
6974
String.format(
70-
"Projects are not ready after %d seconds.",
75+
"Remote IDE server is not ready after %d seconds.",
7176
readyTimeout
7277
)
7378
)
7479
}
7580
}
7681

7782
@Throws(IOException::class)
78-
fun waitProjectsTerminated(): Boolean {
79-
return doWaitProjectsState(false, terminationTimeout)
83+
fun waitServerTerminated(): Boolean {
84+
return doWaitServerState(false, terminationTimeout)
8085
}
8186

8287
@Throws(IOException::class)
83-
fun doWaitProjectsState(isReadyState: Boolean, timeout: Long): Boolean {
88+
fun doWaitServerState(isReadyState: Boolean, timeout: Long): Boolean {
8489
val projectsInDesiredState = AtomicBoolean()
8590
val executor = Executors.newSingleThreadScheduledExecutor()
8691
executor.scheduleAtFixedRate(
8792
{
8893
try {
89-
getProjectStatus().also {
94+
getStatus().also {
9095
if (isReadyState == !Arrays.isNullOrEmpty(it.projects)) {
9196
projectsInDesiredState.set(true)
9297
executor.shutdown()
9398
}
9499
}
95100
} catch (e: Exception) {
96-
thisLogger().debug("Failed to check project status", e)
101+
thisLogger().debug("Failed to check remote IDE server state.", e)
97102
}
98103
}, 0, 5, TimeUnit.SECONDS
99104
)

src/main/kotlin/com/redhat/devtools/gateway/server/ProjectStatus.kt renamed to src/main/kotlin/com/redhat/devtools/gateway/server/RemoteIDEServerStatus.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
package com.redhat.devtools.gateway.server
1414

15-
data class ProjectStatus(
15+
data class RemoteIDEServerStatus(
1616
val joinLink: String,
1717
val httpLink: String,
1818
val gatewayLink: String,
@@ -21,15 +21,15 @@ data class ProjectStatus(
2121
val projects: Array<ProjectInfo>
2222
) {
2323
companion object {
24-
fun empty(): ProjectStatus {
25-
return ProjectStatus("", "", "", "", "", emptyArray())
24+
fun empty(): RemoteIDEServerStatus {
25+
return RemoteIDEServerStatus("", "", "", "", "", emptyArray())
2626
}
2727
}
2828
override fun equals(other: Any?): Boolean {
2929
if (this === other) return true
3030
if (javaClass != other?.javaClass) return false
3131

32-
other as ProjectStatus
32+
other as RemoteIDEServerStatus
3333

3434
if (joinLink != other.joinLink) return false
3535
if (httpLink != other.httpLink) return false

0 commit comments

Comments
 (0)