sectionnet.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // package sectionnet -- сетевая секция
  2. package sectionnet
  3. import (
  4. "fmt"
  5. "log"
  6. "strings"
  7. "sync"
  8. // "time"
  9. "wartank/pkg/types"
  10. )
  11. /*
  12. Базовый тип для сетевых секций
  13. */
  14. // SectionNet -- базовый тип для сетевых секций
  15. type SectionNet struct {
  16. client types.INetClient
  17. section types.ISection
  18. strUrl string
  19. block sync.Mutex
  20. }
  21. // NewSectionNet -- возвращает новый *SectionNet
  22. func NewSectionNet(bot types.IBot, section types.ISection, strUrl string) *SectionNet {
  23. log.Printf("NewSectionNet(): url=%q\n", strUrl)
  24. { // Предусловия
  25. if bot == nil {
  26. panic("NewSectionNet(): IServBot == nil")
  27. }
  28. if section == nil {
  29. panic("NewSectionNet(): ISection == nil")
  30. }
  31. if strUrl == "" {
  32. panic("NewSectionNet(): strUrl is empty")
  33. }
  34. }
  35. sf := &SectionNet{
  36. section: section,
  37. strUrl: strUrl,
  38. client: bot.BotNet().Net(),
  39. }
  40. return sf
  41. }
  42. func (sf *SectionNet) Run() {
  43. }
  44. // Обновляет список строк
  45. func (sf *SectionNet) UpdateLst(name string) (err error) {
  46. if sf == nil {
  47. return
  48. }
  49. sf.block.Lock()
  50. defer sf.block.Unlock()
  51. // FIXME: попытка разобраться, что за фигня творится
  52. // time.Sleep(time.Millisecond * 500)
  53. // log._rintf("INFO SectionNet.UpdateLst(): name=%s\tlink=%v\n", name, sf.strUrl)
  54. lstString, err := sf.client.Get(sf.strUrl)
  55. if err != nil {
  56. return fmt.Errorf("SectionNet.UpdateLst(): in make request, err=\n\t%w", err)
  57. }
  58. if err := sf.section.Update(lstString); err != nil {
  59. return fmt.Errorf("SectionNet.UpdateLst(): in update ISection, err=\n\t%w", err)
  60. }
  61. return nil
  62. }
  63. // Get -- выполняет GET-запрос по указанному URL
  64. func (sf *SectionNet) Get(strLink string) (lstString []string, err error) {
  65. sf.block.Lock()
  66. defer sf.block.Unlock()
  67. // log._rintf("INFO SectionNet.Get(): link=%v\n", sf.strUrl)
  68. if !strings.Contains(strLink, sf.strUrl) {
  69. return nil, fmt.Errorf("SectionNet.Get(): strLink(%v) не содержит strUrl(%v)", strLink, sf.strUrl)
  70. }
  71. lstString, err = sf.client.Get(strLink)
  72. if err != nil {
  73. return nil, fmt.Errorf("SectionNet.Get(): err=\n\t%v", err)
  74. }
  75. return lstString, nil
  76. }