Skip to content

Commit

Permalink
refactor: lastwill-message rename to history-message
Browse files Browse the repository at this point in the history
  • Loading branch information
asforest committed Oct 31, 2022
1 parent 8c2508c commit cc23f07
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ MShell的消息合并机制是依赖2个参数运行的,一个是合并时间

如果你的程序在运行过程中有比较频繁的输出,那么请适当调大改这些选项的值

### 3.遗愿消息
### 3.历史消息

在断开与会话的连接期间,会话输出的最新一部分会被保留,并在你重连会会话之后发送给你,以告诉你当你不在的时候,当前会话最后都输出了什么,发生了什么。这部分被保留的消息,就叫遗愿消息
在断开与会话的连接期间,会话输出的最新一部分会被保留,并在你重连会会话之后发送给你,以告诉你当你不在的时候,当前会话最后都输出了什么,发生了什么。这部分被保留的消息,就叫历史消息

当然这个保留区的大小可以使用环境预设指令来配置:

```
/mshell preset lastwill <preset> <capacity-in-chars>: 设置会话的遗愿消息缓冲区大小(单位是字符数)
/mshell preset history <preset> <capacity-in-chars>: 设置会话的历史消息缓冲区大小(单位是字符数)
```

### 4.群聊会话
Expand Down Expand Up @@ -495,8 +495,8 @@ MShell有4个大指令,分别是:
# 设置会话的stdout合并上限,单位:字符数
/ms preset truncation <preset> <threshold-in-chars>

