rsync_server_node.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (c) 2016, Alex McClung
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the author may be used to endorse or promote 
00019 #    products derived from this software without specific prior
00020 #    written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 import rospy
00036 import roslib; roslib.load_manifest('rsync_ros')
00037 import actionlib
00038 import os
00039 from rsync import Rsync
00040 from rsync_ros.msg import RsyncAction, RsyncResult, RsyncFeedback
00041 
00042 class RsyncActionServer:
00043 
00044     def __init__(self, name):
00045         self._action_name = name
00046         self.server = actionlib.SimpleActionServer(self._action_name, RsyncAction, self.execute, False)
00047         self.server.start()
00048         rospy.loginfo("Ready to sync files.")
00049 
00050     def progress_update_cb(self, line, percent_complete, transfer_rate):
00051         #This is run everytime the progress is published to stdout
00052         #rospy.loginfo('Total transfer percentage: {}'.format(percent_complete))
00053 
00054         self.feedback.percent_complete = percent_complete
00055         self.feedback.transfer_rate = transfer_rate
00056         self.server.publish_feedback(self.feedback)
00057 
00058         if line:
00059             rospy.loginfo(line)
00060 
00061         # check if preempt (cancel action) has been requested by the client
00062         if self.server.is_preempt_requested():
00063             # Get the process id & try to terminate it gracefuly
00064             pid = self.rsync.p.pid
00065             self.rsync.p.terminate()
00066 
00067             # Check if the process has really terminated & force kill if not.
00068             try:
00069                 os.kill(pid, 0)
00070                 self.rsync.p.kill()
00071                 print "Forced kill"
00072             except OSError, e:
00073                 print "Terminated gracefully"
00074 
00075 
00076             rospy.loginfo('%s: Preempted' % self._action_name)
00077             self.server.set_preempted() #TO-DO, fix logic error changing states upon preempt request
00078 
00079     def execute(self, goal):
00080         self.result = RsyncResult()
00081         self.feedback = RsyncFeedback()
00082 
00083         rospy.loginfo("Executing rsync command '%s %s %s'", 'rsync ' + ' '.join(goal.rsync_args) + ' --progress --outbuf=L', goal.source_path, goal.destination_path)
00084 
00085         self.rsync = Rsync(goal.rsync_args, goal.source_path, goal.destination_path, progress_callback=self.progress_update_cb)
00086 
00087         self.result.sync_success = self.rsync.sync()
00088 
00089         if not self.server.is_preempt_requested():
00090 
00091             if self.rsync.stderr_block:
00092                 rospy.logerr('\n{}'.format(self.rsync.stderr_block))
00093 
00094             rospy.loginfo("Rsync command result '%s'", self.result.sync_success)
00095             self.server.set_succeeded(self.result)
00096 
00097 if __name__ == "__main__":
00098     try:
00099         rospy.init_node('rsync_ros')
00100         RsyncActionServer(rospy.get_name())
00101         rospy.spin()
00102     except rospy.ROSInterruptException:
00103         pass


rsync_ros
Author(s):
autogenerated on Thu Jul 21 2016 06:13:41