⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commserver.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
字号:
/*************************************************************************** *   Copyright (C) 2004 - 2006 by ZJUBase                                  *
 *                                National Lab of Industrial Control Tech. * *                                Zhejiang University, China               *
*                                                                         * *   Team members:                                                         *
 *    Currently the team leader is,                                        * *           Hao JIANG (jianghao@iipc.zju.edu.cn; riveria@gmail.com)       *
 *    In the next season, the leader will be                               * *           Yifeng ZHANG (yfzhang@iipc.zju.edu.cn)                        *
 *    ZJUBase 3D agent is created by                                       * *           Dijun LUO (djluo@iipc.zju.edu.cn)                             *
 *    All the members who has ever contributed:                            * *           Jun JIANG                                                     *
 *           Xinfeng DU (xfdu@iipc.zju.edu.cn)                             *
 *           Yang ZHOU (yzhou@iipc.zju.edu.cn)                             *
 *           Zhipeng YANG                                                  *
 *           Xiang FAN                                                     *
 *                                                                         *
 *   Team Manager:                                                          *
 *      Ms. Rong XIONG (rxiong@iipc.zju.edu.cn)                            *
 *                                                                         *
 *   If you met any problems or you have something to discuss about        * *   ZJUBase. Please feel free to contact us through EMails given below.   * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   This program is distributed in the hope that it will be useful,       * *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * *   GNU General Public License for more details.                          * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this program; if not, write to the                         * *   Free Software Foundation, Inc.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/
//#include <netinet/in.h>
#include "commserver.h"
#include <iostream>
#include <fstream>
using namespace std;

CommServer::CommServer()
{
}

bool
CommServer::Init(const string& host, int port)
{
/*
    mParser = shared_dynamic_cast<oxygen::BaseParser>(GetCore()->New(parser));

    if (mParser.get() == 0)
    {
        GetLog()->Error() <<
            "ERROR: (CommServer) failed to create parser " << parser << endl;
        return false;
    }

    return mCommUnit.OpenConnection(host,port);
*/
    return mCommUnit.OpenConnection(host,port);
}

bool
CommServer::ReadMessage()
{
    string line;
    return ReadMessage(line);
}

bool
CommServer::ReadMessage(string& msg)
{
    msg = mCommUnit.GetMessage();
    if (msg == "") {
        return false;
    }

	static bool first = true;
	if (first) {
		ofstream os("receive.log");
		os << endl << msg << endl;
	} else {
		ofstream os("receive.log", ios::app);
		os << endl << msg << endl;
	}
    Parse(msg);
    return true;
}

void
CommServer::Parse(const string& message)
{
/*
    if (mParser.get() == 0)
    {
        mPredicates = shared_ptr<PredicateList>();
        return;
    }

    mPredicates = mParser->Parse(message);
*/
    mPredicates.push_back(Predicate());
    Parser::Parse(message, mPredicates[mPredicates.size() - 1]);
}

vector<Predicate>&
CommServer::GetPredicates()
{
    return mPredicates;
}

void
CommServer::SendMessage(const std::string& message)
{
    unsigned long len = htonl(message.size());
    string s((const char*)&len,sizeof(len));
    s += message;
    mCommUnit.PutMessage(s);
}

void
CommServer::SendPauseCmd()
{
    SendMessage("P");
}

void
CommServer::SendRunCmd()
{
    SendMessage("R");
}

void
CommServer::SendDisconnectCmd()
{
    SendMessage("D");
}

void
CommServer::SendDropBallCmd()
{
    SendToWorldModel("(dropBall)");
}

void
CommServer::SendToWorldModel(const std::string& msg)
{
    SendMessage("W"+msg);
}

void
CommServer::SendTrainerCmd(const std::string& cmd)
{
    SendToWorldModel(cmd);
}

void
CommServer::SendKickOffCmd(EKickOff mode)
{
    switch (mode)
    {
    case eRandom:
        SendToWorldModel("(kickOff)");
        break;
    case eLeft:
        SendToWorldModel("(playMode KickOff_Left)");
        break;
    case eRight:
        SendToWorldModel("(playMode KickOff_Right)");
        break;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -