forked from TarsCloud/Tars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
///////////////////////////////////////////////////////////////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
#----------------------------------------------------------------------- |