| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- // package sectionnet -- сетевая секция
- package sectionnet
- import (
- "fmt"
- "log"
- "strings"
- "sync"
- // "time"
- "wartank/pkg/types"
- )
- /*
- Базовый тип для сетевых секций
- */
- // SectionNet -- базовый тип для сетевых секций
- type SectionNet struct {
- client types.INetClient
- section types.ИСценаСтр
- strUrl string
- block sync.Mutex
- }
- // NewSectionNet -- возвращает новый *SectionNet
- func NewSectionNet(section types.ИСценаСтр, strUrl string) (*SectionNet, error) {
- log.Printf("NewSectionNet(): url=%q\n", strUrl)
- { // Предусловия
- if section == nil {
- return nil, fmt.Errorf("NewSectionNet(): ISection == nil")
- }
- if strUrl == "" {
- return nil, fmt.Errorf("NewSectionNet(): strUrl is empty")
- }
- }
- sf := &SectionNet{
- section: section,
- strUrl: strUrl,
- client: section.Бот().Сеть().КлиентСеть(),
- }
- return sf, nil
- }
- // Обновляет список строк
- func (sf *SectionNet) UpdateLst() (err error) {
- if sf == nil {
- return
- }
- sf.block.Lock()
- defer sf.block.Unlock()
- // FIXME: попытка разобраться, что за фигня творится
- // time.Sleep(time.Millisecond * 500)
- log.Printf("SectionNet.UpdateLst(): bot=%s\tsection=%v\n", sf.section.Бот().Имя(), sf.section.Имя())
- lstString, err := sf.client.Get(sf.strUrl)
- if err != nil {
- return fmt.Errorf("SectionNet.UpdateLst(): in make request, err=\n\t%w", err)
- }
- if err := sf.section.СтрОбновить(lstString); err != nil {
- return fmt.Errorf("SectionNet.UpdateLst(): in update ISection, err=\n\t%w", err)
- }
- return nil
- }
- // Get -- выполняет GET-запрос по указанному URL
- func (sf *SectionNet) Get(strLink string) (lstString []string, err error) {
- sf.block.Lock()
- defer sf.block.Unlock()
- // log._rintf("INFO SectionNet.Get(): link=%v\n", sf.strUrl)
- if !strings.Contains(strLink, sf.strUrl) {
- return nil, fmt.Errorf("SectionNet.Get(): strLink(%v) не содержит strUrl(%v)", strLink, sf.strUrl)
- }
- lstString, err = sf.client.Get(strLink)
- if err != nil {
- return nil, fmt.Errorf("SectionNet.Get(): err=\n\t%v", err)
- }
- return lstString, nil
- }
|