~ubuntu-branches/ubuntu/trusty/komba2/trusty

« back to all changes in this revision

Viewing changes to komba2/mymaster.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jean-Michel Kelbert
  • Date: 2002-03-30 02:19:01 UTC
  • Revision ID: james.westby@ubuntu.com-20020330021901-x0twiy5zo21yhwas
Tags: upstream-0.73
ImportĀ upstreamĀ versionĀ 0.73

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          mymaster.cpp  -  description
 
3
                             -------------------
 
4
    begin                : Thu Apr 6 2000
 
5
    copyright            : (C) 2000 by 
 
6
    email                : 
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
#include <netinet/in.h>
 
18
#include <sys/socket.h>
 
19
#include <arpa/inet.h>
 
20
 
 
21
#include <kdebug.h>
 
22
 
 
23
#include "mymaster.h"
 
24
 
 
25
MyMaster::MyMaster (const QString & workgroup, QObject * parent):
 
26
QObject (parent),
 
27
proc (0),
 
28
wg (workgroup)
 
29
{
 
30
}
 
31
 
 
32
MyMaster::~MyMaster ()
 
33
{
 
34
  if (proc)
 
35
    {
 
36
      kdWarning (7199) << "MyMaster: Process not exited..." << endl;
 
37
      proc->kill ();
 
38
      delete proc;
 
39
    }
 
40
}
 
41
 
 
42
/** Liest den Masterbrowser ein */
 
43
void
 
44
MyMaster::getMaster ()
 
45
{
 
46
  proc = new KProcess ();
 
47
  if (!proc)
 
48
    {
 
49
      kdError (7199) << "MyMaster : Can't create a process." << endl;
 
50
      return;
 
51
    }
 
52
  (*proc) << "nmblookup" << "-M" << wg << "-S";
 
53
  connect (proc, SIGNAL (processExited (KProcess *)), this,
 
54
           SLOT (onProcExit (KProcess *)));
 
55
  connect (proc, SIGNAL (receivedStdout (KProcess *, char *, int)), this,
 
56
           SLOT (MyMasterStdout (KProcess *, char *, int)));
 
57
  proc->start (KProcess::NotifyOnExit, KProcess::Stdout);
 
58
  return;
 
59
}
 
60
 
 
61
 
 
62
void
 
63
MyMaster::MyMasterStdout (KProcess *, char *buffer, int n)
 
64
{
 
65
  out.append (QString::fromLocal8Bit (buffer, n));
 
66
}
 
67
 
 
68
/** Wird aufgerufen wenn der Process sich beendet */
 
69
void
 
70
MyMaster::onProcExit (KProcess * killedproc)
 
71
{
 
72
  if (killedproc == proc)
 
73
    {
 
74
      delete proc;
 
75
      proc = 0;
 
76
    }
 
77
  else
 
78
    kdWarning (7199) << "MyMaster : Mmmhhh... not my process" << endl;
 
79
  int i;
 
80
  u_long ip = 0;
 
81
  QString s = out;
 
82
  i = s.find (wg + "<1d>");     // <1d> means ->Workgroup
 
83
  // Find the IP
 
84
  if (i > 0)
 
85
    {
 
86
      s.truncate (i);
 
87
      i = s.findRev ("\n");
 
88
      if (i > 0)
 
89
        s = s.right (s.length () - i - 1);
 
90
      ip = htonl (inet_addr ((const char *) s.stripWhiteSpace ()));
 
91
      s = out;
 
92
      // Find the Mastername
 
93
      i = s.find ("<00>");      // <00> means ->Workstation
 
94
      if (i > 0)
 
95
        {
 
96
          s.truncate (i);
 
97
          i = s.findRev ("\n");
 
98
          if (i > 0)
 
99
            {
 
100
              s = s.right (s.length () - i - 1);
 
101
              s = s.stripWhiteSpace ();
 
102
            }
 
103
        }
 
104
      else
 
105
        s = "NONAMEFOUND";
 
106
      emit isMasterName (s, wg, ip);
 
107
    }
 
108
  emit isExited (this);
 
109
}