Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connectivity report #125

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/comunicationsmanagerfilesockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,13 @@ void ComunicationsManagerFileSockets::returnAndClosePetition(CmdPetition *inf, O
connectedsocket = accept(((CmdPetitionPosixSockets *)inf)->outSocket, (struct sockaddr*)&cliAddr, &cliLength);
if (fcntl(connectedsocket, F_SETFD, FD_CLOEXEC) == -1)
{
LOG_err << "ERROR setting CLOEXEC to socket: " << errno;
cerr << "ERROR setting CLOEXEC to socket: " << errno << endl;
}
((CmdPetitionPosixSockets *)inf)->acceptedOutSocket = connectedsocket; //So that it gets closed in destructor
}
if (connectedsocket == -1)
{
LOG_fatal << "Return and close: Unable to accept on outsocket " << ((CmdPetitionPosixSockets *)inf)->outSocket << " error: " << errno;
cerr << "Return and close: Unable to accept on outsocket " << ((CmdPetitionPosixSockets *)inf)->outSocket << " error: " << errno << endl;
delete inf;
return;
}
Expand All @@ -322,12 +322,12 @@ void ComunicationsManagerFileSockets::returnAndClosePetition(CmdPetition *inf, O
int n = send(connectedsocket, (void*)&outCode, sizeof( outCode ), MSG_NOSIGNAL);
if (n < 0)
{
LOG_err << "ERROR writing output Code to socket: " << errno;
cerr << "ERROR writing output Code to socket: " << errno << endl;
}
n = send(connectedsocket, sout.data(), max(1,(int)sout.size()), MSG_NOSIGNAL); // for some reason without the max recv never quits in the client for empty responses
if (n < 0)
{
LOG_err << "ERROR writing to socket: " << errno;
cerr << "ERROR writing to socket: " << errno << endl;
}

delete inf;
Expand Down
16 changes: 8 additions & 8 deletions src/comunicationsmanagernamedpipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ void ComunicationsManagerNamedPipes::sendPartialOutput(CmdPetition *inf, char *s
DWORD n;
if (!WriteFile(outNamedPipe,(const char*)&outCode, sizeof(outCode), &n, NULL))
{
LOG_err << "ERROR writing output Code to namedPipe: " << ERRNO;
cerr << "ERROR writing output Code to namedPipe: " << ERRNO << endl;
if (errno == ERROR_NO_DATA)
{
std::cerr << "WARNING: Client disconnected, the rest of the output will be discarded" << endl;
Expand All @@ -405,12 +405,12 @@ void ComunicationsManagerNamedPipes::sendPartialOutput(CmdPetition *inf, char *s
size_t thesize = size > 1 ? size : 1; // client does not like empty responses
if (!WriteFile(outNamedPipe,(const char*)&thesize, sizeof(thesize), &n, NULL))
{
LOG_err << "ERROR writing output Code to namedPipe: " << ERRNO;
cerr << "ERROR writing output Code to namedPipe: " << ERRNO<< endl;
return;
}
if (!WriteFile(outNamedPipe,s, thesize, &n, NULL))
{
LOG_err << "ERROR writing to namedPipe: " << ERRNO;
cerr << "ERROR writing to namedPipe: " << ERRNO << endl;
}
}

Expand Down Expand Up @@ -475,7 +475,7 @@ CmdPetition * ComunicationsManagerNamedPipes::getPetition()
DWORD total_available_bytes;
if (FALSE == PeekNamedPipe(pipeGeneral,0,0,0,&total_available_bytes,0))
{
LOG_err << "Failed to PeekNamedPipe. errno: L" << ERRNO;
cerr << "Failed to PeekNamedPipe. errno: L" << ERRNO << endl;
break;
}
if (total_available_bytes == 0)
Expand All @@ -495,7 +495,7 @@ CmdPetition * ComunicationsManagerNamedPipes::getPetition()

if (!readok)
{
LOG_err << "Failed to read petition from named pipe. errno: L" << ERRNO;
cerr << "Failed to read petition from named pipe. errno: L" << ERRNO << endl;
inf->line = strdup("ERROR");
return inf;
}
Expand All @@ -508,21 +508,21 @@ CmdPetition * ComunicationsManagerNamedPipes::getPetition()
inf->outNamedPipe = create_new_namedPipe(&namedPipe_id);
if (!namedPipeValid(inf->outNamedPipe) || !namedPipe_id)
{
LOG_fatal << "ERROR creating output namedPipe at getPetition";
cerr << "ERROR creating output namedPipe at getPetition" << endl;
inf->line = strdup("ERROR");
return inf;
}

if(!WriteFile(pipeGeneral,(const char*)&namedPipe_id, sizeof( namedPipe_id ), &n, NULL))
{
LOG_fatal << "ERROR writing to namedPipe at getPetition: ERRNO = " << ERRNO;
cerr << "ERROR writing to namedPipe at getPetition: ERRNO = " << ERRNO << endl;
inf->line = strdup("ERROR");
return inf;
}

if (!DisconnectNamedPipe(pipeGeneral) )
{
LOG_fatal << " Error disconnecting from general pip. errno: " << ERRNO;
cerr << " Error disconnecting from general pipe. errno: " << ERRNO << endl;
}

inf->line = strdup(receivedutf8.c_str());
Expand Down
17 changes: 17 additions & 0 deletions src/listeners.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
using namespace mega;
using namespace std;

#ifndef SSTR
#define SSTR( x ) static_cast< const std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
#endif

#ifdef ENABLE_CHAT
void MegaCmdGlobalListener::onChatsUpdate(MegaApi*, MegaTextChatList*)
{
Expand Down Expand Up @@ -168,6 +173,18 @@ void MegaCmdGlobalListener::onEvent(MegaApi *api, MegaEvent *event)
LOG_err << "Received event account blocked: " << event->getText();
sandboxCMD->reasonblocked = event->getText();
}
else if (event->getType() == MegaEvent::EVENT_CONNECTIVITY_CHANGED)
{
LOG_debug << "Received event connectivity changed: " << event->getNumber();
if (event->getNumber() == RETRY_CONNECTIVITY)
{
broadcastMessage(SSTR(event->getNumber()), "connectivity:");
}
else if (event->getNumber() == RETRY_NONE)
{
broadcastMessage(SSTR(event->getNumber()), "connectivity:");
}
}
else if (event->getType() == MegaEvent::EVENT_STORAGE)
{
if (event->getNumber() == MegaApi::STORAGE_STATE_CHANGE)
Expand Down
51 changes: 42 additions & 9 deletions src/megacmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ string newpasswd;
bool doExit = false;
bool consoleFailed = false;
bool stopcheckingforUpdaters = false;
bool loginended = false;

string dynamicprompt = "MEGA CMD> ";

Expand Down Expand Up @@ -304,12 +305,12 @@ void informStateListener(string message, int clientID)
cm->informStateListenerByClientId(s, clientID);
}

void broadcastMessage(string message)
void broadcastMessage(string message, const char *suffix)
{
string s;
if (message.size())
{
s += "message:";
s += suffix;
s+=message;
}
cm->informStateListeners(s);
Expand Down Expand Up @@ -3508,6 +3509,21 @@ void * retryConnections(void *pointer)
return NULL;
}

void * startSession(void *pointer)
{
bool *loginended = (bool *) pointer;
if (!ConfigurationManager::session.empty())
{
loginInAtStartup = true;
stringstream logLine;
logLine << "login " << ConfigurationManager::session;
LOG_debug << "Executing ... " << logLine.str().substr(0,9) << "...";
process_line((char*)logLine.str().c_str());
loginInAtStartup = false;
}
*loginended = true;
return NULL;
}

void startcheckingForUpdates()
{
Expand Down Expand Up @@ -3787,6 +3803,22 @@ void megacmd()
s+=(char)0x1F;
}

if (!loginended)
{
s += "message:";
s += "--------------------------------------------------\n"
"-- MEGAcmd Server has not finished login in. --\n"
"-- Please ensure your connection is OK --\n"
"--------------------------------------------------\n";

s+=(char)0x1F;
}

// communicate connectivity status
s += "connectivity:";
s += SSTR(api->isWaiting());
s += (char)0x1F;

// communicate status info
s+= "prompt:";
s+=dynamicprompt;
Expand Down Expand Up @@ -4541,14 +4573,15 @@ int main(int argc, char* argv[])

printWelcomeMsg();

if (!ConfigurationManager::session.empty())
MegaThread *tlogin = new MegaThread();
tlogin->start(startSession, &loginended);

// give it a while to try to login before answering petitions
long long millisecondstowait = 30000;
while (!loginended && millisecondstowait > 0)
{
loginInAtStartup = true;
stringstream logLine;
logLine << "login " << ConfigurationManager::session;
LOG_debug << "Executing ... " << logLine.str().substr(0,9) << "...";
process_line((char*)logLine.str().c_str());
loginInAtStartup = false;
sleepMilliSeconds(100);
millisecondstowait -= 100;
}

megacmd();
Expand Down
2 changes: 1 addition & 1 deletion src/megacmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ enum confirmresponse
void changeprompt(const char *newprompt);

void informStateListener(std::string message, int clientID);
void broadcastMessage(std::string message);
void broadcastMessage(std::string message, const char *suffix = "message:");

mega::MegaApi* getFreeApiFolder();
void freeApiFolder(mega::MegaApi *apiFolder);
Expand Down
2 changes: 2 additions & 0 deletions src/megacmdcommonutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ int toInteger(std::string what, int failValue = -1);

std::string joinStrings(const std::vector<std::string>& vec, const char* delim = " ", bool quoted=true);

unsigned int getstringutf8size(const std::string &str);

std::string getFixLengthString(const std::string origin, unsigned int size, const char delimm=' ', bool alignedright = false);

std::string getRightAlignedString(const std::string origin, unsigned int minsize);
Expand Down
Loading