Skip to content

Commit b420cac

Browse files
committed
add MemoryCard.kt
1 parent 5ed4fc5 commit b420cac

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed

wechaty-puppet/src/main/kotlin/Puppet.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.github.wechaty.io.github.wechaty.watchdag.WatchDog
1212
import io.github.wechaty.io.github.wechaty.watchdag.WatchdogFood
1313
import io.github.wechaty.io.github.wechaty.watchdag.WatchdogListener
1414
import io.github.wechaty.listener.*
15+
import io.github.wechaty.memorycard.MemoryCard
1516
import io.github.wechaty.schemas.*
1617
import io.github.wechaty.utils.FutureUtils
1718
import io.github.wechaty.utils.JsonUtils
@@ -41,6 +42,7 @@ abstract class Puppet : EventEmitter {
4142
private val HEARTBEAT_COUNTER = AtomicLong()
4243
private val HOSTIE_KEEPALIVE_TIMEOUT = 15 * 1000L
4344
private val DEFAULT_WATCHDOG_TIMEOUT = 60L
45+
private var memory: MemoryCard
4446

4547
private val executorService = Executors.newSingleThreadScheduledExecutor()
4648

@@ -65,6 +67,10 @@ abstract class Puppet : EventEmitter {
6567
count.addAndGet(1)
6668
this.puppetOptions = puppetOptions
6769

70+
this.memory = MemoryCard()
71+
this.memory.load()
72+
73+
6874
val timeOut = puppetOptions.timeout ?: DEFAULT_WATCHDOG_TIMEOUT
6975
watchDog = WatchDog(1000 * timeOut, "puppet")
7076

@@ -900,6 +906,10 @@ abstract class Puppet : EventEmitter {
900906
}
901907
}
902908

909+
fun setMemory(memoryCard: MemoryCard){
910+
this.memory = memoryCard
911+
}
912+
903913
// fun getEventBus(): EventBus {
904914
// return eb
905915
// }

wechaty-puppet/src/main/kotlin/io/github/wechaty/memorycard/MemoryCard.kt

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,63 @@ import java.util.concurrent.Future
55

66
class MemoryCard{
77

8-
public var name:String? = null
8+
private var name:String? = null
99
protected var parent:MemoryCard? = null
1010
protected var payload:MemoryCardPayload ? = null
1111
protected var storage:StorageBackend? = null
1212
protected val multiplexNameList = mutableListOf<String>()
1313

1414
private var options:MemoryCardOptions
1515

16-
constructor(name: String?,options:MemoryCardOptions){
16+
constructor(name: String?=null,options:MemoryCardOptions? = null){
17+
18+
val _optiones:MemoryCardOptions = options ?: MemoryCardOptions()
19+
1720
if(name != null){
18-
options.name = name
21+
if(options != null) {
22+
_optiones.name = name
23+
}
1924
}
20-
this.options = options
21-
this.name = options.name
25+
this.options = _optiones
26+
this.name = _optiones.name
2227

23-
(options.multiplex != null).let {
24-
this.parent = options.multiplex!!.parent
28+
(_optiones.multiplex != null).let {
29+
this.parent = _optiones.multiplex!!.parent
2530
this.payload = this.parent!!.payload
2631
this.multiplexNameList.addAll(parent!!.multiplexNameList)
27-
this.multiplexNameList.add(options.multiplex!!.name)
32+
this.multiplexNameList.add(_optiones.multiplex!!.name)
2833
this.storage = null
2934
}
3035

31-
(options.multiplex == null).let {
36+
(_optiones.multiplex == null).let {
3237
this.payload = null
3338
this.multiplexNameList.clear()
34-
35-
3639
}
3740
}
3841

3942
private fun getStore():StorageBackend?{
4043
log.debug("getStorage() for storage type: %s'",this.options)
41-
TODO()
44+
45+
return StorageBackend.getStorage(
46+
this.options.name!!,
47+
this.options.storageOptions
48+
)
49+
}
50+
51+
fun load(){
52+
this.payload = this.storage!!.load()
53+
}
54+
55+
fun save(){
56+
this.storage!!.save(this.payload!!)
4257
}
4358

4459
companion object{
4560
private val log = LoggerFactory.getLogger(MemoryCard::class.java)
61+
62+
fun multiplex(memory:MemoryCard,name:String):MemoryCard{
63+
return MemoryCard()
64+
}
4665
}
4766

4867

wechaty-puppet/src/main/kotlin/io/github/wechaty/memorycard/StorageOptions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ class StorageNopOptions: StorageBackendOptions() {
2424
}
2525

2626
typealias StorageFileOptions = StorageNopOptions
27+
28+
val BACKEND_DICT = mapOf(
29+
"file" to StorageFile::class
30+
)

wechaty-puppet/src/main/kotlin/io/github/wechaty/memorycard/StrorageBackend.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.github.wechaty.memorycard
22

33
import org.slf4j.LoggerFactory
4+
import kotlin.reflect.full.createInstance
5+
import kotlin.reflect.full.primaryConstructor
46

57
abstract class StorageBackend(name: String, option: StorageBackendOptions) {
68

@@ -14,6 +16,27 @@ abstract class StorageBackend(name: String, option: StorageBackendOptions) {
1416

1517
companion object{
1618
private val log = LoggerFactory.getLogger(StorageBackend::class.java)
19+
20+
fun getStorage(name: String,options: StorageBackendOptions?):StorageBackend{
21+
22+
var _options = options
23+
24+
if(options == null) {
25+
_options = StorageFileOptions()
26+
_options.type = "file"
27+
}
28+
29+
if(_options?.type == null || _options.type !in BACKEND_DICT.keys){
30+
throw Exception("backed unknown : ${_options?.type}")
31+
}
32+
33+
val kClass = BACKEND_DICT[_options.type]
34+
val createInstance = kClass?.primaryConstructor
35+
return createInstance?.call(name,_options)!!
36+
37+
38+
}
39+
1740
}
1841

1942
}

wechaty/src/main/kotlin/io/github/wechaty/Wechaty.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.github.wechaty.eventEmitter.EventEmitter
66
import io.github.wechaty.eventEmitter.Listener
77
import io.github.wechaty.grpc.GrpcPuppet
88
import io.github.wechaty.listener.*
9+
import io.github.wechaty.memorycard.MemoryCard
910
import io.github.wechaty.schemas.*
1011
import io.github.wechaty.user.*
1112
import io.github.wechaty.user.manager.*
@@ -29,12 +30,18 @@ class Wechaty private constructor(private var wechatyOptions: WechatyOptions) :
2930
@Volatile
3031
private var status = StateEnum.OFF
3132

33+
private var memory:MemoryCard? = null
34+
3235
val tagManager: TagManager = TagManager(this)
3336
val contactManager = ContactManager(this)
3437
val messageManager = MessageManager(this)
3538
val roomManager = RoomManager(this)
3639
val roomInvitationMessage = RoomInvitationManager(this)
3740

41+
init {
42+
this.memory = wechatyOptions.memory
43+
}
44+
3845

3946
fun start(await: Boolean = false) {
4047

wechaty/src/main/kotlin/io/github/wechaty/WechatyOptions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package io.github.wechaty
22

3+
import io.github.wechaty.memorycard.MemoryCard
34
import io.github.wechaty.schemas.PuppetOptions
45

56
class WechatyOptions {
67

8+
var memory:MemoryCard? = null
9+
710
var name:String = "Wechaty"
811

912
var puppet:String = "wechaty-puppet-hostie"

0 commit comments

Comments
 (0)