Example #1
0
// ****************************************************************************
// Method: SharedDaemon::handleConnection
//
// Purpose:
//  Handle the incomming connection..
//
// Arguments:
//
// Returns:
//
// Note:
//
// Programmer: Hari Krishnan
// Creation:   Oct 13, 2012
//
// Modifications:
//   Kathleen Biagas, Mon Dec 3 12:01:15 PST 2012
//   Use operator[] instead of 'at' to support older MSVC compiler.
//
// ****************************************************************************
bool
SharedDaemon::ParseInput(const QString& input, JSONNode& output)
{
    if(input.startsWith("{"))
    {
        JSONNode node;
        node.Parse(input.toStdString());
        //std::cout << node.ToString() << std::endl;
        /// also check to make sure password is coorect..
        if(node.GetType() != JSONNode::JSONOBJECT ||
           !node.HasKey("password") ||
            node.GetJsonObject()["password"].GetString() != password.toStdString())
            return false;

        output = node;
        return true;
    }

    return false;
}
Example #2
0
// ****************************************************************************
// Method: SharedDaemon::handleConnection
//
// Purpose:
//  Handle the incomming connection..
//
// Arguments:
//
// Returns:
//
// Note:
//
// Programmer: Hari Krishnan
// Creation:   Oct 13, 2012
//
// Modifications:
//   Kathleen Biagas, Mon Dec 3 12:01:15 PST 2012
//   Use operator[] instead of 'at' to support older MSVC compiler.
//
// ****************************************************************************
bool
SharedDaemon::ParseInput(const QString& input, std::string& lpasswd, bool& canRender)
{
    if(input.startsWith("{"))
    {
        JSONNode node;
        node.Parse(input.toStdString());

        if(node.GetType() != JSONNode::JSONOBJECT ||
           !node.HasKey("password") ||
            node.GetJsonObject()["password"].GetString() != password)
            return false;

        lpasswd = node.GetJsonObject()["password"].GetString();

        if(node.HasKey("canRender") == true &&
           node.GetJsonObject()["canRender"].GetType() == JSONNode::JSONBOOL)
            canRender = node.GetJsonObject()["canRender"].GetBool();

        return true;
    }

    return false;
}
Example #3
0
bool ViewerProxy::ConnectToExistingViewer(const std::string& host, const int& port, const std::string& password)
{

    //Step 1: Check and see if connection can be made..
    int testSocket = socket(AF_INET, SOCK_STREAM, 0);
    if( testSocket < 0 )
    {
        std::cerr << "Socket not created (ERROR)" << std::endl;
        return false;
    }

    std::cout << "connecting to host: " << host << " port " << port << std::endl;
    struct sockaddr_in sin;
    struct hostent *server = gethostbyname(host.c_str());
    memset(&sin, 0, sizeof(sin));
    memcpy(&(sin.sin_addr), server->h_addr, server->h_length);
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);

    if (connect(testSocket,(struct sockaddr*) &sin,sizeof(sin)) < 0)
    {
        std::cerr << "Unable to connect to Viewer" << std::endl;
        CloseSocket(testSocket);
        return false;
    }

    //Step 2: Send password to verify that you should be added
    std::ostringstream handshake;
    handshake << "{ \"password\" : \"" << password << "\" }";

#ifndef _WIN32
    int nwrite = write(testSocket,handshake.str().c_str(),handshake.str().length());
#else
    int nwrite = _write(testSocket,handshake.str().c_str(),(unsigned int)handshake.str().length());
#endif
    if(nwrite < 0)
    {
        std::cerr << "Error writing to Viewer" << std::endl;
        CloseSocket(testSocket);
        return false;
    }

    //Step 3: receive arguments to establish reverse connection

    char buffer[1024];

#ifndef _WIN32
    int bytes = read(testSocket,buffer,1024);
#else
    int bytes = _read(testSocket,buffer,1024);
