-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
66 lines (54 loc) · 1.17 KB
/
process.go
File metadata and controls
66 lines (54 loc) · 1.17 KB
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
package main
import (
"fmt"
)
type EventType int
const (
local EventType = iota
sent
recv
)
type Event struct {
t EventType
timestamp int64
src, dst int
}
func (e Event) String() string {
return fmt.Sprintf("%d", e.timestamp)
}
type Message struct {
timestamp int64
}
type Process struct {
ID int
Clock Clock
MsgChan []chan Message
events []Event
}
func NewProcess(ID int, numProcesses int) *Process {
process := new(Process)
process.ID = ID
process.Clock = Clock{ID: process.ID}
for i := 0; i < numProcesses; i++ {
msgChan := make(chan Message, 10)
process.MsgChan = append(process.MsgChan, msgChan)
}
return process
}
func (p *Process) log(event Event) {
p.events = append(p.events, event)
}
func (p *Process) Local() {
timestamp := p.Clock.tick(0)
p.log(Event{t: local, timestamp: timestamp})
}
func (p *Process) Send(dst *Process) {
timestamp := p.Clock.tick(0)
dst.MsgChan[p.ID] <- Message{timestamp}
p.log(Event{t: sent, src: p.ID, dst: dst.ID, timestamp: timestamp})
}
func (p *Process) Recv(src int) {
msg := <-p.MsgChan[src]
timestamp := p.Clock.tick(msg.timestamp)
p.log(Event{t: recv, src: src, dst: p.ID, timestamp: timestamp})
}