def from_cache(filepath): """Try to retrieve a file from the cache, the mode isn't specified, it's up to the user to deal with it, otherwise we may have multiple instance of the same file open in different mode. filepath: the path of the file to retrieve from cache. Return None if the file isn't in the cache or if the cache expired. """ if filepath in File._cache: f = File._cache[filepath] host, port = utils.get_host_port(_config['nameserver']) fs = utils.get_server(filepath, host, port) host, port = utils.get_host_port(fs) with closing(HTTPConnection(host, port)) as con: con.request('HEAD', filepath) if (f.last_modified == con.getresponse().getheader( 'Last-Modified')): f.seek(0) return f else: del File._cache[filepath] return None
def from_cache(filepath): """Try to retrieve a file from the cache, the mode isn't specified, it's up to the user to deal with it, otherwise we may have multiple instance of the same file open in different mode. filepath: the path of the file to retrieve from cache. Return None if the file isn't in the cache or if the cache expired. """ if filepath in File._cache: f = File._cache[filepath] host, port = utils.get_host_port(_config['nameserver']) fs = utils.get_server(filepath, host, port) host, port = utils.get_host_port(fs) with closing(HTTPConnection(host, port)) as con: con.request('HEAD', filepath) if (f.last_modified == con.getresponse().getheader('Last-Modified')): f.seek(0) return f else: del File._cache[filepath] return None
def cache(filepath): if filepath in File._cache: filec = File._cache[filepath] #stores filepath that is to be retrieved from cache host, port = utils.get_host_port(_config['nameserver']) fs = utils.get_server(filepath, host, port) host, port = utils.get_host_port(fs) with closing(HTTPConnection(host, port)) as con: con.request('HEAD', filepath) if (f.last_modified ==con.getresponse().getheader('Last-Modified')): return f
def __init__(self, filepath, mode='rtc'): print("FilePath", filepath) print("FilePath", mode) #self.mode = mode #self.filepath = filepath host, port = utils.get_host_port(_config['nameserver']) self.srv = utils.get_server(filepath, host, port) print(self.srv) print("FilePath", filepath) print("FilePath", mode) if self.srv is None: raise DFSIOError('Could not find a server that serves :', filepath) self.last_modified = None SpooledTemporaryFile.__init__( self, _config['max_size'], mode.replace('c', '')) #mode 'c' meaning store in cache host, port = utils.get_host_port(_config['lockserver']) if utils.is_locked(filepath, host, port): raise DFSIOError('The file %s is locked.' % filepath) if 'w' not in mode: print(type(self.srv)) host, port = utils.get_host_port(self.srv.decode('UTF-8')) with closing(HTTPConnection(host, port)) as con: con.request('GET', filepath) response = con.getresponse() self.last_modified = response.getheader('Last-Modified') status = response.status print(status) if status not in (200, 204): raise DFSIOError('Error (%d) while opening file.' % status) if status != 204: self.write(response.read()) if 'r' in mode: self.seek(0) self.lock_id = None if 'a' in mode or 'w' in mode: # automatically gets a lock if we're in write/append mode host, port = utils.get_host_port(_config['lockserver']) self.lock_id = int(utils.get_lock(filepath, host, port)) if 'c' in mode: File._cache[filepath] = self
def unlink(path, lock_id=None): host, port = utils.get_host_port(_config['nameserver']) fs = utils.get_server(path, host, port) host, port = utils.get_host_port(fs) with closing(HTTPConnection(host, port)) as con: con.request('DELETE', path + '?lock_id=%s' % lock_id) status = con.getresponse().status if status != 200: pass
def __init__(self, filepath, mode='rtc'): """filepath: the path of the distant file mode: take the same argument as mode argument of the global open() + optional flag c (which mean store in cache). """ self.mode = mode self.filepath = filepath host, port = utils.get_host_port(_config['nameserver']) self.srv = utils.get_server(filepath, host, port) if self.srv is None: raise DFSIOError('Impossible to find a server that serve %s.' % filepath) self.last_modified = None SpooledTemporaryFile.__init__(self, _config['max_size'], mode.replace('c', '')) host, port = utils.get_host_port(_config['lockserver']) if utils.is_locked(filepath, host, port): raise DFSIOError('The file %s is locked.' % filepath) if 'w' not in mode: host, port = utils.get_host_port(self.srv) with closing(HTTPConnection(host, port)) as con: con.request('GET', filepath) response = con.getresponse() self.last_modified = response.getheader('Last-Modified') status = response.status if status not in (200, 204): raise DFSIOError('Error (%d) while opening file.' % status) if status != 204: self.write(response.read()) if 'r' in mode: self.seek(0) self.lock_id = None if 'a' in mode or 'w' in mode: # automatically gets a lock if we're in write/append mode host, port = utils.get_host_port(_config['lockserver']) self.lock_id = int(utils.get_lock(filepath, host, port)) if 'c' in mode: File._cache[filepath] = self
def __init__(self, f_path, mode='rtc'): self.mode = mode self.f_path = f_path host, port = utils.get_host_port(_config['nameserver']) self.srv = utils.get_server(f_path, host, port) if self.srv is None: print('error')
def unlink(filepath, lock_id=None): """Delete the file from the filesystem. If lock_id is provided, it's used to delete the file.""" host, port = utils.get_host_port(config['nameserver']) fs = utils.get_server(filepath, host, port) host, port = utils.get_host_port(fs) with closing(http.client(host, port)) as con: con.request('DELETE', filepath + '?lock_id=%s' % lock_id) status = con.getresponse().status if status != 200: raise DFSError('Error (%d) while deleting %s.' % (status, filepath))
def _raise_if_locked(filepath): #Exception 401 if file is locked and lockid doesn't match as per the request i = web.input() host, port = utils.get_host_port(_config['lockserver']) if utils.is_locked(filepath, host, port, i.get('lock_id', None)): raise web.unauthorized()
def _init_file_server(): """Just notify the nameserver about which directories we serve.""" host, port = utils.get_host_port(_config['nameserver']) with closing(HTTPConnection(host, port)) as con: data = 'srv=%s&dirs=%s' % (_config['srv'], '\n'.join(_config['directories']),) con.request('POST', '/', data)
def _init_file_server(): #notify the nameserver about out directories host, port = utils.get_host_port(_config['DirectoryService']) with closing(HTTPConnection(host, port)) as con: data = 'srv=%s&dirs=%s' % ( _config['srv'], '\n'.join(_config['directories']), ) con.request('POST', '/', data)
def unlink(filepath, lock_id=None): """Delete the file from the filesystem (if possible). If lock_id is provided, it's used to delete the file.""" # ns host, port = utils.get_host_port(_config['nameserver']) # fs fs = utils.get_server(filepath, host, port) host, port = utils.get_host_port(fs) with closing(HTTPConnection(host, port)) as con: con.request('DELETE', filepath + '?lock_id=%s' % lock_id) status = con.getresponse().status if status != 200: raise DFSIOError('Error (%d) while deleting %s.' % (status, filepath))
def commit(self): if 'a' in self.mode or 'w' in self.mode: self.seek(0) data = self.read() host, port = utils.get_host_port(self.srv) with closing(HTTPConnection(host, port)) as con: con.request('PUT', self.path + '?lock_id=%s' % self.lock_id, data) response = con.getresponse() self.last_modified = response.getheader('Last-Modified') status = response.status if status != 200: pass if self.lock_id is not None: host, port = utils.get_host_port(_config['lockserver']) utils.revoke_lock(self.path, host, port, self.lock_id)
def _raise_if_locked(filepath): """Raise a 401 unauthorized it the filepath is locked, and the appropriate locked wasn't given in the request. """ i = web.input() host, port = utils.get_host_port(_config['lockserver']) if utils.is_locked(filepath, host, port, i.get('lock_id', None)): raise web.unauthorized()
def __init__(self, filepath, mode='rtc'): self.mode = mode self.filepath = filepath host, port = utils.get_host_port(_config['nameserver']) self.srv = utils.get_server(filepath, host, port) self.last_modified = None SpooledTemporaryFile.__init__(self, _config['max_size'], mode.replace('c','')) host, port = utils.get_host_port(_config['lockserver']) if utils.is_locked(filepath, host, port): raise DFSIOError('The file %s is locked.' % filepath) if 'w' not in mode: host, port = utils.get_host_port(self.srv) with closing(HTTPConnection(host, port)) as con: con.request('GET', filepath) response = con.getresponse() self.last_modified = response.getheader('Last-Modified') status = response.status if status not in (200, 204): raise DFSIOError('Error (%d)while opening file.' % status) if status != 204: self.write(response.read()) if 'r' in mode: self.seek(0) self.lock_id = None if 'a' in mode or 'w' in mode: # automatically lock file if appending or writing in file host, port = utils.get_host_port(_config['lockserver']) self.lock_id = int(utils.get_lock(filepath, host, port)) if 'c' in mode: File._cache[filepath] = self
def from_cache(filepath): """save file in local disk""" if filepath in File._cache: f = File._cache[filepath] host, port = utils.get_host_port(config['nameserver']) fs = utils.get_server(filepath, host, port) host, port = utils.get_host_port(fs) with closing(http.client(host, port)) as con: con.request('HEAD', filepath) if (f.last_modified == con.getresponse().getheader( 'Last-Modified')): f.seek(0) return f else: del File._cache[filepath] return None
def commit(self): # send local file to fileserver if 'a' in self.mode or 'w' in self.mode: data = self.read() host, port = utils.get_host_port(self.srv) with closing(HTTPConnection(host, port)) as con: con.request('PUT', self.filepath + '?lock_id=%s' % self.lock_id,data) response = con.getresponse() status = response.status if status != 200: raise DFSIOError
def __init__(self, path, mode='rtc'): self.mode = mode self.path = path host, port = utils.get_host_port(_config['nameserver']) self.srv = utils.get_server(path, host, port) if self.srv is None: pass self.last_modified = None SpooledTemporaryFile.__init__(self, _config['max_size'], mode.replace('c', '')) host, port = utils.get_host_port(_config['lockserver']) if utils.is_locked(path, host, port): pass if 'w' not in mode: host, port = utils.get_host_port(self.srv) with closing(HTTPConnection(host, port)) as con: con.request('GET', path) response = con.getresponse() self.last_modified = response.getheader('Last-Modified') status = response.status if status != 204: self.write(response.read()) if 'r' in mode: self.seek(0) self.lock_id = None if 'a' in mode or 'w' in mode: host, port = utils.get_host_port(_config['lockserver']) self.lock_id = int(utils.get_lock(path, host, port)) if 'c' in mode: File._cache[path] = self
def commit(self): """Send the local file to the remote fileserver.""" if 'a' in self.mode or 'w' in self.mode: # send the file from the begining self.seek(0) data = self.read() host, port = utils.get_host_port(self.srv) with closing(http.client(host, port)) as con: con.request('PUT', self.filepath + '?lock_id=%s' % self.lock_id, data) response = con.getresponse() #self.last_modified = response.getheader('Last-Modified') status = response.status if status != 200: raise DFSError('Error (%d) while committing change to' ' the file.' % status) if self.lock_id is not None: host, port = utils.get_host_port(config['lockserver']) utils.revoke_lock(self.filepath, host, port, self.lock_id)
def commit(self): """Send the local file to the remote fileserver.""" if 'a' in self.mode or 'w' in self.mode: # send the file from the begining self.seek(0) data = self.read() host, port = utils.get_host_port(self.srv) with closing(HTTPConnection(host, port)) as con: con.request('PUT', self.filepath + '?lock_id=%s' % self.lock_id, data) response = con.getresponse() self.last_modified = response.getheader('Last-Modified') status = response.status if status != 200: raise DFSIOError('Error (%d) while committing change to' ' the file.' % status) if self.lock_id is not None: host, port = utils.get_host_port(_config['lockserver']) utils.revoke_lock(self.filepath, host, port, self.lock_id)
def client_handler(): arguments = argument_parser() print(arguments) client_identifier, hostname, port, args = arguments[0], arguments[1], arguments[2], arguments[3] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # s.bind(('127.28.3.1', 65431)) ip_det = get_host_port(client_identifier) print("User is now: ", ip_det) s.bind((ip_det[0], ip_det[1])) s.connect((hostname, port)) print("connection successfull") for arg in args: s.sendall(arg.encode('utf-8')) data = s.recv(1024) if arg.split(' ')[0] == "get": print(data.decode('utf-8')) s.close() free_ip(ip_det[0])
def main(argv=None): _usage = """Usage: %prog [options]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://0.0.0.0:5672", help="The address of the server [amqp://0.0.0.0:5672]") parser.add_option("--node", type='string', default='amq.topic', help='Name of source/target node') parser.add_option("--count", type='int', default=100, help='Send N messages (send forever if N==0)') parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") opts, _ = parser.parse_args(args=argv) if opts.debug: LOG.setLevel(logging.DEBUG) host, port = get_host_port(opts.server) my_socket = connect_socket(host, port) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container(uuid.uuid4().hex) conn_properties = {'hostname': host, 'x-server': False} if opts.trace: conn_properties["x-trace-protocol"] = True c_handler = ConnectionEventHandler() connection = container.create_connection("perf_tool", c_handler, conn_properties) r_handler = ReceiverHandler(opts.count, opts.count or 1000) receiver = connection.create_receiver(opts.node, opts.node, r_handler) s_handler = SenderHandler(opts.count) sender = connection.create_sender(opts.node, opts.node, s_handler) connection.open() receiver.open() while not receiver.active: process_connection(connection, my_socket) sender.open() # Run until all messages transfered while not sender.closed or not receiver.closed: process_connection(connection, my_socket) connection.close() while not connection.closed: process_connection(connection, my_socket) duration = s_handler.stop_time - s_handler.start_time thru = s_handler.calls / duration permsg = duration / s_handler.calls ack = s_handler.total_ack_latency / s_handler.calls lat = r_handler.tx_total_latency / r_handler.receives print("Stats:\n" " TX Avg Calls/Sec: %f Per Call: %f Ack Latency %f\n" " RX Latency: %f" % (thru, permsg, ack, lat)) sender.destroy() receiver.destroy() connection.destroy() container.destroy() my_socket.close() return 0
def main(argv=None): _usage = """Usage: %prog [options]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://127.0.0.1:8888", help="The address of the server [amqp://127.0.0.1:8888]") parser.add_option("--idle", dest="idle_timeout", type="int", default=0, help="Idle timeout for connection (seconds).") parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--source", dest="source_addr", type="string", help="Address for link source.") parser.add_option("--target", dest="target_addr", type="string", help="Address for link target.") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") parser.add_option("--ca", help="Certificate Authority PEM file") opts, extra = parser.parse_args(args=argv) if opts.debug: LOG.setLevel(logging.DEBUG) host, port = get_host_port(opts.server) my_socket = connect_socket(host, port) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container(uuid.uuid4().hex) conn_properties = {'hostname': host} if opts.trace: conn_properties["x-trace-protocol"] = True if opts.ca: conn_properties["x-ssl-ca-file"] = opts.ca if opts.idle_timeout: conn_properties["idle-time-out"] = opts.idle_timeout connection = container.create_connection("receiver", None, # no events conn_properties) connection.pn_sasl.mechanisms("ANONYMOUS") connection.pn_sasl.client() connection.open() class ReceiveCallback(pyngus.ReceiverEventHandler): def __init__(self): self.done = False self.message = None self.handle = None def message_received(self, receiver, message, handle): self.done = True self.message = message self.handle = handle target_address = opts.target_addr or uuid.uuid4().hex cb = ReceiveCallback() receiver = connection.create_receiver(target_address, opts.source_addr, cb) receiver.add_capacity(1) receiver.open() # Poll connection until something arrives while not cb.done and not connection.closed: process_connection(connection, my_socket) if cb.done: print("Receive done, message=%s" % str(cb.message)) receiver.message_accepted(cb.handle) else: print("Receive failed due to connection failure!") receiver.close() connection.close() # Poll connection until close completes: while not connection.closed: process_connection(connection, my_socket) receiver.destroy() connection.destroy() container.destroy() my_socket.close() return 0
def __init__(self, server, ca, ssl_cert_file, ssl_key_file, ssl_key_password, source): # opts, extra = parser.parse_args(args=argv) # if opts.debug: # LOG.setLevel(logging.DEBUG) #self.server = server host, port = get_host_port(server) my_socket = connect_socket(host, port, blocking=False) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container(uuid.uuid4().hex) conn_properties = {'hostname': host, 'x-server': False} conn_properties["x-trace-protocol"] = True conn_properties["x-ssl-ca-file"] = ca conn_properties["x-ssl-identity"] = (ssl_cert_file, ssl_key_file, ssl_key_password) # if opts.idle_timeout: # conn_properties["idle-time-out"] = opts.idle_timeout # if opts.username: # conn_properties['x-username'] = opts.username # if opts.password: # conn_properties['x-password'] = opts.password # if opts.sasl_mechs: # conn_properties['x-sasl-mechs'] = opts.sasl_mechs # if opts.sasl_config_dir: # conn_properties["x-sasl-config-dir"] = opts.sasl_config_dir # if opts.sasl_config_name: # conn_properties["x-sasl-config-name"] = opts.sasl_config_name #conn_properties["x-force-sasl"] = True with open(ca, "r") as f: print("reading first line from certificate to ensure we have it") print(f.readline()) conn_properties["x-ssl-verify-mode"] = "no-verify" c_handler = ConnectionEventHandler() connection = container.create_connection("receiver", c_handler, conn_properties) connection.open() target_address = uuid.uuid4().hex cb = ReceiverEventHandler() receiver = connection.create_receiver(target_address, source, cb) receiver.add_capacity(1) receiver.open() # Poll connection until something arrives while not cb.done and not connection.closed: process_connection(connection, my_socket) if cb.done: print("Receive done, message=%s" % str(cb.message) if cb. message else "ERROR: no message received") if cb.handle: receiver.message_accepted(cb.handle) else: print("Receive failed due to connection failure!") # flush any remaining output before closing (optional) while connection.has_output > 0: process_connection(connection, my_socket) receiver.close() connection.close() # Poll connection until close completes: while not connection.closed: process_connection(connection, my_socket) receiver.destroy() connection.destroy() container.destroy() my_socket.close()
def main(argv=None): _usage = """Usage: %prog [options] [message content string]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://0.0.0.0:5672", help="The address of the server [amqp://0.0.0.0:5672]") parser.add_option("--idle", dest="idle_timeout", type="int", default=0, help="Idle timeout for connection (seconds).") parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--source", dest="source_addr", type="string", help="Address for link source.") parser.add_option("--target", dest="target_addr", type="string", help="Address for link target.") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") parser.add_option("-f", "--forever", action="store_true", help="Keep sending forever") parser.add_option("--ca", help="Certificate Authority PEM file") parser.add_option("--ssl-cert-file", help="Self-identifying certificate (PEM file)") parser.add_option("--ssl-key-file", help="Key for self-identifying certificate (PEM file)") parser.add_option("--ssl-key-password", help="Password to unlock SSL key file") parser.add_option("--username", type="string", help="User Id for authentication") parser.add_option("--password", type="string", help="User password for authentication") parser.add_option("--sasl-mechs", type="string", help="The list of acceptable SASL mechs") parser.add_option("--sasl-config-dir", type="string", help="Path to directory containing sasl config") parser.add_option("--sasl-config-name", type="string", help="Name of the sasl config file (without '.config')") opts, payload = parser.parse_args(args=argv) if not payload: payload = "Hi There!" if opts.debug: LOG.setLevel(logging.DEBUG) host, port = get_host_port(opts.server) my_socket = connect_socket(host, port) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container(uuid.uuid4().hex) conn_properties = {'hostname': host, 'x-server': False} if opts.trace: conn_properties["x-trace-protocol"] = True if opts.ca: conn_properties["x-ssl-ca-file"] = opts.ca if opts.ssl_cert_file: conn_properties["x-ssl-identity"] = (opts.ssl_cert_file, opts.ssl_key_file, opts.ssl_key_password) if opts.idle_timeout: conn_properties["idle-time-out"] = opts.idle_timeout if opts.username: conn_properties['x-username'] = opts.username if opts.password: conn_properties['x-password'] = opts.password if opts.sasl_mechs: conn_properties['x-sasl-mechs'] = opts.sasl_mechs if opts.sasl_config_dir: conn_properties["x-sasl-config-dir"] = opts.sasl_config_dir if opts.sasl_config_name: conn_properties["x-sasl-config-name"] = opts.sasl_config_name c_handler = ConnectionEventHandler() connection = container.create_connection("sender", c_handler, conn_properties) connection.open() source_address = opts.source_addr or uuid.uuid4().hex s_handler = SenderEventHandler() sender = connection.create_sender(source_address, opts.target_addr, s_handler) sender.open() class SendCallback(object): def __init__(self): self.done = False self.status = None def __call__(self, link, handle, status, error): self.done = True self.status = status while True: # Send a single message: msg = Message() msg.body = str(payload) cb = SendCallback() sender.send(msg, cb) # Poll connection until SendCallback is invoked: while not cb.done: process_connection(connection, my_socket) if c_handler.error: break if connection.closed: break if cb.done: print("Send done, status=%s" % SEND_STATUS.get(cb.status, "???")) else: print("Send failed due to connection failure: %s" % c_handler.error or "remote closed unexpectedly") break if not opts.forever: break if not sender.closed: sender.close() if not connection.closed: connection.close() # Poll connection until close completes: while not c_handler.error and not connection.closed: process_connection(connection, my_socket) sender.destroy() connection.destroy() container.destroy() my_socket.close() return 0
def main(argv=None): _usage = """Usage: %prog [options]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://0.0.0.0:5672", help="The address of the server [amqp://0.0.0.0:5672]") parser.add_option("--idle", dest="idle_timeout", type="int", default=0, help="Idle timeout for connection (seconds).") parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--source", dest="source_addr", type="string", help="Address for link source.") parser.add_option("--target", dest="target_addr", type="string", help="Address for link target.") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") parser.add_option("-f","--forever", action="store_true", help="don't stop receiving") parser.add_option("--ca", help="Certificate Authority PEM file") parser.add_option("--ssl-cert-file", help="Self-identifying certificate (PEM file)") parser.add_option("--ssl-key-file", help="Key for self-identifying certificate (PEM file)") parser.add_option("--ssl-key-password", help="Password to unlock SSL key file") parser.add_option("--username", type="string", help="User Id for authentication") parser.add_option("--password", type="string", help="User password for authentication") parser.add_option("--sasl-mechs", type="string", help="The list of acceptable SASL mechs") parser.add_option("--sasl-config-dir", type="string", help="Path to directory containing sasl config") parser.add_option("--sasl-config-name", type="string", help="Name of the sasl config file (without '.config')") opts, extra = parser.parse_args(args=argv) if opts.debug: LOG.setLevel(logging.DEBUG) host, port = get_host_port(opts.server) my_socket = connect_socket(host, port) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container(uuid.uuid4().hex) conn_properties = {'hostname': host, 'x-server': False} if opts.trace: conn_properties["x-trace-protocol"] = True if opts.ca: conn_properties["x-ssl-ca-file"] = opts.ca if opts.ssl_cert_file: conn_properties["x-ssl-identity"] = (opts.ssl_cert_file, opts.ssl_key_file, opts.ssl_key_password) if opts.idle_timeout: conn_properties["idle-time-out"] = opts.idle_timeout if opts.username: conn_properties['x-username'] = opts.username if opts.password: conn_properties['x-password'] = opts.password if opts.sasl_mechs: conn_properties['x-sasl-mechs'] = opts.sasl_mechs if opts.sasl_config_dir: conn_properties["x-sasl-config-dir"] = opts.sasl_config_dir if opts.sasl_config_name: conn_properties["x-sasl-config-name"] = opts.sasl_config_name c_handler = ConnectionEventHandler() connection = container.create_connection("receiver", c_handler, conn_properties) connection.open() target_address = opts.target_addr or uuid.uuid4().hex cb = ReceiverEventHandler() receiver = connection.create_receiver(target_address, opts.source_addr, cb) receiver.add_capacity(1) receiver.open() while True: # Poll connection until something arrives while not cb.done: process_connection(connection, my_socket) if c_handler.error: break if connection.closed: break if cb.done: print("Receive done, message=%s" % str(cb.message) if cb.message else "ERROR: no message received") if cb.handle: receiver.message_accepted(cb.handle) else: print("Receive failed due to connection failure: %s" % c_handler.error or "remote closed unexpectedly") break if not opts.forever: break cb.done = False receiver.add_capacity(1) # Poll connection until close completes: while not c_handler.error and not connection.closed: process_connection(connection, my_socket) receiver.destroy() connection.destroy() container.destroy() my_socket.close() return 0
def main(argv=None): _usage = """Usage: %prog [options]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="address", type="string", default="amqp://0.0.0.0:5672", help="Server address [amqp://0.0.0.0:5672]") parser.add_option("--idle", dest="idle_timeout", type="float", default=30, help="timeout for an idle link, in seconds") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--ssl-cert-file", help="PEM File containing the server's certificate") parser.add_option("--ssl-key-file", help="PEM File containing the server's private key") parser.add_option("--ssl-key-password", help="Password used to decrypt key file") parser.add_option("--ca", help="Certificate Authority PEM file") parser.add_option("--require-auth", action="store_true", help="Require clients to authenticate") parser.add_option("--sasl-mechs", type="string", help="The list of acceptable SASL mechs") parser.add_option("--sasl-cfg-name", type="string", help="name of SASL config file (no suffix)") parser.add_option("--sasl-cfg-dir", type="string", help="Path to the SASL config file") opts, arguments = parser.parse_args(args=argv) if opts.debug: LOG.setLevel(logging.DEBUG) # Create a socket for inbound connections # host, port = get_host_port(opts.address) my_socket = server_socket(host, port) # create an AMQP container that will 'provide' the Server service # container = pyngus.Container("Server") socket_connections = set() # Main loop: process I/O and timer events: # while True: readers, writers, timers = container.need_processing() # map pyngus Connections back to my SocketConnections: readfd = [c.user_context for c in readers] writefd = [c.user_context for c in writers] timeout = None if timers: deadline = timers[0].next_tick # [0] == next expiring timer now = time.time() timeout = 0 if deadline <= now else deadline - now LOG.debug("select() start (t=%s)", str(timeout)) readfd.append(my_socket) readable, writable, ignore = select.select(readfd, writefd, [], timeout) LOG.debug("select() returned") worked = set() for r in readable: if r is my_socket: # new inbound connection request received, # create a new SocketConnection for it: client_socket, client_address = my_socket.accept() # name = uuid.uuid4().hex name = str(client_address) conn_properties = {'x-server': True} if opts.require_auth: conn_properties['x-require-auth'] = True if opts.sasl_mechs: conn_properties['x-sasl-mechs'] = opts.sasl_mechs if opts.sasl_cfg_name: conn_properties['x-sasl-config-name'] = opts.sasl_cfg_name if opts.sasl_cfg_dir: conn_properties['x-sasl-config-dir'] = opts.sasl_cfg_dir if opts.idle_timeout: conn_properties["idle-time-out"] = opts.idle_timeout if opts.trace: conn_properties["x-trace-protocol"] = True if opts.ca: conn_properties["x-ssl-server"] = True conn_properties["x-ssl-ca-file"] = opts.ca conn_properties["x-ssl-verify-mode"] = "verify-cert" if opts.ssl_cert_file: conn_properties["x-ssl-server"] = True identity = (opts.ssl_cert_file, opts.ssl_key_file, opts.ssl_key_password) conn_properties["x-ssl-identity"] = identity sconn = SocketConnection(container, client_socket, name, conn_properties) socket_connections.add(sconn) LOG.debug("new connection created name=%s", name) else: assert isinstance(r, SocketConnection) r.process_input() worked.add(r) for t in timers: now = time.time() if t.next_tick > now: break t.process(now) sc = t.user_context assert isinstance(sc, SocketConnection) worked.add(sc) for w in writable: assert isinstance(w, SocketConnection) w.send_output() worked.add(w) closed = False while worked: sc = worked.pop() # nuke any completed connections: if sc.closed: socket_connections.discard(sc) sc.destroy() closed = True else: # can free any closed links now (optional): for link in sc.sender_links | sc.receiver_links: if link.closed: link.destroy() if closed: LOG.debug("%d active connections present", len(socket_connections)) return 0
def main(argv=None): _usage = """Usage: %prog [options] [message content string]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://127.0.0.1:8888", help="The address of the server [amqp://127.0.0.1:8888]") parser.add_option("--idle", dest="idle_timeout", type="int", default=0, help="Idle timeout for connection (seconds).") parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--source", dest="source_addr", type="string", help="Address for link source.") parser.add_option("--target", dest="target_addr", type="string", help="Address for link target.") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") parser.add_option("--ca", help="Certificate Authority PEM file") opts, payload = parser.parse_args(args=argv) if not payload: payload = "Hi There!" if opts.debug: LOG.setLevel(logging.DEBUG) host, port = get_host_port(opts.server) my_socket = connect_socket(host, port) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container("Sender") conn_properties = {'hostname': host} if opts.trace: conn_properties["x-trace-protocol"] = True if opts.ca: conn_properties["x-ssl-ca-file"] = opts.ca if opts.idle_timeout: conn_properties["idle-time-out"] = opts.idle_timeout connection = container.create_connection("sender", None, # no events conn_properties) connection.pn_sasl.mechanisms("ANONYMOUS") connection.pn_sasl.client() connection.open() source_address = opts.source_addr or uuid.uuid4().hex sender = connection.create_sender(source_address, opts.target_addr) sender.open() # Send a single message: msg = Message() msg.body = str(payload) class SendCallback(object): def __init__(self): self.done = False self.status = None def __call__(self, link, handle, status, error): self.done = True self.status = status cb = SendCallback() sender.send(msg, cb) # Poll connection until SendCallback is invoked: while not cb.done and not connection.closed: process_connection(connection, my_socket) if cb.done: print("Send done, status=%s" % SEND_STATUS.get(cb.status, "???")) else: print("Send failed due to connection failure!") sender.close() connection.close() # Poll connection until close completes: while not connection.closed: process_connection(connection, my_socket) sender.destroy() connection.destroy() container.destroy() my_socket.close() return 0
def main(argv=None): _usage = """Usage: %prog [options]""" parser = optparse.OptionParser(usage=_usage) parser.add_option("-a", dest="server", type="string", default="amqp://0.0.0.0:5672", help="The address of the server [amqp://0.0.0.0:5672]") parser.add_option("--idle", dest="idle_timeout", type="int", default=0, help="Idle timeout for connection (seconds).") parser.add_option("--debug", dest="debug", action="store_true", help="enable debug logging") parser.add_option("--source", dest="source_addr", type="string", help="Address for link source.") parser.add_option("--target", dest="target_addr", type="string", help="Address for link target.") parser.add_option("--trace", dest="trace", action="store_true", help="enable protocol tracing") parser.add_option("--ca", help="Certificate Authority PEM file") parser.add_option("--ssl-cert-file", help="Self-identifying certificate (PEM file)") parser.add_option("--ssl-key-file", help="Key for self-identifying certificate (PEM file)") parser.add_option("--ssl-key-password", help="Password to unlock SSL key file") parser.add_option("--username", type="string", help="User Id for authentication") parser.add_option("--password", type="string", help="User password for authentication") parser.add_option("--sasl-mechs", type="string", help="The list of acceptable SASL mechs") parser.add_option("--sasl-config-dir", type="string", help="Path to directory containing sasl config") parser.add_option("--sasl-config-name", type="string", help="Name of the sasl config file (without '.config')") opts, extra = parser.parse_args(args=argv) if opts.debug: LOG.setLevel(logging.DEBUG) host, port = get_host_port(opts.server) my_socket = connect_socket(host, port) # create AMQP Container, Connection, and SenderLink # container = pyngus.Container(uuid.uuid4().hex) conn_properties = {'hostname': host, 'x-server': False} if opts.trace: conn_properties["x-trace-protocol"] = True if opts.ca: conn_properties["x-ssl-ca-file"] = opts.ca if opts.ssl_cert_file: conn_properties["x-ssl-identity"] = (opts.ssl_cert_file, opts.ssl_key_file, opts.ssl_key_password) if opts.idle_timeout: conn_properties["idle-time-out"] = opts.idle_timeout if opts.username: conn_properties['x-username'] = opts.username if opts.password: conn_properties['x-password'] = opts.password if opts.sasl_mechs: conn_properties['x-sasl-mechs'] = opts.sasl_mechs if opts.sasl_config_dir: conn_properties["x-sasl-config-dir"] = opts.sasl_config_dir if opts.sasl_config_name: conn_properties["x-sasl-config-name"] = opts.sasl_config_name c_handler = ConnectionEventHandler() connection = container.create_connection("receiver", c_handler, conn_properties) connection.open() target_address = opts.target_addr or uuid.uuid4().hex cb = ReceiverEventHandler() receiver = connection.create_receiver(target_address, opts.source_addr, cb) receiver.add_capacity(1) receiver.open() # Poll connection until something arrives while not cb.done and not connection.closed: process_connection(connection, my_socket) if cb.done: print("Receive done, message=%s" % str(cb.message) if cb.message else "ERROR: no message received") if cb.handle: receiver.message_accepted(cb.handle) else: print("Receive failed due to connection failure!") # flush any remaining output before closing (optional) while connection.has_output > 0: process_connection(connection, my_socket) receiver.close() connection.close() # Poll connection until close completes: while not connection.closed: process_connection(connection, my_socket) receiver.destroy() connection.destroy() container.destroy() my_socket.close() return 0