forked from BlumaSavrasov/ipfs-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.go
132 lines (120 loc) · 3.8 KB
/
World.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"math/rand"
"strings"
)
//*********************************CLASS CONNECTION****************************************
type Connection struct{
_nFirstNodeId int
_nSecondNodeId int
_dbLatency int
}
func NewConnection(nFirstNodeId int,nSecondNodeId int)*Connection{
connection:=new(Connection)
connection._nFirstNodeId=nFirstNodeId
connection._nSecondNodeId=nSecondNodeId
connection._dbLatency=rand.Intn(20)
return connection
}
func (c Connection) String() string {
return fmt.Sprintf("node: %d connected to node: %d with latency: %d",c._nFirstNodeId,c._nSecondNodeId,c._dbLatency)
}
//*******************************END OF CLASS CONNECTION**************************************
//******************************CLASS NODE****************************************************
var g_nGlobalId=0
type Node struct {
_nId int
_nMyASId int//++++++++++++++++++++++++++++++++++++++
_nMyCountryId int//+++++++++++++++++++++++++++++++++++++++need to had in constuctor
_rgContent [] int
_nNumOfBlocks int
_rgConnections []Connection
_cNumOfConnections int
}
func NewNode(numOfBlocks int) *Node {
g_nGlobalId++
fmt.Println(g_nGlobalId)
node := new(Node)
node._nId=g_nGlobalId
node._rgContent=make([]int,numOfBlocks)
for i:=0;i<numOfBlocks;i++{
node._rgContent[i]=rand.Intn(1000)
}
node._cNumOfConnections=-1//rand.Intn(10-3)+3
node._rgConnections=nil//make([]Connection,node._cNumOfConnections)
//node.fnSetConnections()
node._nNumOfBlocks=numOfBlocks
return node
}
func (n Node) String() string {
var node []string
node=append(node,fmt.Sprintf("[node with id: %d,",n._nId))
if n._rgConnections!= nil{node = append(node,fmt.Sprintf("connected to nodes:"))}
for i:=0;i<n._cNumOfConnections;i++{
node = append(node,n._rgConnections[i].String())
}
node=append(node,fmt.Sprintf("number of blocks: %d content: %d]", n._nNumOfBlocks,n._rgContent))
return fmt.Sprintf(strings.Join(node,"\n"))
}
//*******************************************END OF CLASS NODE**********************************************
//*****************************************CLASS ASN********************************************************
var g_nGlobalASN=0
type AS struct{
_rgSetOfNodes []Node
_numOfNodes_n int
_ASN int
}
func NewAS(numOfNodes_n int)*AS{
fmt.Println("newAS")
AS:=new(AS)
g_nGlobalASN++
AS._rgSetOfNodes=make([]Node,numOfNodes_n)
AS._ASN=g_nGlobalASN
for i:=0 ;i<numOfNodes_n ;i++ {
AS._rgSetOfNodes[i]=*NewNode(rand.Intn(100))
}
AS._numOfNodes_n=numOfNodes_n
return AS
}
func (a AS) String() string {
var as []string
as=append(as,fmt.Sprintf("AS with ASN:%d, Contains nodes:\n",a._ASN))
for i:=0;i<a._numOfNodes_n ;i++ {
as=append(as,a._rgSetOfNodes[i].String()+"\n")
}
return fmt.Sprintf(strings.Join(as,"\n"))
}
//*********************************END OF CLASS ASN***********************************************************
//*********************************CLASS COUNTRY**************************************************************
type Country struct{
_Name string
_CentralAS AS
_rgAS []AS
_numOfAS int
}
func newCountry(numOfAS int) *Country {
fmt.Println("newCountry")
country:=new(Country)
country._Name=""
country._numOfAS=numOfAS
country._CentralAS=*NewAS(rand.Intn(100))
country._rgAS=make([]AS,numOfAS)
for i:=0;i<country._numOfAS ;i++ {
country._rgAS[i]=*NewAS(rand.Intn(100))
}
return country
}
func (c Country) String() string {
var country []string
country = append(country,fmt.Sprintf("country's name: %d \n",c._Name))
country=append(country,c._CentralAS.String()+"\n")
for i:=0;i<c._numOfAS;i++{
//world=append(world,w._rgSetOfNodes[i].String()+"\n")
country=append(country,c._rgAS[i].String()+"\n")
}
return fmt.Sprintf(strings.Join(country,"\n"))
}
//*****************************END OF CLASS COUNTRY*********************************************************
func main() {
}