# 设置会话的遗愿消息缓冲区大小,单位是字符数
/ms preset lastwill <preset> <capacity-in-chars>
# 设置会话的历史消息缓冲区大小,单位是字符数
/ms preset history <preset> <capacity-in-chars>
```

### 3.权限管理指令 /ms auth
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/command/PresetCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ object PresetCommand : TreeCommand()
}


@Command(desc = "设置会话的遗愿消息缓冲区大小", permission = Admin)
suspend fun CallingContext.lastwill(preset: String, capacityInChars: Int)
@Command(desc = "设置会话的历史消息缓冲区大小", aliases = ["lastwill"], permission = Admin)
suspend fun CallingContext.history(preset: String, capacityInChars: Int)
{
withCatch {
val _preset = getPresetWithThrow(preset)
_preset.lastwillCapacity = capacityInChars
sendMessage("环境预设 $preset 的遗愿消息缓冲区大小已更新为 $capacityInChars 字符")
_preset.historyCapacity = capacityInChars
sendMessage("环境预设 $preset 的历史消息缓冲区大小已更新为 $capacityInChars 字符")
ep.write()
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/configuration/PresetsConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object PresetsConfig : YamlConfig("presets.yml")
preset.rows = fromMap["terminal-rows"] as Int? ?: preset.rows
preset.truncationThreshold = fromMap["truncation-threshold"] as Int? ?: preset.truncationThreshold
preset.batchingInteval = fromMap["batching-inteval"] as Int? ?: preset.batchingInteval
preset.lastwillCapacity = fromMap["lastwill-capacity"] as Int? ?: preset.lastwillCapacity
preset.historyCapacity = fromMap["history-capacity"] as Int? ?: fromMap["lastwill-capacity"] as Int? ?: preset.historyCapacity
preset.jsonMode = fromMap["json-mode"] as Boolean? ?: preset.jsonMode
preset.ptyMode = fromMap["pty-mode"] as Boolean? ?: preset.ptyMode

Expand All @@ -73,7 +73,7 @@ object PresetsConfig : YamlConfig("presets.yml")
map["terminal-rows"] = preset.rows
map["truncation-threshold"] = preset.truncationThreshold
map["batching-inteval"] = preset.batchingInteval
map["lastwill-capacity"] = preset.lastwillCapacity
map["history-capacity"] = preset.historyCapacity
map["json-mode"] = preset.jsonMode
map["pty-mode"] = preset.ptyMode

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/data/Preset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package com.github.asforest.mshell.data
* @param rows PTY的高度(单位:行)
* @param truncationThreshold 子进程stdout合并字符数上限(单位:字符)
* @param batchingInteval 子进程stdout合并间隔(单位:字符)
* @param lastwillCapacity 遗愿消息缓冲区大小(单位:字符)
* @param historyCapacity 历史消息缓冲区大小(单位:字符)
* @param jsonMode 是否启用JsonMode
* @param ptyMode 是否使用PTY运行会话
* @param silentMode 是否屏蔽群聊会话内的连接和断开等状态消息,其它消息不受影响
Expand All @@ -29,7 +29,7 @@ data class Preset(
var rows: Int = 24,
var truncationThreshold: Int = 2048,
var batchingInteval: Int = 300,
var lastwillCapacity: Int = 2048,
var historyCapacity: Int = 2048,
var jsonMode: Boolean = false,
var ptyMode: Boolean = true,
var silentMode: Boolean = false,
Expand Down Expand Up @@ -57,8 +57,8 @@ data class Preset(
if (batchingInteval != 300)
add("batch= $batchingInteval")

if (lastwillCapacity != 2048)
add("lastwill= $lastwillCapacity")
if (historyCapacity != 2048)
add("history= $historyCapacity")

if (singleInstance)
add("single-instance")
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/session/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.github.asforest.mshell.stream.BatchingWriter
* Connection 代表一个用户与会话之间连接对象
*
* 当用户从会话上断开时,连接对象不会销毁,而是被标记为offline状态,
* 以便当用户重新连接上来时,向用户发送遗愿消息
* 以便当用户重新连接上来时,向用户发送历史消息
*/
class Connection(
val user: SessionUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package com.github.asforest.mshell.session

import kotlin.math.abs

class LastwillMessage(
var capacityInBytes: Int
) {
/**
* 代表一个历史消息保留类
*/
class HistoryMessageReserver(var capacityInBytes: Int)
{
val bytesUsed:Int get() {
var bytesUsed = 0
for (message in lastwillBuffer)
for (message in buffer)
bytesUsed += message.message.length
return bytesUsed
}

val lastwillBuffer = ArrayList<LastwillMessage>(capacityInBytes)
val buffer = ArrayList<HistoryMessage>(capacityInBytes)

fun append(message: String)
{
Expand All @@ -22,46 +24,46 @@ class LastwillMessage(
val capacity = abs(capacityInBytes)

// 单次消息超过大小限制了
lastwillBuffer += if(message.length > capacity) {
buffer += if(message.length > capacity) {
val overflowAllowed = capacityInBytes < 0 // 要么溢出,要么截断
lastwillBuffer.clear()
buffer.clear()

val msg = if(overflowAllowed) message else message.substring(message.length - capacity)
LastwillMessage(System.currentTimeMillis(), msg)
HistoryMessage(System.currentTimeMillis(), msg)
} else {
while (bytesUsed + message.length > capacity)
lastwillBuffer.removeFirst()
buffer.removeFirst()

LastwillMessage(System.currentTimeMillis(), message)
HistoryMessage(System.currentTimeMillis(), message)
}
}

fun hasMessage(since: Long): Boolean
{
return lastwillBuffer.any { it.time > since }
return buffer.any { it.time > since }
}

fun getAllMessage(since: Long): List<String>
{
return lastwillBuffer.filter { it.time > since }.map { it.message }
return buffer.filter { it.time > since }.map { it.message }
}

fun getAllLines(since: Long): List<LastwillMessage>
fun getAllLines(since: Long): List<HistoryMessage>
{
return lastwillBuffer.filter { it.time > since }
return buffer.filter { it.time > since }
}

override fun toString(): String
{
val sb = StringBuffer()

for (message in lastwillBuffer)
for (message in buffer)
sb.append(message)

return sb.toString()
}

data class LastwillMessage(
data class HistoryMessage(
val time: Long,
val message: String
)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/session/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Session(
val process: Process
val stdin: PrintWriter
var pid: Long = -1
val lwm = LastwillMessage(preset.lastwillCapacity)
val lwm = HistoryMessageReserver(preset.historyCapacity)
var isLive = true
val connectionManager = ConnectionManager(this)
val startCommandLine: String
Expand Down Expand Up @@ -171,7 +171,7 @@ class Session(

if (isReconnection)
{
// 发送遗愿消息
// 发送历史消息
if(whenOnlineChanged != -1L && lwm.hasMessage(whenOnlineChanged))
{
var last: Long = 0
Expand Down

0 comments on commit cc23f07

Please sign in to comment.