source: trunk/textNewCnx.py @ 2290

Revision 1584, 5.7 KB checked in by tolteque, 2 years ago (diff)

o Lotatc:

  • Modify radarServer to send and receive frame in the new Fashion.

--- OF COURSE NOT TESTED --- :)

Line 
1# -*- coding: utf-8 -*-
2# vim: ts=4:sw=4
3#    This file is part of LOME.
4#
5#    LOME is free software: you can redistribute it and/or modify
6#    it under the terms of the GNU General Public License as published by
7#    the Free Software Foundation, either version 3 of the License, or
8#    (at your option) any later version.
9#
10#    LOME is distributed in the hope that it will be useful,
11#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#    GNU General Public License for more details.
14#
15#    You should have received a copy of the GNU General Public License
16#    along with LOME.  If not, see <http://www.gnu.org/licenses/>.
17#
18
19import os
20import sys
21import socket
22import SocketServer
23import threading
24import struct
25
26from time            import *
27from PyQt4           import uic
28from PyQt4.QtGui     import *
29from PyQt4.QtCore    import *
30from PyQt4.QtNetwork import QTcpSocket
31from os.path         import join, dirname
32
33
34sys.path.append( os.path.join( sys.path[0], 'common'))
35from cnxObj          import *
36
37
38#==============================================================================
39## Class which implements the request handler used by plugRouter
40class ServerHandler( SocketServer.BaseRequestHandler):
41# ----------------------------------------------------------------------------
42  ## Constructor, invoked when the plug connect to the router
43  # @param self              object instance
44  # @param request           instance of the request
45  # @param clientAddress     IP Address of the client (plug)
46  # @param server            instance of the caller
47  def __init__( self, request, clientAddress, server):
48    print "[ServerHandler] connection established with client"
49   
50    self.cnxObj = CnxObj( request)
51   
52    # Invoke __init__ method of the parent class
53    SocketServer.BaseRequestHandler.__init__( self, request, clientAddress, server)
54
55# ----------------------------------------------------------------------------
56  ## Setup method. It is time to init some internal variables
57  # The router is informed about this plug only after the plug name is known
58  # @param self                object instance
59  def setup( self):
60    print "[ServerHandler] setup done"   
61    return SocketServer.BaseRequestHandler.setup( self)
62
63# ----------------------------------------------------------------------------
64  ## Method to handle data available on socket
65  # @param self                object instance
66  def handle( self):
67    print "[ServerHandler] handle invoked"   
68   
69    # Loop on receive
70    while( True):
71      # Get one message
72      try:
73        frames = self.cnxObj.receiveFrame()
74      except:
75        # Connection probably reset by peer
76        return
77     
78      if len( frames[0]):
79        for frame in frames:
80          print "Frame received: %s" % frame
81      else: 
82        return
83      # End of: if len(message):
84    # End of: while(True):
85
86# ----------------------------------------------------------------------------
87  ## Call back invoked when the connection is closed
88  # @param self                object instance
89  def finish( self):
90    print "[ServerHandler] Socket closed. Bye ..."   
91    return SocketServer.BaseRequestHandler.finish( self)
92
93
94#==============================================================================
95class Server( SocketServer.ThreadingMixIn, SocketServer.TCPServer):
96  allow_reuse_address = True
97  daemon_threads      = True
98
99# --------------------------------------------------------------------------------------------------------------------------------
100  # Constructor
101  # @param self                     object instance
102  # @param serverAddress            Address of the server
103  def __init__( self, serverAddress):
104    # Start routerSocket manager
105    SocketServer.TCPServer.__init__( self, serverAddress, ServerHandler)
106
107g_window=None
108cnxObj = None
109def connected():
110  print "connected"
111  global cnxObj
112  cnxObj.sendFrame( "This is a buffer tansmitted in the clear mode")
113  cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode", forceZip=True)
114  cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode\n" * 10)
115  cnxObj.flush()
116 
117  frame = ""
118  for index in range(0,5):
119    frame += cnxObj.buildFrame( "This is the frame: %02d in a multiframe" % index)
120  cnxObj.sendFrame( frame, multiFrame = True)
121 
122 
123  cnxObj.disconnectFromHost()
124 
125   
126   
127def launch():
128  print "launched"
129  global cnxObj
130  # QT test
131  cnxObj = QTCnxObj()
132 
133  g_window.connect( cnxObj, SIGNAL("connected()"), connected )
134  cnxObj.connectToHost( 'localhost', 10312)
135   
136
137def TestTransmition():
138  global g_window
139  g_pathname = os.path.abspath(dirname( sys.argv[0] ))
140
141  app = QApplication(sys.argv)
142
143  g_window = QMainWindow()
144  button = QPushButton("launch")
145  g_window.layout().addWidget( button )
146  g_window.connect( button, SIGNAL("clicked()"), launch )
147
148
149  # Start graphical interface
150  g_window.show()
151  status = app.exec_()
152
153  # Create and connect socket
154  sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
155  print "socket created"
156  try:
157    sock.connect( ( 'localhost', 10312))
158    print "socket connected"
159  except socket.error, msg:
160    print msg
161    return
162  cnxObj = CnxObj( sock)
163  cnxObj.sendFrame( "This is a buffer tansmitted in the clear mode")
164  cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode" ,  forceZip=True)   
165  cnxObj.sendFrame( "This is a buffer tansmitted in the zip mode\n"*10)   
166 
167  sleep(1)
168  sock.shutdown(2)
169
170# =================================================
171if __name__ == "__main__":
172  server = Server(('localhost', 10312))
173  serverThread = threading.Thread( target = server.serve_forever)
174  serverThread.setDaemon( True) 
175  serverThread.start()
176 
177  sleep(1)
178 
179  TestTransmition()
180 
Note: See TracBrowser for help on using the repository browser.