Skip to content

Commit

Permalink
멀티캐스트의 채널 Join 시 메세지 핸들러도 같이 등록하도록 변경.
Browse files Browse the repository at this point in the history
이 커밋 이전에는 특정 채널에 대한 메세지 핸들러를 등록하면 핸들러가 유지되기를 기대하지만
채널을 Leave 할때 핸들러를 제거를 하는 문제가 있었다.

연결된 채널의 핸들러만 관리하고 호출되는 것이 기대되는 동작이므로
메세지 핸들러는 채널 Join 시 등록하도록 수정하였다.

Change-Id: I5c5bd0eecd8f231e5011762b2d71da3245ba42a3
  • Loading branch information
SungjinB committed Jun 3, 2020
1 parent 8ae73cd commit 61b4782
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 34 deletions.
84 changes: 54 additions & 30 deletions Classes/FunapiTestScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,45 +939,69 @@ void FunapiTest::CreateMulticast()
multicast_ = nullptr;
}
});
}

// add callback
multicast_->AddJsonChannelMessageCallback
(kMulticastTestChannel,
[this]
(const std::shared_ptr<fun::FunapiMulticast>& funapi_multicast,
const std::string &channel_id,
const std::string &sender,
const std::string &json_string)
{
fun::DebugUtils::Log("channel_id=%s, sender=%s, body=%s", channel_id.c_str(), sender.c_str(), json_string.c_str());
});
multicast_->Connect();
}

multicast_->AddProtobufChannelMessageCallback
(kMulticastTestChannel,
[this]
(const std::shared_ptr<fun::FunapiMulticast> &funapi_multicast,
const std::string &channel_id,
const std::string &sender,
void FunapiTest::JoinMulticastChannel()
{
auto json_msg_handler =
[this](const std::shared_ptr<fun::FunapiMulticast>& funapi_multicast,
const fun::string &channel_id,
const fun::string &sender_string,
const fun::string &json_string)
{
fun::DebugUtils::Log("Arrived the chatting message. channel_id = %s, sender = %s, body = %s",
channel_id.c_str(), sender_string.c_str(), json_string.c_str());
};

auto protobuf_msg_handler =
[this](const std::shared_ptr<fun::FunapiMulticast> &funapi_multicast,
const fun::string &channel_id,
const fun::string &sender_string,
const FunMessage& message)
{
if (message.HasExtension(multicast))
{
FunMulticastMessage mcast_msg = message.GetExtension(multicast);
FunChatMessage chat_msg = mcast_msg.GetExtension(chat);
std::string text = chat_msg.text();
if (mcast_msg.HasExtension(chat)) {
FunChatMessage chat_msg = mcast_msg.GetExtension(chat);
fun::string text = chat_msg.text();
fun::DebugUtils::Log("Arrived the chatting message. channel_id = %s, sender = %s, message = %s", channel_id.c_str(), sender_string.c_str(), text.c_str());
}
}
};

fun::DebugUtils::Log("channel_id=%s, sender=%s, message=%s", channel_id.c_str(), sender.c_str(), text.c_str());
});
fun::DebugUtils::Log("JoinMulticastChannel button was clicked.");
if (multicast_ == nullptr)
{
fun::DebugUtils::Log("Afunapi_tester::JoinMulticastChannel was called, but the FunapiMulticast instance is not created yet");
return;
}

multicast_->Connect();
}
if (!multicast_->IsConnected())
{
fun::DebugUtils::Log("Afunapi_tester::JoinMulticastChannel was called, but the FunapiMulticast instance is not connected yet");
return;
}

