Arduino TinyFTP
All Classes Functions Pages
FTPSessionMgr.h
1 #pragma once
2 #include "FTPSession.h"
3 #include "IPAddress.h"
4 
5 namespace ftp_client {
16 template <class ClientType>
18  public:
19  FTPSessionMgr() { FTPLogger::writeLog(LOG_DEBUG, "FTPSessionMgr"); }
20 
21  ~FTPSessionMgr() {
22  FTPLogger::writeLog(LOG_DEBUG, "~FTPSessionMgr");
23  end();
24  }
25 
27  bool begin(IPAddress &address, int port, const char *username,
28  const char *password) {
29  FTPLogger::writeLog(LOG_DEBUG, "FTPSessionMgr", "createSession");
30  this->address = address;
31  this->port = port;
32  this->username = username;
33  this->password = password;
34  return true;
35  }
36 
37  void end() {
38  FTPLogger::writeLog(LOG_DEBUG, "FTPSessionMgr", "end");
39  for (int i = 0; i < FTP_MAX_SESSIONS; i++) {
40  if (sessions[i] != nullptr) {
41  FTPSession<ClientType> &session = *sessions[i];
42  session.api().quit(); // Send QUIT command to the server
43  session.end();
44  delete sessions[i];
45  sessions[i] = nullptr;
46  }
47  }
48  }
49 
52  for (int i = 0; i < FTP_MAX_SESSIONS; i++) {
53  if (sessions[i] == nullptr) {
54  sessions[i] = new FTPSession<ClientType>();
55  if (sessions[i]->begin(address, port, username, password)) {
56  return *sessions[i];
57  } else {
58  delete sessions[i];
59  sessions[i] = nullptr;
60  }
61  } else if (sessions[i]->api().currentOperation() == NOP) {
62  // Reuse existing session if it is not currently in use
63  return *sessions[i];
64  }
65  }
66  FTPLogger::writeLog(LOG_ERROR, "FTPSessionMgr", "No available sessions");
67  static FTPSession<ClientType> empty_session;
68  empty_session.setValid(false);
69  return empty_session; // No available session
70  }
71 
73  bool abort(CurrentOperation op) {
74  FTPLogger::writeLog(LOG_DEBUG, "FTPSessionMgr", "abort");
75  for (int i = 0; i < FTP_MAX_SESSIONS; i++) {
76  if (sessions[i] != nullptr &&
77  sessions[i]->api().currentOperation() == op) {
78  return sessions[i]->api().abort();
79  }
80  }
81  return false; // No session found with the specified operation
82  }
83 
85  int count() {
86  int result = 0;
87  for (auto &session : sessions) {
88  if (session != nullptr) {
89  result++;
90  }
91  }
92  return result;
93  }
94 
96  int count(CurrentOperation op) {
97  int result = 0;
98  for (auto &session : sessions) {
99  if (session != nullptr && session->api().currentOperation() == op) {
100  result++;
101  }
102  }
103  return result;
104  }
105 
106  protected:
107  FTPSession<ClientType> *sessions[FTP_MAX_SESSIONS] = {nullptr};
108  IPAddress address;
109  int port;
110  const char *username;
111  const char *password;
112 };
113 
114 } // namespace ftp_client
FTPSession This class manages the FTP session, including command and data connections....
Definition: FTPSession.h:17
void setValid(bool valid)
Used to set as invalid for dummy error session.
Definition: FTPSession.h:48
FTPSessionMgr This class manages multiple FTP sessions, allowing for concurrent operations and sessio...
Definition: FTPSessionMgr.h:17
int count(CurrentOperation op)
Count the sessions with a specific current operation.
Definition: FTPSessionMgr.h:96
int count()
Count the sessions.
Definition: FTPSessionMgr.h:85
bool abort(CurrentOperation op)
Aborts the current operation in all sessions.
Definition: FTPSessionMgr.h:73
bool begin(IPAddress &address, int port, const char *username, const char *password)
Initializes the session manager with the FTP server details.
Definition: FTPSessionMgr.h:27
FTPSession< ClientType > & session()
Provides a session for the FTP operations.
Definition: FTPSessionMgr.h:51