]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/BamHttp_p.h
371ccce35d08b3d6a08b8bdaa78b363d8a305dd0
[bamtools.git] / src / api / internal / io / BamHttp_p.h
1 // ***************************************************************************
2 // BamHttp_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides reading/writing of BAM files on HTTP server
8 // ***************************************************************************
9
10 #ifndef BAMHTTP_P_H
11 #define BAMHTTP_P_H
12
13 //  -------------
14 //  W A R N I N G
15 //  -------------
16 //
17 // This file is not part of the BamTools API.  It exists purely as an
18 // implementation detail. This header file may change from version to version
19 // without notice, or even be removed.
20 //
21 // We mean it.
22
23 #include "api/IBamIODevice.h"
24 #include <string>
25
26 namespace BamTools {
27 namespace Internal {
28
29 class HttpRequestHeader;
30 class HttpResponseHeader;
31 class TcpSocket;
32
33 class BamHttp : public IBamIODevice {
34
35     // ctor & dtor
36     public:
37         BamHttp(const std::string& url);
38         ~BamHttp(void);
39
40     // IBamIODevice implementation
41     public:
42         void Close(void);
43         bool IsOpen(void) const;
44         bool IsRandomAccess(void) const;
45         bool Open(const IBamIODevice::OpenMode mode);
46         int64_t Read(char* data, const unsigned int numBytes);
47         bool Seek(const int64_t& position, const int origin = SEEK_SET);
48         int64_t Tell(void) const;
49         int64_t Write(const char* data, const unsigned int numBytes);
50
51     // internal methods
52     private:
53         bool ConnectSocket(void);
54         bool EnsureSocketConnection(void);
55         void ParseUrl(const std::string& url);
56         int64_t ReadFromSocket(char* data, const unsigned int numBytes);
57         bool ReceiveResponse(void);
58         bool SendRequest(const size_t numBytes = 0);
59         int64_t WriteToSocket(const char* data, const unsigned int numBytes);
60
61     // data members
62     private:
63
64         // our main socket
65         TcpSocket* m_socket;
66
67         // our connection data
68         std::string m_hostname;
69         std::string m_port;
70         std::string m_filename;
71
72         // our last (active) request & response info
73         HttpRequestHeader*  m_request;
74         HttpResponseHeader* m_response;
75
76         // internal state flags
77         bool m_isUrlParsed;
78
79         // file position
80         int64_t m_filePosition;
81         int64_t m_endRangeFilePosition;
82 };
83
84 } // namespace Internal
85 } // namespace BamTools
86
87 #endif // BAMHTTP_P_H