Skip to content

Commit

Permalink
添加创建C++ http服务的脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
deepzliu committed Feb 27, 2018
1 parent dbe8ba1 commit 7f7c51b
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cpp/servant/script/create_http_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh

if [ $# -lt 3 ]
then
echo "<Usage: $0 App Server Servant>"
exit 0
fi

APP=$1
SERVER=$2
SERVANT=$3

if [ "$SERVER" == "$SERVANT" ]
then
echo "Error!(ServerName == ServantName)"
exit -1
fi

if [ ! -d $APP/$SERVER ]
then
echo "[mkdir: $APP/$SERVER]"
mkdir -p $APP/$SERVER
fi

echo "[create server: $APP.$SERVER ...]"

DEMO_PATH=/usr/local/tars/cpp/script/http_demo

cp $DEMO_PATH/* $APP/$SERVER/

cd $APP/$SERVER/

SRC_FILE="DemoServer.h DemoServer.cpp DemoServantImp.h DemoServantImp.cpp makefile"

for FILE in $SRC_FILE
do
cat $FILE | sed "s/DemoServer/$SERVER/g" > $FILE.tmp
mv $FILE.tmp $FILE

cat $FILE | sed "s/DemoApp/$APP/g" > $FILE.tmp
mv $FILE.tmp $FILE

cat $FILE | sed "s/DemoServant/$SERVANT/g" > $FILE.tmp
mv $FILE.tmp $FILE
done

rename "DemoServer" "$SERVER" $SRC_FILE
rename "DemoServant" "$SERVANT" $SRC_FILE

cd ../../

echo "[done.]"
44 changes: 44 additions & 0 deletions cpp/servant/script/http_demo/DemoServantImp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "DemoServantImp.h"
#include "servant/Application.h"

using namespace std;

//////////////////////////////////////////////////////
void DemoServantImp::initialize()
{
//initialize servant here:
//...
}

//////////////////////////////////////////////////////
void DemoServantImp::destroy()
{
//destroy servant here:
//...
}

int DemoServantImp::doRequest(TarsCurrentPtr current, vector<char> &buffer)
{
TC_HttpRequest req;
TC_HttpResponse rsp;

// parse request header
vector<char> v = current->getRequestBuffer();
string sBuf;
sBuf.assign(&v[0], v.size());
req.decode(sBuf);

int ret = doRequest(req, rsp);

rsp.encode(buffer);

return ret;
}

int DemoServantImp::doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp)
{
string msg = "Hello Tars!";
rsp.setContentType("html/text");
rsp.setResponse(msg.c_str(), msg.size());
return 0;
}
37 changes: 37 additions & 0 deletions cpp/servant/script/http_demo/DemoServantImp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef _DemoServantImp_H_
#define _DemoServantImp_H_

#include "servant/Application.h"

/**
*
*
*/
class DemoServantImp : public Servant
{
public:
/**
*
*/
virtual ~DemoServantImp() {}

/**
*
*/
virtual void initialize();

/**
*
*/
virtual void destroy();

/**
*
*/
int doRequest(TarsCurrentPtr current, vector<char> &buffer);

private:
int doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp);
};
/////////////////////////////////////////////////////
#endif
82 changes: 82 additions & 0 deletions cpp/servant/script/http_demo/DemoServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "DemoServer.h"
#include "DemoServantImp.h"

using namespace std;

DemoServer g_app;

struct HttpProtocol
{
/**
* 解析http请求
* @param in
* @param out
*
* @return int
*/
static int parseHttp(string &in, string &out)
{
try
{
//判断请求是否是HTTP请求
bool b = TC_HttpRequest ::checkRequest(in.c_str(), in.length());
//完整的HTTP请求
if(b)
{
out = in;
in = "";
//TLOGDEBUG("out size: " << out.size() << endl);
return TC_EpollServer::PACKET_FULL;
}
else
{
return TC_EpollServer::PACKET_LESS;
}
}
catch(exception &ex)
{
return TC_EpollServer::PACKET_ERR;
}

return TC_EpollServer::PACKET_LESS; //表示收到的包不完全
}

};

/////////////////////////////////////////////////////////////////
void
DemoServer::initialize()
{
//initialize application here:
//...

addServant<DemoServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".DemoServantObj");
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".DemoServantObj", &HttpProtocol::parseHttp);
}
/////////////////////////////////////////////////////////////////
void
DemoServer::destroyApp()
{
//destroy application here:
//...
}
/////////////////////////////////////////////////////////////////
int
main(int argc, char* argv[])
{
try
{
g_app.main(argc, argv);
g_app.waitForShutdown();
}
catch (std::exception& e)
{
cerr << "std::exception:" << e.what() << std::endl;
}
catch (...)
{
cerr << "unknown exception." << std::endl;
}
return -1;
}
/////////////////////////////////////////////////////////////////
34 changes: 34 additions & 0 deletions cpp/servant/script/http_demo/DemoServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef _DemoServer_H_
#define _DemoServer_H_

#include <iostream>
#include "servant/Application.h"

using namespace tars;

/**
*
**/
class DemoServer : public Application
{
public:
/**
*
**/
virtual ~DemoServer() {};

/**
*
**/
virtual void initialize();

/**
*
**/
virtual void destroyApp();
};

extern DemoServer g_app;

////////////////////////////////////////////
#endif
17 changes: 17 additions & 0 deletions cpp/servant/script/http_demo/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#-----------------------------------------------------------------------

APP := DemoApp
TARGET := DemoServer
CONFIG :=
STRIP_FLAG:= N
TARS2CPP_FLAG:=

INCLUDE +=
LIB +=

#-----------------------------------------------------------------------

include /usr/local/tars/cpp/makefile/makefile.tars

#-----------------------------------------------------------------------

0 comments on commit 7f7c51b

Please sign in to comment.