summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOthmar Gsenger <otti@anytun.org>2008-12-21 20:12:05 +0000
committerOthmar Gsenger <otti@anytun.org>2008-12-21 20:12:05 +0000
commitad6380fbfd67d618f1dcdbc3e2fd57d98e4a5c46 (patch)
treead7dd3313c6ec8bba27f9dcabe6922fb2f4a3639
parentremoved wrong logmessage from anytun-config (diff)
rewrote syncClient and anytun-showtables
-rw-r--r--src/anytun-showtables.cpp73
-rw-r--r--src/syncClient.cpp97
-rw-r--r--src/syncClient.h10
3 files changed, 71 insertions, 109 deletions
diff --git a/src/anytun-showtables.cpp b/src/anytun-showtables.cpp
index d3e8569..0ba8bdb 100644
--- a/src/anytun-showtables.cpp
+++ b/src/anytun-showtables.cpp
@@ -48,8 +48,9 @@
#include <boost/archive/text_iarchive.hpp>
-void output(ConnectionList &cl)
+void output()
{
+ ConnectionList &cl(gConnectionList);
if( !cl.empty() )
{
ConnectionMap::iterator it = cl.getBeginUnlocked();
@@ -74,7 +75,7 @@ void output(ConnectionList &cl)
{
RoutingMap::iterator it = gRoutingTable.getBeginUnlocked(type);
NetworkPrefix pref( it->first );
- std::cout << "Route: " << pref.toString() << "/" << pref.getNetworkPrefixLength() << " -> ";
+ std::cout << "Route: " << pref.toString() << "/" << (int)pref.getNetworkPrefixLength() << " -> ";
mux_t mux = it->second;
std::cout << mux << std::endl;
gRoutingTable.clear(type);
@@ -83,49 +84,43 @@ void output(ConnectionList &cl)
}
}
+void readExactly(size_t toread, std::iostream & result)
+{
+ size_t hasread = 0;
+ while (toread > hasread && std::cin.good())
+ {
+ char a[1];
+ std::cin.read(a,1);
+ result.write(a,1);
+ hasread++;
+ }
+}
+
+void readAndProcessOne()
+{
+ size_t message_lenght ;
+ std::stringstream message_lenght_stream;
+ readExactly(5,message_lenght_stream);
+ message_lenght_stream >> message_lenght;
+ std::stringstream void_stream;
+ readExactly(1,void_stream); //skip space
+ std::stringstream sync_command_stream;
+ readExactly(message_lenght, sync_command_stream);
+ //std::cout << message_lenght << std::endl;
+ //std::cout << sync_command_stream.str()<< std::endl;
+ boost::archive::text_iarchive ia(sync_command_stream);
+ SyncCommand scom(gConnectionList);
+ ia >> scom;
+}
+
int main(int argc, char* argv[])
{
int ret = 0;
- ConnectionList cl;
- std::stringstream iss_;
- int32_t missing_chars=-1;
- int32_t buffer_size_=0;
while( std::cin.good() )
{
- char c;
- std::cin.get(c);
- iss_ << c;
- buffer_size_++;
- while (1)
- {
- if(missing_chars==-1 && buffer_size_>5)
- {
- char * buffer = new char [6+1];
- iss_.read(buffer,6);
- std::stringstream tmp;
- tmp.write(buffer,6);
- tmp>>missing_chars;
- delete[] buffer;
- buffer_size_-=6;
- }
- else if( missing_chars>0 && missing_chars<=buffer_size_ )
- {
- char * buffer = new char [missing_chars+1];
- iss_.read(buffer,missing_chars);
- std::stringstream tmp;
- tmp.write(buffer,missing_chars);
- boost::archive::text_iarchive ia(tmp);
- SyncCommand scom(cl);
- ia >> scom;
- buffer_size_-=missing_chars;
- missing_chars=-1;
- output(cl);
- delete[] buffer;
- }
- else
- break;
- }
+ readAndProcessOne();
+ output();
}
return ret;
}
diff --git a/src/syncClient.cpp b/src/syncClient.cpp
index c6be0d8..2cc1f91 100644
--- a/src/syncClient.cpp
+++ b/src/syncClient.cpp
@@ -45,7 +45,7 @@
SyncClient::SyncClient(std::string hostname,std::string port)
-:hostname_( hostname),port_(port),missing_chars(-1)
+:hostname_( hostname),port_(port)
{
}
@@ -74,20 +74,7 @@ void SyncClient::run()
if (!connected)
cLog.msg(Log::PRIO_NOTICE) << "sync: connected to " << hostname_ <<":"<< port_;
connected=true;
- for (;;)
- {
- boost::array<char, 1> buf;
- boost::system::error_code error;
-
- size_t len = socket.read_some(boost::asio::buffer(buf), error);
-
- if (error == boost::asio::error::eof)
- break; // Connection closed cleanly by peer.
- else if (error)
- throw boost::system::system_error(error); // Some other error.
-
- OnRawData(buf.data(), len);
- }
+ readAndProcess(socket); //endless loop
}
catch (std::exception& e)
{
@@ -99,59 +86,41 @@ void SyncClient::run()
}
}
-void SyncClient::OnRawData(const char *buf,size_t len)
-//void SyncClientSocket::OnLine(const std::string& line)
+void SyncClient::readAndProcess(SyncTcpConnection::proto::socket & socket)
{
ConnectionList & cl_ (gConnectionList);
- for(size_t index=0;index<len;index++)
+ size_t message_lenght ;
+ for (;;)
{
-// std::cout << buf[index];
- iss_ << buf[index];
- buffer_size_++;
+ std::stringstream message_lenght_stream;
+ readExactly(socket,5,message_lenght_stream);
+ message_lenght_stream >> message_lenght;
+ std::stringstream void_stream;
+ readExactly(socket,1,void_stream); //skip space
+ std::stringstream sync_command_stream;
+ readExactly(socket,message_lenght, sync_command_stream);
+ //cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<<tmp.str()<< std::endl;
+ boost::archive::text_iarchive ia(sync_command_stream);
+ SyncCommand scom(cl_);
+ ia >> scom;
}
- while (1)
+}
+
+void SyncClient::readExactly(SyncTcpConnection::proto::socket & socket,size_t toread, std::iostream & result)
+{
+ size_t hasread = 0;
+ while (toread > hasread)
{
-// cLog.msg(Log::PRIO_NOTICE) << "buffer size "<< buffer_size_ << " missing_chars " << missing_chars;
- if(missing_chars==-1 && buffer_size_>5)
- {
- char * buffer = new char [6+1];
- iss_.read(buffer,6);
- std::stringstream tmp;
- tmp.write(buffer,6);
- tmp>>missing_chars;
-// cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion "<<tmp.str()<<"bytes of data"<< std::endl;
- delete[] buffer;
- buffer_size_-=6;
- } else
- if(missing_chars>0 && missing_chars<=buffer_size_)
- {
- char * buffer = new char [missing_chars+1];
- iss_.read(buffer,missing_chars);
- std::stringstream tmp;
- tmp.write(buffer,missing_chars);
-// cLog.msg(Log::PRIO_NOTICE) << "recieved sync inforamtaion from " << GetRemoteHostname() <<" \""<<tmp.str()<<'"'<< std::endl;
- boost::archive::text_iarchive ia(tmp);
- SyncCommand scom(cl_);
- ia >> scom;
- buffer_size_-=missing_chars;
- missing_chars=-1;
- delete[] buffer;
- } else
- break;
+ //TODO read bigger buffers
+ boost::array<char, 1> buf;
+ boost::system::error_code error;
+ size_t len = socket.read_some(boost::asio::buffer(buf), error);
+ if (error == boost::asio::error::eof)
+ break; // Connection closed cleanly by peer.
+ else if (error)
+ throw boost::system::system_error(error); // Some other error.
+ //for (size_t pos=0; pos<len; pos++)
+ result<<buf[0];
+ hasread+=len;
}
-
- //u_int16_t mux = scom.getMux();
- //const ConnectionParam & conn = cl_.getConnection(mux)->second;
- //cLog.msg(Log::PRIO_NOTICE) << "sync connection #"<<mux<<" remote host " << conn.remote_host_ << ":" << conn.remote_port_ << std::endl;
}
-
-//void StatusClientSocket::InitSSLServer()
-//{
-// InitializeContext("server.pem", "keypwd", SSLv23_method());
-//}
-//
-//
-//void StatusClientSocket::Init()
-//{
-// EnableSSL();
-//}
diff --git a/src/syncClient.h b/src/syncClient.h
index 0714e9b..26c946f 100644
--- a/src/syncClient.h
+++ b/src/syncClient.h
@@ -36,8 +36,8 @@
#include <sstream>
#include <iostream>
#include <string>
-#include <boost/asio.hpp>
-
+//#include <boost/asio.hpp>
+#include "syncTcpConnection.h"
class SyncClient
{
public:
@@ -45,12 +45,10 @@ public:
void run();
private:
- void OnRawData(const char *buf,size_t len);
+ void readAndProcess(SyncTcpConnection::proto::socket & socket);
+ void readExactly(SyncTcpConnection::proto::socket & socket,size_t toread, std::iostream &);
std::string hostname_;
std::string port_;
- std::stringstream iss_;
- int32_t missing_chars;
- int32_t buffer_size_;
};