JSON Encoding for PeerMaps

This commit is contained in:
Jimmy Zelinskie 2014-08-04 07:05:13 -04:00
parent 9e45f77efe
commit 01fa778ce2

View file

@ -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) {