Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: MoonSHRD/p2chat
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.2.4
Choose a base ref
...
head repository: MoonSHRD/p2chat
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Sep 7, 2019

  1. Verified

    This commit was signed with the committer’s verified signature.
    Ant0wan Antoine Barthelemy
    Copy the full SHA
    c6bae2b View commit details
  2. Copy the full SHA
    bd35995 View commit details
  3. Release v1.2.5

    oykmnk authored Sep 7, 2019
    Copy the full SHA
    c18786a View commit details

Commits on Sep 8, 2019

  1. Copy the full SHA
    178dae9 View commit details
  2. Copy the full SHA
    9db2bc5 View commit details
  3. Copy the full SHA
    a18e627 View commit details
  4. Release v1.2.6

    oykmnk authored Sep 8, 2019
    Copy the full SHA
    1770a93 View commit details

Commits on Sep 21, 2019

  1. Copy the full SHA
    6af919e View commit details
  2. Release v1.2.7

    oykmnk authored Sep 21, 2019
    Copy the full SHA
    9946feb View commit details

Commits on Sep 22, 2019

  1. Copy the full SHA
    64f65ef View commit details
  2. Copy the full SHA
    9590443 View commit details
  3. Copy the full SHA
    d329da4 View commit details
  4. Release v2.0.0

    ChronosXYZ committed Sep 22, 2019
    Copy the full SHA
    28ca0e9 View commit details
  5. Copy the full SHA
    5c88973 View commit details
  6. Release v2.0.1

    ChronosXYZ committed Sep 22, 2019
    Copy the full SHA
    79a36b1 View commit details
  7. feat/fix: Implement handling FlagGreetingRespond message, add json se…

    …rialized names to TextMessage model
    ChronosXYZ committed Sep 22, 2019
    Copy the full SHA
    a727559 View commit details
  8. Release v2.0.2

    ChronosXYZ committed Sep 22, 2019
    Copy the full SHA
    fb95a47 View commit details

Commits on Sep 23, 2019

  1. Copy the full SHA
    886eef1 View commit details
  2. Release v2.0.3

    ChronosXYZ committed Sep 23, 2019
    Copy the full SHA
    6a5285d View commit details

Commits on Nov 22, 2019

  1. Copy the full SHA
    e1b2c0b View commit details

Commits on Sep 23, 2024

  1. autodoc

    JackBekket committed Sep 23, 2024
    Copy the full SHA
    a70517b View commit details
Showing with 793 additions and 84 deletions.
  1. +335 −0 FILES.md
  2. +27 −0 api/README.md
  3. +36 −0 api/README_GENERATED.md
  4. +13 −11 api/protocol.go
  5. +19 −0 cmd/README.md
  6. +82 −0 cmd/README_GENERATED.md
  7. +3 −3 cmd/main.go
  8. +2 −2 cmd/main_test.go
  9. +2 −1 go.mod
  10. +2 −0 go.sum
  11. +23 −0 pkg/README.md
  12. +134 −0 pkg/README_GENERATED.md
  13. +115 −67 pkg/handler.go
335 changes: 335 additions & 0 deletions FILES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
# cmd/main_test.go
## Package: p2chat/v2/pkg

### Imports:

```
context
crypto/rand
encoding/json
fmt
testing
time
github.com/MoonSHRD/p2chat/v2/api
github.com/MoonSHRD/p2chat/v2/pkg
github.com/libp2p/go-libp2p
github.com/libp2p/go-libp2p-core/crypto
github.com/libp2p/go-libp2p-core/host
github.com/libp2p/go-libp2p-core/peer
github.com/libp2p/go-libp2p-core/peerstore
github.com/libp2p/go-libp2p-core/protocol
pubsub "github.com/libp2p/go-libp2p-pubsub"
github.com/phayes/freeport
```

### External Data, Input Sources:

- `numberOfNodes`: Constant representing the number of nodes in the network (3).
- `serviceTag`: Constant representing the service tag used for communication (moonshard).

### Code Summary:

#### TestCreateHosts:

- Creates multiple mock host objects using `createHost` function.
- Each host has its own context, private key, and listening port.
- Appends the created hosts and contexts to respective arrays.

#### TestMDNS:

- Initializes a PubSub instance for each host using `pubsub.NewFloodsubWithProtocols`.
- Creates a handler for each PubSub instance using `pkg.NewHandler`.
- Initializes MDNS using `pkg.InitMDNS` and waits for the correct setup of PubSub.
- Connects each host to the other nodes in the network.

#### TestGetPeers:

- Checks if all nodes are connected to each other by verifying the number of peers returned by `handler.GetPeers`.

#### TestSendMessage:

- Creates a sample message and marshals it into JSON format.
- Publishes the message to the service topic using `testPubsubs[0].Publish`.

#### TestGetMessage:

- Subscribes to the service topic and receives the published message.
- Decodes the received message and compares it with the original message.

#### TestCloseHosts:

- Closes all host objects using `host.Close`.

#### End of Output:



# pkg/handler.go
## Package: pkg

### Imports:

- encoding/json
- log
- sync
- github.com/MoonSHRD/p2chat/v2/api
- github.com/deckarep/golang-set
- github.com/libp2p/go-libp2p-core/peer
- github.com/libp2p/go-libp2p-pubsub

### External Data, Input Sources:

- PubSub instance (pb)
- Service topic (serviceTopic)
- Network topics (networkTopics)
- Identity map (identityMap)
- Peer ID (peerID)
- Matrix ID (matrixID)

### Code Summary:

#### Handler struct:

- The Handler struct is responsible for handling incoming network events, such as messages.
- It has a PubSub instance (pb), a service topic (serviceTopic), a set of network topics (networkTopics), an identity map (identityMap), a peer ID (peerID), and a matrix ID (matrixID).

#### TextMessage struct:

- The TextMessage struct represents a regular text message with fields for topic, body, fromPeerID, and fromMatrixID.

#### NewHandler function:

- Creates a new Handler instance with the given PubSub instance, service topic, peer ID, and network topics.

#### HandleIncomingMessage function:

- Handles incoming messages by extracting the message type, peer ID, and matrix ID.
- Based on the message type, it performs different actions, such as sending a response, adding topics to the network topics set, or mapping Multiaddress/MatrixID.

#### GetTopics function:

- Returns a list of topics that the handler is subscribed to.

#### GetPeers function:

- Returns a list of peers subscribed to a specific topic.

#### BlacklistPeer function:

- Blacklists a peer by its ID.

#### RequestNetworkTopics function:

- Requests topics from other peers.

#### RequestPeerIdentity function:

- Requests the MatrixID of a specific peer.

#### SendGreetingInTopic and SendFarewellInTopic functions:

- Sends greeting and farewell messages to a specific topic.

#### sendMessageToServiceTopic and sendMessageToTopic functions:

- Sends marshaled messages to the service topic or a specific topic.

#### SetMatrixID function:

- Sets the Matrix ID for the handler.

#### GetIdentityMap function:

- Returns a copy of the handler's identity map.

# pkg/mdns.go
## Package: pkg

### Imports:

- context
- log
- time
- github.com/libp2p/go-libp2p-core/host
- github.com/libp2p/go-libp2p-core/peer
- github.com/libp2p/go-libp2p/p2p/discovery

### External Data, Input Sources:

- context.Context
- host.Host
- rendezvous string

### Code Summary:

#### discoveryNotifee struct:

This struct is used to receive notifications about newly discovered peers. It has a channel called PeerChan that will receive peer.AddrInfo when a new peer is found.

#### HandlePeerFound function:

This function is called when a new peer is found. It takes a peer.AddrInfo as input and sends it to the PeerChan channel.

#### InitMDNS function:

This function initializes the MDNS service and returns a channel that will receive notifications about newly discovered peers. It takes a context.Context, host.Host, and rendezvous string as input.

1. It creates a new MDNS service using the provided context, host, and a one-hour timeout.
2. It registers a discoveryNotifee instance with the service to receive notifications about new peers.
3. It returns a channel that will receive peer.AddrInfo when a new peer is found.



# pkg/mdns_test.go
## Package: pkg

### Imports:

- `context`
- `testing`
- `time`
- `github.com/libp2p/go-libp2p-core/peer`
- `github.com/libp2p/go-libp2p/p2p/discovery`
- `github.com/libp2p/go-libp2p-swarm/testing`
- `github.com/libp2p/go-libp2p/p2p/host/basic`

### External Data, Input Sources:

- `context.Background()`
- `time.Hour`
- `moonshard`

### TestInitMDNS:

This function tests the initialization of an MDNS service. It creates a new Libp2p host using `bhost.New` and a swarm using `swarmt.GenSwarm`. Then, it creates a new MDNS service using `discovery.NewMdnsService` with the host, a timeout of one hour, and the service name "moonshard".

A new discovery notifee is created and registered with the MDNS service. The notifee has a channel for receiving peer address information. The test function then proceeds to verify that the MDNS service is initialized correctly.

