Skip to content

fix: Enhance cooperation status management#662

Merged
re2zero merged 2 commits intolinuxdeepin:masterfrom
re2zero:bugfix
Aug 7, 2025
Merged

fix: Enhance cooperation status management#662
re2zero merged 2 commits intolinuxdeepin:masterfrom
re2zero:bugfix

Conversation

@re2zero
Copy link
Contributor

@re2zero re2zero commented Aug 7, 2025

  • Updated ServiceManager to set "share_connect_ip" to an empty string.
  • Added methods in HandleIpcService to update and retrieve cooperation status.
  • Enhanced HandleRpcService to check current cooperation status before accepting new connection requests.
  • Implemented busy status handling in NetworkUtil for share requests.
  • Updated ShareHelper to manage cooperation status during connection and disconnection events.

Log: Enhance cooperation status management for better handling of connection requests and status updates.

Summary by Sourcery

Improve cooperation status management by centralizing status updates, implementing busy-state handling to reject overlapping share requests, and synchronizing status across NetworkUtil, ShareHelper, and IPC/RPC services

New Features:

  • Add explicit busy rejection support in NetworkUtil with replyShareRequestBusy and specialized response codes
  • Introduce cooperation status methods (isCurrentlyCooperating, updateCooperationStatus) and corresponding IPC calls in HandleIpcService

Enhancements:

  • Prevent new share connections during active sessions by checking cooperation status in ShareHelper and HandleRpcService
  • Notify IPC backend of cooperation status changes on connection start and end in ShareHelper
  • Initialize ServiceManager.share_connect_ip to empty and streamline fallback detection in RPC handler

- Updated ServiceManager to set "share_connect_ip" to an empty string.
- Added methods in HandleIpcService to update and retrieve cooperation status.
- Enhanced HandleRpcService to check current cooperation status before accepting new connection requests.
- Implemented busy status handling in NetworkUtil for share requests.
- Updated ShareHelper to manage cooperation status during connection and disconnection events.

Log: Enhance cooperation status management for better handling of connection requests and status updates.
@sourcery-ai
Copy link

sourcery-ai bot commented Aug 7, 2025

Reviewer's Guide

Centralize and enhance cooperation status management by introducing IPC interfaces, adding status checks in RPC logic, extending NetworkUtil for busy handling and status tracking, integrating status updates in ShareHelper, and resetting initial share_connect_ip in ServiceManager.

Sequence diagram for handling incoming share connection requests with busy status

sequenceDiagram
    participant Requester as Requesting Device
    participant ShareHelper as ShareHelper
    participant NetworkUtil as NetworkUtil
    participant CompatWrapper as CompatWrapper (optional)
    participant IPC as HandleIpcService (optional)

    Requester->>ShareHelper: Send connection request
    ShareHelper->>NetworkUtil: isCurrentlyCooperating()
    NetworkUtil->>ShareCooperationServiceManager: Check server/client running
    alt ENABLE_COMPAT
        NetworkUtil->>CompatWrapper: getCurrentCooperationStatus()
        CompatWrapper->>IPC: getCurrentCooperationStatus()
        IPC-->>CompatWrapper: Return status
    end
    NetworkUtil-->>ShareHelper: Return cooperation status
    alt Already cooperating
        ShareHelper->>NetworkUtil: replyShareRequestBusy(senderIp)
        NetworkUtil->>Requester: Send BUSY_COOPERATING rejection
    else Not cooperating
        ShareHelper->>...: Proceed with normal connection flow
    end
Loading

ER diagram for cooperation status and share_connect_ip changes

