Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
移除配置文件里的高性能模式,并将永久开启
Browse files Browse the repository at this point in the history
  • Loading branch information
asforest committed Aug 29, 2022
1 parent 626e136 commit 088720a
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 239 deletions.
1 change: 0 additions & 1 deletion config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
address: 0.0.0.0
port: 8000
performance-mode: false
jks-certificate-file:
jks-certificate-pass:

Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ data class AppConfig(
val configYaml: HashMap<String, Any>,
val host: String?,
val port: Int,
val performanceMode: Boolean,
val certificateFile: String,
val certificatePass: String,
)
41 changes: 24 additions & 17 deletions src/main/kotlin/FileDiff.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@

class FileDiff(val current: SimpleFileDir.SimpleDirectory, val contrast: File2, val base: File2, val hashCache: HashCache)
/**
* 文件差异对比类
*
* @param current 要进行对比的目录
* @param contrast 对比时使用的对照目录
* @param base 基准目录,用来计算相对路径
* @param hashCacher hash缓存对象
*/
class FileDiff(val current: VirtualFile, val contrast: File2, val base: File2, val hashCacher: HashCacher)
{
private val differences: Difference = Difference()

Expand All @@ -8,29 +15,29 @@ class FileDiff(val current: SimpleFileDir.SimpleDirectory, val contrast: File2,
* @param contrast 要拿来进行对比的目录
* @param base 基准目录,用于计算相对路径
*/
private fun findNews(dir: SimpleFileDir.SimpleDirectory, contrast: File2, base: File2) {
private fun findNews(dir: VirtualFile, contrast: File2, base: File2) {
for (c in contrast.files)
{
val corresponding = dir.getFile(c.name) // 此文件可能不存在

if(corresponding == null) // 如果文件不存在的话,就不用校验了,可以直接进行下载
{
markAsNew(SimpleFileDir.fromRealFile(c), c)
markAsNew(VirtualFile.fromRealFile(c), c)
continue
}

// 文件存在的话要进行进一步判断
if(c.isDirectory) // 远程文件是一个目录
{
if(corresponding is SimpleFileDir.SimpleFile) // 本地文件和远程文件的文件类型对不上
if(corresponding.isFile) // 本地文件和远程文件的文件类型对不上
{
markAsOld(corresponding, contrast.relativizedBy(base))
markAsNew(corresponding, c)
} else { // 本地文件和远程文件都是目录,则进行进一步判断
findNews(corresponding as SimpleFileDir.SimpleDirectory, c, base)
findNews(corresponding, c, base)
}
} else if(c.isFile) { // 远程文件是一个文件
if(corresponding is SimpleFileDir.SimpleFile) // 本地文件和远程文件都是文件,则对比校验
if(corresponding.isFile) // 本地文件和远程文件都是文件,则对比校验
{
if (!compareSingleFile(corresponding, c))
{
Expand All @@ -50,15 +57,15 @@ class FileDiff(val current: SimpleFileDir.SimpleDirectory, val contrast: File2,
* @param contrast 要拿来进行对比的目录
* @param base 基准目录,用于计算相对路径
*/
private fun findOlds(dir: SimpleFileDir.SimpleDirectory, contrast: File2, base: File2)
private fun findOlds(dir: VirtualFile, contrast: File2, base: File2)
{
for (f in dir.files)
{
val corresponding = contrast + f.name // 尝试获取对应远程文件

if(corresponding.exists) // 如果远程文件存在
{
if(f is SimpleFileDir.SimpleDirectory && corresponding.isDirectory)
if(f.isDirectory && corresponding.isDirectory)
findOlds(f, corresponding, base)
} else { // 远程文件不存在,就直接删掉好了
markAsOld(f, contrast.relativizedBy(base))
Expand All @@ -69,12 +76,12 @@ class FileDiff(val current: SimpleFileDir.SimpleDirectory, val contrast: File2,
/**
* 对比两个路径相同的文件是否一致
*/
private fun compareSingleFile(a: SimpleFileDir.SimpleFile, b: File2): Boolean
private fun compareSingleFile(a: VirtualFile, b: File2): Boolean
{
if(a.modified == b.modified)
return true

return if(hashCache.getHash(b.relativizedBy(base)) != a.hash) {
return if(hashCacher.getHash(b.relativizedBy(base)) != a.hash) {
false
} else {
b._file.setLastModified(a.modified)
Expand All @@ -85,16 +92,16 @@ class FileDiff(val current: SimpleFileDir.SimpleDirectory, val contrast: File2,
/**
* 将一个文件文件或者目录标记为旧文件
*/
private fun markAsOld(existing: SimpleFileDir, directory: String)
private fun markAsOld(existing: VirtualFile, directory: String)
{
var path = directory + (if (directory.isNotEmpty()) "/" else "") + existing.name
path = if (path.startsWith("./")) path.substring(2) else path

if(existing is SimpleFileDir.SimpleDirectory)
if(existing.isDirectory)
{
for (f in existing.files)
{
if(f is SimpleFileDir.SimpleDirectory)
if(f.isDirectory)
markAsOld(f, path)
else {
var path = path + "/" + f.name
Expand All @@ -113,14 +120,14 @@ class FileDiff(val current: SimpleFileDir.SimpleDirectory, val contrast: File2,
/**
* 将一个文件文件或者目录标记为新文件
*/
private fun markAsNew(missing: SimpleFileDir, contrast: File2)
private fun markAsNew(missing: VirtualFile, contrast: File2)
{
if(missing is SimpleFileDir.SimpleDirectory)
if(missing.isDirectory)
{
differences.newFolders += contrast.relativizedBy(base)
for (n in missing.files)
markAsNew(n, contrast + n.name)
} else if (missing is SimpleFileDir.SimpleFile){
} else if (missing.isFile){
differences.newFiles += contrast.relativizedBy(base)
}
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/kotlin/HashCache.kt

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/kotlin/HashCacher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 用于缓存文件生成的hash值的一个Map,已经生成的hash可以直接获取到,不用重复生成了,可以提升效率
*
* @param base 基本目录,用来配合relativePath来定位具体文件
*/
class HashCacher(val base: File2)
{
private val cache = mutableMapOf<String, String>()

fun getHash(relativePath: String): String
{
if (relativePath !in cache)
{
val file = base + relativePath
cache[relativePath] = file.sha1
}

return cache[relativePath]!!
}
}
24 changes: 2 additions & 22 deletions src/main/kotlin/LittleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class LittleServer
configYaml = configYaml,
host = configYaml["address"]?.run { this as String } ?: "0.0.0.0",
port = configYaml["port"]?.run { this as Int } ?: 8850,
performanceMode = configYaml["performance-mode"]?.run { this as Boolean } ?: false,
certificateFile = configYaml["jks-certificate-file"]?.run { this as String } ?: "",
certificatePass = configYaml["jks-certificate-pass"]?.run { this as String } ?: "",
)
Expand Down Expand Up @@ -86,12 +85,6 @@ class LittleServer
println("SSL证书已加载")
}

if (config.performanceMode)
{
println("高性能模式已经开启,正在生成资源目录缓存...")
server.regenDirStructureInfoCache()
}

server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false)

val host = config.host
Expand All @@ -101,14 +94,8 @@ class LittleServer
println("API地址: http://$host:$port/index.json")

println()
if (config.performanceMode)
println("使用提示1:修改更新规则需要退出程序之后修改,修改res目录下的文件之后请重启或者使用reload指令来重新加载")
else
println("使用提示1:修改更新规则需要退出程序之后修改,修改res目录下的文件会实时生效,不用退出程序")
println("使用提示2:显示的所有报错信息都不用管,直接忽略就好!")
println("使用提示3:可用指令:")
println(" stop或者s:退出程序")
println(" reload或者r:重新生成资源目录缓存(仅开启高性能时有效)")
println("使用提示1:显示的所有报错信息都不用管,直接忽略就好!")
println("使用提示2:可以使用之类stop或者s来退出程序")
} catch (e: BindException) {
println("端口监听失败,可能是端口冲突,原因: ${e.message}")
exitProcess(1)
Expand All @@ -120,14 +107,7 @@ class LittleServer
{
val line = scanner.nextLine()
if (line == "stop" || line == "s")
{
exitProcess(1)
} else if (line == "reload" || line == "r") {
print("正在重新生成资源目录缓存...")
server.regenDirStructureInfoCache()
println(" 完毕")
}

}
}
}
Expand Down
Loading

0 comments on commit 088720a

Please sign in to comment.