Files
@ cd2ba192bf12
Branch filter:
Location: Morevna/src/client.py
cd2ba192bf12
3.0 KiB
text/x-python
ssl connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | import collections
import socket
import ssl
import logging as log
from datetime import datetime
import config as conf
import stats
from util import Progress
from hashtree import HashTree
from networkers import NetworkReader,NetworkWriter
class Connection:
def __init__(self,sslContext):
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket=sslContext.wrap_socket(sock)
self._socket.connect((conf.hosts[0], conf.port))
fr=self._socket.makefile(mode="rb")
fw=self._socket.makefile(mode="wb")
self.incoming=NetworkReader(fr)
self.outcoming=NetworkWriter(fw)
def __enter__(self):
return self.incoming,self.outcoming
def __exit__(self, exc_type, exc_val, exc_tb):
self._socket.shutdown(socket.SHUT_RDWR)
self._socket.close()
class Client:
def __init__(self,filename):
self._filename=filename
self._ssl=ssl.create_default_context(cafile=conf.peers)
self._ssl.check_hostname=False
self._ssl.load_cert_chain(conf.certfile,conf.keyfile)
def negotiate(self):
print(datetime.now(), "initializing...")
localTree=HashTree.fromFile(self._filename)
blocksToTransfer=[]
nodeStack=collections.deque([0]) # root
# initialize session
with Connection(self._ssl) as (incoming,outcoming):
jsonData={"command":"init", "blockSize":localTree.BLOCK_SIZE, "blockCount":localTree.leafCount, "version":conf.version}
outcoming.writeMsg(jsonData)
jsonData,binData=incoming.readMsg()
assert jsonData["command"]=="ack"
# determine which blocks to send
print(datetime.now(), "negotiating:")
progress=Progress(localTree.leafCount)
while len(nodeStack)>0:
i=nodeStack.pop()
outcoming.writeMsg({"command":"req", "index":i, "dataType":"hash"})
jsonData,binData=incoming.readMsg()
assert jsonData["index"]==i
assert jsonData["dataType"]=="hash"
stats.logExchangedNode()
if localTree.store[i]!=binData:
if 2*i+3<len(localTree.store): # inner node
nodeStack.append(2*i+2)
nodeStack.append(2*i+1)
else:
blocksToTransfer.append(i-localTree.leafStart) # leaf
progress.p(i-localTree.leafStart)
progress.done()
return blocksToTransfer
def sendData(self,blocksToTransfer):
log.info(blocksToTransfer)
dataFile=open(self._filename, mode="rb")
i1=-1
print(datetime.now(), "sending data:")
with Connection(self._ssl) as (incoming,outcoming):
progress=Progress(len(blocksToTransfer))
for (k,i2) in enumerate(blocksToTransfer):
jsonData={"command":"send", "index":i2, "dataType":"data"}
if i1+1!=i2:
dataFile.seek(i2*HashTree.BLOCK_SIZE)
binData=dataFile.read(HashTree.BLOCK_SIZE)
log.info("block #{0}: {1}...{2}".format(i2,binData[:5],binData[-5:]))
outcoming.writeMsg(jsonData,binData)
stats.logTransferredBlock()
jsonData,binData=incoming.readMsg()
assert jsonData["command"]=="ack" and jsonData["index"]==i2, jsonData
i1=i2
progress.p(k)
progress.done()
with Connection(self._ssl) as (incoming,outcoming):
outcoming.writeMsg({"command":"end"})
log.info("closing session...")
dataFile.close()
|