Modify dart generator implementation 32/282132/10
authorChanggyu Choi <changyu.choi@samsung.com>
Tue, 27 Sep 2022 07:41:14 +0000 (16:41 +0900)
committerChanggyu Choi <changyu.choi@samsung.com>
Fri, 30 Sep 2022 07:06:45 +0000 (07:06 +0000)
Changes:
 - Changes some method names.
 - Adds final keyword.
 - Adds type to return variable.
 - Adds disconnect() method in proxy.
 - Removes await keyword from some changed api.
 - Fix some descriptions.

Change-Id: I3b3cfb1c3b9681b4bb7b1b11c1503d56c52c4b55
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
idlc/gen/dart_gen_base.cc
idlc/gen/dart_gen_base.h
idlc/gen/dart_gen_base_cb.h
idlc/gen/dart_proxy_gen.cc
idlc/gen/dart_proxy_gen_cb.h
idlc/gen/dart_stub_gen.cc
idlc/gen/dart_stub_gen_cb.h

index e8ff539..281ed10 100644 (file)
@@ -101,12 +101,6 @@ void DartGeneratorBase::GenImport(std::ofstream& stream) {
   stream << CB_IMPORT << std::endl;
 }
 
-void DartGeneratorBase::GenLogTag(std::ofstream& stream,
-    const std::string& log_tag) {
-  ReplaceAll(CB_LOG_TAG, "<LOG_TAG>", log_tag).Out(stream);
-  stream << NLine(1);
-}
-
 void DartGeneratorBase::GenVersion(std::ofstream& stream) {
   ReplaceAll(CB_VERSION_DEF, "<VERSION>", FULLVER).Out(stream);
   stream << NLine(1);
index a2241a1..018a553 100644 (file)
@@ -35,7 +35,6 @@ class DartGeneratorBase : public Generator {
 
   void GenAnnotation(std::ofstream& stream);
   void GenImport(std::ofstream& stream);
-  void GenLogTag(std::ofstream& stream, const std::string& log_tag);
   void GenVersion(std::ofstream& stream);
   void GenDelegateId(std::ofstream& stream);
   void GenMethodId(std::ofstream& stream);
index 3dbc755..36c55ed 100644 (file)
@@ -29,7 +29,6 @@ R"__dart_cb(
 
 constexpr const char CB_IMPORT[] =
 R"__dart_cb(
-import 'package:tizen_log/tizen_log.dart';
 import 'package:tizen_rpc_port/tizen_rpc_port.dart';
 )__dart_cb";
 
@@ -85,39 +84,32 @@ R"__dart_cb(<NAME>(<VALUE>))__dart_cb";
  */
 constexpr const char CB_CALLBACK_BASE[] =
 R"__dart_cb(
-/// Abstract class for creating a [CallbackBase] class for RPC.
-abstract class CallbackBase extends Parcelable {
-  /// Constructor for this class.
-  CallbackBase(this._id, this._once) {
-    _seqId = _seqNum++;
-  }
-
-  /// Creating a [CallbackBase] class from the parcel.
-  CallbackBase.fromParcel(Parcel parcel) {
-    deserialize(parcel);
+abstract class _CallbackBase extends Parcelable {
+  _CallbackBase(this.id, this.once) {
+    sequenceId = sequenceNum++;
   }
 
-  int _id = 0;
-  bool _once = false;
-  int _seqId = 0;
-  static int _seqNum = 0;
+  int id = 0;
+  bool once = false;
+  int sequenceId = 0;
+  static int sequenceNum = 0;
 
   /// Gets the tag.
-  String get tag => '$_id::$_seqId';
+  String get tag => '$id::$sequenceId';
   <ON_RECEIVED_EVENT>
 
   @override
   void serialize(Parcel parcel) {
-    parcel.writeInt32(_id);
-    parcel.writeInt32(_seqId);
-    parcel.writeBool(_once);
+    parcel.writeInt32(id);
+    parcel.writeInt32(sequenceId);
+    parcel.writeBool(once);
   }
 
   @override
   void deserialize(Parcel parcel) {
-    _id = parcel.readInt32();
-    _seqId = parcel.readInt32();
-    _once = parcel.readBool();
+    id = parcel.readInt32();
+    sequenceId = parcel.readInt32();
+    once = parcel.readBool();
   }
 }
 )__dart_cb";
@@ -208,14 +200,14 @@ R"__dart_cb(<NAME> = <PARCEL>.read<PARCEL_TYPE>();)__dart_cb";
  * <ARG> The argument.
  */
 constexpr const char CB_FILE_SET_PRIVATE_SHARING[] =
-R"__dart_cb(await <PORT>.setPrivateSharing(<ARG>);)__dart_cb";
+R"__dart_cb(<PORT>.shareFile(<ARG>);)__dart_cb";
 
 /**
  * <PORT> The rpc port handle.
  * <ARG> The argument.
  */
 constexpr const char CB_LIST_FILE_SET_PRIVATE_SHARING[] =
-R"__dart_cb(await <PORT>.setPrivateSharingList(<ARG>);)__dart_cb";
+R"__dart_cb(<PORT>.shareFiles(<ARG>);)__dart_cb";
 
 /**
  * <SERIALIZE> The implementation to serialize the data to the parcel.
@@ -240,7 +232,6 @@ class ListSerializer {
       <SERIALIZE>
       default:
         {
-          Log.error(_logTag, 'No such type');
           return;
         }
     }
@@ -252,7 +243,6 @@ class ListSerializer {
       <DESERIALIZE>
       default:
         {
-          Log.error(_logTag, 'No such type');
           return;
         }
     }
index 522f302..7364140 100644 (file)
@@ -30,7 +30,6 @@ DartProxyGen::DartProxyGen(std::shared_ptr<Document> doc)
 void DartProxyGen::OnInitGen(std::ofstream& stream) {
   GenAnnotation(stream);
   GenImport(stream);
-  GenLogTag(stream, "RPC_PORT_PROXY");
   GenVersion(stream);
   GenDelegateId(stream);
   GenMethodId(stream);
@@ -167,23 +166,24 @@ std::string DartProxyGen::GenMethodResultParcelRead(
     const Declaration& decl) {
   std::string code;
   auto& type = decl.GetType();
+  std::string prefix = "final " + ConvertTypeToString(type) + " ret";
   if (type.IsUserDefinedType()) {
     code += ReplaceAll(CB_USER_DEFINED_PARCEL_READ, {
           { "<PARCEL>", "parcelReceived" },
-          { "<NAME>", "final ret" }
+          { "<NAME>", prefix }
         });
     code += NLine(1);
   } else if (type.ToString() == "list" || type.ToString() == "array") {
     code += ReplaceAll(CB_LIST_PARCEL_READ, {
           { "<PARCEL>", "parcelReceived" },
-          { "<NAME>", "final ret" }
+          { "<NAME>", prefix }
         });
     code += NLine(1);
   } else {
     code += ReplaceAll(CB_BASE_PARCEL_READ, {
           { "<PARCEL>", "parcelReceived" },
           { "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
-          { "<NAME>" , "final ret" }
+          { "<NAME>" , prefix }
         });
     code += NLine(1);
   }
@@ -297,7 +297,7 @@ std::string DartProxyGen::GenDelegateParcelRead(const Declaration& decl) {
   for (const auto& p : decl.GetParameters()) {
     auto& param_type = p->GetParameterType();
     auto& type = param_type.GetBaseType();
-    code += ConvertTypeToString(type) + " ";
+    code += "final " + ConvertTypeToString(type) + " ";
     if (type.IsUserDefinedType()) {
       code += ReplaceAll(CB_USER_DEFINED_PARCEL_READ, {
             { "<NAME>", p->GetID() },
index 5035279..33cd0f4 100644 (file)
@@ -28,7 +28,7 @@ namespace tidl {
 constexpr const char CB_DELEGATE_BASE[] =
 R"__dart_cb(
 /// The <DELEGATE_NAME> class to invoke the delegate method.
-abstract class <DELEGATE_NAME> extends CallbackBase {
+abstract class <DELEGATE_NAME> extends _CallbackBase {
   /// Constructor for this class.
   <DELEGATE_NAME>({bool once = false}) : super(_DelegateId.<DELEGATE_ID>.id, once);
 
@@ -54,15 +54,15 @@ abstract class <INTERFACE_NAME> extends ProxyBase {
   <INTERFACE_NAME>(String appid) : super(appid, '<INTERFACE_NAME>');
 
   bool _online = false;
-  final List<CallbackBase> _delegateList = <CallbackBase>[];
+  final List<_CallbackBase> _delegateList = <_CallbackBase>[];
 
-  /// This abstract method will be called when the connet() is succeed.
+  /// The abstract method for receiving connected event.
   Future<void> onConnected();
 
-  /// This abstract method will be called when the connection with the stub is disconnected.
+  /// The abstract method for receiving disconnected event.
   Future<void> onDisconnected();
 
-  /// This abstract method will be called when connect() is failed.
+  /// The abstract method for receiving rejected event.
   Future<void> onRejected(String errorMessage);
 
   @override
@@ -89,18 +89,14 @@ abstract class <INTERFACE_NAME> extends ProxyBase {
       return;
     }
 
-    await _processReceivedEvent(parcel);
-  }
-
-  Future<void> _processReceivedEvent(Parcel parcel) async {
     final int id = parcel.readInt32();
     final int seqId = parcel.readInt32();
     final bool once = parcel.readBool();
 
-    for (final CallbackBase delegate in _delegateList) {
-      if (delegate._id == id && delegate._seqId == seqId) {
+    for (final _CallbackBase delegate in _delegateList) {
+      if (delegate.id == id && delegate.sequenceId == seqId) {
         await delegate._onReceivedEvent(parcel);
-        if (delegate._once) {
+        if (delegate.once) {
           _delegateList.remove(delegate);
         }
         break;
@@ -109,31 +105,35 @@ abstract class <INTERFACE_NAME> extends ProxyBase {
   }
 
   Future<Parcel> _consumeCommand(Port port) async {
-    while (true) {
-      try {
-        final Parcel parcel = await port.receive();
-        final int cmd = parcel.readInt32();
-        if (cmd == _MethodId.result.id) {
-          return parcel;
-        }
-      } catch (e) {
-        Log.error(_logTag, e.toString());
-        return Parcel();
+    try {
+      final Parcel parcel = Parcel.fromPort(port);
+      final int cmd = parcel.readInt32();
+
+      if (cmd != _MethodId.result.id) {
+        throw Exception('Received parcel is invalid. $cmd');
       }
+
+      return parcel;
+    } catch (e) {
+      return Parcel();
     }
   }
 
   /// Connects with the stub application.
   @override
   Future<void> connect() async {
-    Log.info(_logTag, 'connect()');
     await super.connect();
   }
 
+  /// Disconnects with the stub application.
+  Future<void> disconnect() async {
+    final Port port = getPort(PortType.main);
+    port.disconnect();
+  }
+
   /// Dispose registered delegate interface.
   void disposeCallback(String tag) {
-    Log.info(_logTag, 'disposeCallback($tag)');
-    _delegateList.removeWhere((CallbackBase element) => element.tag == tag);
+    _delegateList.removeWhere((_CallbackBase element) => element.tag == tag);
   }
 
   <METHODS>
@@ -152,7 +152,6 @@ constexpr const char CB_METHOD_BASE[] =
 R"__dart_cb(
 /// This method is used to send '<METHOD_NAME>' request to the stub app.
 Future<<RETURN_TYPE>> <LOWER_METHOD_NAME>(<ARGS>) async {
-  Log.info(_logTag, '<METHOD_NAME>');
   <METHOD_PARCEL_WRITE>
   <METHOD_PARCEL_READ>
 }
@@ -177,7 +176,7 @@ parcel.writeInt32(_MethodId.<METHOD_NAME>.id);
 
 <PARCEL_WRITE>
 
-await port.send(parcel);
+parcel.send(port);
 )__dart_cb";
 
 /**
index df58931..199f7d7 100644 (file)
@@ -27,7 +27,6 @@ DartStubGen::DartStubGen(std::shared_ptr<Document> doc)
 void DartStubGen::OnInitGen(std::ofstream& stream) {
   GenAnnotation(stream);
   GenImport(stream);
-  GenLogTag(stream, "RPC_PORT_STUB");
   GenVersion(stream);
   GenDelegateId(stream);
   GenMethodId(stream);
index af7b8a3..a92a5f8 100644 (file)
@@ -40,7 +40,7 @@ abstract class ServiceBase {
 
   /// Disconnects from the client application.
   Future<void> disconnect() async {
-    await _port?.disconnect();
+    _port?.disconnect();
     _port = null;
   }
 
@@ -72,7 +72,7 @@ Future<<RETURN_TYPE>> on<METHOD_NAME>(<METHOD_PARAMS>);)__dart_cb";
 constexpr const char CB_DELEGATE_BASE[] =
 R"__dart_cb(
 /// The '<DELEGATE_NAME> class to invoke the delegate method.
-class <DELEGATE_NAME> extends CallbackBase {
+class <DELEGATE_NAME> extends _CallbackBase {
   /// Constructor for this class.
   <DELEGATE_NAME>(this._port, this.service, {bool once = false})
       : super(_DelegateId.<DELEGATE_ID>.id, once);
@@ -86,12 +86,10 @@ class <DELEGATE_NAME> extends CallbackBase {
   /// Invokes the delegate method of the client.
   Future<void> invoke(<DELEGATE_PARAMS>) async {
     if (_port == null) {
-      Log.error(_logTag, 'port is null');
       throw Exception('NotConnectedSocketException');
     }
 
-    if (_once && !_valid) {
-      Log.error(_logTag, 'invalid');
+    if (once && !_valid) {
       throw Exception('InvalidCallbackException');
     }
 
@@ -101,7 +99,10 @@ class <DELEGATE_NAME> extends CallbackBase {
 
     <DELEGATE_PARCEL_WRITE>
 
-    await _port?.send(parcel);
+    if (_port != null) {
+      parcel.send(_port!);
+    }
+
     _valid = false;
   }
 }
@@ -128,7 +129,6 @@ abstract class <INTERFACE_NAME> extends StubBase {
   /// Listens to the requests for connections.
   @override
   Future<void> listen() async {
-    Log.warn(_logTag, 'listen. portName: $portName');
     return await super.listen();
   }
 
@@ -137,7 +137,6 @@ abstract class <INTERFACE_NAME> extends StubBase {
 
   @override
   Future<void> onConnectedEvent(String sender, String instance) async {
-    Log.info(_logTag, 'sender: $sender, instance: $instance');
     final ServiceBase service = createInstance(sender, instance);
     service._port = getPort(instance, PortType.callback);
     await service.onCreate();
@@ -146,7 +145,6 @@ abstract class <INTERFACE_NAME> extends StubBase {
 
   @override
   Future<void> onDisconnectedEvent(String sender, String instance) async {
-    Log.info(_logTag, 'sender: $sender, instance: $instance');
     for (final ServiceBase service in services) {
       if (service.instance == instance) {
         await service.onTerminate();
@@ -161,7 +159,6 @@ abstract class <INTERFACE_NAME> extends StubBase {
   @override
   Future<void> onReceivedEvent(String sender, String instance,
       Parcel parcel) async {
-    Log.info(_logTag, 'sender: $sender, instance: $instance');
     ServiceBase? service;
     for (final ServiceBase s in services) {
       if (s.instance == instance) {
@@ -171,7 +168,6 @@ abstract class <INTERFACE_NAME> extends StubBase {
     }
 
     if (service == null) {
-      Log.error(_logTag, 'service is null');
       return;
     }
 
@@ -179,8 +175,6 @@ abstract class <INTERFACE_NAME> extends StubBase {
     final int cmd = parcel.readInt32();
     if (_methodHandlers.containsKey(cmd)) {
       await _methodHandlers[cmd](service, port, parcel);
-    } else {
-      Log.error(_logTag, 'Unknown cmd: $cmd');
     }
   }
 }
@@ -204,7 +198,6 @@ constexpr const char CB_METHOD_HANDLER_BASE[] =
 R"__dart_cb(
 Future<void> _on<METHOD_NAME>Method(ServiceBase service, Port port,
     Parcel parcel) async {
-  Log.info(_logTag, '<METHOD_NAME>');
   <METHOD_HANDLER_PARCEL_READ>
   <METHOD_HANDLER_INVOKE>
   <METHOD_HANDLER_PARCEL_WRITE>
@@ -232,7 +225,7 @@ resultHeader.sequenceNumber = header.sequenceNumber;
 result.writeInt32(_MethodId.result.id);
 <PARCEL_WRITE>
 
-await port.send(result);
+result.send(port);
 )__dart_cb";
 
 }  // namespace tidl