func (gv *GoLog) LogLocalEvent(Message string) bool { //Converting Vector Clock from Bytes and Updating the gv clock vc, err := vclock.FromBytes(gv.currentVC) if err != nil { panic(err) } currenttime, found := vc.FindTicks(gv.pid) if found == false { panic("Couldnt find this process's id in its own vector clock!") } currenttime++ vc.Update(gv.pid, currenttime) gv.currentVC = vc.Bytes() var ok bool if gv.logging == true { ok = gv.LogThis(Message, gv.pid, vc.ReturnVCString()) } return ok }
func (gv *GoLog) UnpackReceive(mesg string, buf []byte) []byte { /* This function is meant to be called immediatly after receiving a packet. It unpacks the data by the program, the vector clock. It updates vector clock and logs it. and returns the user data */ //if only our program is logging there is nothing attached in the byte buffer if gv.VConWire == false { //simple adding to current time vc, err := vclock.FromBytes(gv.currentVC) if err != nil { panic(err) } currenttime, found := vc.FindTicks(gv.pid) if found == false { panic("Couldnt find own process in local clock!") } currenttime++ vc.Update(gv.pid, currenttime) gv.currentVC = vc.Bytes() if gv.debugmode == true { fmt.Println("A Message was Recieved") fmt.Print("New Clock : ") vc.PrintVC() } //logging local var ok1 bool if gv.logging == true { ok1 = gv.LogThis(mesg, gv.pid, vc.ReturnVCString()) } if ok1 == false { fmt.Println("Something went Wrong, Could not Log!") } return buf } //if we are receiving a packet with a time stamp then: //Create a new buffer holder and decode into E, a new Data Struct buffer := new(bytes.Buffer) buffer = bytes.NewBuffer(buf) e := new(Data) //Decode Relevant Data... if fail it means that this doesnt not hold vector clock (probably) dec := gob.NewDecoder(buffer) err := dec.Decode(e) if err != nil { fmt.Println("You said that I would be receiving a vector clock but I didnt! or decoding failed :P") panic(err) } //In this case you increment your old clock vc, err := vclock.FromBytes(gv.currentVC) if gv.debugmode == true { fmt.Println("Received :") e.PrintDataString() fmt.Print("Received Vector Clock : ") vc.PrintVC() } currenttime, found := vc.FindTicks(gv.pid) if found == false { fmt.Println("Couldnt find Local Process's ID in the Vector Clock. Could it be a stray message?") return nil } if err != nil { panic(err) } currenttime++ vc.Update(gv.pid, currenttime) //merge it with the new clock and update GV tmp := []byte(e.vcinbytes[:]) tempvc, err := vclock.FromBytes(tmp) if err != nil { panic(err) } vc.Merge(tempvc) if gv.debugmode == true { fmt.Print("Now, Vector Clock is : ") vc.PrintVC() } gv.currentVC = vc.Bytes() //Log it var ok bool if gv.logging == true { ok = gv.LogThis(mesg, gv.pid, vc.ReturnVCString()) } if ok == false { fmt.Println("Something went Wrong, Could not Log!") } // Out put the recieved Data tmp2 := []byte(e.programdata[:]) return tmp2 }
func (gv *GoLog) PrepareSend(mesg string, buf []byte) []byte { /* This function is meant to be called before sending a packet. Usually, it should Update the Vector Clock for its own process, package with the clock using gob support and return the new byte array that should be sent onwards using the Send Command */ //Converting Vector Clock from Bytes and Updating the gv clock vc, err := vclock.FromBytes(gv.currentVC) if err != nil { panic(err) } currenttime, found := vc.FindTicks(gv.pid) if found == false { panic("Couldnt find this process's id in its own vector clock!") } currenttime++ vc.Update(gv.pid, currenttime) gv.currentVC = vc.Bytes() //WILL HAVE TO CHECK THIS OUT!!!!!!! if gv.debugmode == true { fmt.Println("Sending Message") fmt.Print("Current Vector Clock : ") vc.PrintVC() } var ok bool if gv.logging == true { ok = gv.LogThis(mesg, gv.pid, vc.ReturnVCString()) } if ok == false { fmt.Println("Something went Wrong, Could not Log!") } //if only local logging the next is unnecceary since we can simply return buf as is if gv.VConWire == true { //if true, then we add relevant info and encode it // Create New Data Structure and add information: data to be transfered d := Data{} d.pid = []byte(gv.pid) d.vcinbytes = gv.currentVC d.programdata = buf //create a buffer to hold data and Encode it buffer := new(bytes.Buffer) enc := gob.NewEncoder(buffer) err := enc.Encode(&d) if err != nil { panic(err) } //return buffer bytes which are encoded as gob. Now these bytes can be sent off and // received on the other end! return buffer.Bytes() } // if we are only performing local logging, we have updated vector clock and logged it buffer can // be returned as is return buf }