## Broadcaster

Краен срок
01.11.2014 12:00

Срокът за предаване на решения е отминал

Да се направи тип Broadcaster, който да разпраща съобщения до регистрираните слушатели.

За улеснение, приемаме, че съобщенията ще са само цели числа.

За целта напишете функция NewBroadcaster, която да връща *Broadcaster (да ви служи като конструктор).

При извикването на метода Register(), да се регистрира нов слушател и да се връща канал за получаване на съобщенията (еднопосочен).

Изпращането на съобщенията става посредством метода Send(), който връща канал, в който се изпращат съобщенията (еднопосочен).

Всеки може да изпраща съобщения (не е нужно да е регистриран слушател).

Ако слушател изпрати съобщение, то е нормално той да получи своето си съобщение.

  b := NewBroadcaster()
  one := b.Register()
  two := b.Register()
  go func() {
    b.Send() <- 1
  }()
  fmt.Printf("One just recieved %v, two recieved %v, too!\n", <-one, <-two)
  // One just recieved 1, two recieved 1, too!

Решения

Мартин Добрев
  • Коректно
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)
Мартин Добрев
package main
type Broadcaster struct {
channels map[chan int]bool
input chan int
}
func NewBroadcaster() (b *Broadcaster) {
result := new(Broadcaster)
result.channels = make(map[chan int]bool)
result.input = make(chan int)
go func(b *Broadcaster) {
for {
p := <-b.input
for next := range b.channels {
next <- p
}
}
}(result)
return result
}
func (b *Broadcaster) Send() chan<- int {
return b.input
}
func (b *Broadcaster) Register() <-chan int {
newChannel := make(chan int)
b.channels[newChannel] = true
return newChannel
}
PASS
ok  	_/tmp/d20141203-11237-1neysyi	0.011s
PASS
ok  	_/tmp/d20141203-11237-1neysyi	0.121s
PASS
ok  	_/tmp/d20141203-11237-1neysyi	0.011s
PASS
ok  	_/tmp/d20141203-11237-1neysyi	0.012s
PASS
ok  	_/tmp/d20141203-11237-1neysyi	0.012s
Калина Бухлева
  • Коректно
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)
Калина Бухлева
package main
type Broadcaster struct {
sendc chan int
listenc []chan int
}
func NewBroadcaster() *Broadcaster {
sendc := make(chan int)
return &Broadcaster{sendc: sendc}
}
func (b *Broadcaster) Register() <-chan int {
c := make(chan int)
b.listenc = append(b.listenc, c)
return c
}
func (b *Broadcaster) Send() chan<- int {
go func() {
received := <-b.sendc
for _, member := range b.listenc {
go func(out chan int, received int) {
out <- received
}(member, received)
}
}()
return b.sendc
}
PASS
ok  	_/tmp/d20141203-11237-ard5rj	0.011s
PASS
ok  	_/tmp/d20141203-11237-ard5rj	0.121s
PASS
ok  	_/tmp/d20141203-11237-ard5rj	0.012s
PASS
ok  	_/tmp/d20141203-11237-ard5rj	0.011s
PASS
ok  	_/tmp/d20141203-11237-ard5rj	0.012s
Александър Деспотов
  • Коректно
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)
Александър Деспотов
package main
type Broadcaster struct {
Channels []*chan (int)
input chan (int)
}
func NewBroadcaster() *Broadcaster {
var broadcaster Broadcaster
broadcaster.Channels = make([]*chan (int), 0)
broadcaster.input = make(chan int)
return &broadcaster
}
func (b *Broadcaster) Register() <-chan (int) {
tmpChan := make(chan int)
b.Channels = append(b.Channels, &tmpChan)
return tmpChan
}
func (b *Broadcaster) Send() chan<- (int) {
go b.broadcast()
return b.input
}
func (b *Broadcaster) broadcast() {
msg := <-b.input
for _, crnChan := range b.Channels {
*crnChan <- msg
}
}
PASS
ok  	_/tmp/d20141203-11237-1udtmuy	0.011s
PASS
ok  	_/tmp/d20141203-11237-1udtmuy	0.121s
PASS
ok  	_/tmp/d20141203-11237-1udtmuy	0.028s
PASS
ok  	_/tmp/d20141203-11237-1udtmuy	0.013s
PASS
ok  	_/tmp/d20141203-11237-1udtmuy	0.012s
Евгений Бояджиев
  • Некоректно
  • 0 успешни тест(а)
  • 5 неуспешни тест(а)
