Revision 1a73ae30

b/lib/hypervisor/hv_kvm.py
325 325
    return self.data == other.data
326 326

  
327 327

  
328
class QmpConnection:
329
  """Connection to the QEMU Monitor using the QEMU Monitor Protocol (QMP).
330

  
331
  """
332
  _FIRST_MESSAGE_KEY = "QMP"
333
  _EVENT_KEY = "event"
334
  _ERROR_KEY = "error"
335
  _RETURN_KEY = RETURN_KEY = "return"
336
  _ACTUAL_KEY = ACTUAL_KEY = "actual"
337
  _ERROR_CLASS_KEY = "class"
338
  _ERROR_DATA_KEY = "data"
339
  _ERROR_DESC_KEY = "desc"
340
  _EXECUTE_KEY = "execute"
341
  _ARGUMENTS_KEY = "arguments"
342
  _CAPABILITIES_COMMAND = "qmp_capabilities"
343
  _MESSAGE_END_TOKEN = "\r\n"
328
class MonitorSocket(object):
344 329
  _SOCKET_TIMEOUT = 5
345 330

  
346 331
  def __init__(self, monitor_filename):
347
    """Instantiates the QmpConnection object.
332
    """Instantiates the MonitorSocket object.
348 333

  
349 334
    @type monitor_filename: string
350 335
    @param monitor_filename: the filename of the UNIX raw socket on which the
351
                             QMP monitor is listening
336
                             monitor (QMP or simple one) is listening
352 337

  
353 338
    """
354 339
    self.monitor_filename = monitor_filename
......
357 342
    # in a reasonable amount of time
358 343
    self.sock.settimeout(self._SOCKET_TIMEOUT)
359 344
    self._connected = False
360
    self._buf = ""
361 345

  
362 346
  def _check_socket(self):
363 347
    sock_stat = None
......
365 349
      sock_stat = os.stat(self.monitor_filename)
366 350
    except EnvironmentError, err:
367 351
      if err.errno == errno.ENOENT:
368
        raise errors.HypervisorError("No qmp socket found")
352
        raise errors.HypervisorError("No monitor socket found")
369 353
      else:
370
        raise errors.HypervisorError("Error checking qmp socket: %s",
354
        raise errors.HypervisorError("Error checking monitor socket: %s",
371 355
                                     utils.ErrnoOrStr(err))
372 356
    if not stat.S_ISSOCK(sock_stat.st_mode):
373
      raise errors.HypervisorError("Qmp socket is not a socket")
357
      raise errors.HypervisorError("Monitor socket is not a socket")
374 358

  
375 359
  def _check_connection(self):
376 360
    """Make sure that the connection is established.
377 361

  
378 362
    """
379 363
    if not self._connected:
380
      raise errors.ProgrammerError("To use a QmpConnection you need to first"
364
      raise errors.ProgrammerError("To use a MonitorSocket you need to first"
381 365
                                   " invoke connect() on it")
382 366

  
383 367
  def connect(self):
384
    """Connects to the QMP monitor.
368
    """Connects to the monitor.
385 369

  
386
    Connects to the UNIX socket and makes sure that we can actually send and
387
    receive data to the kvm instance via QMP.
370
    Connects to the UNIX socket
388 371

  
389 372
    @raise errors.HypervisorError: when there are communication errors
390
    @raise errors.ProgrammerError: when there are data serialization errors
391 373

  
392 374
    """
393 375
    if self._connected:
......
402 384
      raise errors.HypervisorError("Can't connect to qmp socket")
403 385
    self._connected = True
404 386

  
387
  def close(self):
388
    """Closes the socket
389

  
390
    It cannot be used after this call.
391

  
392
    """
393
    self.sock.close()
394

  
395

  
396
class QmpConnection(MonitorSocket):
397
  """Connection to the QEMU Monitor using the QEMU Monitor Protocol (QMP).
398

  
399
  """
400
  _FIRST_MESSAGE_KEY = "QMP"
401
  _EVENT_KEY = "event"
402
  _ERROR_KEY = "error"
403
  _RETURN_KEY = RETURN_KEY = "return"
404
  _ACTUAL_KEY = ACTUAL_KEY = "actual"
405
  _ERROR_CLASS_KEY = "class"
406
  _ERROR_DATA_KEY = "data"
407
  _ERROR_DESC_KEY = "desc"
408
  _EXECUTE_KEY = "execute"
409
  _ARGUMENTS_KEY = "arguments"
410
  _CAPABILITIES_COMMAND = "qmp_capabilities"
411
  _MESSAGE_END_TOKEN = "\r\n"
412

  
413
  def __init__(self, monitor_filename):
414
    super(QmpConnection, self).__init__(monitor_filename)
415
    self._buf = ""
416

  
417
  def connect(self):
418
    """Connects to the QMP monitor.
419

  
420
    Connects to the UNIX socket and makes sure that we can actually send and
421
    receive data to the kvm instance via QMP.
422

  
423
    @raise errors.HypervisorError: when there are communication errors
424
    @raise errors.ProgrammerError: when there are data serialization errors
425

  
426
    """
427
    super(QmpConnection, self).connect()
405 428
    # Check if we receive a correct greeting message from the server
406 429
    # (As per the QEMU Protocol Specification 0.1 - section 2.2)
407 430
    greeting = self._Recv()

Also available in: Unified diff