Changes

Jump to navigation Jump to search
Line 282: Line 282:  
PATH = "/org/laptop/Annotate"
 
PATH = "/org/laptop/Annotate"
   −
####### Create both a RequestHandler and an Server that will work together to ######
  −
####### serve data over a stream tube.                                        ######
      +
##########################################################################
 +
REQUESTHANDLER AND HTTPSERVER WORK TOGETHER TO SERVE DATA OVER STREAM TUBE
 +
##########################################################################
 
class AnnotateHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
 
class AnnotateHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
 
     #### Method translate_path, which, for our simple activity, just returns the same
 
     #### Method translate_path, which, for our simple activity, just returns the same
Line 298: Line 299:  
</pre>
 
</pre>
   −
In the example code above, most of the work is done by the superclasses defined in sugar.network, namely ChunkedGlibHTTPRequestHandler and GLibTCPServer.
+
In the example code above, most of the work is done by the superclasses defined in sugar.network, namely ChunkedGlibHTTPRequestHandler and GLibTCPServer. However, if you want some custom behavior in how data is served, then it is a good idea to subclass those or other Server and RequestHandler classes.
 +
 
 +
On the client side, there must be a class that handles receiving data from a server. In Annotate, this is accomplished with the standard sugar.network.GlibURLDownloader class.
 +
 
 +
 
 +
==== Step 3: Coordinate sharing and joining in your activity using stream tubes and the client/server classes defined above. ====
 +
<pre>
 +
ANNOTATE_STREAM_SERVICE = 'annotate-activity-http'
 +
 
 +
class AnnotateActivity(activity.Activity):
 +
 
 +
    #### Method: __init__, initialize this AnnotateActivity instance
 +
    def __init__(self, handle):
 +
 
 +
        #Joining activity needs to know which stream tubes are available for downloading
 +
        #and if it still needs to download document.
 +
        self.unused_download_tubes = set()
 +
        self._want_document = True
 +
 
 +
        #Set the port number on which sharing activity will serve data
 +
        h = hash(self._activity_id)
 +
        self.port = 1024 + (h % 64511)
 +
 
 +
        self.connect('shared', self._shared_cb)
 +
        if self._shared_activity:
 +
            # We're joining
 +
            if self.get_shared():
 +
                # Already joined for some reason, just get the document
 +
                self._joined_cb(self)
 +
            else:
 +
                # Wait for a successful join before trying to get the document
 +
                self.connect("joined", self._joined_cb)
 +
 
 +
 
 +
 
 +
 
 +
</pre>
    
=== When downloading data from a stream tube, how can I show the download progress? ===
 
=== When downloading data from a stream tube, how can I show the download progress? ===
Anonymous user

Navigation menu