-
Notifications
You must be signed in to change notification settings - Fork 6
/
messages.go
78 lines (69 loc) · 2 KB
/
messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package model
import (
"fmt"
"strings"
)
const (
clientHeadFormat = "[%s|%s|%s]"
)
// NewPushPullMessage creates a new PushPullRequest
func NewPushPullMessage(seq uint32, client *Client, pushPullPackList ...*PushPullPack) *PushPullMessage {
return &PushPullMessage{
Header: NewMessageHeader(RequestType_PUSHPULLS),
Collection: client.Collection,
Cuid: client.CUID,
PushPullPacks: pushPullPackList,
}
}
// ToString returns customized string
func (its *PushPullMessage) ToString(isFull bool) string {
var b strings.Builder
_, _ = fmt.Fprintf(&b, "Head[%s] PushPullPack[%d]{", its.Header.ToString(), len(its.PushPullPacks))
for _, ppp := range its.PushPullPacks {
b.WriteString(" ")
b.WriteString(ppp.ToString(isFull))
}
b.WriteString("}")
return b.String()
}
// GetClient returns the model of the client
func (its *PushPullMessage) GetClient() *Client {
return &Client{
CUID: its.Cuid,
Alias: "",
Collection: its.Collection,
SyncType: SyncType_LOCAL_ONLY,
}
}
// NewClientMessage creates a new ClientRequest
func NewClientMessage(client *Client) *ClientMessage {
return &ClientMessage{
Header: NewMessageHeader(RequestType_CLIENTS),
Collection: client.Collection,
Cuid: client.CUID,
ClientAlias: client.Alias,
SyncType: client.SyncType,
}
}
// ToString returns customized string
func (its *ClientMessage) ToString() string {
var b strings.Builder
_, _ = fmt.Fprintf(&b, clientHeadFormat, its.Header.ToString(), its.Collection, its.Cuid)
b.WriteString(" SyncType:")
b.WriteString(its.SyncType.String())
return b.String()
}
// GetClient returns the model of client
func (its *ClientMessage) GetClient() *Client {
return &Client{
CUID: its.Cuid,
Alias: its.ClientAlias,
Collection: its.Collection,
Type: its.ClientType,
SyncType: its.SyncType,
}
}
// GetClientSummary returns the summary of client
func (its *ClientMessage) GetClientSummary() string {
return fmt.Sprintf("%s(%s)", its.ClientAlias, its.Cuid)
}