Beta

3 connection modes in content sharing app

Description:

There is a content sharing app.
A user can create a content as owner, then others may connect to the content as guest.

A content can be connected up to N clients concurrently.
Each client belongs to a user, a user may have multiple clients.

And there are 3 modes to check the N limit:

  • 0 (multi): each user can connect to a content with multiple clients concurrently.
  • 1 (single refuse new): each user can only connect to a content with up to 1 client concurrently, a 2nd client of the user will be refused.
  • 2 (single kick out old): each user can only connect to a content with up to 1 client concurrently, the user's 2nd client will kick out the user's previous client first, and get connected.

There are several special design to make the app works reasonable:

  • The owner can always connect to it's own content, if not yet, even N is reached.
    Because the owner might need to maintain the content, (if you can't connect to your own content, that's weird, right?).
    That means there might be up to N+1 connections, if the owner connects as the last user.
  • In mode 2 (single kick out old), since a user's 2nd client will kick out its old client first; thus should always allow the user's 2nd client to connect, (aka. no need to check the max N again for the 2nd client).
  • In mode 2 (single kick out old), kick out happens before check of N limit.

The content server knows:

  • The upper limit N.
  • The owner of each content.
  • Full list of connected clients for a content, and which user each client represents.

When a client try to connect to a content, the server will call function IsNeedCheckMaxConn(mode, isUserConnected, isOwner) to decide whether need to check the N limit for this client, (true -> pass, false -> refuse).

The task is to implement the function IsNeedCheckMaxConn().

Tips:

  • There are 12 cases, you need to pass each of them.
    Think about each of the cases 1 by 1.
  • In mode 1 (single refuse new), refuse happens first, if user already connected, before check max limit.

(BTW: the solution & logic is taken from a real project where client & server are connected via websocket, the logic is tested, and works well so far.)

Logic
Algorithms

More By Author:

Check out these other kata created by kuchaguangjie

Stats:

CreatedJun 15, 2022
PublishedJun 15, 2022
Warriors Trained31
Total Skips7
Total Code Submissions67
Total Times Completed3
Go Completions3
Total Stars0
% of votes with a positive feedback rating0% of 1
Total "Very Satisfied" Votes0
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes1
Total Rank Assessments2
Average Assessed Rank
8 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • kuchaguangjie Avatar
Ad