github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/p2p/timedsync.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package p2p
    18  
    19  //import "fmt"
    20  //import "net"
    21  import "sync/atomic"
    22  import "time"
    23  
    24  //import "container/list"
    25  import "github.com/romana/rlog"
    26  import "github.com/vmihailenco/msgpack"
    27  
    28  //import "github.com/deroproject/derosuite/crypto"
    29  //import "github.com/deroproject/derosuite/globals"
    30  
    31  // reads our data, length prefix blocks
    32  func (connection *Connection) Send_TimedSync(request bool) {
    33  
    34  	var sync Sync_Struct
    35  
    36  	fill_common(&sync.Common) // fill common info
    37  	sync.Command = V2_COMMAND_SYNC
    38  	sync.Request = request
    39  
    40  	serialized, err := msgpack.Marshal(&sync) // serialize and send
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	if request { // queue command that we are expecting a response
    45  		//connection.Lock()
    46  		connection.request_time.Store(time.Now())
    47  		//connection.Unlock()
    48  	}
    49  	//rlog.Tracef(2, "Timed sync sent successfully %s", connection.logid)
    50  	connection.Send_Message(serialized)
    51  
    52  }
    53  
    54  // handles  incoming timed syncs
    55  func (connection *Connection) Handle_TimedSync(buf []byte) {
    56  	var sync Sync_Struct
    57  	err := msgpack.Unmarshal(buf, &sync)
    58  	if err != nil {
    59  		rlog.Warnf("Error while decoding incoming chain request err %s %s", err, connection.logid)
    60  		connection.Exit()
    61  		return
    62  	}
    63  	//rlog.Tracef(2, "Timed sync received %s", connection.logid)
    64  	if sync.Request {
    65  		connection.Send_TimedSync(false) // send it as response
    66  	} else { // this is a response for our request track latency
    67  		//connection.Lock()
    68  		atomic.StoreInt64(&connection.Latency, int64(time.Now().Sub(connection.request_time.Load().(time.Time))/2)) // divide by 2 is for round-trip
    69  		//connection.Unlock()
    70  
    71  	}
    72  }