Server.cpp
Go to the documentation of this file.
00001 #include <qt4/QtNetwork/QtNetwork>
00002 
00003 #include "Server.h"
00004 namespace server {
00005 
00006 Server::Server(int argc, char** argv, QObject* pParent)
00007     :   QObject(pParent),
00008       m_RobotThread(argc, argv)
00009 {
00010 
00011     m_pTcpServer = new QTcpServer(this);
00012     if (!m_pTcpServer->listen(QHostAddress::Any, 5512)) {
00013         std::cerr << tr("TCP Server").toStdString(),
00014                 tr("Unable to start the server: %1.\n")
00015                 .arg(m_pTcpServer->errorString()).toStdString();
00016         exit(-1);
00017         return;
00018     }
00019 
00020     QString ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
00021     std::cout << tr("The server is running on\n\nIP: %1\nport: %2\n\n"
00022                     "Run the Client now.")
00023                  .arg(ipAddress).arg(m_pTcpServer->serverPort()).toStdString() << std::endl;
00024 
00025     connect(m_pTcpServer, SIGNAL(newConnection()), SLOT(NewClientConnection()));
00026     m_RobotThread.init();
00027     m_RobotThread.start();
00028 }
00029 
00030 void Server::NewClientConnection() {
00031         QTcpSocket * pClientSocket = m_pTcpServer->nextPendingConnection();
00032         if(pClientSocket) {
00033                 connect(pClientSocket, SIGNAL(disconnected()), pClientSocket, SLOT(deleteLater()));
00034                 connect(pClientSocket, SIGNAL(readyRead()), SLOT(NewClientCommand()));
00035         }
00036 }
00037 
00038 void Server::writeData()
00039 {
00040     //QTcpSocket *pClientSocket = qobject_cast<QTcpSocket*>(sender());
00041     //Q_UNUSED(pClientSocket);
00042     //const QRegExp rxlen("^(\\w+)\\s+(-*\\d*\\.?\\d*)\\s+(-*\\d*\\.?\\d*)$");
00043     //QString text(pClientSocket->read(length));
00044         
00045     //pClientSocket->write(m_RobotThread.getForwardSpeed());
00046 }
00047 
00048 void Server::NewClientCommand() {
00049     // "\w": match a word character
00050     // "\s": match a whitespace character
00051     // "\d": match a digit
00052     const QRegExp rxlen("^(\\w+)\\s+(-*\\d*\\.?\\d*)\\s+(-*\\d*\\.?\\d*)$");
00053     // [word] [digit]
00054     int length;
00055 
00056     QTcpSocket * pClientSocket = qobject_cast<QTcpSocket *>(sender());
00057 
00058     while(pClientSocket->bytesAvailable() > 0) {
00059         length = static_cast<int>(pClientSocket->read(1).at(0));        // Read the command size.
00060         QString text(pClientSocket->read(length));
00061 
00062         if (rxlen.indexIn(text) > -1) {
00063             QString command = rxlen.cap(1);
00064             double speed = rxlen.cap(2).toDouble();
00065             QString l_speed; QString a_speed;
00066             double changeInAngle = rxlen.cap(3).toDouble();
00067             l_speed.setNum(speed); a_speed.setNum(changeInAngle);
00068 
00069             QString toRos = command + ", m/sec: " + l_speed + ", rad/sec: " + a_speed;
00070             std::cout << "Command: " << command.toStdString();
00071             m_RobotThread.setCommand(toRos);
00072 
00073             if (command == "SetSpeed")
00074             {
00075                 m_RobotThread.SetSpeed(speed, changeInAngle);
00076                 std::cout << "\tX m/sec: " << speed << "\t radians/sec: " <<
00077 changeInAngle;
00078             }
00079 
00080             else if (command == "getPosition")
00081             {
00082                 std::stringstream stm;
00083                 stm << "("; stm << m_RobotThread.getXPos();
00084                 stm << ", "; stm << m_RobotThread.getYPos();
00085                 stm << ", "; stm << m_RobotThread.getAPos();
00086                 stm << ")"; //format the string in a stringstream.
00087 
00088                 std::string pos = stm.str();
00089 
00090                 pClientSocket->write(pos.c_str());
00091             }//if the command is a get position.
00092 
00093             else if (command == "getXSpeed")
00094             {
00095                 std::stringstream stm;
00096                 stm << m_RobotThread.getXSpeed();
00097                 pClientSocket->write(stm.str().c_str());
00098             }//returns the linear speed of the robot to tcp.
00099 
00100             else if (command == "getASpeed")
00101             {
00102                 std::stringstream stm;
00103                 stm << m_RobotThread.getASpeed();
00104                 pClientSocket->write(stm.str().c_str());
00105             }//returns the angular speed of the robot to tcp.
00106 
00107             else if (command == "setPosition") {
00108                 double xPos = speed;
00109                 double yPos = changeInAngle;
00110 
00111                 QList<double> pose;
00112                 pose.push_back(xPos);
00113                 pose.push_back(yPos);
00114                 m_RobotThread.setPose(pose);
00115             }//end else if
00116 
00117             else if (command == "setGoal")
00118             {
00119                 double xPos = speed, yPos = changeInAngle;
00120 
00121                 geometry_msgs::Point goal;
00122                 goal.x = xPos;
00123                 goal.y = yPos;
00124                 goal.z = PI/2;
00125                 m_RobotThread.goToXYZ(goal);
00126             }//end else if
00127 
00128             std::cout << std::endl;
00129         }
00130     }
00131 }
00132 
00133 /*void Server::NewClientCommand() {
00134     //const QRegExp rxlen("^(\\w+)\\s+(-*\\d*\\.?\\d*)\\s+(-*\\d*\\.?\\d*)$");
00135     const QRegExp rxlen("^(\\w+)\\s+(-*\\d*\\.?\\d*)\\s+(-*\\d*\\.?\\d*)\\s+(-*\\d*\\.?\\d*)$");
00136     int length;
00137 
00138     QTcpSocket * pClientSocket = qobject_cast<QTcpSocket *>(sender());
00139 
00140     while(pClientSocket->bytesAvailable() > 0) {
00141         length = static_cast<int>(pClientSocket->read(1).at(0));        // Read the command size.
00142         QString text(pClientSocket->read(length));
00143 
00144         if (rxlen.indexIn(text) > -1) {
00145             QString command = rxlen.cap(1);
00146             double speed = rxlen.cap(2).toDouble();
00147             double changeInAngle = rxlen.cap(3).toDouble();
00148             double par4 = rxlen.cap(4).toDouble();
00149 
00150             m_RobotThread.setCommand(command);
00151 
00152             else if(command == "getGoalDirObDist") {
00153                 //std::cout << "before read is good. " << std::endl;
00154                 //m_RobotThread.Read();
00155                 //std::cout << "after read is good. " << std::endl;
00156                 std::string pos = stringify(m_RobotThread.goalDirObDist());
00157                 pClientSocket->write(pos.c_str());
00158             }
00159             else if(command == "goTo") {
00160                 double xPos = speed,yPos = changeInAngle;
00161 
00162                 player_pose2d_t pos = {xPos,yPos,0}, vel = {par4,0,0};
00163                 std::cout << "target pos: " << pos << " vel: " << vel << std::endl;
00164                 m_RobotThread.goTo(pos,vel);
00165             }//end else if
00166             else if(command == "go") {
00167                 double xPos = speed,yPos = changeInAngle,yaw = par4;
00168 
00169                 player_pose2d_t pos = {xPos,yPos,yaw};
00170                 std::cout << "target pos: " << pos << std::endl;
00171                 m_RobotThread.goTo(pos);
00172             }//end else if
00173             else if(command == "getForwardObDist") {
00174                 int mode = speed;
00175                 std::string dist = stringify(m_RobotThread.ForwardObDist(mode));
00176                 pClientSocket->write(dist.c_str());
00177             }//end else if
00178             else if(command == "setMotor") {
00179                 double mode = speed; bool enable = true;
00180                 if (mode==0)
00181                     enable = false;
00182                 m_RobotThread.setMotor(enable);
00183             }//end else if
00184         }//end if
00185     }//end while
00186 }//callback for tcp messages.*/
00187 }//namespace


tcp_command
Author(s): Hunter Allen
autogenerated on Mon Oct 6 2014 08:35:12