mapeditor.cpp
changeset 531 be24af55da40
parent 528 36d140349897
child 534 32b71ba642aa
     1.1 --- a/mapeditor.cpp	Fri Jun 29 09:43:53 2007 +0000
     1.2 +++ b/mapeditor.cpp	Fri Jun 29 09:43:53 2007 +0000
     1.3 @@ -149,6 +149,9 @@
     1.4  	// autosave
     1.5  	autosaveTimer=new QTimer (this);
     1.6  	connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
     1.7 +
     1.8 +	// Network
     1.9 +	netstate=Offline;
    1.10  }
    1.11  
    1.12  MapEditor::~MapEditor()
    1.13 @@ -1549,8 +1552,6 @@
    1.14  
    1.15  int MapEditor::save (const SaveMode &savemode)
    1.16  {
    1.17 -	int returnCode=0;
    1.18 -
    1.19  	// Create mapName and fileDir
    1.20  	makeSubDirs (fileDir);
    1.21  	QString fname;
    1.22 @@ -1572,20 +1573,17 @@
    1.23  			saveFloatImage();
    1.24  		else	
    1.25  			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),xelection.getBranch());	
    1.26 +		return 0;	
    1.27  	}	
    1.28  
    1.29  	if (!saveStringToDisk(fileDir+fname,saveFile))
    1.30  		return 1;
    1.31  
    1.32 -	if (returnCode==0)
    1.33 -	{
    1.34 -		mapChanged=false;
    1.35 -		mapUnsaved=false;
    1.36 -		autosaveTimer->stop();
    1.37 -		updateActions();
    1.38 -	}
    1.39 -
    1.40 -	return returnCode;
    1.41 +	mapChanged=false;
    1.42 +	mapUnsaved=false;
    1.43 +	autosaveTimer->stop();
    1.44 +	updateActions();
    1.45 +	return 0;
    1.46  }
    1.47  
    1.48  void MapEditor::setZipped (bool z)
    1.49 @@ -2448,6 +2446,15 @@
    1.50  				QString ("Add new branch to %1").arg(getName(bo)));	
    1.51  
    1.52  			mapCenter->reposition();
    1.53 +			// In Network mode, the client needs to know where the new branch is,
    1.54 +			// so we have to pass on this information via saveState.
    1.55 +			// TODO: Get rid of this positioning workaround
    1.56 +			QString ps=qpointfToString (newbo->getAbsPos());
    1.57 +			saveState(
    1.58 +				newbo, "move "+ps, 
    1.59 +				newbo, "move "+ps, 
    1.60 +				QString("Move %1 to  %2").arg(getName(newbo)).arg(ps));
    1.61 +
    1.62  			xelection.update();
    1.63  		}
    1.64  	}	
    1.65 @@ -4689,7 +4696,7 @@
    1.66  			event->acceptProposedAction();
    1.67  }
    1.68  
    1.69 -void MapEditor::dragMoveEvent(QDragMoveEvent *event)
    1.70 +void MapEditor::dragMoveEvent(QDragMoveEvent *)
    1.71  {
    1.72  }
    1.73  
    1.74 @@ -4776,33 +4783,46 @@
    1.75  
    1.76  void MapEditor::sendSelection()
    1.77  {
    1.78 -	sendData (xelection.getSelectString() );
    1.79 +	if (netstate!=Server) return;
    1.80 +	sendData (QString("select (\"%1\")").arg(xelection.getSelectString()) );
    1.81  }
    1.82  
    1.83  void MapEditor::newServer()
    1.84  {
    1.85 +	port=54321;
    1.86 +	sendCounter=0;
    1.87      tcpServer = new QTcpServer(this);
    1.88 -    if (!tcpServer->listen(QHostAddress::Any,54321)) {
    1.89 +    if (!tcpServer->listen(QHostAddress::Any,port)) {
    1.90          QMessageBox::critical(this, "vym server",
    1.91                                QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
    1.92          close();
    1.93          return;
    1.94      }
    1.95  	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
    1.96 +	netstate=Server;
    1.97  	if (debug)
    1.98  		cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
    1.99  }
   1.100  
   1.101 +void MapEditor::connectToServer()
   1.102 +{
   1.103 +	port=54321;
   1.104 +	server="salam.suse.de";
   1.105 +	server="localhost";
   1.106 +	clientSocket = new QTcpSocket (this);
   1.107 +	clientSocket->abort();
   1.108 +    clientSocket->connectToHost(server ,port);
   1.109 +	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
   1.110 +    connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
   1.111 +            this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
   1.112 +	netstate=Client;		
   1.113 +	cout<<"connected to "<<server.ascii()<<" port "<<port<<endl;
   1.114 +
   1.115 +	
   1.116 +}
   1.117 +
   1.118  void MapEditor::newClient()
   1.119  {
   1.120 -    QByteArray block;
   1.121 -    QDataStream out(&block, QIODevice::WriteOnly);
   1.122 -    out.setVersion(QDataStream::Qt_4_0);
   1.123 -    out << (quint16)0;
   1.124 -    out << xelection.getSelectString();
   1.125 -    out.device()->seek(0);
   1.126 -    out << (quint16)(block.size() - sizeof(quint16));
   1.127 -
   1.128      QTcpSocket *newClient = tcpServer->nextPendingConnection();
   1.129      connect(newClient, SIGNAL(disconnected()),
   1.130              newClient, SLOT(deleteLater()));
   1.131 @@ -4810,9 +4830,6 @@
   1.132  	cout <<"ME::newClient  at "<<newClient->peerAddress().toString().ascii()<<endl;
   1.133  
   1.134  	clientList.append (newClient);
   1.135 -
   1.136 -    newClient->write(block);
   1.137 -    //newClient->disconnectFromHost();
   1.138  }
   1.139  
   1.140  
   1.141 @@ -4826,19 +4843,81 @@
   1.142  	// Reserve some space for blocksize
   1.143      out << (quint16)0;
   1.144  
   1.145 +	// Write sendCounter
   1.146 +    out << sendCounter++;
   1.147 +
   1.148  	// Write data
   1.149      out << s;
   1.150  
   1.151  	// Go back and write blocksize so far
   1.152      out.device()->seek(0);
   1.153 -    out << (quint16)(block.size() - sizeof(quint16));
   1.154 +    quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
   1.155 +	out << bs;
   1.156 +
   1.157 +	cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<s.ascii()<<endl;
   1.158  
   1.159  	for (int i=0; i<clientList.size(); ++i)
   1.160  	{
   1.161 -		cout << "Sending to "<<clientList.at(i)->peerAddress().toString().ascii()<<endl;
   1.162 +		//cout << "Sending \""<<s.ascii()<<"\" to "<<clientList.at(i)->peerAddress().toString().ascii()<<endl;
   1.163  		clientList.at(i)->write (block);
   1.164  	}
   1.165  }
   1.166 +
   1.167 +void MapEditor::readData ()
   1.168 +{
   1.169 +	cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable()<<endl;
   1.170 +    quint16 recCounter;
   1.171 +	quint16 blockSize;
   1.172 +
   1.173 +    QDataStream in(clientSocket);
   1.174 +    in.setVersion(QDataStream::Qt_4_0);
   1.175 +
   1.176 +    in >> blockSize;
   1.177 +    in >> recCounter;
   1.178 +	
   1.179 +	char *sp; 
   1.180 +	uint l;
   1.181 +	in.readBytes (sp,l);
   1.182 +	cout << "  sp="<<sp<<"  l="<<l<<endl;
   1.183 +	QString t;
   1.184 +	in >>t;
   1.185 +	cout << "  t="<<t.ascii()<<endl;
   1.186 +
   1.187 +	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
   1.188 +	{
   1.189 +		//cout <<"readData 1 bytesAvail="<<clientSocket->bytesAvailable()<<"  readData bS="<<blockSize<<"  rC="<<recCounter<<endl;
   1.190 +		QString s;
   1.191 +		in >> s;
   1.192 +		//cout << "Received: "<< recCounter<< "  "<<s.ascii() <<endl;
   1.193 +	cout <<"  bytesAvail="<<clientSocket->bytesAvailable()<<"   bS="<<blockSize<<"  counter="<<recCounter<<"  s="<<s.ascii()<<endl;
   1.194 +		parseAtom (s);
   1.195 +	}
   1.196 +}
   1.197 +
   1.198 +void MapEditor::displayNetworkError(QAbstractSocket::SocketError socketError)
   1.199 +{
   1.200 +    switch (socketError) {
   1.201 +    case QAbstractSocket::RemoteHostClosedError:
   1.202 +        break;
   1.203 +    case QAbstractSocket::HostNotFoundError:
   1.204 +        QMessageBox::information(this, __VYM_NAME " Network client",
   1.205 +                                 "The host was not found. Please check the "
   1.206 +                                    "host name and port settings.");
   1.207 +        break;
   1.208 +    case QAbstractSocket::ConnectionRefusedError:
   1.209 +        QMessageBox::information(this, __VYM_NAME " Network client",
   1.210 +                                 "The connection was refused by the peer. "
   1.211 +                                    "Make sure the fortune server is running, "
   1.212 +                                    "and check that the host name and port "
   1.213 +                                    "settings are correct.");
   1.214 +        break;
   1.215 +    default:
   1.216 +        QMessageBox::information(this, __VYM_NAME " Network client",
   1.217 +                                 QString("The following error occurred: %1.")
   1.218 +                                 .arg(clientSocket->errorString()));
   1.219 +    }
   1.220 +}
   1.221 +
   1.222  void MapEditor::autosave()
   1.223  {
   1.224  	// Disable autosave, while we have gone back in history