sectionnet.go 2.2 KB

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