# api/protocol.go
## Package: api

### Imports:

None

### External Data, Input Sources:

None

### BaseMessage:

The `BaseMessage` struct represents the basic message format of the protocol. It has the following fields:

- `Body`: The message body.
- `To`: The recipient of the message.
- `Flag`: An integer representing the message type.
- `FromMatrixID`: The sender's MatrixID.

### GetTopicsRespondMessage:

The `GetTopicsRespondMessage` struct is used to respond to a request for existing PubSub topics at the network. It inherits from the `BaseMessage` struct and has an additional field:

- `Topics`: A list of strings representing the available PubSub topics.

The `Flag` field for this message type is set to 0x2.



# cmd/flags.go
Package: main

Imports:
- flag

External data, input sources:
- Command-line flags

## Parsing Flags

This function parses command-line flags and returns a configuration struct. It initializes a new config struct and then uses the flag package to define and parse the following flags:

- `rendezvous`: A string that identifies a group of nodes. This flag is used to connect with friends.
- `wrapped_host`: The bootstrap node's wrapped host listen address.
- `pid`: Sets a protocol id for stream headers.
- `port`: The node's listen port.

The function parses the flags using `flag.Parse()` and returns the populated config struct.



# cmd/main.go
## Package: chat-with-mdns

This package provides a simple example for peer discovery using mDNS. mDNS is great when you have multiple peers in a local LAN.

### Imports:

- `bufio`
- `context`
- `crypto/rand`
- `encoding/json`
- `flag`
- `fmt`
- `log`
- `io`
- `os`
- `sync`
- `time`
- `github.com/MoonSHRD/p2chat/v2/api`
- `github.com/MoonSHRD/p2chat/v2/pkg`
- `github.com/deckarep/golang-set`
- `github.com/libp2p/go-libp2p`
- `github.com/libp2p/go-libp2p-core/crypto`
- `github.com/libp2p/go-libp2p-core/host`
- `github.com/libp2p/go-libp2p-core/peer`
- `github.com/libp2p/go-libp2p-core/peerstore`
- `github.com/libp2p/go-libp2p-core/protocol`
- `github.com/libp2p/go-libp2p-pubsub`
- `github.com/multiformats/go-multiaddr`

### External Data, Input Sources:

- Command-line arguments:
- `-help`: Display help information.
- `-wrapped_host`: Hostname or IP address to listen on.
- `-port`: Port to listen on.
- `-rendezvous`: Rendezvous string (service tag) to use for peer discovery.
- `-pid`: Protocol ID to use for PubSub.

### Code Summary:

1. **Initialization and Setup:**
- Create a new RSA key pair for the wrapped host.
- Create a new libp2p Host with the specified listen address, identity, and other options.
- Initialize a PubSub instance with the specified protocol ID and message signing options.
- Create a new Handler instance to handle incoming messages and network topics.

2. **Peer Discovery and Connection:**
- Use the provided rendezvous string to discover peers with the same service tag.
- Subscribe to the rendezvous topic to receive messages from other peers.
- Connect to newly discovered peers and add their addresses to the local peerstore.

3. **Message Handling and Communication:**
- Read messages from the PubSub subscription and handle them using the Handler instance.
- Write messages to the PubSub subscription using the provided rendezvous string.

4. **Network Topics Management:**
- Request network topics from the Handler instance to keep track of all active topics.

5. **Main Loop:**
- Continuously listen for incoming messages, new peers, and network topics.
- Handle incoming messages and connect to new peers as needed.
- Close the host and exit the program when the context is canceled.

6. **Error Handling:**
- Handle errors during key generation, host creation, PubSub initialization, peer discovery, and connection.

7. **Logging:**
- Log messages, errors, and other relevant information to the console.

8. **Help Information:**
- Provide help information when the `-help` flag is set.

This summary provides a high-level overview of the code and its functionality. It covers the main components, data sources, and processes involved in the package.

27 changes: 27 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Package: api

### Imports:

None

### External Data, Input Sources:

None

### BaseMessage:

The `BaseMessage` struct represents the basic message format of the protocol. It has the following fields:

- `Body`: The message body.
- `To`: The recipient of the message.
- `Flag`: An integer representing the message type.
- `FromMatrixID`: The sender's MatrixID.

### GetTopicsRespondMessage:

The `GetTopicsRespondMessage` struct is used to respond to a request for existing PubSub topics at the network. It inherits from the `BaseMessage` struct and has an additional field:

- `Topics`: A list of strings representing the available PubSub topics.

The `Flag` field for this message type is set to 0x2.

Loading