Commit d036f396 authored by Jakub Stachowski's avatar Jakub Stachowski

DNS-SD support for announcing and discovery of LAN games.

New network dialog.

svn path=/trunk/kdegames/lskat/; revision=371640
parent 837bc75e
......@@ -39,6 +39,8 @@
#include "KRemoteConnect.moc"
const char* LSKAT_SERVICE = "_lskat._tcp";
KRemoteConnect::KRemoteConnect()
: KChildConnect()
{
......@@ -46,6 +48,7 @@ KRemoteConnect::KRemoteConnect()
IP="localhost";
socketStatus=KR_INVALID;
kSocket=0;
service=0;
kServerSocket=0;
bufferMsg=0;
buffer=new char[4097];
......@@ -56,7 +59,8 @@ KRemoteConnect::~KRemoteConnect()
{
Exit();
delete buffer;
// printf("DESTGRUCTING KRemoteConenct\n");
delete service;
printf("DESTGRUCTING KRemoteConenct\n");
}
KR_STATUS KRemoteConnect::QueryStatus()
......@@ -84,6 +88,11 @@ bool tryserver;
IP=QCString(p);
msg->Remove(QCString("IP"));
}
if (msg->GetData(QCString("Name"),p,size))
{
Name=QString::fromUtf8(p);
msg->Remove(QCString("Name"));
}
}
/*
printf("Connecting to %s %u (remain=%d)\n",
......@@ -138,6 +147,8 @@ bool tryserver;
{
delete kSocket;
kSocket=0;
delete service;
service = 0;
// Store message
if (msg->QueryNumberOfKeys()>0)
{
......@@ -173,6 +184,9 @@ bool KRemoteConnect::OfferServerSocket()
socketStatus=KR_NO_SOCKET;
return false;
}
printf("Offering socket and publishing stuff\n");
service = new DNSSD::PublicService(Name,LSKAT_SERVICE,port);
service->publishAsync();
connect(kServerSocket,SIGNAL(accepted(KSocket *)),
this,SLOT(socketRequest(KSocket *)));
......
......@@ -22,7 +22,7 @@
#include "KRSocket.h"
//#include "ksock.h"
#include "KChildConnect.h"
#include <dnssd/publicservice.h>
......@@ -34,10 +34,12 @@ class KRemoteConnect: public KChildConnect
KRServerSocket *kServerSocket;
KSocket *kSocket;
QString IP;
QString Name;
ushort port;
KR_STATUS socketStatus;
char *buffer;
KEMessage *bufferMsg;
DNSSD::PublicService *service;
// QString inputbuffer;
// bool input_pending;
// QStrList inputcache;
......
####### kdevelop will overwrite this part!!! (begin)##########
bin_PROGRAMS = lskat
lskat_SOURCES = msgdlg.cpp networkdlg.cpp namedlg.cpp KRemoteConnect.cpp KRSocket.cpp KProcessConnect.cpp KMessageEntry.cpp KInteractiveConnect.cpp KInputChildProcess.cpp KEMessage.cpp KEInput.cpp KConnectEntry.cpp KChildConnect.cpp lskatview.cpp lskatdoc.cpp lskat.cpp main.cpp
lskat_SOURCES = msgdlg.cpp networkdlg.cpp namedlg.cpp KRemoteConnect.cpp KRSocket.cpp KProcessConnect.cpp KMessageEntry.cpp KInteractiveConnect.cpp KInputChildProcess.cpp KEMessage.cpp KEInput.cpp KConnectEntry.cpp KChildConnect.cpp lskatview.cpp lskatdoc.cpp lskat.cpp main.cpp networkdlgbase.ui
lskat_LDADD = $(LIB_KFILE) $(LIB_KDEGAMES)
lskat_LDADD = $(LIB_KFILE) $(LIB_KDEGAMES) $(LIB_KDNSSD)
lskat_DEPENDENCIES = $(LIB_KDEGAMES_DEP)
INCLUDES = -I$(top_srcdir)/libkdegames $(all_includes)
......
......@@ -655,6 +655,7 @@ bool LSkatApp::MakeInputDevice(int no)
{
QString host;
short port;
QString Name;
msg=new KEMessage;
PrepareGame(msg);
// Build new connection
......@@ -663,8 +664,10 @@ bool LSkatApp::MakeInputDevice(int no)
OptionsNetwork();
port=doc->QueryPort();
host=doc->QueryHost();
Name=doc->QueryName();
msg->AddData(QCString("Port"),(short)port);
msg->AddData(QCString("IP"),(char *)(host.latin1()));
msg->AddData(QCString("Name"),(const char *)(Name.utf8()));
res=mInput->SetInputDevice(no,type,msg);
if (!res)
{
......@@ -751,9 +754,11 @@ void LSkatApp::OptionsNetwork()
NetworkDlg *dlg=new NetworkDlg(this,QCString("Configure a network game..."));
dlg->SetPort(doc->QueryPort());
dlg->SetHost(doc->QueryHost());
dlg->SetName(doc->QueryName());
res=dlg->exec();
doc->SetPort(dlg->QueryPort());
doc->SetHost(dlg->QueryHost());
doc->SetName(dlg->QueryName());
delete dlg;
}
......
......@@ -74,6 +74,7 @@ LSkatDoc::LSkatDoc(QWidget *parent, const char *name) : QObject(parent, name)
server=false;
port=7432;
host="";
Name="";
remoteswitch=false;
......@@ -654,6 +655,10 @@ void LSkatDoc::SetHost(QString h)
{
host=h;
}
void LSkatDoc::SetName(const QString& n)
{
Name=n;
}
void LSkatDoc::SetPort(short p)
{
port=p;
......@@ -666,6 +671,10 @@ short LSkatDoc::QueryPort()
{
return port;
}
QString LSkatDoc::QueryName() const
{
return Name;
}
int *LSkatDoc::GetCardP()
{
return card;
......@@ -736,6 +745,7 @@ void LSkatDoc::ReadConfig(KConfig *config)
host=config->readEntry("host");
port=(unsigned short)config->readNumEntry("port",7432);
procfile=config->readEntry("process",QCString("lskatproc"));
Name=config->readEntry("gamename");
names[0]=config->readEntry("Name1",i18n("Alice"));
// names[1]=config->readEntry("Name2",i18n("Bob"));
names[1]=config->readEntry("Name2", name.isEmpty() ? i18n("Bob") : name);
......@@ -782,6 +792,7 @@ void LSkatDoc::WriteConfig(KConfig *config)
config->writeEntry("delpath",delpath);
config->writeEntry("Name1",names[0]);
config->writeEntry("Name2",names[1]);
config->writeEntry("gamename",Name);
config->writeEntry("Startplayer",startplayer);
config->writeEntry("Level",computerlevel);
......
......@@ -144,9 +144,11 @@ class LSkatDoc : public QObject
bool IsServer();
void SetServer(bool b);
void SetHost(QString h);
void SetName(const QString& n);
void SetPort(short p);
QString QueryHost();
short QueryPort();
QString QueryName() const;
// Only for fast remote access
int *GetCardP();
int *GetCardHeightP();
......@@ -182,6 +184,7 @@ public:
KEInput *inputHandler;
short port;
QString host;
QString Name;
bool server;
bool lock;
int startplayer;
......
/***************************************************************************
FILENAME| - description
networkdlg.cpp - description
-------------------
begin : Thu Mar 30 2000
copyright : (C) |1995-2000 by Martin Heni
email : martin@heni-online.de
copyright : (C) 2004 by Jakub Stachowski
email : qbast@go2.pl
***************************************************************************/
/***************************************************************************
......@@ -14,118 +13,93 @@
* (at your option) any later version. *
* *
***************************************************************************/
/*
Networkdlg.cpp
$Id$
Confiugre game over IP
(c) Martin Heni, martin@heni-online.de
June 1999
License: GPL
*/
#include "networkdlg.h"
#include <qgroupbox.h>
#include <kpushbutton.h>
#include <kstdguiitem.h>
#include <klocale.h>
#include <qspinbox.h>
#include <qlineedit.h>
#include <qcombobox.h>
#include <qbuttongroup.h>
#include <qwidgetstack.h>
extern const char* LSKAT_SERVICE;
// Create the dialog
NetworkDlg::NetworkDlg( QWidget *parent, const char *name,const char * /*sufi*/ )
: QDialog( parent, name,TRUE )
NetworkDlg::NetworkDlg( QWidget *parent, const char *name )
: NetworkDlgBase( parent, name, TRUE )
{
QGroupBox* grp;
KApplication *app=KApplication::kApplication();
config=app->sessionConfig();
QString s;
QLabel *Label;
QPushButton *PushButton;
setCaption(i18n("Configure Network Options"));
setMinimumSize(330,260);
setMaximumSize(330,260);
resize( 330, 260 );
grp = new QGroupBox(i18n("Remote Settings"), this);
grp->resize(290,175);
grp->move(20,10);
IPEdit = new QLineEdit( grp, "Edit_2" );
IPEdit->setGeometry( 95, 25, 170, 30 );
IPEdit->setText(i18n( "localhost") );
Label=new QLabel(grp,"&host");
Label->setGeometry(10,25,85,30);
Label->setText(i18n("Remote host" ));
PortEdit = new QLineEdit( grp, "Edit_3" );
PortEdit->setGeometry( 95, 60, 50, 30 );
PortEdit->setText(i18n( "7442" ));
PortEdit->setMaxLength(5);
Label=new QLabel(grp,"&Port");
Label->setGeometry(10,60,85,30);
Label->setText(i18n("Port" ));
Label=new QLabel(grp,"Label_3");
Label->setGeometry(10,95,270,75);
Label->setText(i18n("You can leave the remote host entry "
"empty.\nYour computer then acts as a server only and\ndoes not "
"try to connect to a remote host."));
browser = new DNSSD::ServiceBrowser(LSKAT_SERVICE);
connect(browser,SIGNAL(finished()),SLOT(gamesFound()));
browser->startBrowse();
}
NetworkDlg::~NetworkDlg()
{
delete browser;
}
PushButton = new KPushButton( KStdGuiItem::ok(), this, "PushButton_1" );
PushButton->setGeometry( 130, 210, 65, 30 );
connect( PushButton, SIGNAL(clicked()), SLOT(accept()) );
PushButton->setAutoRepeat( FALSE );
PushButton->setDefault( TRUE );
void NetworkDlg::SetHost(const QString& host)
{
hostname->setText(host);
}
/*
PushButton = new KPushButton( KStdGuiItem::cancel(), this, "PushButton_2" );
PushButton->setGeometry( 165, 210, 65, 30 );
connect( PushButton, SIGNAL(clicked()), SLOT(reject()) );
PushButton->setAutoRepeat( FALSE );
*/
void NetworkDlg::SetName(const QString& name)
{
serverName->setText(name);
}
QString NetworkDlg::QueryName() const
{
return serverName->text();
}
// setBackgroundColor(DLGBACK);
void NetworkDlg::SetPort(unsigned short port)
{
this->port->setValue(port);
}
void NetworkDlg::accept()
void NetworkDlg::gamesFound()
{
QDialog::accept();
bool autoselect=false;
if (!clientName->count() && group->selectedId()==1) autoselect=true;
clientName->clear();
QStringList names;
QValueList<DNSSD::RemoteService::Ptr>::ConstIterator itEnd = browser->services().end();
for (QValueList<DNSSD::RemoteService::Ptr>::ConstIterator it = browser->services().begin();
it!=itEnd; ++it) names << (*it)->serviceName();
clientName->insertStringList(names);
if (autoselect && clientName->count()) gameSelected(0);
}
void NetworkDlg::SetHost(QString s)
void NetworkDlg::gameSelected(int nr)
{
IPEdit->setText( s );
if (nr>=browser->services().count() || nr<0) return;
DNSSD::RemoteService::Ptr srv = browser->services()[nr];
if (!srv->isResolved() && !srv->resolve()) return;
hostname->setText(srv->hostName());
port->setValue(srv->port());
}
void NetworkDlg::SetPort(unsigned short port)
unsigned short NetworkDlg::QueryPort() const
{
QString s;
s.setNum(port);
PortEdit->setText(s);
return port->value();
}
unsigned short NetworkDlg::QueryPort()
QString NetworkDlg::QueryHost() const
{
QString s;
unsigned short port;
s=PortEdit->text();
port=(unsigned short)s.toInt();
return port;
return hostname->text();
}
QString NetworkDlg::QueryHost()
void NetworkDlg::toggleServerClient()
{
return IPEdit->text();
stack->raiseWidget(group->selectedId());
if (group->selectedId()==1) {
gameSelected(clientName->currentItem());
hostname->setEnabled(true);
}
else {
hostname->setText(QString::null);
hostname->setEnabled(false);
}
}
#include "networkdlg.moc"
/***************************************************************************
networkdlg
networkdlg.h - description
-------------------
begin : March 2000
copyright : (C) 1995-2000 by Martin Heni
email : martin@heni-online.de
copyright : (C) 2004 by Jakub Stachowski
email : qbast@go2.pl
***************************************************************************/
/***************************************************************************
......@@ -14,45 +13,36 @@
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef __NETWORKDLG_H_
#define __NETWORKDLG_H_
#include <qlineedit.h>
//#include <ktabctl.h>
//#include <qkeycode.h>
//#include <qmultilineedit.h>
//#include <unistd.h>
#include <kapplication.h>
#include <qlabel.h>
//#include <kconfig.h>
#include <qdialog.h>
//#include <qtooltip.h>
//#include <kmsgbox.h>
//#include <ktopwidget.h>
//#include <kprocess.h>
//#include <kstdaccel.h>
#include <qstring.h>
class NetworkDlg : public QDialog
{
Q_OBJECT
public:
NetworkDlg (QWidget* parent = NULL,const char* name = NULL,const char *sufi=NULL);
#ifndef NETWORKDLG_H
#define NETWORKDLG_H
#include <qstring.h>
#include <dnssd/servicebrowser.h>
#include "networkdlgbase.h"
void SetHost(QString s);
void SetPort(unsigned short port);
unsigned short QueryPort();
QString QueryHost();
protected slots:
void accept();
class NetworkDlg : public NetworkDlgBase
{
Q_OBJECT
public:
NetworkDlg(QWidget* parent=NULL, const char* name=NULL);
~NetworkDlg();
void SetName(const QString& name);
void SetHost(const QString& host);
void SetPort(unsigned short port);
QString QueryName() const;
unsigned short QueryPort() const;
QString QueryHost() const;
protected:
virtual void toggleServerClient();
virtual void gameSelected(int nr);
private slots:
void gamesFound();
private:
DNSSD::ServiceBrowser* browser;
};
protected:
KConfig *config;
KApplication *app;
QLineEdit *PortEdit;
// QLineEdit *SPortEdit;
QLineEdit *IPEdit;
#endif // NETWORKDLG_H
};
#endif
<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
<class>NetworkDlgBase</class>
<widget class="QDialog">
<property name="name">
<cstring>NetworkDlgBase</cstring>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>320</width>
<height>218</height>
</rect>
</property>
<property name="caption">
<string>Network options</string>
</property>
<vbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QButtonGroup">
<property name="name">
<cstring>group</cstring>
</property>
<property name="sizePolicy">
<sizepolicy>
<hsizetype>5</hsizetype>
<vsizetype>0</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Play as</string>
</property>
<vbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QRadioButton">
<property name="name">
<cstring>serverBtn</cstring>
</property>
<property name="text">
<string>Server</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
<widget class="QRadioButton">
<property name="name">
<cstring>clientBtn</cstring>
</property>
<property name="text">
<string>Client</string>
</property>
</widget>
</vbox>
</widget>
<widget class="QWidgetStack">
<property name="name">
<cstring>stack</cstring>
</property>
<widget class="QWidget">
<property name="name">
<cstring>serverPage</cstring>
</property>
<attribute name="id">
<number>0</number>
</attribute>
<hbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<property name="margin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<widget class="QLabel">
<property name="name">
<cstring>serverLabel</cstring>
</property>
<property name="text">
<string>Game name:</string>
</property>
<property name="buddy" stdset="0">
<cstring>serverName</cstring>
</property>
</widget>
<widget class="QLineEdit">
<property name="name">
<cstring>serverName</cstring>
</property>
<property name="sizePolicy">
<sizepolicy>
<hsizetype>1</hsizetype>
<vsizetype>0</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</hbox>
</widget>
<widget class="QWidget">
<property name="name">
<cstring>clientPage</cstring>
</property>
<attribute name="id">
<number>1</number>
</attribute>
<hbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<property name="margin">
<number>0</number>
</property>
<property name="spacing">
<number>0</number>
</property>
<widget class="QLabel">
<property name="name">
<cstring>clientLabel</cstring>
</property>
<property name="text">
<string>Network games</string>
</property>
<property name="buddy" stdset="0">
<cstring>clientName</cstring>
</property>
</widget>
<widget class="QComboBox">
<property name="name">
<cstring>clientName</cstring>
</property>
</widget>
</hbox>
</widget>
</widget>
<widget class="QLayoutWidget">
<property name="name">
<cstring>layout9</cstring>
</property>
<hbox>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QLabel">
<property name="name">
<cstring>hostLabel</cstring>
</property>
<property name="sizePolicy">
<sizepolicy>
<hsizetype>5</hsizetype>
<vsizetype>0</vsizetype>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>