erDiagram
    NODE_PEER_INFO {
        string username
        string hostname
        string ipv4
        string share_connect_ip
        int port
        string os_type
        string mode_type
    }
    NODE_PEER_INFO ||--o{ COOPERATION_STATUS : has
    COOPERATION_STATUS {
        int status
    }
Loading

Class diagram for enhanced cooperation status management

classDiagram
    class NetworkUtil {
        +void replyShareRequestBusy(QString targetIp)
        +bool isCurrentlyCooperating()
        +void updateCooperationStatus(int status)
    }
    class ShareHelper {
        +void notifyConnectRequest(QString info)
        +void handleConnectResult(int result, QString clientprint)
        +void disconnectToDevice(DeviceInfoPointer info)
        +void handleDisConnectResult(QString devName)
    }
    class HandleIpcService {
        +void updateCooperationStatus(int status)
        +bool getCurrentCooperationStatus()
    }
    class HandleRpcService {
        +void handleRemoteShareConnect(co::Json &info)
    }
    class ServiceManager {
        +fastring genPeerInfo()
    }
    NetworkUtil --|> ShareHelper : used by
    NetworkUtil -- HandleIpcService : IPC calls
    ShareHelper -- NetworkUtil : uses
    HandleRpcService -- HandleIpcService : may call for status
    ServiceManager ..> NetworkUtil : sets share_connect_ip
    HandleIpcService <..> Comshare : updates/gets status
Loading

File-Level Changes

Change Details Files
Introduce IPC methods for updating and querying cooperation status
  • Added updateCooperationStatus(int) in HandleIpcService
  • Added getCurrentCooperationStatus() in HandleIpcService
  • Invoke Comshare::instance()->updateStatus and query currentStatus
src/compat/plugins/daemon/core/service/ipc/handleipcservice.h
src/compat/plugins/daemon/core/service/ipc/handleipcservice.cpp
Enforce cooperation status check in RPC handler
  • Query currentStatus from Comshare before processing
  • Reject new connections if status==SHARE_START with SHARE_CONNECT_ERR_CONNECTED
  • Fallback to DiscoveryJob share_connect_ip check
src/compat/plugins/daemon/core/service/rpc/handlerpcservice.cpp
Extend NetworkUtil with busy response and status tracking
  • Refactored replyShareRequest to compute and pass resultCode
  • Implemented replyShareRequestBusy for BUSY_COOPERATING flag
  • Added isCurrentlyCooperating() to detect ongoing sessions
  • Added updateCooperationStatus(int) to send IPC status updates
src/lib/cooperation/core/net/networkutil.cpp
src/lib/cooperation/core/net/networkutil.h
Integrate cooperation status updates and busy rejection in ShareHelper
  • Call updateCooperationStatus(CURRENT_STATUS_SHARE_START) on connect success
  • Call updateCooperationStatus(0) on disconnect
  • In notifyConnectRequest, check isCurrentlyCooperating and send busy rejection
src/lib/cooperation/core/net/helper/sharehelper.cpp
Reset share_connect_ip to empty string in ServiceManager
  • Changed default share_connect_ip from Util::getFirstIp() to empty string
src/compat/plugins/daemon/core/service/servicemanager.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @re2zero - I've reviewed your changes - here's some feedback:

  • Replace hard-coded status codes (e.g., 6 and 0) and string literals like "BUSY_COOPERATING" with named enums or constants to improve readability and reduce maintenance risk.
  • Consider consolidating the cooperation-status check logic in NetworkUtil and the RPC/IPC handlers into a shared utility or service to avoid duplication and ensure consistent behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Replace hard-coded status codes (e.g., 6 and 0) and string literals like "BUSY_COOPERATING" with named enums or constants to improve readability and reduce maintenance risk.
- Consider consolidating the cooperation-status check logic in NetworkUtil and the RPC/IPC handlers into a shared utility or service to avoid duplication and ensure consistent behavior.

## Individual Comments

### Comment 1
<location> `src/lib/cooperation/core/net/networkutil.cpp:120` </location>
<code_context>
+            int resultCode;
+            if (agree) {
+                resultCode = SHARE_CONNECT_COMFIRM;
+            } else if (req.nick == "BUSY_COOPERATING") {
+                WLOG << "Device is busy, use ERR_CONNECTED to show appropriate message";
+                // Device is busy, use ERR_CONNECTED to show appropriate message
</code_context>

<issue_to_address>
Using a magic string for busy status may reduce maintainability.

Consider replacing the hardcoded "BUSY_COOPERATING" string with a constant or enum to improve clarity and reduce maintenance risks.
</issue_to_address>

### Comment 2
<location> `src/lib/cooperation/core/net/networkutil.cpp:870` </location>
<code_context>
+    // 直接发送忙碌拒绝消息
+    ApplyMessage msg;
+    msg.flag = REPLY_REJECT;
+    msg.nick = "BUSY_COOPERATING"; // Special marker for busy status
+    msg.host = CooperationUtil::localIPAddress().toStdString();
+    msg.fingerprint = ""; // No fingerprint needed for busy response
</code_context>

<issue_to_address>
Consider using a constant for the busy status marker.

Using a shared constant or enum for "BUSY_COOPERATING" will reduce duplication and make updates easier.

Suggested implementation:

```cpp
    msg.nick = BUSY_COOPERATING_NICK; // Special marker for busy status

```

You also need to:
1. Define `BUSY_COOPERATING_NICK` in a suitable header file (e.g., `networkutil.h` or a shared constants header), for example:
   ```cpp
   constexpr const char* BUSY_COOPERATING_NICK = "BUSY_COOPERATING";
   ```
2. Include that header in `networkutil.cpp` if it is not already included.
</issue_to_address>

### Comment 3
<location> `src/compat/plugins/daemon/core/service/rpc/handlerpcservice.cpp:267` </location>
<code_context>
+    DLOG << "Current Comshare status:" << currentStatus << " isCurrentlyCooperating:" << isCurrentlyCooperating;
+
+    // 如果正在协同,直接拒绝新的连接请求
+    if (isCurrentlyCooperating) {
+        ShareConnectReply reply;
+        reply.ip = Util::getFirstIp();
+        reply.appName = lo.tarAppname;  // 本地(被请求方)app名称
+        reply.tarAppname = lo.appName;  // 远端(请求方)app名称
+        reply.reply = SHARE_CONNECT_ERR_CONNECTED;
+
+        // 发送给远端控制方
+        SendRpcService::instance()->doSendProtoMsg(APPLY_SHARE_CONNECT_RES,
+                                                   lo.tarAppname.c_str(), reply.as_json().str().c_str());
+        return;
+    }
+    
</code_context>

<issue_to_address>
Immediate return on cooperation may prevent further checks.

Returning early bypasses the DiscoveryJob fallback check, which may cause issues if Comshare status is outdated. Please review whether both checks are needed or if their order should be adjusted.
</issue_to_address>

### Comment 4
<location> `src/compat/plugins/daemon/core/service/rpc/handlerpcservice.cpp:265` </location>
<code_context>
         //NodeInfo
         NodePeerInfo nodepeer;
         nodepeer.from_json(baseJson);
-        if (!nodepeer.share_connect_ip.empty()
-                && nodepeer.share_connect_ip.compare(nodepeer.ipv4) != 0) {
+        DLOG << "DiscoveryJob baseInfo share_connect_ip:" << nodepeer.share_connect_ip.c_str() 
+             << " ipv4:" << nodepeer.ipv4.c_str();
</code_context>

<issue_to_address>
Removed check for share_connect_ip != ipv4 may affect logic.

Consider whether removing the check for share_connect_ip != ipv4 could cause issues if these values are equal, as this may alter the intended logic.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

int resultCode;
if (agree) {
resultCode = SHARE_CONNECT_COMFIRM;
} else if (req.nick == "BUSY_COOPERATING") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Using a magic string for busy status may reduce maintainability.

Consider replacing the hardcoded "BUSY_COOPERATING" string with a constant or enum to improve clarity and reduce maintenance risks.

// 直接发送忙碌拒绝消息
ApplyMessage msg;
msg.flag = REPLY_REJECT;
msg.nick = "BUSY_COOPERATING"; // Special marker for busy status
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider using a constant for the busy status marker.

Using a shared constant or enum for "BUSY_COOPERATING" will reduce duplication and make updates easier.

Suggested implementation:

    msg.nick = BUSY_COOPERATING_NICK; // Special marker for busy status

You also need to:

  1. Define BUSY_COOPERATING_NICK in a suitable header file (e.g., networkutil.h or a shared constants header), for example:
    constexpr const char* BUSY_COOPERATING_NICK = "BUSY_COOPERATING";
  2. Include that header in networkutil.cpp if it is not already included.

Comment on lines +267 to +276
if (isCurrentlyCooperating) {
ShareConnectReply reply;
reply.ip = Util::getFirstIp();
reply.appName = lo.tarAppname; // 本地(被请求方)app名称
reply.tarAppname = lo.appName; // 远端(请求方)app名称
reply.reply = SHARE_CONNECT_ERR_CONNECTED;

// 发送给远端控制方
SendRpcService::instance()->doSendProtoMsg(APPLY_SHARE_CONNECT_RES,
lo.tarAppname.c_str(), reply.as_json().str().c_str());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Immediate return on cooperation may prevent further checks.

Returning early bypasses the DiscoveryJob fallback check, which may cause issues if Comshare status is outdated. Please review whether both checks are needed or if their order should be adjusted.

Comment on lines -265 to -266
if (!nodepeer.share_connect_ip.empty()
&& nodepeer.share_connect_ip.compare(nodepeer.ipv4) != 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (bug_risk): Removed check for share_connect_ip != ipv4 may affect logic.

Consider whether removing the check for share_connect_ip != ipv4 could cause issues if these values are equal, as this may alter the intended logic.

Update changelog for version 1.1.14 .

Log: New v1.1.14
@github-actions
Copy link

github-actions bot commented Aug 7, 2025

TAG Bot

TAG: 1.1.14
EXISTED: no
DISTRIBUTION: unstable

@re2zero re2zero merged commit 1bd0894 into linuxdeepin:master Aug 7, 2025
13 of 14 checks passed
@re2zero re2zero deleted the bugfix branch August 7, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants