/[ambar]/ambar/connection.py
ViewVC logotype

Diff of /ambar/connection.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2.4 by amoyav, Sat Dec 7 23:09:35 2002 UTC revision 2.5 by amoyav, Sun Sep 7 15:10:01 2003 UTC
# Line 37  class Connection(object): Line 37  class Connection(object):
37    
38      It allows to send and receive lines of text messages from a client.      It allows to send and receive lines of text messages from a client.
39    
40        It does 'page waiting': it counts the lines sent to the client, and
41        when the count exceeds the page size, it sends a prompt, stops
42        sending lines, and store subsequent ones in a buffer, until a
43        message is received from the client.
44    
45        It also does line wrapping: lines longer than the screen width (by
46        default 79 lines) are cut.
47    
48      Do not directly instantiate it, but rather use instances returned      Do not directly instantiate it, but rather use instances returned
49      by the accept() method of ConnectionHandler.      by the accept() method of ConnectionHandler.
50    
# Line 71  class Connection(object): Line 79  class Connection(object):
79          #passed to the function: this connection.          #passed to the function: this connection.
80          self.error_callback = error_callback          self.error_callback = error_callback
81    
82            #The line width in characters
83            self.line_width = 79
84    
85            #The page length in lines
86            self.page_length = 21
87    
88            #The buffer to store lines until the client has accepted
89            self.page_wait_buffer = []
90    
91            #The number of lines sent in the current page
92            self.line_count = 0
93    
94            #The prompt to send when page full
95            self.page_prompt = "+++"
96    
97          self.is_open = 1          self.is_open = 1
98    
99    
100        def set_line_width(self, line_width):
101            """Change the width of the lines from now on.
102            
103            If 0, no line wrapping is done.
104            """
105            self.line_width = line_width
106    
107    
108        def set_page_length(self, page_length):
109            """Change the length of the page from now on.
110            
111            If 0, no page waiting is done.
112            """
113            self.page_length = page_length
114    
115    
116        def set_page_prompt(self, page_prompt):
117            """Change the prompt to send when page full, from now on."""
118            self.page_prompt = page_prompt
119    
120    
121      def receive(self):      def receive(self):
122          """Return the input data received from the client.          """Return the input data received from the client.
123    
# Line 83  class Connection(object): Line 127  class Connection(object):
127          return self.input          return self.input
128    
129    
130      def send_paginado(self, string, end_of_line = 0):      def send(self, string, end_of_line = 1):
         """Pagina un documento al cliente.  
           
         si end_of_line == 1, un final de línea se añade al string"""  
   
         contador_lineas = 0  
         parada = 1  
         for linea in string:  
             contador_lineas += 1  
             if contador_lineas % 20 == 0:  
                 self.socket.send("\n---- presiona ENTER ----\n\n")  
 #               while parada == 1:  
   
 #                   stop = raw_input(' ')  
 #                   if stop == 'sigue\n': parada = 0  
 #                   else: pass  
                       
 #           else:  
                   
                 self.socket.send(linea+'\n')  
         if (end_of_line): self.socket.send("\n")  
   
           
     def send(self, string, end_of_line = 1, width = 79):  
131          """Send a string of text to the client.          """Send a string of text to the client.
132    
133            The string may contain multiple lines separated by "\n".
134    
135          If end_of_line == 1, an end of line is appended to the string.          If end_of_line == 1, an end of line is appended to the string.
136          If width != 0, if the text is longer than width, it is cut to  
137            If line_width != 0, if the text is longer than line_width, it is cut to
138          this width, breaking in spaces.          this width, breaking in spaces.
139    
140            If page_length != 0, if the page has been filled, the line is not
141            sent to the client, but instead is stored in a buffer until the
142            client sends an acknowledge message.
143          """          """
144          try: #si todos se desconectan a la vez ocurre una catástrofe. mejor con try          lines_to_send = []
145               # %%Andres-> esto ke es????  
146            # Split the string in one or more lines
147            for line in string.split("\n"):
148    
149              # Loop splitting the string in pieces of length              # Split the line in pieces of length less than or equal than line_width.
             # less than or equal than width.  
150              start = 0              start = 0
151              end = width              end = self.line_width
152              while start < len(string):              while start < len(line):
153                                    
154                  if end > len(string):                  if end > len(line):
155                      # We have reached the end of the string.                      # We have reached the end of the line.
156                      end = len(string)                      end = len(line)
157                  else:                  else:
158                      # Loop back searching the first blank character.                      # Loop back searching the first blank character.
159                      while (end > start+1) and (string[end-1] != ' '):                      while (end > start+1) and (line[end-1] != ' '):
160                          end -= 1                          end -= 1
161    
162                      # If no blanks in the string, split just at the                      # If no blanks in the line, split just at the
163                      # given width.                      # given width.
164                      if end == start+1:                      if end == start+1:
165                          end = start + width                          end = start + self.line_width
166    
167                  # Send the piece through the socket, with new line.                  # Add the piece to the lines to send list.
168                  if start > 0: self.socket.send("\n")                  if start == 0:
169                  self.socket.send(string[start:end])                      piece = line[start:end]
170                    else:
171                        piece = "\n" + line[start:end]
172                    lines_to_send.append(piece)
173    
174                  # Advance.                  # Advance.
175                  start = end                  start = end
176                  end = start + width                  end = start + self.line_width
177    
178              # Send the last new line, if asked to do so.              # Add the last new line, if asked to do so.
179              if (end_of_line):              if (end_of_line):
180                  self.socket.send("\n")                  lines_to_send.append("\n")
181    
182          except:          # Send the lines, or store in the buffer, wether the screen is filled out or not.
183              pass  #%% ???          for piece in lines_to_send:
184                if self.line_count < self.page_length:
185                    self.socket.send(piece)
186                    if len(piece) > 0 and piece[0] == "\n":
187                        self.line_count += 1
188                else:
189                    if len(self.page_wait_buffer) == 0:
190                        self.socket.send(self.page_prompt + "\n")
191                    self.page_wait_buffer.append(piece)
192    
193    
194      def close(self, with_error = 0):      def close(self, with_error = 0):
# Line 203  class Connection(object): Line 243  class Connection(object):
243                          #Add the new data to the end of the queue.                          #Add the new data to the end of the queue.
244                          self._enqueue(data)                          self._enqueue(data)
245    
246                          #Get the entire string of input for processing from the queue.                          #Two cases: a) if the output was blocked by page full, this input
247                          self.input = self._queue()                          #unblocks the next page of output, and is ignored otherwise.
248    
249                            if len(self.page_wait_buffer) != 0:
250    
251                                self.line_count = 0
252                                while self.line_count < self.page_length \
253                                  and len(self.page_wait_buffer) > 0:
254    
255                                      piece = self.page_wait_buffer[0]
256                                      del self.page_wait_buffer[0]
257                                      self.socket.send(piece)
258                                      if len(piece) > 0 and piece[0] == "\n":
259                                          self.line_count += 1
260    
261                                if len(self.page_wait_buffer) > 0:
262                                    self.socket.send(self.page_prompt + "\n")
263    
264                            else:
265    
266                                # b) This is a normal input. Resets the page counting.
267                                self.line_count = 0
268    
269                                #Get the entire string of input for processing from the queue.
270                                self.input = self._queue()
271    
272                          #Clear the queue.                                                                            #Clear the queue.                                              
273                          self._resetqueue()                              self._resetqueue()
274    
275                          #Notify to the client that there is available data.                              #Notify to the client that there is available data.
276                          self.receive_callback(self)                              self.receive_callback(self)
277    
278    
279      def _enqueue(self, string):      def _enqueue(self, string):

Legend:
Removed from v.2.4  
changed lines
  Added in v.2.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26