ソースを参照

Veränderte Datenbank angepasst

Kolja Strohm 7 年 前
コミット
939b89a420
5 ファイル変更63 行追加188 行削除
  1. 1 10
      ChatServer/ChatServer.cpp
  2. 10 6
      ChatServer/ChatServer.vcxproj
  3. 35 156
      ChatServer/Datenbank.cpp
  4. 4 6
      ChatServer/Datenbank.h
  5. 13 10
      ChatServer/main.cpp

+ 1 - 10
ChatServer/ChatServer.cpp

@@ -20,19 +20,11 @@ ChatServer::ChatServer( InitDatei *zIni )
 	gesendet = 0;
 	fehler = new Text();
 	ini = zIni->getThis();
-	if( !db->serverAnmelden( zIni ) )
-	{
-		std::cout << "CS: Der Server konnte nicht in die Datenbank eingetragen werden:\n";
-		Text *txt = db->getLetzterFehler();
-		std::cout << txt->getText() << "\nDas Programm wird beendet.";
-		txt->release();
-		exit( 1 );
-	}
 	id = *zIni->zWert( "ServerId" );
 	server = new Server();
 	aServer = new Server();
 	std::cout << "CS: Starten des Admin Servers...\n";
-	if( !aServer->verbinde( (unsigned short)db->getAdminPort( id ), 10 ) )
+	if( !aServer->verbinde( (unsigned short)TextZuInt( ini->zWert( "AdminServerPort" )->getText(), 10 ), 10 ) )
 	{
 		std::cout << "CS: Der Admin Server konnte nicht gestartet werden. Das Programm wird beendet.\n";
 		exit( 1 );
@@ -47,7 +39,6 @@ ChatServer::ChatServer( InitDatei *zIni )
 		serverStarten();
 		serverFortsetzen();
 	}
-	ref = 1;
 }
 
 // Destruktor 

+ 10 - 6
ChatServer/ChatServer.vcxproj

@@ -63,14 +63,18 @@
   <ImportGroup Label="PropertySheets" />
   <PropertyGroup Label="UserMacros" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
-    <RemoteProjectDir>$(RemoteRootDir)/Server/$(ProjectName)/Debug</RemoteProjectDir>
-    <IncludePath>..\..\..\..\Allgemein\Framework;..\..\..\..\Allgemein\Network\Network;..\..\..\..\Allgemein\sql\sql;../../../Framework/Debug;../../../Network/Debug;../../../sql/Debug;$(IncludePath)</IncludePath>
+    <RemoteProjectDir>$(RemoteRootDir)/Server/$(ProjectName)/debug</RemoteProjectDir>
+    <IncludePath>..\..\..\..\Allgemein\Framework;..\..\..\..\Allgemein\Network\Network;..\..\..\..\Allgemein\sql\sql;../../../Framework/debug;../../../Network/debug;../../../sql/debug;$(IncludePath)</IncludePath>
     <TargetExt />
+    <OutDir>$(ProjectDir)bin\$(Platform)\debug\</OutDir>
+    <IntDir>$(ProjectDir)obj\$(Platform)\debug\</IntDir>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
-    <RemoteProjectDir>$(RemoteRootDir)/Server/$(ProjectName)/Release</RemoteProjectDir>
-    <IncludePath>..\..\..\..\Allgemein\Framework;..\..\..\..\Allgemein\Network\Network;..\..\..\..\Allgemein\sql\sql;../../../Framework/Release;../../../Network/Release;../../../sql/Release;$(IncludePath)</IncludePath>
+    <RemoteProjectDir>$(RemoteRootDir)/Server/$(ProjectName)/release</RemoteProjectDir>
+    <IncludePath>..\..\..\..\Allgemein\Framework;..\..\..\..\Allgemein\Network\Network;..\..\..\..\Allgemein\sql\sql;../../../Framework/release;../../../Network/release;../../../sql/release;$(IncludePath)</IncludePath>
     <TargetExt />
+    <OutDir>$(ProjectDir)bin\$(Platform)\release\</OutDir>
+    <IntDir>$(ProjectDir)obj\$(Platform)\release\</IntDir>
   </PropertyGroup>
   <ItemGroup>
     <ClCompile Include="ChatServer.cpp" />
@@ -84,7 +88,7 @@
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
     <Link>
       <LibraryDependencies>dbgFramework;dbgNetwork;dbgSQL;pq;pthread</LibraryDependencies>
