JSON Encoding for PeerMaps
This commit is contained in:
parent
9e45f77efe
commit
01fa778ce2
1 changed files with 21 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
|
@ -68,6 +69,26 @@ func (pm *PeerMap) Len() int {
|
|||
return len(pm.peers)
|
||||
}
|
||||
|
||||
func (pm *PeerMap) MarshalJSON() ([]byte, error) {
|
||||
pm.RLock()
|
||||
defer pm.RUnlock()
|
||||
return json.Marshal(pm.peers)
|
||||
}
|
||||
|
||||
func (pm *PeerMap) UnmarshalJSON(b []byte) error {
|
||||
pm.Lock()
|
||||
defer pm.Unlock()
|
||||
|
||||
peers := make(map[PeerKey]Peer)
|
||||
err := json.Unmarshal(b, &peers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pm.peers = peers
|
||||
return nil
|
||||
}
|
||||
|
||||
// Purge iterates over all of the peers within a PeerMap and deletes them if
|
||||
// they are older than the provided time.
|
||||
func (pm *PeerMap) Purge(unixtime int64) {
|
||||
|
|
Loading…
Add table
Reference in a new issue