# Get some basic modules DEFHOST = 'flapjack.cs.orst.edu' DEFPORT = 55571 import sys,time,string import ihBrowser, ihDoc, ihURL class DocTracker(ihURL.LineURL): def __init__(self,doc,host,port): # Last document state which was sent to the server. self.last_clock = time.time() self.last_top = 0 self.last_bottom = 0 self.last_lines = 0 self.doc = doc ihURL.LineURL.__init__(self,doc,'tcp://' + host + ':' + `port`) result = (self.GetStatus(),self.GetURL()) self.sock_result = `result` self.sock_address = self.GetURL() self.Write("* send\n") # Connect to the server. ## try: ## ihURL.LineURL.__init__(self,doc,'tcp:'+host+':'+port) ## self.sock_result = '(0, Okay)' ## self.sock_address = self.GetURL() ## ## except: ## self.conn = None ## self.server = None ## self.sock_result = `sys.get_exception()` ## self.sock_address = "-nothing-" def OnLine(self,lines): self.doc.ShowProgress(self.GetURL() + ' - ' + lines[-1]) def OnError(self,error): addr = self.GetURL() if( not addr ): addr = '[tcp connection]' self.doc.ShowProgress(addr + ' - ' + error) # Global document event handler. After instantiating this class, # this method can be attached to the document's list of handlers. def doc_handler(self,doc,event): self.send_scrollpos(doc,event.Code) # Send a new document state message to the server. def send_scrollpos(self,doc,action): if self.last_clock != 0: new_clock = time.time() elapsed = new_clock - self.last_clock #print "last_clock", self.last_clock, "new_clock", new_clock, "elapsed", elapsed self.last_clock = new_clock if(elapsed > 0.2): action = string.lower(action+"") txt = "* " + action + ": top=" \ + `self.last_top` \ + "; bot=" + `self.last_bottom` \ + "; total=" + `self.last_lines` \ + "; time=" + ("%.2f" % elapsed) + " secs\n" txt = "* " + action + " " + `self.last_top` + " " \ + `self.last_bottom` + " " + `self.last_lines` + " " \ + ("%.2f" % elapsed) + "\n" #print 'Going to send:', txt if( self.GetStatus() == ihURL.URL_INTERACTIVE ): self.Write(txt) else: self.last_clock = time.time() self.last_top = doc.ViewTop self.last_bottom = doc.ViewBottom self.last_lines = doc.DocHeight class ihMain(ihDoc.Document): def __init__(self,**args): apply(ihDoc.Document.__init__,(self,),args) self.tracker = DocTracker(self,DEFHOST,DEFPORT) self.ShowProgress() # Display the document. self.Reformat() # Install handler #print 'Ready to send data...' self.tracker.send_scrollpos(self,"initpos") self.Handlers.append(self.tracker.doc_handler) __export__ = ['ihMain']