void FunapiTest::JoinMulticastChannel()
{
fun::DebugUtils::Log("(Button)JoinMulticastChannel");
if (multicast_) {
if (!multicast_->IsInChannel(kMulticastTestChannel)) {
multicast_->JoinChannel(kMulticastTestChannel);
}
if (multicast_->IsInChannel(kMulticastTestChannel))
{
fun::DebugUtils::Log(
"Afunapi_tester::JoinMulticastChannel was called, but the FunapiMulticast instance was already joined %s channel",
kMulticastTestChannel.c_str());
return;
}

auto encoding = multicast_->GetEncoding();
if (encoding == fun::FunEncoding::kJson)
{
multicast_->JoinChannel(kMulticastTestChannel, json_msg_handler);
}
else if (encoding == fun::FunEncoding::kProtobuf)
{
multicast_->JoinChannel(kMulticastTestChannel, protobuf_msg_handler);
}
}

Expand Down
136 changes: 133 additions & 3 deletions Classes/funapi/funapi_multicasting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ class FunapiMulticastImpl : public std::enable_shared_from_this<FunapiMulticastI

bool IsConnected() const;
bool IsInChannel(const fun::string &channel_id) const;

// Deprecated function. Use FunapiMulticast::JoinChannel(chnnel_id, message_handler, token) function
bool JoinChannel(const fun::string &channel_id, const fun::string &token);

bool JoinChannel(const fun::string &channel_id, const JsonChannelMessageHandler &handler, const fun::string &token);
bool JoinChannel(const fun::string &channel_id, const ProtobufChannelMessageHandler &handler, const fun::string &token);

bool LeaveChannel(const fun::string &channel_id);
bool LeaveAllChannels();

Expand All @@ -65,7 +71,10 @@ class FunapiMulticastImpl : public std::enable_shared_from_this<FunapiMulticastI
void AddErrorCallback(const ErrorNotify &handler);
void AddChannelListCallback(const ChannelListNotify &handler);

// Deprecated fucntion. Use FunapiMulticast::JoinChannel(chnnel_id, protobuf_channel_message_handler, token) function
void AddProtobufChannelMessageCallback(const fun::string &channel_id, const ProtobufChannelMessageHandler &handler);

// Deprecated function. Use FunapiMulticast::JoinChannel(chnnel_id, json_channel_message_handler, token) function
void AddJsonChannelMessageCallback(const fun::string &channel_id, const JsonChannelMessageHandler &handler);

bool RequestChannelList();
Expand Down Expand Up @@ -355,6 +364,109 @@ bool FunapiMulticastImpl::JoinChannel(const fun::string &channel_id, const fun::
}


bool FunapiMulticastImpl::JoinChannel(const fun::string &channel_id, const JsonChannelMessageHandler &handler, const fun::string &token)
{
if (!IsConnected())
{
DebugUtils::Log("Connect first before joining a multicast channel.");
return false;
}

// encoding 은 생성시점에 결졍되고 상태가 변경되지 않는다.
if (encoding_ != FunEncoding::kJson)
{
DebugUtils::Log("This multicast was created with json encoding. You should pass JsonChannelMessageHandler.");
return false;
}

if (IsInChannel(channel_id))
{
DebugUtils::Log("Already joined the channel: %s", channel_id.c_str());
return false;
}

{
std::unique_lock<std::mutex> lock(channels_mutex_);
channels_.insert(channel_id);
json_channel_handlers_[channel_id] += handler;
}

// Send Join message
rapidjson::Document msg;
msg.SetObject();

rapidjson::Value channel_id_node(channel_id.c_str(), msg.GetAllocator());
msg.AddMember(rapidjson::StringRef(kChannelId), channel_id_node, msg.GetAllocator());

rapidjson::Value sender_node(sender_.c_str(), msg.GetAllocator());
msg.AddMember(rapidjson::StringRef(kSender), sender_node, msg.GetAllocator());

rapidjson::Value join_node(true);
msg.AddMember(rapidjson::StringRef(kJoin), join_node, msg.GetAllocator());

if (token.length() > 0)
{
rapidjson::Value token_node(token.c_str(), msg.GetAllocator());
msg.AddMember(rapidjson::StringRef(kToken), token_node, msg.GetAllocator());
}

rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
msg.Accept(writer);
fun::string json_string = buffer.GetString();

session_->SendMessage(kMulticastMsgType, json_string, protocol_);
return true;
}


bool FunapiMulticastImpl::JoinChannel(const fun::string &channel_id, const ProtobufChannelMessageHandler &handler, const fun::string &token)
{
if (!IsConnected())
{
DebugUtils::Log("Connect first before joining a multicast channel.");
return false;
}

// encoding 은 생성시점에 결졍되고 상태가 변경되지 않는다.
if (encoding_ != FunEncoding::kProtobuf)
{
DebugUtils::Log("This multicast was created with protobuf encoding. You should pass ProtobufChannelMessageHandler.");
return false;
}

if (IsInChannel(channel_id)) {
DebugUtils::Log("Already joined the channel: %s", channel_id.c_str());
return false;
}

{
std::unique_lock<std::mutex> lock(channels_mutex_);
channels_.insert(channel_id);
protobuf_channel_handlers_[channel_id] += handler;
}

// Send Join message
FunMessage msg;
msg.set_msgtype(kMulticastMsgType);

FunMulticastMessage *mcast_msg = msg.MutableExtension(multicast);
mcast_msg->set_channel(channel_id.c_str());
mcast_msg->set_join(true);
mcast_msg->set_sender(sender_.c_str());

if (token.length() > 0)
{
mcast_msg->set_token(token.c_str());
}

session_->SendMessage(msg, protocol_);
return true;
}




bool FunapiMulticastImpl::LeaveChannel(const fun::string &channel_id) {
if (!IsConnected()) {
DebugUtils::Log("Not connected. If you are trying to leave a channel in which you were, connect first while preserving the session id you used for join.");
Expand Down Expand Up @@ -889,19 +1001,37 @@ void FunapiMulticast::Close() {
}


bool FunapiMulticast::JoinChannel(const fun::string &channel_id, const fun::string &token) {
bool FunapiMulticast::JoinChannel(const fun::string &channel_id, const fun::string &token)
{
DebugUtils::Log("JoinChannel(channel_id, token) function was deprecated. Please Use FunapiMulticast::JoinChannel(chnnel_id, message_handler, token) function");
return impl_->JoinChannel(channel_id, token);
}


bool FunapiMulticast::JoinChannel(const fun::string &channel_id, const JsonChannelMessageHandler &handler, const fun::string &token)
{
return impl_->JoinChannel(channel_id, handler, token);
}


bool FunapiMulticast::JoinChannel(const fun::string &channel_id, const ProtobufChannelMessageHandler &handler, const fun::string &token)
{
return impl_->JoinChannel(channel_id, handler, token);
}


void FunapiMulticast::AddProtobufChannelMessageCallback(const fun::string &channel_id,
const ProtobufChannelMessageHandler &handler) {
const ProtobufChannelMessageHandler &handler)
{
DebugUtils::Log("FunapiMulticast::AddProtobufChannelMessageCallback function was deprecated. Please Use FunapiMulticast::JoinChannel(chnnel_id, message_handler, token) function");
impl_->AddProtobufChannelMessageCallback(channel_id, handler);
}


void FunapiMulticast::AddJsonChannelMessageCallback(const fun::string &channel_id,
const JsonChannelMessageHandler &handler) {
const JsonChannelMessageHandler &handler)
{
DebugUtils::Log("FunapiMulticast::AddJsonChannelMessageCallback function was deprecated. Please Use FunapiMulticast::JoinChannel(chnnel_id, message_handler, token) function");
impl_->AddJsonChannelMessageCallback(channel_id, handler);
}

Expand Down
9 changes: 9 additions & 0 deletions Classes/funapi/funapi_multicasting.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,25 @@ class FUNAPI_API FunapiMulticast : public std::enable_shared_from_this<FunapiMul
void AddLeftCallback(const ChannelNotify &handler);
void AddErrorCallback(const ErrorNotify &handler);
void AddChannelListCallback(const ChannelListNotify &handler);

// Deprecated fucntion. Please Use FunapiMulticast::JoinChannel(chnnel_id, protobuf_channel_message_handler, token) function
void AddProtobufChannelMessageCallback(const fun::string &channel_id, const ProtobufChannelMessageHandler &handler);

// Deprecated function. Please Use FunapiMulticast::JoinChannel(chnnel_id, json_channel_message_handler, token) function
void AddJsonChannelMessageCallback(const fun::string &channel_id, const JsonChannelMessageHandler &handler);

void AddSessionEventCallback(const FunapiMulticast::SessionEventHandler &handler);
void AddTransportEventCallback(const FunapiMulticast::TransportEventHandler &handler);

bool IsConnected() const;
bool IsInChannel(const fun::string &channel_id) const;

// Deprecated function. Please Use FunapiMulticast::JoinChannel(chnnel_id, message_handler, token) function
bool JoinChannel(const fun::string &channel_id, const fun::string &token = "");

bool JoinChannel(const fun::string &channel_id, const JsonChannelMessageHandler &handler, const fun::string &token = "");
bool JoinChannel(const fun::string &channel_id, const ProtobufChannelMessageHandler &handler, const fun::string &token = "");

bool LeaveChannel(const fun::string &channel_id);
bool LeaveAllChannels();

Expand Down
2 changes: 1 addition & 1 deletion Classes/funapi/funapi_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace fun {
enum class FunapiVersion : int
{
kProtocolVersion = 1,
kPluginVersion = 200,
kPluginVersion = 201,
};

}
Expand Down

0 comments on commit 61b4782

Please sign in to comment.