From 468d955bb9d408314ae38e9779f50d0ac03f71fd Mon Sep 17 00:00:00 2001 From: jusung Date: Fri, 28 Jan 2022 10:00:04 +0900 Subject: [PATCH] [cion] Implement java group generator Change-Id: I24968e0107e5228ad242950a168e4f3589e0c747 Signed-off-by: jusung --- idlc/gen_cion/java_cion_gen_base.cc | 13 +- idlc/gen_cion/java_cion_gen_base.h | 2 +- idlc/gen_cion/java_cion_gen_cb.h | 62 +++++++++ idlc/gen_cion/java_cion_group_gen.cc | 221 ++++++++++++++++++++++++++++++ idlc/gen_cion/java_cion_group_gen.h | 51 +++++++ idlc/gen_cion/java_cion_group_gen_cb.h | 83 +++++++++++ idlc/gen_cion/java_cion_group_repo_gen.cc | 61 +++++++++ idlc/gen_cion/java_cion_group_repo_gen.h | 39 ++++++ idlc/main.cc | 19 ++- 9 files changed, 545 insertions(+), 6 deletions(-) create mode 100644 idlc/gen_cion/java_cion_group_gen.cc create mode 100644 idlc/gen_cion/java_cion_group_gen.h create mode 100644 idlc/gen_cion/java_cion_group_gen_cb.h create mode 100644 idlc/gen_cion/java_cion_group_repo_gen.cc create mode 100644 idlc/gen_cion/java_cion_group_repo_gen.h diff --git a/idlc/gen_cion/java_cion_gen_base.cc b/idlc/gen_cion/java_cion_gen_base.cc index 3272922..dd3c20f 100644 --- a/idlc/gen_cion/java_cion_gen_base.cc +++ b/idlc/gen_cion/java_cion_gen_base.cc @@ -67,10 +67,15 @@ JavaCionGeneratorBase::JavaCionGeneratorBase(std::shared_ptr doc) } void JavaCionGeneratorBase::GenMethodId(std::ofstream& stream, - const Interface& iface) { - int cnt = 2; - stream << Tab(1) << "public static final int __RESULT = 0;" << NLine(2); - stream << Tab(1) << "public static final int __CALLBACK = 1;" << NLine(2); + const Interface& iface, bool is_group) { + int cnt = 0; + + if (!is_group) { + stream << Tab(1) << "public static final int __RESULT = 0;" << NLine(2); + stream << Tab(1) << "public static final int __CALLBACK = 1;" << NLine(2); + cnt = 2; + } + for (const auto& i : iface.GetDeclarations()) { if (i->GetMethodType() == Declaration::MethodType::DELEGATE) continue; diff --git a/idlc/gen_cion/java_cion_gen_base.h b/idlc/gen_cion/java_cion_gen_base.h index 9ed0db7..46cb539 100644 --- a/idlc/gen_cion/java_cion_gen_base.h +++ b/idlc/gen_cion/java_cion_gen_base.h @@ -32,7 +32,7 @@ class JavaCionGeneratorBase : public Generator { explicit JavaCionGeneratorBase(std::shared_ptr doc); virtual ~JavaCionGeneratorBase() = default; void GenMethodId(std::ofstream& stream, - const Interface& iface); + const Interface& iface, bool is_group = false); void GenDeclaration(std::ofstream& stream, const Declaration& decl, bool semicol = true); void GenParameters(std::ofstream& stream, const Parameters& ps); diff --git a/idlc/gen_cion/java_cion_gen_cb.h b/idlc/gen_cion/java_cion_gen_cb.h index 0724bf5..0800c1d 100644 --- a/idlc/gen_cion/java_cion_gen_cb.h +++ b/idlc/gen_cion/java_cion_gen_cb.h @@ -242,6 +242,68 @@ public abstract class ServerBase implements ServerConnectionLifecycleCallback, )__java_cb"; +const char DEFAULT_GROUP_REPO[] = +R"__java_cb( +import android.content.Context; + +import org.tizen.cion.*; + +import java.net.SocketTimeoutException; + +/** + * Abstract class for making a group + */ +public abstract class GroupBase implements GroupConnectionLifecycleCallback{ + private GroupChannel mGroup; + + private Context mContext; + + /** + * Constructor + * @param mContext The Context + * @param topicName Topic name + */ + public GroupBase(Context mContext, String topicName) { + mGroup = new GroupChannel(mContext, topicName); + this.mContext = mContext; + } + + /** + * Constructor with security information + * @param mContext The context + * @param topicName Topic name + * @param sec Security information + */ + public GroupBase(Context mContext, String topicName, SecurityInfo sec) { + mGroup = new GroupChannel(mContext, topicName, sec); + this.mContext = mContext; + } + + /** + * Subscribes the topic. + */ + public void subscribe() { + mGroup.subscribe(this); + } + + /** + * Unsubscribes the topic. + */ + public void unsubscribe() { + mGroup.unsubscribe(); + } + + /** + * Publishes payload to current group. + * @param data Data payload + */ + public void publish(IPayload data) { + mGroup.publish(data); + } +} + +)__java_cb"; + const char OUT_CLASS[] = R"__java_cb( diff --git a/idlc/gen_cion/java_cion_group_gen.cc b/idlc/gen_cion/java_cion_group_gen.cc new file mode 100644 index 0000000..26c8a35 --- /dev/null +++ b/idlc/gen_cion/java_cion_group_gen.cc @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "idlc/gen_cion/java_cion_group_gen.h" + +namespace { +#include "idlc/gen_cion/java_cion_gen_cb.h" +#include "idlc/gen_cion/java_cion_group_gen_cb.h" +} + +namespace tidl { + +JavaCionGroupGen::JavaCionGroupGen(std::shared_ptr doc) + : JavaCionGeneratorBase(doc) {} + +void JavaCionGroupGen::OnInitGen(std::ofstream& stream) { + std::string fname = MakeDir(FileName, "/group"); + if (fname.empty()) + return; + + for (auto& i : GetDocument().GetBlocks()) { + if (i->GetType() != Block::TYPE_INTERFACE) + continue; + Interface& iface = static_cast(*i); + stream.open(fname + "/" + iface.GetID() + ".java"); + GenVersion(stream); + stream << "package org.tizen.gen." << FileName + ".group;" << NLine(2); + stream << std::endl; + + stream << "import org.tizen.gen." << FileName + ".common.*;" << NLine(2); + stream << "import org.tizen.cion.*;" << NLine(1) + << "import android.content.Context;" << NLine(1) + << "import java.util.ArrayList;" << NLine(1) + << "import java.util.LinkedList;" << NLine(1) + << "import java.util.List;" << NLine(2); + + GenInterface(stream, iface); + stream << NLine(1); + stream.close(); + } +} + +void JavaCionGroupGen::OnFiniGen(std::ofstream& stream) { +} + +void JavaCionGroupGen::GenInterface(std::ofstream& stream, const Interface& iface) { + stream << "public abstract class " << iface.GetID() + << " extends GroupBase "; + GenBrace(stream, TAB_SIZE * 0, [&]() { + stream << ReplaceAll(CB_DATA_MEMBERS, "", FULLVER); + GenMethodId(stream, iface, true); + GenCionPayloadReceivedEvent(stream, iface); + GenEvents(stream, iface); + GenSerializer(stream); + GenListSerializer(stream); + GenCtor(stream, iface); + GenMethods(stream, iface); + }, false); +} + +void JavaCionGroupGen::GenEvents(std::ofstream& stream, const Interface& iface) { + stream << CB_EVENT_METHODS << NLine(1); + + for (const auto& decl : iface.GetDeclarations()) + GenEvent(stream, iface, *decl); + + stream << NLine(1); +} + +void JavaCionGroupGen::GenEvent(std::ofstream& stream, const Interface& iface, + const Declaration& decl) { + + stream << Tab(1) << "/**"<< NLine(1) << Tab(1) + << "/* This method will be invoked when the " + << decl.GetID() << " request is delivered." + << NLine(1) << Tab(1) << "*/" << NLine(1); + + stream << Tab(1) << "public abstract void On" << decl.GetID() << "(PeerInfo peerInfo, "; + GenParameters(stream, decl.GetParameters()); + stream << ");" << NLine(1); +} + +void JavaCionGroupGen::GenCtor(std::ofstream& stream, const Interface& iface) { + bool securityCheck = false; + std::string m = "public $$(Context context, String topicName) {\n"; + + m += Tab(1) + "super(context, topicName, new SecurityInfo("; + + for (const auto& attr : iface.GetAttributes()) { + if (attr->GetKey() == "ca_path") { + m += "\"" + attr->GetValue() + "\", "; + securityCheck = true; + } else if (attr->GetKey() == "cert_path") { + m += "\"" + attr->GetValue() + "\", "; + securityCheck = true; + } else if (attr->GetKey() == "private_key") { + m += "\"" + attr->GetValue() + "\", "; + securityCheck = true; + } + } + + auto const pos = m.find_last_of(','); + m = m.substr(0, pos); + + if (securityCheck) + m += "));"; /* super(topicName, new SecurityInfo(ca, cert, private_key) */ + else + m += ");"; /* super(topicName) */ + + m += NLine(1); + m += " this.topicName = topicName;\n"; + m += "}"; + m += NLine(1); + + GenTemplate(AddIndent(TAB_SIZE, m), stream, + [&]()->std::string { + return iface.GetID(); + }); +} + +void JavaCionGroupGen::GenMethods(std::ofstream& stream, const Interface& iface) { + auto& decls = iface.GetDeclarations(); + + for (const auto& i : decls) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) + continue; + + if (!i->GetComments().empty()) + stream << AddIndent(TAB_SIZE * 1, i->GetComments()); + + stream << NLine(1) << Tab(1) << "public "; + GenDeclaration(stream, *i, false); + GenBrace(stream, TAB_SIZE * 1, [&]() { + GenInvocation(stream, *i); + }, false); + stream << NLine(1); + } +} + +void JavaCionGroupGen::GenCionPayloadReceivedEvent(std::ofstream& stream, + const Interface& iface) { + stream << CB_ON_PAYLOAD_RECEIVED_FRONT; + + for (const auto& i : iface.GetDeclarations()) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE || + i->GetMethodType() == Declaration::MethodType::SYNC) + continue; + stream << Tab(3) << "case __" << i->GetID() << ":" << NLine(1); + GenBrace(stream, TAB_SIZE * 3, [&]() { + GenHandlerInvocation(stream, *i); + stream << Tab(4) << "break;" << NLine(1); + }); + } + + stream << CB_ON_PAYLOAD_RECEIVED_BACK; +} + +void JavaCionGroupGen::GenHandlerInvocation(std::ofstream& stream, + const Declaration& decl) { + int cnt = 1; + // Deserialize + for (const auto& i : decl.GetParameters()) { + std::string v = "param" + std::to_string(cnt); + std::string c = ConvertTypeToDeserializer( + i->GetParameterType().GetBaseType(), v, "parcelReceived"); + stream << AddIndent(TAB_SIZE * 4, c); + cnt++; + } + + // Invoke + std::string m; + m += "On" + decl.GetID() + "(peerInfo"; + cnt = 1; + for (auto i = decl.GetParameters().begin(); + i != decl.GetParameters().end(); ++i) { + m += ", param" + std::to_string(cnt++); + } + + m += ");\n"; + stream << AddIndent(TAB_SIZE * 4, m); +} + +void JavaCionGroupGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { + GenTemplate(CB_INVOCATION_PRE, stream, + [&]()->std::string { + std::string st; + st += Tab(2) + + "p.write(__" + decl.GetID() + ");" + NLine(1); + std::string m; + + for (const auto& i : decl.GetParameters()) { + auto& pt = i->GetParameterType(); + m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p"); + } + + st += AddIndent(TAB_SIZE * 2, m) + + NLine(1) + += Tab(2) + + "synchronized (mLock) {" + + CB_ASYNC_INVOCATION_MID + + Tab(2) + + "}"; + + return st; + }); +} + +} // namespace tidl diff --git a/idlc/gen_cion/java_cion_group_gen.h b/idlc/gen_cion/java_cion_group_gen.h new file mode 100644 index 0000000..831476e --- /dev/null +++ b/idlc/gen_cion/java_cion_group_gen.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_JAVA_CION_GEN_JAVA_GROUP_GEN_H_ +#define IDLC_JAVA_CION_GEN_JAVA_GROUP_GEN_H_ + +#include +#include + +#include "idlc/gen_cion/java_cion_gen_base.h" + +namespace tidl { + +class JavaCionGroupGen : public JavaCionGeneratorBase { + public: + explicit JavaCionGroupGen(std::shared_ptr doc); + virtual ~JavaCionGroupGen() = default; + + void OnInitGen(std::ofstream& stream) override; + void OnFiniGen(std::ofstream& stream) override; + + private: + void GenInterface(std::ofstream& stream, const Interface& iface); + void GenCtor(std::ofstream& stream, const Interface& iface); + void GenConnectMethod(std::ofstream& stream, const Interface& iface); + void GenMethods(std::ofstream& stream, const Interface& iface); + void GenInvocation(std::ofstream& stream, const Declaration& decl); + void GenCionPayloadReceivedEvent(std::ofstream& stream, + const Interface& iface); + void GenEvents(std::ofstream& stream, const Interface& iface); + void GenEvent(std::ofstream& stream, + const Interface& iface, const Declaration& decl); + void GenHandlerInvocation(std::ofstream& stream, const Declaration& decl); +}; + +} // namespace tidl + +#endif // IDLC_JAVA_CION_GEN_JAVA_GROUP_GEN_H_ \ No newline at end of file diff --git a/idlc/gen_cion/java_cion_group_gen_cb.h b/idlc/gen_cion/java_cion_group_gen_cb.h new file mode 100644 index 0000000..a27d653 --- /dev/null +++ b/idlc/gen_cion/java_cion_group_gen_cb.h @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_JAVA_CION_GEN_JAVA_GROUP_GEN_CB_H_ +#define IDLC_JAVA_CION_GEN_JAVA_GROUP_GEN_CB_H_ + + +const char CB_DATA_MEMBERS[] = +R"__java_cb( + public String topicName; + + private static final String sTidlVersion = ""; + + private Object mLock = new Object(); + +)__java_cb"; + +const char CB_EVENT_METHODS[] = +R"__java_cb( + /** + * This method will be invoked when another peer left from the current group. + */ + @Override + public void onLeft(PeerInfo peerInfo) { + } + + /** + * This method will be invoked when another peer joined from the current group. + */ + @Override + public void onJoined(PeerInfo peerInfo) { + } +)__java_cb"; + +const char CB_ON_PAYLOAD_RECEIVED_FRONT[] = +R"__java_cb( @Override + public final void onPayloadReceived(PeerInfo peerInfo, IPayload payload) { + if(payload.getType() == IPayload.PayloadType.PAYLOAD_FILE) { + return; + } + + CionParcel parcelReceived; + + parcelReceived = new CionParcel(((DataPayload)payload).getData()); + int cmd = parcelReceived.readInt(); + switch (cmd) + { +)__java_cb"; + +const char CB_ON_PAYLOAD_RECEIVED_BACK[] = +R"__java_cb( + default: + break; + } + } +)__java_cb"; + +const char CB_INVOCATION_PRE[] = +R"__java_cb( CionParcel p = new CionParcel(); +$$ +)__java_cb"; + +const char CB_ASYNC_INVOCATION_MID[] = +R"__java_cb( + // Send + DataPayload dp = new DataPayload(p.toByteArray()); + super.publish(dp); +)__java_cb"; + +#endif // IDLC_JAVA_CION_GEN_JAVA_GROUP_GEN_CB_H_ diff --git a/idlc/gen_cion/java_cion_group_repo_gen.cc b/idlc/gen_cion/java_cion_group_repo_gen.cc new file mode 100644 index 0000000..2e8a4f9 --- /dev/null +++ b/idlc/gen_cion/java_cion_group_repo_gen.cc @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "idlc/gen_cion/java_cion_group_repo_gen.h" +#include "idlc/gen_cion/java_cion_gen_cb.h" + +#include +#include + +#include +#include + +namespace tidl { + +JavaCionGroupRepoGen::JavaCionGroupRepoGen( + std::shared_ptr doc) : JavaCionGeneratorBase(doc) { +} + +void JavaCionGroupRepoGen::OnInitGen(std::ofstream& stream) { + std::string fname = MakeDir(FileName, "/group"); + if (fname.empty()) + return; + + for (auto& i : GetDocument().GetBlocks()) { + if (i->GetType() != Block::TYPE_INTERFACE) + continue; + + stream.open(fname + "/GroupBase.java"); + stream << "package org.tizen.gen." << FileName + ".group;" << NLine(2); + stream << DEFAULT_GROUP_REPO; + stream.close(); + } +} + +void JavaCionGroupRepoGen::OnFiniGen(std::ofstream& stream) { +} + +std::string JavaCionGroupRepoGen::NLine(int cnt) { + std::string t; + + for (int i = 0; i < cnt; i++) { + t += "\n"; + } + + return t; +} + +} // namespace tidl diff --git a/idlc/gen_cion/java_cion_group_repo_gen.h b/idlc/gen_cion/java_cion_group_repo_gen.h new file mode 100644 index 0000000..5fb2665 --- /dev/null +++ b/idlc/gen_cion/java_cion_group_repo_gen.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_GEN_CION_JAVA_CION_GROUP_REPO_GEN_H_ +#define IDLC_GEN_CION_JAVA_CION_GROUP_REPO_GEN_H_ + +#include +#include +#include + +#include "idlc/gen_cion/java_cion_gen_base.h" + +namespace tidl { + +class JavaCionGroupRepoGen : public JavaCionGeneratorBase { + public: + explicit JavaCionGroupRepoGen(std::shared_ptr doc); + virtual ~JavaCionGroupRepoGen() = default; + + void OnInitGen(std::ofstream& stream) override; + void OnFiniGen(std::ofstream& stream) override; + std::string NLine(int cnt); +}; + +} // namespace tidl +#endif // IDLC_GEN_CION_JAVA_CION_GROUP_REPO_GEN_H_ diff --git a/idlc/main.cc b/idlc/main.cc index 6dad213..a170a54 100644 --- a/idlc/main.cc +++ b/idlc/main.cc @@ -53,6 +53,8 @@ #include "idlc/gen_cion/java_cion_stub_repo_gen.h" #include "idlc/gen_cion/java_cion_proxy_gen.h" #include "idlc/gen_cion/java_cion_proxy_repo_gen.h" +#include "idlc/gen_cion/java_cion_group_gen.h" +#include "idlc/gen_cion/java_cion_group_repo_gen.h" #include "idlc/gen_cion/java_cion_utility_gen.h" #include "idlc/gen_cion/java_cion_structure_gen.h" #include "idlc/gen_cion/java_cion_common_gen.h" @@ -273,8 +275,23 @@ void GenerateGroupCodes(std::shared_ptr options, break; } case tidl::Options::LANGUAGE_TYPE_JAVA: - break; + { + tidl::JavaCionGroupRepoGen repo(ps.GetDoc()); + repo.Run(options->GetOutput(), true); + + tidl::JavaCionGroupGen view_model(ps.GetDoc()); + view_model.Run(options->GetOutput(), true); + + tidl::JavaCionUtilityGen utilities(ps.GetDoc()); + utilities.Run(options->GetOutput(), true); + tidl::JavaCionStructureGen structures(ps.GetDoc()); + structures.Run(options->GetOutput(), true); + + tidl::JavaCionCommonGen cgen(ps.GetDoc()); + cgen.Run(options->GetOutput(), true); + break; + } default: break; } -- 2.7.4