-      <AdditionalLibraryDirectories>$(RemoteRootDir)/sql/Debug/bin/x64/debug;$(RemoteRootDir)/Network/Debug/bin/x64/debug;$(RemoteRootDir)/Framework/Debug/bin/x64/debug;/usr/lib/;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>$(RemoteRootDir)/sql/debug/bin/x64/debug;$(RemoteRootDir)/Network/debug/bin/x64/debug;$(RemoteRootDir)/Framework/debug/bin/x64/debug;/usr/lib/;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <AdditionalOptions>-Wl,-rpath,../lib %(AdditionalOptions)</AdditionalOptions>
     </Link>
     <ClCompile>
@@ -94,7 +98,7 @@
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
     <Link>
       <LibraryDependencies>Framework;Network;SQL;pq;pthread</LibraryDependencies>
-      <AdditionalLibraryDirectories>$(RemoteRootDir)/sql/Release/bin/x64/release;$(RemoteRootDir)/Network/Release/bin/x64/release;$(RemoteRootDir)/Framework/Release/bin/x64/release;/usr/lib/;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+      <AdditionalLibraryDirectories>$(RemoteRootDir)/sql/release/bin/x64/release;$(RemoteRootDir)/Network/release/bin/x64/release;$(RemoteRootDir)/Framework/release/bin/x64/release;/usr/lib/;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <AdditionalOptions>-Wl,-rpath,../lib %(AdditionalOptions)</AdditionalOptions>
     </Link>
     <ClCompile>

+ 35 - 156
ChatServer/Datenbank.cpp

@@ -1,24 +1,32 @@
 #include "Datenbank.h"
+#include <iostream>
 
 // Inhalt der CSDatenbank Klasse aus Datenbank.h
 // Konstruktor
 CSDatenbank::CSDatenbank( InitDatei *zIni )
 {
-	if( !zIni->wertExistiert( "DBBenutzer" ) )
-		zIni->addWert( "DBBenutzer", "chatserveru" );
-	if( !zIni->wertExistiert( "DBPasswort" ) )
-		zIni->addWert( "DBPasswort", "LTChatServerPW" );
-	if( !zIni->wertExistiert( "DBName" ) )
-		zIni->addWert( "DBName", "lenck_tech_db" );
-	if( !zIni->wertExistiert( "DBIP" ) )
-		zIni->addWert( "DBIP", "127.0.0.1" );
-	if( !zIni->wertExistiert( "DBPort" ) )
-		zIni->addWert( "DBPort", "5432" );
 	datenbank = new Datenbank( zIni->zWert( "DBBenutzer" )->getText(), zIni->zWert( "DBPasswort" )->getText(),
 							   zIni->zWert( "DBName" )->getText(), zIni->zWert( "DBIP" )->getText(),
 							   (unsigned short)TextZuInt( zIni->zWert( "DBPort" )->getText(), 10 ) );
+    if( !datenbank->istOk() )
+    {
+        std::cout << "CS: Die Verbindung zur Datenbank konnte nicht hergestellt werden.\nDas Programm wird beendet.";
+        exit( 1 );
+    }
 	InitializeCriticalSection( &cs );
 	ref = 1;
+    Text befehl = "SELECT port, admin_port  FROM server WHERE id = ";
+    befehl += zIni->zWert( "ServerId" )->getText();
+    lock();
+    datenbank->befehl( befehl );
+    Result res = datenbank->getResult();
+    unlock();
+    if( res.zeilenAnzahl == 1 )
+    {
+        zIni->addWert( "ServerPort", res.values[ 0 ] );
+        zIni->addWert( "AdminServerPort", res.values[ 1 ] );
+    }
+    res.destroy();
 }
 
 // Destruktor
@@ -45,11 +53,11 @@ int CSDatenbank::istAdministrator( const char *name, const char *passwort )
 	Text n( name );
 	n.ersetzen( "'", "''" );
 	befehl->append( (char*)n );
-	befehl->append( "' AND passwort = '" );
+	befehl->append( "' AND passwort = md5('" );
 	Text p( passwort );
 	p.ersetzen( "'", "''" );
 	befehl->append( (char*)p );
-	befehl->append( "'" );
+	befehl->append( "')" );
 	lock();
 	datenbank->befehl( befehl->getText() );
 	Result res = datenbank->getResult();
