Changeset 165
- Timestamp:
- 06/26/12 03:13:44 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
etherws/trunk/etherws.py
r164 r165 49 49 import getpass 50 50 import argparse 51 import t hreading51 import traceback 52 52 53 53 import pytun … … 150 150 self._clients = [] 151 151 self._table = SwitchingTable(debug=debug) 152 self._tablelock = threading.Lock()153 152 self._tap = pytun.TunTapDevice(dev, pytun.IFF_TAP | pytun.IFF_NO_PI) 154 153 self._tap.up() 155 self._taplock = threading.Lock()156 154 self.register_client(self) 157 155 … … 166 164 self._clients.remove(client) 167 165 168 # synchronized methods169 166 def write_message(self, message, binary=False): 170 with self._taplock: 171 self._tap.write(message) 167 self._tap.write(message) 172 168 173 169 def write(self, caller, message): 174 170 frame = EthernetFrame(message) 175 171 176 with self._tablelock: 177 self._table.learn(frame, caller) 172 self._table.learn(frame, caller) 178 173 179 174 if not frame.multicast: 180 with self._tablelock: 181 dst = self._table.lookup(frame) 175 dst = self._table.lookup(frame) 182 176 183 177 if dst: … … 204 198 205 199 while True: 206 with self._taplock: 207 data = self._tap.read(self.READ_SIZE) 200 data = self._tap.read(self.READ_SIZE) 208 201 209 202 if data: … … 264 257 self._sock = None 265 258 self.dprintf('disconnected: %s\n', lambda: self._url) 259 260 def fileno(self): 261 if self.closed: 262 raise websocket.WebSocketException('closed socket') 263 return self._sock.io_sock.fileno() 266 264 267 265 def write_message(self, message, binary=False): … … 274 272 self._sock.send(message, flag) 275 273 276 def run_forever(self):274 def __call__(self, fd, events): 277 275 try: 278 if self.closed: 279 self.open() 280 while True: 281 data = self._sock.recv() 282 if data is None: 283 break 276 data = self._sock.recv() 277 if data is not None: 284 278 self._tap.write(self, data) 285 finally: 286 self.close() 279 return 280 except: 281 traceback.print_exc() 282 tornado.ioloop.IOLoop.instance().stop() 287 283 288 284 … … 432 428 ioloop = tornado.ioloop.IOLoop.instance() 433 429 ioloop.add_handler(tap.fileno(), tap, ioloop.READ) 434 435 t = threading.Thread(target=ioloop.start) 436 t.setDaemon(True) 430 ioloop.add_handler(client.fileno(), client, ioloop.READ) 437 431 438 432 if not args.foreground: 439 433 daemonize() 440 434 441 t.start() 442 client.run_forever() 435 ioloop.start() 443 436 444 437
Note: See TracChangeset
for help on using the changeset viewer.