File: vosesame.cpp

package info (click to toggle)
munipack 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,104 kB
  • sloc: cpp: 29,677; sh: 4,909; f90: 2,872; makefile: 278; python: 140; xml: 72; awk: 12
file content (143 lines) | stat: -rw-r--r-- 3,781 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*

  Virtual observatory Sesame client (to resolve names of astronomical objects)

  Copyright © 2019-20 F.Hroch (hroch@physics.muni.cz)

  This file is part of Munipack.

  Munipack is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Munipack is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Munipack.  If not, see <http://www.gnu.org/licenses/>.

  Reference:
     http://vizier.u-strasbg.fr/vizier/doc/sesame.htx

*/

#include "../config.h"
#include "vosesame.h"
#include <wx/wx.h>
#include <wx/url.h>
#include <wx/tokenzr.h>
#include <wx/txtstrm.h>
#include <wx/protocol/http.h>

#define STRASBOURG "http://cdsweb.u-strasbg.fr/cgi-bin/nph-sesame?"
#define HARVARD    "http://vizier.cfa.harvard.edu/viz-bin/nph-sesame?"

VOSesame::VOSesame(const wxString& o):
  wxHTTP(), object(o), ra(-999.9), dec(-99.9), url(STRASBOURG+object),
  istream(0)
{
  if( ! url.IsOk() ) {
    if( url.GetError() == wxURL_SNTXERR )
      errmsg = "Syntax error in the URL string.";
    else if( url.GetError() == wxURL_NOPROTO )
      errmsg = "Found no protocol which can get this URL.";
    else if( url.GetError() == wxURL_NOHOST )
      errmsg = "A host name is required for this protocol.";
    else if( url.GetError() == wxURL_NOPATH )
      errmsg = "A path is required for this protocol.";
  }
}

VOSesame::~VOSesame()
{
  delete istream;
}

const char* VOSesame::GetErrMsg() const
{
  return errmsg.c_str();
}


bool VOSesame::Connect(const wxString& server)
{
  SetFlags(wxSOCKET_WAITALL);
  SetHeader("User-Agent",wxString("Munipack/")+wxString(PACKAGE_VERSION));
  wxLogInfo("Connecting to VOSesame http://"+server+" ...");

  return wxHTTP::Connect(server);
}

bool VOSesame::Resolve()
{
  if( errmsg != "" ) return false;

  if( ! Connect(url.GetServer()) ) {
    errmsg = "Failed to connect server `http://"+url.GetServer()+"'.";
    return false;
  }

  if( ! Get(url.GetPath() + wxString("?") + url.GetQuery()) )
    return false;

  return true;
}


bool VOSesame::Get(const wxString& query)
{
  wxLogInfo("Sending query `"+query+"'...");
  istream = GetInputStream(query);
  wxASSERT(istream);

  wxLogInfo("HTTP response: %d",GetResponse());
  wxLogInfo("HTTP protocol state: %d",GetError());
  wxLogInfo("Content-Type (code):",GetContentType());
  wxLogInfo("Content-Type (head):",GetHeader("Content-Type"));
  wxLogInfo("Content-Length:",GetHeader("Content-Length"));

  if( GetResponse() != 200 ) {
    errmsg.Printf("HTTP response: %d (failed).",GetResponse());
    return false;
  }

  if( GetError() != wxPROTO_NOERR ) {
    errmsg.Printf("Network error %d",(int) GetError());
    return false;
  }

  wxLogInfo("Downloading and parsing response ...");

  wxTextInputStream text(*istream,"\r\n");
  while( ! istream->Eof() && istream->IsOk()) {
    wxString line = text.ReadLine();
    if( istream->LastRead() > 0 ) {
      wxLogInfo(line);

      if( line.StartsWith("%J ") ) {
	wxStringTokenizer tk(line);
	for(int n = 0; tk.HasMoreTokens(); n++) {
	  wxString item = tk.GetNextToken();
	  double q;
	  if( n == 1 && item.ToDouble(&q) )
	    ra = q;
	  if( n == 2 && item.ToDouble(&q) )
	    dec = q;
	}
      }

      if( line.StartsWith("#!") ) {
	if( line.Find("Nothing found") )
	  errmsg = "Required object not found.";
	else
	  errmsg = line.Mid(3);
	return false;
      }
    }
  }

  return ra > -999 && dec > -99;
}