-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
executable file
·121 lines (92 loc) · 3.64 KB
/
Server.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3 -u
# -*- mode:python; coding:utf-8; tab-width:4 -*-
import sys
import os
import binascii
import Ice
import IceStorm
Ice.loadSlice('downloader.ice')
import Downloader
from work_queue import WorkQueue
class SchedulerFactoryI(Downloader.SchedulerFactory):
def __init__(self, work_queue):
self.work_queue = work_queue
def make(self, nombre, current = None):
serverid = Ice.stringToIdentity(nombre)
dl = DownloadSchedulerI(self.work_queue)
proxy = current.adapter.add(dl,serverid)
return Downloader.DownloadSchedulerPrx.checkedCast(proxy)
def kill(self, nombre, current = None):
serverid = Ice.stringToIdentity(nombre)
current.adapter.remove(serverid)
class DownloadSchedulerI(Downloader.DownloadScheduler):
def __init__(self, work_queue):
self.work_queue = work_queue
self.download_path = os.getcwd() + '/Descargas'
if not os.path.exists(self.download_path):
os.makedirs(self.download_path)
def getSongList(self, current=None):
return [arch.name for arch in os.scandir(os.getcwd() + '/Descargas') if arch.is_file()]
def addDownloadTask(self, url, current=None):
f = Ice.Future()
self.work_queue.add(f, url)
return f
def get(self, song, current = None):
print("[INFO] Nueva solicitud de copia del servidor recibida (", song, ")")
song = './Descargas/' + song
try:
servant = TransferI(song)
proxy = current.adapter.addWithUUID(servant)
return Downloader.TransferPrx.checkedCast(proxy)
except FileNotFoundError as e:
print("[ERROR] La canción escrita no existe en el servidor.")
class TransferI(Downloader.Transfer):
'''
Transfer file
'''
def __init__(self, local_filename):
self.file_contents = open(local_filename, 'rb')
def recv(self, size, current=None):
'''Send data block to client'''
return str(binascii.b2a_base64(self.file_contents.read(size), newline=False))
def end(self, current=None):
'''Close transfer and free objects'''
self.file_contents.close()
current.adapter.remove(current.id)
print("\033[1;36m")
print("[INFO] Solicitud de copia del servidor completada.")
print("\033[1;0m")
print()
class Server(Ice.Application):
def get_topic_manager(self):
key = 'IceStorm.TopicManager.Proxy'
proxy = self.communicator().propertyToProxy(key)
if proxy is None:
print("property {0} not set".format(key))
return None
print("Using IceStorm in: {0}".format(key))
return IceStorm.TopicManagerPrx.checkedCast(proxy)
def run(self, argv):
topic_mgr = self.get_topic_manager()
if not topic_mgr:
print(': Invalid proxy')
return 2
topic_name = "ProgressTopic"
try:
topic = topic_mgr.retrieve(topic_name)
except IceStorm.NoSuchTopic:
topic = topic_mgr.create(topic_name)
publisher = topic.getPublisher()
progress_topic = Downloader.ProgressEventPrx.uncheckedCast(publisher)
work_queue = WorkQueue(progress_topic)
servant = SchedulerFactoryI(work_queue)
broker = self.communicator()
adapter = broker.createObjectAdapter('DlAdapter')
print("[ADAPTER]", adapter.add(servant, broker.stringToIdentity("Downloader1")))
adapter.activate()
work_queue.start()
self.shutdownOnInterrupt()
broker.waitForShutdown()
work_queue.destroy()
return 0
sys.exit(Server().main(sys.argv))