ExistCpp
ServicePublisher.cpp
浏览该文件的文档.
1 #include "EcPreCompile.h" //QApplication
2 
3 #include "ServicePublisher.h"
4 
5 
10 {
11  listen(); //监听。加入组播组。
12 
13 
14 
15 } //ServicePublisher::ServicePublisher()
16 
23 void ServicePublisher::publishService(string LanServiceName,quint16 LanServicePort,exist::ServicePublishMessage::ServiceProtocolType LanServiceProtocolType)
24 {
25  Q_UNUSED(LanServiceName); //不使用这个参数。
26  Q_UNUSED(LanServicePort); //不使用这个参数。
27  Q_UNUSED(LanServiceProtocolType); //不使用这个参数。
28 
29  exist::ExistMessage existMessage; //消息。
30 
31  existMessage.set_messagetype (exist::ExistMessage::SERVICEPUBLISH); //设置消息类型。
32 
33  exist::ServicePublishMessage * servicePublishMessage=existMessage.mutable_servicepublishmessage (); //获取服务发布消息对象。
34 
35  servicePublishMessage->set_name (LanServiceName); //设置服务名字。
36  servicePublishMessage->set_port (LanServicePort); //设置端口号。
37  servicePublishMessage->set_protocoltype (LanServiceProtocolType); //设置协议类型。
38 
39 
40 
41  string packageString=existMessage.SerializeAsString (); //序列化为字符串。
42 
43  //发送:
44  networkThread.castData(packageString);
45 } //void ServicePublisher::publishService(QString LanServiceName,quint16 LanServicePort,ServiceProtocolType LanServiceProtocolType)
46 
50 void ServicePublisher::listen()
51 {
52  startNetworkThread(); //启动网络线程。
53 
54  return;
55 } //void UdpServer::listen()
56 
61 void ServicePublisher::processDatagram(char * datagram,int datagramLength, Poco::Net::SocketAddress senderAddress)
62 {
63 
64  Q_UNUSED(senderAddress); //不使用勒个参数。
65 
66  ExistMessage existMessage;
67 // existMessage.ParseFromString (datagram.toStdString ()); //解析数据包。
68 
69 
70  if (datagramLength>0) //长度有效。
71  {
72  string datagramtoStdString(datagram,datagramLength); //数据包。
73 
74 
75 
76  existMessage.ParseFromString (datagramtoStdString); //解析数据包。
77 
78 
79 
80  switch (existMessage.messagetype ()) //根据消息类型来处理。
81  {
82  case ExistMessage::SERVICEPUBLISH: //服务发布。
83  reportServiceDiscovered(existMessage,senderAddress.host().toString()); //报告,发现了服务。
84 
85  break; //跳出。
86 
87  default: //默认情况。
88  break; //跳出。
89  } //switch (existMessage.messagetype ()) //根据消息类型来处理。
90 
91  } //if (datagramLength>0) //长度有效。
92 
93 
94  return;
95 } //void UdpServer::processDatagram(QByteArray datagram)
96 
101 void ServicePublisher::reportServiceDiscovered(ExistMessage existMessage,string address)
102 {
103  const ServicePublishMessage servicePublishMessage=existMessage.servicepublishmessage (); //获取消息发布消息对象。
104 
105  string serviceName=servicePublishMessage.name (); //获取名字。
106  quint16 servicePort=servicePublishMessage.port (); //获取端口号。
107  ServicePublishMessage::ServiceProtocolType serviceProtocolType=servicePublishMessage.protocoltype (); //获取协议类型。
108 
109  (void)(servicePort);
110  (void)(serviceProtocolType);
111  (void)(address);
112 
113  return;
114 } //void ServicePublisher::reportServiceDiscovered(ExistMessage existMessage)
115 
119 void ServicePublisher::startNetworkThread()
120 {
121  networkPocoThread.start(networkThread); //启动线程。
122 
123 
124  return;
125 } //void ServicePublisher::startNetworkThread()
ExistCpp的主要接口,用于向局域网发布服务。
void publishService(string LanServiceName, quint16 LanServicePort, exist::ServicePublishMessage::ServiceProtocolType LanServiceProtocolType)
发布服务。
::com::stupidbeauty::exist::ServicePublishMessage_ServiceProtocolType protocoltype() const
void set_protocoltype(::com::stupidbeauty::exist::ServicePublishMessage_ServiceProtocolType value)
::com::stupidbeauty::exist::ExistMessage_MessageType messagetype() const
void set_port(::google::protobuf::int32 value)
::google::protobuf::int32 port() const
::com::stupidbeauty::exist::ServicePublishMessage * mutable_servicepublishmessage()
const ::com::stupidbeauty::exist::ServicePublishMessage & servicepublishmessage() const
ServicePublisher()
默认构造函数。
void set_name(const ::std::string &value)
void set_messagetype(::com::stupidbeauty::exist::ExistMessage_MessageType value)