[cion] Fix file transfer logic 30/291530/1
authorjusung son <jusung07.son@samsung.com>
Tue, 18 Apr 2023 05:37:59 +0000 (14:37 +0900)
committerjusung son <jusung07.son@samsung.com>
Tue, 18 Apr 2023 05:37:59 +0000 (14:37 +0900)
Change-Id: I40035e859a92b23265ed8d4387d033dff09ba463
Signed-off-by: jusung son <jusung07.son@samsung.com>
idlc/gen_cion/dart_cion_gen_base.cc
idlc/gen_cion/dart_cion_gen_base.h
idlc/gen_cion/dart_cion_gen_base_cb.h
idlc/gen_cion/dart_cion_proxy_gen.cc
idlc/gen_cion/dart_cion_proxy_gen_cb.h
idlc/gen_cion/dart_cion_stub_gen.cc
idlc/gen_cion/dart_cion_stub_gen_cb.h
idlc/gen_cion/dart_transportable.h
idlc/gen_cion/default_dart_transportable.cc
idlc/gen_cion/default_dart_transportable.h

index 5c3b440375f211e8b95de88ba801a7106886f85b..3d8203293b21bd9ae0674ef9c60c86570f91b351 100644 (file)
@@ -363,7 +363,7 @@ std::string DartCionGeneratorBase::GenBaseParcelWrite(const Elements& elms) {
   }
 
   if (has_file)
-    code = GetFileSharing(code, "this");
+    code = GetFileSharing(code, "this.send(payload);");
   return code;
 }
 
@@ -591,6 +591,19 @@ std::string DartCionGeneratorBase::GetClassName(std::string name) {
   return name;
 }
 
