partial cleanup of FileRequest.py
This commit is contained in:
parent
39acf04b4b
commit
31b6dc4516
1 changed files with 158 additions and 155 deletions
|
@ -1,5 +1,12 @@
|
||||||
import os, msgpack, shutil, gevent, socket, struct, random
|
# Included modules
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
import os
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
|
# Third party modules
|
||||||
|
import gevent
|
||||||
|
|
||||||
from Debug import Debug
|
from Debug import Debug
|
||||||
from Config import config
|
from Config import config
|
||||||
from util import RateLimit, StreamingMsgpack
|
from util import RateLimit, StreamingMsgpack
|
||||||
|
@ -19,16 +26,13 @@ class FileRequest(object):
|
||||||
self.log = server.log
|
self.log = server.log
|
||||||
self.responded = False # Responded to the request
|
self.responded = False # Responded to the request
|
||||||
|
|
||||||
|
|
||||||
def unpackAddress(self, packed):
|
def unpackAddress(self, packed):
|
||||||
return (socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0])
|
return socket.inet_ntoa(packed[0:4]), struct.unpack_from("H", packed, 4)[0]
|
||||||
|
|
||||||
|
|
||||||
def send(self, msg, streaming=False):
|
def send(self, msg, streaming=False):
|
||||||
if not self.connection.closed:
|
if not self.connection.closed:
|
||||||
self.connection.send(msg, streaming)
|
self.connection.send(msg, streaming)
|
||||||
|
|
||||||
|
|
||||||
def response(self, msg, streaming=False):
|
def response(self, msg, streaming=False):
|
||||||
if self.responded:
|
if self.responded:
|
||||||
self.log.debug("Req id %s already responded" % self.req_id)
|
self.log.debug("Req id %s already responded" % self.req_id)
|
||||||
|
@ -40,7 +44,6 @@ class FileRequest(object):
|
||||||
self.responded = True
|
self.responded = True
|
||||||
self.send(msg, streaming=streaming)
|
self.send(msg, streaming=streaming)
|
||||||
|
|
||||||
|
|
||||||
# Route file requests
|
# Route file requests
|
||||||
def route(self, cmd, req_id, params):
|
def route(self, cmd, req_id, params):
|
||||||
self.req_id = req_id
|
self.req_id = req_id
|
||||||
|
@ -49,9 +52,10 @@ class FileRequest(object):
|
||||||
self.actionGetFile(params)
|
self.actionGetFile(params)
|
||||||
elif cmd == "update":
|
elif cmd == "update":
|
||||||
event = "%s update %s %s" % (self.connection.id, params["site"], params["inner_path"])
|
event = "%s update %s %s" % (self.connection.id, params["site"], params["inner_path"])
|
||||||
if not RateLimit.isAllowed(event): # There was already an updat for this file in the last 10 second
|
if not RateLimit.isAllowed(event): # There was already an update for this file in the last 10 second
|
||||||
self.response({"ok": "File update queued"})
|
self.response({"ok": "File update queued"})
|
||||||
RateLimit.callAsync(event, 10, self.actionUpdate, params) # If called more than once within 10 sec only keep the last update
|
# If called more than once within 10 sec only keep the last update
|
||||||
|
RateLimit.callAsync(event, 10, self.actionUpdate, params)
|
||||||
|
|
||||||
elif cmd == "pex":
|
elif cmd == "pex":
|
||||||
self.actionPex(params)
|
self.actionPex(params)
|
||||||
|
@ -62,7 +66,6 @@ class FileRequest(object):
|
||||||
else:
|
else:
|
||||||
self.actionUnknown(cmd, params)
|
self.actionUnknown(cmd, params)
|
||||||
|
|
||||||
|
|
||||||
# Update a site file request
|
# Update a site file request
|
||||||
def actionUpdate(self, params):
|
def actionUpdate(self, params):
|
||||||
site = self.sites.get(params["site"])
|
site = self.sites.get(params["site"])
|
||||||
|
@ -105,7 +108,6 @@ class FileRequest(object):
|
||||||
self.log.debug("Update for %s is invalid" % params["inner_path"])
|
self.log.debug("Update for %s is invalid" % params["inner_path"])
|
||||||
self.response({"error": "File invalid"})
|
self.response({"error": "File invalid"})
|
||||||
|
|
||||||
|
|
||||||
# Send file content request
|
# Send file content request
|
||||||
def actionGetFile(self, params):
|
def actionGetFile(self, params):
|
||||||
site = self.sites.get(params["site"])
|
site = self.sites.get(params["site"])
|
||||||
|
@ -118,13 +120,17 @@ class FileRequest(object):
|
||||||
with StreamingMsgpack.FilePart(file_path, "rb") as file:
|
with StreamingMsgpack.FilePart(file_path, "rb") as file:
|
||||||
file.seek(params["location"])
|
file.seek(params["location"])
|
||||||
file.read_bytes = FILE_BUFF
|
file.read_bytes = FILE_BUFF
|
||||||
back = {}
|
back = {"body": file,
|
||||||
back["body"] = file
|
"size": os.fstat(file.fileno()).st_size,
|
||||||
back["size"] = os.fstat(file.fileno()).st_size
|
"location": min(file.tell()+FILE_BUFF, os.fstat(file.fileno()).st_size)
|
||||||
back["location"] = min(file.tell()+FILE_BUFF, back["size"])
|
}
|
||||||
if config.debug_socket: self.log.debug("Sending file %s from position %s to %s" % (file_path, params["location"], back["location"]))
|
if config.debug_socket:
|
||||||
|
self.log.debug("Sending file %s from position %s to %s" % (file_path,
|
||||||
|
params["location"],
|
||||||
|
back["location"]))
|
||||||
self.response(back, streaming=True)
|
self.response(back, streaming=True)
|
||||||
if config.debug_socket: self.log.debug("File %s sent" % file_path)
|
if config.debug_socket:
|
||||||
|
self.log.debug("File %s sent" % file_path)
|
||||||
|
|
||||||
# Add peer to site if not added before
|
# Add peer to site if not added before
|
||||||
connected_peer = site.addPeer(self.connection.ip, self.connection.port)
|
connected_peer = site.addPeer(self.connection.ip, self.connection.port)
|
||||||
|
@ -136,7 +142,6 @@ class FileRequest(object):
|
||||||
self.response({"error": "File read error: %s" % Debug.formatException(err)})
|
self.response({"error": "File read error: %s" % Debug.formatException(err)})
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
# Peer exchange request
|
# Peer exchange request
|
||||||
def actionPex(self, params):
|
def actionPex(self, params):
|
||||||
site = self.sites.get(params["site"])
|
site = self.sites.get(params["site"])
|
||||||
|
@ -162,14 +167,15 @@ class FileRequest(object):
|
||||||
self.log.debug("Added %s peers to %s using pex, sending back %s" % (added, site, len(packed_peers)))
|
self.log.debug("Added %s peers to %s using pex, sending back %s" % (added, site, len(packed_peers)))
|
||||||
self.response({"peers": packed_peers})
|
self.response({"peers": packed_peers})
|
||||||
|
|
||||||
|
|
||||||
# Get modified content.json files since
|
# Get modified content.json files since
|
||||||
def actionListModified(self, params):
|
def actionListModified(self, params):
|
||||||
site = self.sites.get(params["site"])
|
site = self.sites.get(params["site"])
|
||||||
if not site or not site.settings["serving"]: # Site unknown or not serving
|
if not site or not site.settings["serving"]: # Site unknown or not serving
|
||||||
self.response({"error": "Unknown site"})
|
self.response({"error": "Unknown site"})
|
||||||
return False
|
return False
|
||||||
modified_files = {inner_path: content["modified"] for inner_path, content in site.content_manager.contents.iteritems() if content["modified"] > params["since"]}
|
modified_files = {inner_path: content["modified"]
|
||||||
|
for inner_path, content in site.content_manager.contents.iteritems()
|
||||||
|
if content["modified"] > params["since"]}
|
||||||
|
|
||||||
# Add peer to site if not added before
|
# Add peer to site if not added before
|
||||||
connected_peer = site.addPeer(self.connection.ip, self.connection.port)
|
connected_peer = site.addPeer(self.connection.ip, self.connection.port)
|
||||||
|
@ -178,13 +184,10 @@ class FileRequest(object):
|
||||||
|
|
||||||
self.response({"modified_files": modified_files})
|
self.response({"modified_files": modified_files})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Send a simple Pong! answer
|
# Send a simple Pong! answer
|
||||||
def actionPing(self):
|
def actionPing(self):
|
||||||
self.response("Pong!")
|
self.response("Pong!")
|
||||||
|
|
||||||
|
|
||||||
# Unknown command
|
# Unknown command
|
||||||
def actionUnknown(self, cmd, params):
|
def actionUnknown(self, cmd, params):
|
||||||
self.response({"error": "Unknown command: %s" % cmd})
|
self.response({"error": "Unknown command: %s" % cmd})
|
||||||
|
|
Loading…
Reference in a new issue