Евгений Бояджиев
package main
type Broadcaster struct {
ch interface{}
}
func (caster *Broadcaster) New() {
caster = new(Broadcaster)
caster.ch = make(chan int)
}
func (caster *Broadcaster) Register() <-chan int {
return caster.ch.(chan int)
}
func (caster *Broadcaster) Send() chan<- int {
return caster.ch.(chan int)
}
func NewBroadcaster() *Broadcaster {
br := new(Broadcaster)
br.New()
return br
}
--- FAIL: TestWithTwoListeners (0.00 seconds)
panic: interface conversion: interface is nil, not chan int [recovered]
	panic: interface conversion: interface is nil, not chan int

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20141203-11237-15o6vk7.TestWithTwoListeners(0x182571e0)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:10 +0x3c
testing.tRunner(0x182571e0, 0x81acd80)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x81b0500, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20141203-11237-15o6vk7/_test/_testmain.go:51 +0x81
exit status 2
FAIL	_/tmp/d20141203-11237-15o6vk7	0.012s
--- FAIL: TestLateToTheParty (0.00 seconds)
panic: interface conversion: interface is nil, not chan int [recovered]
	panic: interface conversion: interface is nil, not chan int

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20141203-11237-15o6vk7.TestLateToTheParty(0x18257120)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:24 +0x44
testing.tRunner(0x18257120, 0x81acd8c)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x81b0500, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20141203-11237-15o6vk7/_test/_testmain.go:51 +0x81
exit status 2
FAIL	_/tmp/d20141203-11237-15o6vk7	0.012s
panic: interface conversion: interface is nil, not chan int

goroutine 5 [running]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x0)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:49 +0x4a
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 1 [chan receive]:
testing.RunTests(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x81b0500, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20141203-11237-15o6vk7/_test/_testmain.go:51 +0x81

goroutine 4 [chan receive]:
_/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners(0x18257120)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:60 +0x159
testing.tRunner(0x18257120, 0x81acd98)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 6 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x1)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 7 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x2)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 8 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x3)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 9 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x4)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 10 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x5)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 11 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x6)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 12 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x7)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 13 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x8)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 14 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x9)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 15 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0xa)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 16 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0xb)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 17 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0xc)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 18 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0xd)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 19 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0xe)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 20 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0xf)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 21 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x10)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 22 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x11)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 23 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x12)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122

goroutine 24 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·001(0x13)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:48
created by _/tmp/d20141203-11237-15o6vk7.TestWithConccurentListeners
	/tmp/d20141203-11237-15o6vk7/solution_test.go:56 +0x122
exit status 2
FAIL	_/tmp/d20141203-11237-15o6vk7	0.012s
--- FAIL: TestMultipleMessagesWithoutListeners (0.00 seconds)
panic: interface conversion: interface is nil, not chan int [recovered]
	panic: interface conversion: interface is nil, not chan int

goroutine 4 [running]:
testing.func·004()
	/usr/local/lib/go/src/pkg/testing/testing.go:348 +0x105