+std::string DartCionGeneratorBase::GetFullNameFromType(const BaseType& type) {
+  std::string str;
+
+  str += type.ToString();
+
+  if (type.GetMetaType() != nullptr) {
+    str += "_";
+    str += GetFullNameFromType(*type.GetMetaType());
+  }
+
+  return str;
+}
+
 std::string DartCionGeneratorBase::GetFullNameFromType(const BaseType& type,
     const Interface& iface) {
   std::string str;
@@ -612,23 +625,86 @@ std::string DartCionGeneratorBase::GetFullNameFromType(const BaseType& type,
   return str;
 }
 
-bool DartCionGeneratorBase::HasFile(const Interface& iface) {
+bool DartCionGeneratorBase::HasFile(const Interface& iface, const BaseType& type) {
+  if (type.ToString() == "list" || type.ToString() == "array") {
+     std::string name = GetFullNameFromType(type, iface);
+     if (name == "array_file" || name == "list_file" || name == "file")
+       return true;
+  } else if (type.ToString() == "file") {
+       return true;
+  }
+
+  return false;
+}
+
+bool DartCionGeneratorBase::HasFile(const BaseType& type) {
+  if (type.ToString() == "list" || type.ToString() == "array") {
+     std::string name = GetFullNameFromType(type);
+     if (name == "array_file" || name == "list_file" || name == "file")
+       return true;
+  } else if (type.ToString() == "file") {
+       return true;
+  }
+
+  return false;
+}
+
+bool DartCionGeneratorBase::HasFile(const Declaration& decl) {
+  for (const auto& p : decl.GetParameters()) {
+    auto& param_type = p->GetParameterType();
+    auto& type = param_type.GetBaseType();
+
+    if (type.ToString() == "file")
+      return true;
+
+    if (type.ToString() == "list" || type.ToString() == "array") {
+      if (type.GetMetaType() != nullptr &&
+          type.GetMetaType()->ToString() == "file")
+        return true;
+
+    }
+  }
+  return false;
+}
+
+bool DartCionGeneratorBase::HasFile(const Interface& iface, bool is_proxy) {
   for (const auto& d : iface.GetDeclarations()) {
+    if (is_proxy) {
+      if (HasFile(iface, d->GetType()) == true)
+      return true;
+    }
+
     for (const auto& p : d->GetParameters()) {
       auto& param_type = p->GetParameterType();
-      auto& type = param_type.GetBaseType();
-      auto name = GetFullNameFromType(type, iface);
-      if (name == "file" || name == "list_file" || name == "array_file")
-        return true;
+      if (is_proxy) {
+        if (d->GetMethodType() == Declaration::MethodType::DELEGATE) {
+          if (param_type.GetDirection() != ParameterType::Direction::IN)
+          continue;
+        } else {
+          if (param_type.GetDirection() == ParameterType::Direction::IN)
+            continue;
+        }
+        if (HasFile(iface, param_type.GetBaseType()) == true)
+          return true;
+      } else {
+        if (d->GetMethodType() == Declaration::MethodType::DELEGATE) {
+          if (param_type.GetDirection() == ParameterType::Direction::IN)
+            continue;
+        } else {
+          if (param_type.GetDirection() != ParameterType::Direction::IN)
+            continue;
+        }
+        if (HasFile(iface, param_type.GetBaseType()) == true)
+          return true;
+      }
     }
   }
-
   return false;
 }
 
 std::string DartCionGeneratorBase::GetFileSharing(std::string src, std::string sender) {
   std::string file_send;
-  file_send += ReplaceAll(CB_FILE_SEND, "<SENDER>", sender);
+  file_send += ReplaceAll(CB_FILE_SEND, "<SEND_ASYNC_FILE>", sender);
   return CB_FILE_LIST + NLine(1) + src + NLine(1) + file_send + NLine(1);
 }
 
index 8e13591ff5d7858a1c779af5576f7d54b742107d..077d2c380f9cb5040210c801e78796e38aad6e75 100644 (file)
@@ -54,7 +54,10 @@ class DartCionGeneratorBase : public CionPluginBase {
   std::string Tab(int cnt);
   std::string Trim(const std::string& str);
 
-  bool HasFile(const Interface& iface);
+  bool HasFile(const Interface& iface, bool is_proxy);
+  bool HasFile(const BaseType& type);
+  bool HasFile(const Interface& iface, const BaseType& type);
+  bool HasFile(const Declaration& decl);
   std::string GetFileSharing(std::string src, std::string sender);
 
  private:
@@ -70,6 +73,7 @@ class DartCionGeneratorBase : public CionPluginBase {
   void GenMethodId(std::ofstream& stream, const Interface& iface);
   std::string GetFullNameFromType(const BaseType& type,
     const Interface& iface);
+  std::string GetFullNameFromType(const BaseType& type);
 
  protected:
   const int TAB_SIZE = 2;
index eab8f77a28e7c740634367e6321cd78a35aa66f4..77f7c211e63eb3c4be9afbdcda11739599aed9e2 100644 (file)
@@ -234,7 +234,7 @@ constexpr const char CB_FILE_SEND[] =
 R"__dart_cb(
 for (final String file in fileList) {
   final Payload payload = FilePayload(file);
-  <SENDER>.send(payload);
+  <SEND_ASYNC_FILE>
 }
 )__dart_cb";
 
index a88d933446589a5bb8aa52dbe6451a8569943b1e..ce62622dcb1b014afbe36e798216e384aa35072d 100644 (file)
@@ -64,9 +64,14 @@ void DartCionProxyGen::GenInterfaceBase(std::ofstream& stream,
     const Interface& iface) {
   std::string name = iface.GetID();
   name[0] = ::toupper(name[0]);
+
+  bool has_file = HasFile(iface, true);
+
   ReplaceAll(CB_INTERFACE_BASE, {
         { "<INTERFACE_NAME>", name },
-        { "<METHODS>", GenInterfaceMethods(iface) }
+        { "<METHODS>", GenInterfaceMethods(iface) },
+        { "<FILE_REVEIVE_DEF>", has_file ? ", required OnFileReceived onFileReceived" : "" },
+        { "<FILE_REVEIVE_CB>", has_file ? "_onFileReceived = onFileReceived;" : "" }
       })
       .Transform([&](std::string str) {
         return SmartIndent(std::move(str));
@@ -100,6 +105,7 @@ std::string DartCionProxyGen::GenInterfaceMethods(const Interface& iface) {
           { "<ARGS>", args },
           { "<ASYNC>", GenAsync(*d) },
           { "<METHOD_PARCEL_WRITE>", GenMethodParcelWrite(*d) },
+          { "<SEND_ASYNC_FILE>", "_client.send(payload);" },
         });
   }
 
@@ -176,7 +182,7 @@ std::string DartCionProxyGen::GenMethodParcelWrite(const Declaration& decl) {
                       {{"<PARCEL_WRITE>", code}, {"<METHOD_NAME>", method_id}});
 
     if (has_file)
-      code = GetFileSharing(code, "_client");
+      code = GetFileSharing(code,"_client.send(payload);");
   } else {
     std::string read_code = std::move(GenMethodResultParcelRead(decl));
     read_code += GenMethodOutParamParcelRead(decl);
@@ -190,7 +196,7 @@ std::string DartCionProxyGen::GenMethodParcelWrite(const Declaration& decl) {
         {"<FILE_SEND>", CB_FILE_SEND},
       });
       code = ReplaceAll(code, {
-        {"<SENDER>", "_client"},
+        {"<SEND_ASYNC_FILE>", "_client.send(payload);"},
       });
     } else {
       code = ReplaceAll(code, {
index 8fb37a7b81c6f94b001ff448f4122137a6a145a5..f96a881f4db3a4c186b03c06ad8e7d7488dfa2dc 100644 (file)
@@ -50,6 +50,7 @@ class _<DELEGATE_NAME> extends _Delegate {
 constexpr const char CB_INTERFACE_BASE[] =
 R"__dart_cb(
 typedef OnDisconnected = Future<void> Function(PeerInfo);
+typedef OnFileReceived = Future<void> Function(PeerInfo, Payload, PayloadTransferStatus);
 
 /// [<INTERFACE_NAME>] class for Cion.
 class <INTERFACE_NAME> {
@@ -85,6 +86,12 @@ class <INTERFACE_NAME> {
 
   Future<void> _onReceivedEvent(
       PeerInfo peer, Payload payload, PayloadTransferStatus status) async {
+
+    if (payload.type == PayloadType.file) {
+      _onFileReceived?.call(peer, payload as FilePayload, status);
+      return;
+    }
+
     final Uint8List raw = (payload as DataPayload).data;
     final Parcel parcel = Parcel.fromRaw(raw);
 
@@ -109,7 +116,7 @@ class <INTERFACE_NAME> {
   }
 
   /// Connects with the server.
-  Future<bool> connect({required OnDisconnected onDisconnected}) async {
+  Future<bool> connect({required OnDisconnected onDisconnected<FILE_REVEIVE_DEF>}) async {
     if (_isConnected) {
       return true;
     }
@@ -118,6 +125,8 @@ class <INTERFACE_NAME> {
       throw StateError('Already try to connect');
     }
 
+
+    <FILE_REVEIVE_CB>
     _onDisconnected = onDisconnected;
     await _client.tryDiscovery(onDiscovered: _onDiscoveredEvent);
     _connectCompleter = Completer<bool>();
@@ -145,6 +154,7 @@ class <INTERFACE_NAME> {
   final String serverDisplayName;
   bool _isConnected = false;
   OnDisconnected? _onDisconnected;
+  OnFileReceived? _onFileReceived;
   Completer<bool>? _connectCompleter;
 }
 )__dart_cb";
index fa63a35150c78e8fe29e60a00144f5ce73642e89..bb7afd7f7645142d3017e864cc3ea57f7ac2fbf2 100644 (file)
@@ -67,7 +67,7 @@ void DartCionStubGen::GenInterfaceBase(std::ofstream& stream,
         { "<CTOR>", GenInterfaceCtor(iface) },
         { "<EXTEND_NAME>", GetTransportable().Dart().GenStubExtend() },
         { "<FILE_RECEIVE>",
-            HasFile(iface) ?  GetTransportable().Dart().GenFileRecive() : "" },
+            HasFile(iface, false) ?  GetTransportable().Dart().GenFileRecive() : "" },
         { "<PARAM_DEF>", GetTransportable().Dart().GenStubParamDef() },
         { "<SUPER_PARAM>", GetTransportable().Dart().GenStubSuperParam() },
         { "<INTERFACE_NAME>", name },
@@ -143,6 +143,10 @@ std::string DartCionStubGen::GenInterfaceMethodHandlers(const Interface& iface)
 
 std::string DartCionStubGen::GenMethodHandlerParcelRead(const Declaration& decl) {
   std::string code;
+
+  if (HasFile(decl.GetType()))
+    code += CB_FILE_LIST + NLine(1);
+
   for (const auto& p : decl.GetParameters()) {
     auto& param_type = p->GetParameterType();
     if (param_type.GetDirection() != ParameterType::Direction::IN)
@@ -225,10 +229,12 @@ std::string DartCionStubGen::GenMethodHandlerParcelWrite(const Declaration& decl
 
   std::string code = std::move(GenMethodHandlerResultParcelWrite(decl));
   code += GenMethodHandlerOutParamParcelWrite(decl);
-  code = ReplaceAll(CB_METHOD_HANDLER_PARCEL_WRITE, {
-             { "<PARCEL_WRITE>", code },
-             { "<SEND_ASYNC>", GetTransportable().Dart().GenSendAsync(
-                 "result", "service._serverBase", "service.peer") }});
+
+  if (HasFile(decl.GetType()))
+    code += ReplaceAll(CB_FILE_SEND, "<SEND_ASYNC_FILE>",
+        "service._serverBase!.send(service.peer!, payload);");
+
+  code = ReplaceAll(CB_METHOD_HANDLER_PARCEL_WRITE, "<PARCEL_WRITE>", code);
   return code;
 }
 
@@ -254,6 +260,9 @@ std::string DartCionStubGen::GenMethodHandlerResultParcelWrite(
           { "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
           { "<NAME>" , "ret" }
         });
+
+    if (HasFile(type))
+      code += ReplaceAll(CB_FILE_ADD, "<FILE>", "ret");
     code += NLine(1);
   }
 
@@ -299,7 +308,7 @@ void DartCionStubGen::GenServiceBase(std::ofstream& stream,
   ReplaceAll(CB_SERVICE_BASE, {
         { "<PEER_INFO_T>",  GetTransportable().Dart().GenPeerInfo() },
         { "<FILE_RECEIVE_DECL>",
-            HasFile(iface) ?  GetTransportable().Dart().GenFileReciveDef() : "" },
+            HasFile(iface, false) ?  GetTransportable().Dart().GenFileReciveDef() : "" },
         { "<METHOD_DECLS>", GenServiceBaseMethodDecls(iface) }
       })
       .Transform([&](std::string str) {
@@ -347,7 +356,9 @@ void DartCionStubGen::GenDelegateBase(std::ofstream& stream,
         { "<DELEGATE_NAME>", GetClassName(decl.GetID()) },
         { "<DELEGATE_ID>", id },
         { "<DELEGATE_PARAMS>", GenDelegateParams(decl) },
-        { "<SEND_ASYNC>", GetTransportable().Dart().GenSendAsync("parcel",
+        { "<SEND_FILE>", HasFile(decl) ? CB_FILE_SEND : "" },
+        { "<SEND_ASYNC_FILE>", "service._serverBase!.send(service.peer!, payload);" },
+        { "<SEND_ASYNC>", GetTransportable().Dart().GenStubSendAsync("parcel",
             "service._serverBase", "service.peer" ) },
         { "<DELEGATE_PARCEL_WRITE>", GenDelegateParcelWrite(decl) }
       })
@@ -415,7 +426,8 @@ std::string DartCionStubGen::GenDelegateParcelWrite(const Declaration& decl) {
   }
 
   if (has_file)
-    code = GetFileSharing(code, "this");
+    code = CB_FILE_LIST + NLine(1) + code;
+
   return code;
 }
 
index 3d4b995e0830fff4d6d54e82057a3d50937f5439..59ae4b2528cd983a9d1d3dffcbff3bc2958309fc 100644 (file)
@@ -81,6 +81,7 @@ R"__dart_cb(
 /// This abstract method will be called when the '<METHOD_NAME>' request is delivered.
 Future<<RETURN_TYPE>> on<METHOD_NAME>(<METHOD_PARAMS>);)__dart_cb";
 
+
 /**
  * <DELEGATE_NAME> The name of the delegate.
  * <DELEGATE_ID> The ID of the delegate.
@@ -115,7 +116,9 @@ class <DELEGATE_NAME> extends _Delegate {
 
     <DELEGATE_PARCEL_WRITE>
 
-               <SEND_ASYNC>
+    <SEND_ASYNC>
+
+    <SEND_FILE>
 
     _wasInvoked = true;
   }
@@ -182,7 +185,6 @@ class <INTERFACE_NAME> extends <EXTEND_NAME> {
           if (service == null) {
           return returnData;
           }
-          <FILE_RECEIVE>
 
           final Parcel parcel = Parcel.fromRaw(data);
 
index 5939ad0cc1a6fff13482f08cd496f395ffc4062b..4040e02374ed1d100d6d695addd1e902a5610d21 100644 (file)
@@ -27,7 +27,7 @@ class DartTransportable {
   virtual ~DartTransportable() = default;
   virtual std::string GenImport() const = 0;
   virtual std::string GenPeerInfo() const = 0;
-  virtual std::string GenSendAsync(std::string parcel,
+  virtual std::string GenStubSendAsync(std::string parcel,
       std::string server, std::string peer) const = 0;
   virtual std::string GenFileReciveDef() const = 0;
   virtual std::string GenFileRecive() const = 0;
index da89282854232fe45e5d25fcf05eb88da7a47c83..b82e4b46720120dfc1e7a65536c84030f466de29 100644 (file)
@@ -21,7 +21,7 @@
 #include "idlc/gen/replace_all.h"
 
 namespace {
-constexpr const char ___SEND_ASYNC[] =
+constexpr const char ___STUB_SEND_ASYNC[] =
 R"__c_cb(
   final DataPayload dp = DataPayload(<PARCEL_NAME>.asRaw());
   <SERVER_NAME>!.send(<PEER_NAME>!, dp);
@@ -44,9 +44,9 @@ std::string DefaultDartTransportable::GenPeerInfo() const {
   return "PeerInfo";
 }
 
-std::string DefaultDartTransportable::GenSendAsync(std::string parcel,
+std::string DefaultDartTransportable::GenStubSendAsync(std::string parcel,
     std::string server, std::string peer) const {
-  return std::string(ReplaceAll(___SEND_ASYNC, {
+  return std::string(ReplaceAll(___STUB_SEND_ASYNC, {
       { "<PARCEL_NAME>", parcel },
       { "<PEER_NAME>", peer },
       { "<SERVER_NAME>", server } }));
index 407f39260513c892c7b8fc4dc8d94bba4ad8e4d5..df557581340afce669e2fc93bbd50b1c4c1bbd5c 100644 (file)
@@ -28,7 +28,7 @@ class DefaultDartTransportable : public DartTransportable {
   virtual ~DefaultDartTransportable() = default;
   std::string GenImport() const override;
   std::string GenPeerInfo() const override;
-  std::string GenSendAsync(std::string parcel,
+  std::string GenStubSendAsync(std::string parcel,
       std::string server, std::string peer) const override;
   std::string GenFileReciveDef() const override;
   std::string GenFileRecive() const override;