#endif
    buffer[bytes] = '\0';
    //std::cout << "bytes read: " << bytes << " " << buffer << std::endl;

    CloseSocket(testSocket);
    //Step 4: reverse connect same as if it was originally intented..

    //parse message and create new reverse connect

    std::string message = buffer;

    JSONNode node;
    node.Parse(message);

    stringVector args;

    args.push_back("-v");
    args.push_back(node.GetJsonObject()["version"].GetString());

    args.push_back("-host");
    args.push_back(node.GetJsonObject()["host"].GetString());

    args.push_back("-port");
    args.push_back(node.GetJsonObject()["port"].GetString());

    args.push_back("-key");
    args.push_back(node.GetJsonObject()["securityKey"].GetString());

    args.push_back("-reverse_launch");

    int inputArgc = args.size();

    char** inputArgv = new char* [inputArgc+1];

    for(size_t i = 0; i < args.size(); ++i)
    {
        inputArgv[i] = new char [args[i].length()+1];
        strcpy(inputArgv[i],args[i].c_str());
        inputArgv[i][args[i].length()] = '\0';
    }

    inputArgv[inputArgc] = NULL;

    // Connect to the viewer process. Our command line arguments
    // should contain  The viewer is executed using
    // "visit -viewer".
    //

    viewerP = new ParentProcess;
    viewerP->Connect(1, 1, &inputArgc, &inputArgv, true);

    // Use viewerP's connections for xfer.
    xfer->SetInputConnection(viewerP->GetWriteConnection());
    xfer->SetOutputConnection(viewerP->GetReadConnection());
    return true;
}
int
SocketConnection::Fill()
{
    if(destFormat.Format == TypeRepresentation::ASCIIFORMAT)
    {
        std::string xmlString = "";

        char tmp[1001]; //leave 1 for null termination//

        int amountRead = 0;
        do
        {

#if defined(_WIN32)
            int amountRead = recv(descriptor, (char FAR *)tmp, 1000, 0);
            if(amountRead == SOCKET_ERROR)
            {
                LogWindowsSocketError("SocketConnection", "Fill");
                if(WSAGetLastError() == WSAEWOULDBLOCK)
                    return -1;
            }
#else
            amountRead = recv(descriptor, (void *)tmp, 1000, 0);
#endif

            if(amountRead > 0)
            {
                zeroesRead = 0;
                tmp[amountRead] = 0;
                xmlString += tmp;
            }

            ++zeroesRead;

            // If we have had a certain number of zero length reads in a row,
            // assume the connection died.
            if(zeroesRead > 100)
            {
                 EXCEPTION0(LostConnectionException);
            }
        }while(amountRead == 1000); //if it gets entire list..
        if(xmlString.size() > 0)
        {
            JSONNode node;
            node.Parse(xmlString);

            //std::cout << node.ToString() << std::endl;

            int guido = node["id"].GetInt();

            JSONNode contents = node["contents"];
//            JSONNode metadata = node["typeinfo"];

            /// With the information I have I could probably
            /// just use JSONNode to convert completely..
            /// but that would leave MapNode incomplete..

            MapNode mapnode(contents,false);

            //std::cout << mapnode.ToXML(false) << std::endl;
            //std::cout << metadata["data"] << std::endl;

            buffer.clear();
            return Write(guido,&mapnode); //,&metadata["data"]
        }

        return 0;
    }

    unsigned char tmp[1000];
#if defined(_WIN32)
    int amountRead = recv(descriptor, (char FAR *)tmp, 1000, 0);
    if(amountRead == SOCKET_ERROR)
    {
        LogWindowsSocketError("SocketConnection", "Fill");
        if(WSAGetLastError() == WSAEWOULDBLOCK)
            return -1;
    }
#else
    int amountRead = recv(descriptor, (void *)tmp, 1000, 0);
#endif

    if(amountRead > 0)
    {
        zeroesRead = 0;

        // Add the new bytes to the buffer.
        for(int i = 0; i < amountRead; ++i)
            buffer.push_back(tmp[i]);
    }
    else
        ++zeroesRead;

    // If we have had a certain number of zero length reads in a row,
    // assume the connection died.
    if(zeroesRead > 100)
    {
         EXCEPTION0(LostConnectionException);
    }

    return amountRead;
}