_/tmp/d20141203-11237-15o6vk7.TestMultipleMessagesWithoutListeners(0x18258120)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:74 +0x4b
testing.tRunner(0x18258120, 0x81acda4)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 1 [chan receive]:
testing.RunTests(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x81b0500, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20141203-11237-15o6vk7/_test/_testmain.go:51 +0x81
exit status 2
FAIL	_/tmp/d20141203-11237-15o6vk7	0.012s
panic: interface conversion: interface is nil, not chan int

goroutine 5 [running]:
_/tmp/d20141203-11237-15o6vk7.func·002()
	/tmp/d20141203-11237-15o6vk7/solution_test.go:84 +0x48
created by _/tmp/d20141203-11237-15o6vk7.TestMultipleMessagesWithOneListener
	/tmp/d20141203-11237-15o6vk7/solution_test.go:94 +0xca

goroutine 1 [chan receive]:
testing.RunTests(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b2f8, 0x81acd80, 0x5, 0x5, 0x81b0500, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20141203-11237-15o6vk7/_test/_testmain.go:51 +0x81

goroutine 4 [chan receive]:
_/tmp/d20141203-11237-15o6vk7.TestMultipleMessagesWithOneListener(0x18258120)
	/tmp/d20141203-11237-15o6vk7/solution_test.go:102 +0x10d
testing.tRunner(0x18258120, 0x81acdb0)
	/usr/local/lib/go/src/pkg/testing/testing.go:353 +0x87
created by testing.RunTests
	/usr/local/lib/go/src/pkg/testing/testing.go:433 +0x684

goroutine 6 [runnable]:
_/tmp/d20141203-11237-15o6vk7.func·003()
	/tmp/d20141203-11237-15o6vk7/solution_test.go:96
created by _/tmp/d20141203-11237-15o6vk7.TestMultipleMessagesWithOneListener
	/tmp/d20141203-11237-15o6vk7/solution_test.go:100 +0xf5
exit status 2
FAIL	_/tmp/d20141203-11237-15o6vk7	0.012s
Любомир Райков
  • Коректно
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)
Любомир Райков
package main
type Broadcaster struct {
Channels []chan int
}
func (b *Broadcaster) Register() <-chan int {
registerChannel := make(chan int)
b.Channels = append(b.Channels, registerChannel)
return b.Channels[len(b.Channels)-1]
}
func (b *Broadcaster) Send() chan<- int {
sendChannel := make(chan int)
go func() {
d, _ := <-sendChannel
for i, _ := range b.Channels {
go func(co chan int, value int) {
co <- value
}(b.Channels[i], d)
}
}()
return sendChannel
}
func NewBroadcaster() *Broadcaster {
b := new(Broadcaster)
b.Channels = []chan int{}
return b
}
PASS
ok  	_/tmp/d20141203-11237-175w4zi	0.011s
PASS
ok  	_/tmp/d20141203-11237-175w4zi	0.121s
PASS
ok  	_/tmp/d20141203-11237-175w4zi	0.011s
PASS
ok  	_/tmp/d20141203-11237-175w4zi	0.012s
PASS
ok  	_/tmp/d20141203-11237-175w4zi	0.012s
Любомир Коев
  • Коректно
  • 5 успешни тест(а)
  • 0 неуспешни тест(а)
Любомир Коев
package main
type Broadcaster struct {
pipe chan int
listeners []chan int
}
func NewBroadcaster() *Broadcaster {
b := new(Broadcaster)
b.pipe = make(chan int)
b.listeners = make([]chan int, 0)
go func() {
for message := range b.pipe {
for _, listener := range b.listeners {
listener <- message
}
}
}()
return b
}
func (b *Broadcaster) Register() <-chan int {
b.listeners = append(b.listeners, make(chan int))
return b.listeners[len(b.listeners)-1]
}
func (b *Broadcaster) Send() chan<- int {
return b.pipe
}
PASS
ok  	_/tmp/d20141203-11237-4igqdu	0.011s
PASS
ok  	_/tmp/d20141203-11237-4igqdu	0.121s
PASS
ok  	_/tmp/d20141203-11237-4igqdu	0.012s
PASS
ok  	_/tmp/d20141203-11237-4igqdu	0.011s
PASS
ok  	_/tmp/d20141203-11237-4igqdu	0.015s