| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- // package sectionnet -- сетевая секция
- package sectionnet
- import (
- "fmt"
- "log"
- "strings"
- "sync"
- // "time"
- "wartank/pkg/types"
- )
- /*
- Базовый тип для сетевых секций
- */
- // SectionNet -- базовый тип для сетевых секций
- type SectionNet struct {
- client types.INetClient
- section types.ISection
- strUrl string
- block sync.Mutex
- }
- // NewSectionNet -- возвращает новый *SectionNet
- func NewSectionNet(bot types.IBot, section types.ISection, strUrl string) *SectionNet {
- log.Printf("NewSectionNet(): url=%q\n", strUrl)
- { // Предусловия
- if bot == nil {
- panic("NewSectionNet(): IServBot == nil")
- }
- if section == nil {
- panic("NewSectionNet(): ISection == nil")
- }
- if strUrl == "" {
- panic("NewSectionNet(): strUrl is empty")
- }
- }
- sf := &SectionNet{
- section: section,
- strUrl: strUrl,
- client: bot.BotNet().Net(),
- }
- return sf
- }
- func (sf *SectionNet) Run() {
- }
- // Обновляет список строк
- func (sf *SectionNet) UpdateLst(name string) (err error) {
- if sf == nil {
- return
- }
- sf.block.Lock()
- defer sf.block.Unlock()
- // FIXME: попытка разобраться, что за фигня творится
- // time.Sleep(time.Millisecond * 500)
- // log._rintf("INFO SectionNet.UpdateLst(): name=%s\tlink=%v\n", name, sf.strUrl)
- 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.Update(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
- }
|