@@ -78,7 +86,7 @@ bool CSDatenbank::adminHatRecht( int id, int recht )
 
 bool CSDatenbank::proveKlient( int num, int sNum )
 {
-	Text *befehl = new Text( "SELECT * FROM server_chat_clients WHERE server_chat_id = " );
+	Text *befehl = new Text( "SELECT * FROM server_client WHERE server_id = " );
 	befehl->append( sNum );
 	befehl->append( " AND client_id = " );
 	befehl->append( num );
@@ -97,7 +105,7 @@ bool CSDatenbank::proveKlient( int num, int sNum )
 Text *CSDatenbank::getKlientKey( int cId )
 {
 	lock();
-	if( !datenbank->befehl( Text( "SELECT schluessel FROM clients WHERE id = " ) += cId ) )
+	if( !datenbank->befehl( Text( "SELECT schluessel FROM client WHERE id = " ) += cId ) )
 	{
 		unlock();
 		return 0;
@@ -116,9 +124,9 @@ Text *CSDatenbank::getKlientKey( int cId )
 
 void CSDatenbank::unregisterKlient( int num, int sNum )
 {
-	Text *befehl = new Text( "DELETE FROM server_chat_clients WHERE client_id = " );
+	Text *befehl = new Text( "DELETE FROM server_client WHERE client_id = " );
 	befehl->append( num );
-	befehl->append( " AND server_chat_id = " );
+	befehl->append( " AND server_id = " );
 	befehl->append( sNum );
 	lock();
 	datenbank->befehl( befehl->getText() );
@@ -126,7 +134,7 @@ void CSDatenbank::unregisterKlient( int num, int sNum )
 	unlock();
 	if( za == 1 )
 	{
-		befehl->setText( "UPDATE server_chat SET clients = clients - 1 WHERE id = " );
+		befehl->setText( "UPDATE server SET tasks = tasks - 1 WHERE id = " );
 		befehl->append( sNum );
 		lock();
 		datenbank->befehl( befehl->getText() );
@@ -135,114 +143,9 @@ void CSDatenbank::unregisterKlient( int num, int sNum )
 	befehl->release();
 }
 
-bool CSDatenbank::serverAnmelden( InitDatei *zIni )
-{
-	if( !zIni->wertExistiert( "ServerId" ) )
-		zIni->addWert( "ServerId", "0" );
-	if( !zIni->wertExistiert( "ServerName" ) )
-		zIni->addWert( "ServerName", "Name" );
-	if( !zIni->wertExistiert( "ServerPort" ) )
-		zIni->addWert( "ServerPort", "49144" );
-	if( !zIni->wertExistiert( "ServerIP" ) )
-		zIni->addWert( "ServerIP", "127.0.0.1" );
-	if( !zIni->wertExistiert( "AdminServerPort" ) )
-		zIni->addWert( "AdminServerPort", "49143" );
-	if( !zIni->wertExistiert( "Aktiv" ) )
-		zIni->addWert( "Aktiv", "FALSE" );
-	if( !zIni->wertExistiert( "MaxClients" ) )
-		zIni->addWert( "MaxClients", "50" );
-	bool insert = 0;
-	int id = *zIni->zWert( "ServerId" );
-	if( id )
-	{
-		lock();
-		if( !datenbank->befehl( Text( "SELECT id FROM server_chat WHERE id = " ) += id ) )
-		{
-			unlock();
-			return 0;
-		}
-		int anz = datenbank->getZeilenAnzahl();
-		unlock();
-		insert = anz == 0;
-		if( !insert )
-		{
-			lock();
-			if( !datenbank->befehl( Text( "SELECT id FROM server_chat WHERE server_status_id = 1 AND id = " ) += id ) )
-			{
-				unlock();
-				return 0;
-			}
-			int anz = datenbank->getZeilenAnzahl();
-			unlock();
-			if( !anz ) // Server läuft bereits
-				return 0;
-		}
-	}
-	if( insert || !id )
-	{ // Neuer Eintrag in Tabelle server_chat
-		Text *befehl = new Text( "INSERT INTO server_chat( " );
-		if( id )
-			*befehl += "id, ";
-		*befehl += "name, ip, port, admin_port, server_status_id, max_clients ) VALUES( ";
-		if( id )
-		{
-			*befehl += id;
-			*befehl += ", ";
-		}
-		*befehl += "'";
-		*befehl += zIni->zWert( "ServerName" )->getText();
-		*befehl += "', '";
-		*befehl += zIni->zWert( "ServerIP" )->getText();
-		*befehl += "', ";
-		*befehl += zIni->zWert( "ServerPort" )->getText();
-		*befehl += ", ";
-		*befehl += zIni->zWert( "AdminServerPort" )->getText();
-		*befehl += ", 1, ";
-		*befehl += zIni->zWert( "MaxClients" )->getText();
-		*befehl += " ) RETURNING id";
-		lock();
-		if( !datenbank->befehl( *befehl ) )
-		{
-			unlock();
-			befehl->release();
-			return 0;
-		}
-		Result res = datenbank->getResult();
-		unlock();
-		befehl->release();
-		if( !res.zeilenAnzahl )
-		{
-			res.destroy();
-			return 0;
-		}
-		zIni->setWert( "ServerId", res.values[ 0 ] );
-		return 1;
-	}
-	else
-	{ // Alten Eintrag aus Tabelle server_chat ändern
-		Text *befehl = new Text( "UPDATE server_chat SET name = '" );
-		*befehl += zIni->zWert( "ServerName" )->getText();
-		*befehl += "', port = ";
-		*befehl += zIni->zWert( "ServerPort" )->getText();
-		*befehl += ", ip = '";
-		*befehl += zIni->zWert( "ServerIP" )->getText();
-		*befehl += "', max_clients = ";
-		*befehl += zIni->zWert( "MaxClients" )->getText();
-		*befehl += ", admin_port = ";
-		*befehl += zIni->zWert( "AdminServerPort" )->getText();
-		*befehl += " WHERE id = ";
-		*befehl += id;
-		lock();
-		bool ret = datenbank->befehl( *befehl );
-		unlock();
-		befehl->release();
-		return ret;
-	}
-}
-
 bool CSDatenbank::setServerStatus( int id, int status )
 {
-	Text *befehl = new Text( "UPDATE server_chat SET server_status_id = " );
+	Text *befehl = new Text( "UPDATE server SET server_status_id = " );
 	*befehl += status;
 	*befehl += "WHERE id = ";
 	*befehl += id;
@@ -261,7 +164,7 @@ bool CSDatenbank::setServerStatus( int id, int status )
 
 bool CSDatenbank::setMaxClients( int id, int maxC )
 {
-	Text *befehl = new Text( "UPDATE server_chat SET max_clients = " );
+	Text *befehl = new Text( "UPDATE server SET max_tasks = " );
 	befehl->append( maxC );
 	befehl->append( " WHERE id = " );
 	befehl->append( id );
@@ -278,33 +181,9 @@ bool CSDatenbank::setMaxClients( int id, int maxC )
 	return ret;
 }
 
-int CSDatenbank::getAdminPort( int id )
-{
-	Text *befehl = new Text( "SELECT admin_port FROM server_chat WHERE id = " );
-	befehl->append( id );
-	lock();
-	if( !datenbank->befehl( befehl->getText() ) )
-	{
-		unlock();
-		befehl->release();
-		return 0;
-	}
-	Result res = datenbank->getResult();
-	unlock();
-	befehl->release();
-	if( !res.zeilenAnzahl )
-	{
-		res.destroy();
-		return 0;
-	}
-	int ret = TextZuInt( res.values[ 0 ].getText(), 10 );
-	res.destroy();
-	return ret;
-}
-
 bool CSDatenbank::serverIstNichtPausiert( int id )
 {
-	Text *befehl = new Text( "SELECT server_status_id FROM server_chat WHERE id = " );
+	Text *befehl = new Text( "SELECT server_status_id FROM server WHERE id = " );
 	befehl->append( id );
 	lock();
 	if( !datenbank->befehl( befehl->getText() ) )
@@ -328,7 +207,7 @@ bool CSDatenbank::serverIstNichtPausiert( int id )
 
 int CSDatenbank::getKlientAccountId( int klientId )
 {
-	Text *befehl = new Text( "SELECT account_id FROM account_clients WHERE client_id = " );
+	Text *befehl = new Text( "SELECT account_id FROM account_client WHERE client_id = " );
 	befehl->append( klientId );
 	lock();
 	datenbank->befehl( befehl->getText() );
@@ -365,10 +244,10 @@ int CSDatenbank::getAccountFreunde( int accountId, Array< int > *fAccountId )
 
 int CSDatenbank::getAccountOnlineFreunde( int accountId, Array< int > *fAccountId )
 {
-	Text *befehl = new Text( "SELECT freund.account_id AS freundId FROM freund, account_clients WHERE freund.freund_account_id = " );
+	Text *befehl = new Text( "SELECT freund.account_id AS freundId FROM freund, account_client WHERE freund.freund_account_id = " );
 	befehl->append( accountId );
 	befehl->append( " AND freund.account_id = account_clients.account_id "
-					  "UNION SELECT freund.freund_account_id AS freundId FROM freund, account_clients WHERE freund.account_id = " );
+					  "UNION SELECT freund.freund_account_id AS freundId FROM freund, account_client WHERE freund.account_id = " );
 	befehl->append( accountId );
 	befehl->append( " AND freund.freund_account_id = account_clients.account_id " );
 	lock();
@@ -550,7 +429,7 @@ Text *CSDatenbank::getAccountRufName( int accountId )
 
 bool CSDatenbank::accountIstOnline( int accountId )
 {
-	Text *befehl = new Text( "SELECT * FROM account_clients WHERE account_id = " );
+	Text *befehl = new Text( "SELECT * FROM account_client WHERE account_id = " );
 	befehl->append( accountId );
 	lock();
 	datenbank->befehl( befehl->getText() );
@@ -575,8 +454,8 @@ bool CSDatenbank::accountIstImSpiel( int accountId )
 
 int CSDatenbank::getChatServerId( int accountId )
 {
-	Text *befehl = new Text( "SELECT server_chat_clients.server_chat_id FROM server_chat_clients, account_clients "
-							 "WHERE server_chat_clients.client_id = account_clients.client_id AND account_clients.account_id = " );
+	Text *befehl = new Text( "SELECT a.server_id FROM server_client a, account_client b, server c "
+							 "WHERE a.client_id = b.client_id AND and a.server_id = c.id AND c.server_typ_name = 'chat' AND b.account_id = " );
 	befehl->append( accountId );
 	lock();
 	datenbank->befehl( befehl->getText() );
@@ -595,7 +474,7 @@ int CSDatenbank::getChatServerId( int accountId )
 
 bool CSDatenbank::getChatServerIpPort( int serverId, unsigned short *port, char **ip )
 {
-	Text *befehl = new Text( "SELECT port, ip FROM server_chat WHERE id = " );
+	Text *befehl = new Text( "SELECT port, ip FROM server WHERE id = " );
 	befehl->append( serverId );
 	lock();
 	datenbank->befehl( befehl->getText() );

+ 4 - 6
ChatServer/Datenbank.h

@@ -11,10 +11,10 @@ using namespace sql;
 
 namespace Admin_Recht
 {
-	const int CSStarten = 0x00000018;
-	const int CSBeenden = 0x00000019;
-	const int CSPausieren = 0x0000001A;
-	const int CSMCChange = 0x0000001B;
+	const int CSStarten = 24;
+	const int CSBeenden = 25;
+	const int CSPausieren = 26;
+	const int CSMCChange = 27;
 }
 
 class CSDatenbank
@@ -37,10 +37,8 @@ public:
 	bool proveKlient( int num, int sNum );
 	Text *getKlientKey( int cId );
 	void unregisterKlient( int num, int sNum );
-	bool serverAnmelden( InitDatei *zIni );
 	bool setServerStatus( int id, int status );
 	bool setMaxClients( int id, int maxC );
-	int getAdminPort( int id );
 	bool serverIstNichtPausiert( int id );
 	int getKlientAccountId( int klientId );
 	int getAccountFreunde( int accountId, Array< int > *fAccountId );

+ 13 - 10
ChatServer/main.cpp

@@ -1,20 +1,15 @@
 #include "ChatServer.h"
-#ifdef WIN32
-#include <main.h>
-#endif
 #include <Zeit.h>
 #include <iostream>
 #include <fstream>
 #include <Globals.h>
 
-#ifdef WIN32
-int KSGStart Framework::Start( Startparam p )
-{
-#else
 int main()
 {
-    Framework::initFramework();
+#ifdef DEBUG
+    cd( "/test/anmeldung" );
 #endif
+    Framework::initFramework();
 	Zeit *z = getZeit();
 	Text *pfad = new Text( "../log/chat/" );
 	pfad->append( z->getZeit( "y-m-d h-i-s.log" ) );
@@ -35,6 +30,16 @@ int main()
 		dat->release();
 		exit( 1 );
 	}
+    const char *wichtig[] = { "ServerId", "DBBenutzer", "DBPasswort", "DBName", "DBIP", "DBPort", "Aktiv" };
+    for( const char *w : wichtig )
+    {
+        if( !dat->wertExistiert( w ) )
+        {
+            std::cout << "CS: error: Der Wert '" << w << "' wurde nicht gefunden. Das Programm wird geschlossen.\n";
+            dat->release();
+            exit( 1 );
+        }
+    }
 
 	ChatServer *cServer = new ChatServer( dat );
 
@@ -48,8 +53,6 @@ int main()
 	std::cout << "CS: Der Server ist heruntergefahren.\n";
 	file.close();
 	std::cout.rdbuf( sbuf );
-#ifndef WIN32
     Framework::releaseFramework();
-#endif
 	return 0;
 }