| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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(server types.IServer, bot types.IServBot, section types.ISection, strUrl string) *SectionNet {
- log.Printf("NewSectionNet(): url=%q\n", strUrl)
- { // Предусловия
- if server == nil {
- panic("NewSectionNet(): IServer == nil")
- }
- 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
- }
|