Commit 54c120cd authored by LUXEY Adrien's avatar LUXEY Adrien
Browse files

removed sequence loading from package

parent f5d82695
package storyteller
import (
"encoding/csv"
"encoding/json"
"fmt"
"gitlab.inria.fr/aluxey/gossip"
"io/ioutil"
"os"
"path"
"strings"
"time"
)
// JSON
// The column names are coupled with Golang's initial_sequence.go
type itJson struct {
// D only contains the IP, not the port: we need to add it
ID string `json:"ID"`
IP string `json:"IP"`
Timestamp string `json:"Timestamp"`
// SessionHash [sha1.Size]byte
}
type sequenceJson []itJson
// CSV
var COLUMNS_NAME []string = []string{"device_name", "device_IP", "timestamp"}
func loadInitialSequence(seqPath string, newDescriber NewDescriberType) (*Sequence, error) {
ext := strings.ToLower(path.Ext(seqPath))
switch ext {
case ".csv":
return loadInitialSequenceCsv(seqPath, newDescriber)
case ".json":
return loadInitialSequenceJson(seqPath, newDescriber)
default:
return nil, fmt.Errorf("Invalid file extension %q for sequence file %v.",
ext, seqPath)
}
}
func loadInitialSequenceJson(seqPath string, newDescriber NewDescriberType) (*Sequence, error) {
bytes, err := ioutil.ReadFile(seqPath)
if err != nil {
return nil, err
}
sequence := NewSequence()
var jsonSeq sequenceJson
if err = json.Unmarshal(bytes, &jsonSeq); err != nil {
return nil, err
}
for _, itJson := range jsonSeq {
var t time.Time
if t, err = time.Parse(gossip.TIME_LAYOUT, itJson.Timestamp); err != nil {
fmt.Fprintf(os.Stderr, "[loadInitialSequence] Failed decoding time %q: %v\n",
itJson.Timestamp, err)
continue
}
sequence.Add(Interaction{
D: newDescriber(itJson.ID, itJson.IP),
Timestamp: t,
})
}
//fmt.Printf("[loadInitialSequence] We successfully extracted the initial sequence:\n%v\n", sequence)
return sequence, nil
}
func loadInitialSequenceCsv(seqPath string, newDescriber NewDescriberType) (*Sequence, error) {
file, err := os.Open(seqPath)
if err != nil {
return nil, err
}
r := csv.NewReader(file)
var header []string
if header, err = r.Read(); err != nil {
return nil, err
}
var headerIndices []int
if headerIndices, err = getHeaderIndices(header, COLUMNS_NAME); err != nil {
return nil, err
}
var rows [][]string
if rows, err = r.ReadAll(); err != nil {
return nil, err
}
seq := NewSequence()
for _, row := range rows {
var timestamp time.Time
if timestamp, err = time.Parse(gossip.TIME_LAYOUT, row[headerIndices[2]]); err != nil {
fmt.Fprintf(os.Stderr, "[loadInitialSequence] Failed decoding time %q: %v\n",
row[headerIndices[2]], err)
continue
}
seq.Add(Interaction{
D: newDescriber(row[headerIndices[0]], row[headerIndices[1]]),
Timestamp: timestamp,
})
}
fmt.Printf("[loadInitialSequence] We successfully extracted the initial sequence:\n%v\n", seq)
return seq, nil
}
func getHeaderIndices(fullHeader, colsToFind []string) ([]int, error) {
ret := make([]int, len(colsToFind))
for i := 0; i < len(colsToFind); i++ {
ret[i] = -1
}
for i, col := range fullHeader {
for j, colToFind := range colsToFind {
if col == colToFind {
ret[j] = i
}
}
}
for i := 0; i < len(colsToFind); i++ {
if ret[i] == -1 {
return nil, fmt.Errorf("Could not find column {} in header {}.",
colsToFind[i], fullHeader)
}
}
return ret, nil
}
......@@ -15,8 +15,6 @@ import (
"time"
)
type NewDescriberType func(IP, ID string) gossip.Describer
const (
ACK_TIMEOUT = 300 // ms
// defined in gossip
......@@ -64,8 +62,8 @@ type StoryTeller struct {
mutex sync.RWMutex
}
func New(myself gossip.Describer,
newDescriber NewDescriberType, tm transport.Manager,
func New(myself gossip.Describer, initialSequence *Sequence,
tm transport.Manager,
fanout int, sequencePath string,
logToCsv, debug bool) (*StoryTeller, error) {
......@@ -75,10 +73,7 @@ func New(myself gossip.Describer,
st.myself = myself
// ---------- Sequences & Descriptor ----------
var err error
if st.initialSequence, err = loadInitialSequence(sequencePath, newDescriber); err != nil {
return nil, err
}
st.initialSequence = initialSequence
st.sequence = st.initialSequence.Copy()
// Initialise the otherSequences map:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment