Beispiel #1
0
/********************************************************************
 SqlCommandExecuteQuery - executes a SQL command and returns the results if desired

 NOTE: ppirs and pcRoes are optional
********************************************************************/
extern "C" HRESULT DAPI SqlCommandExecuteQuery(
    __in IDBCreateCommand* pidbCommand, 
    __in __sql_command LPCWSTR wzSql, 
    __out IRowset** ppirs,
    __out DBROWCOUNT* pcRows
    )
{
    Assert(pidbCommand);

    HRESULT hr = S_OK;
    ICommandText* picmdText = NULL;
    ICommand* picmd = NULL;
    DBROWCOUNT cRows = 0;

    if (pcRows)
    {
        *pcRows = NULL;
    }

    //
    // create the command
    //
    hr = pidbCommand->CreateCommand(NULL, IID_ICommand, (IUnknown**)&picmd);
    ExitOnFailure(hr, "failed to create command to execute session");

    //
    // set the sql text into the command
    //
    hr = picmd->QueryInterface(IID_ICommandText, (LPVOID*)&picmdText);
    ExitOnFailure(hr, "failed to get command text object for command");
    hr = picmdText->SetCommandText(DBGUID_DEFAULT , wzSql);
    ExitOnFailure1(hr, "failed to set SQL string: %ls", wzSql);

    //
    // execute the command
    //
    hr = picmd->Execute(NULL, (ppirs) ? IID_IRowset : IID_NULL, NULL, &cRows, reinterpret_cast<IUnknown**>(ppirs));
    ExitOnFailure1(hr, "failed to execute SQL string: %ls", wzSql);

    if (DB_S_ERRORSOCCURRED == hr)
    {
        hr = E_FAIL;
    }

    if (pcRows)
    {
        *pcRows = cRows;
    }

LExit:
    ReleaseObject(picmd);
    ReleaseObject(picmdText);

    return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
    ICommand* doorOpenCmd = new RemoteDoorCommand(new RemoteDoor("open"));
    Result doorOpenResult = doorOpenCmd->Execute();
    assert(Result::CRITICAL == doorOpenResult.getStatus());
    assert(0 == doorOpenResult.getMessage().compare("door open"));
    delete doorOpenCmd;

    ICommand* doorCloseCmd = new RemoteDoorCommand(new RemoteDoor("not open"));
    Result doorCloseResult = doorCloseCmd->Execute();
    assert(Result::OK == doorCloseResult.getStatus());
    assert(true == doorCloseResult.getMessage().empty());
    delete doorCloseCmd;

    Server server;
    doorOpenCmd = new RemoteDoorCommand(new RemoteDoor("open"));
    server.AddCommand(doorOpenCmd);
    server.Monitor();

	return 0;
}
int main()
{
	stack<ICommand*> cmd_stack;
	vector<Shape*> v;

	while (1)
	{
		int cmd;
		cin >> cmd;

		if (cmd == 1)
		{
			ICommand* p = new AddRectCommand(v);
			p->Execute();
			cmd_stack.push(p);
		}
		else if (cmd == 2)
		{
			ICommand* p = new AddCircleCommand(v);
			p->Execute();
			cmd_stack.push(p);
		}
		else if (cmd == 9)
		{
			ICommand* p = new DrawCommand(v);
			p->Execute();
			cmd_stack.push(p);
		}
		else if (cmd == 0)
		{
			ICommand* p = cmd_stack.top();
			if (p->CanUndo())
				p->Undo();
			cmd_stack.pop();
			delete p;
		}
	}
}
Beispiel #4
0
void Game::Play()
{
  string cmd;
  renderer->Render("Welcome to Fiends'n'Frogs adventure game!\n");
  ostringstream s;
  s << "by " << DEV_NAME << "(c) " << YEAR << ". Licensed under GPLv3.\n";
  renderer->Render(s.str());
  Player::AskInfo(player);
  renderer->Render("\nPlayer statistics:\n\n");
  player.PrintSummary();
  renderer->Render("\nAnd behold, the adventure begins!\n");
  
  player.SetGame(this);

  srand(time(NULL));
  while(running)
  {

    renderer->Render(GetCurrentRoom()->GetDescription());
    renderer->Render("\n> ");

    getline(cin,cmd);

    CommandFactory comm(this);
    ICommand *pCommand = comm.Create( cmd ); 
    if ( pCommand ) pCommand->Execute();

    delete pCommand;

    GetCurrentRoom()->Update();
      
    if ( player.GetHitpoints() <= 0 ) {
      
      renderer->Render("You're dead. Game over.\n");
      running = false;
    }
  }
  // final message to player
  renderer->Render("Exiting, bye!\n");
}
Beispiel #5
0
/********************************************************************
 SqlSessionExecuteQuery - executes a query and returns the results if desired

 NOTE: ppirs and pcRoes and pbstrErrorDescription are optional
********************************************************************/
extern "C" HRESULT DAPI SqlSessionExecuteQuery(
    __in IDBCreateSession* pidbSession, 
    __in __sql_command LPCWSTR wzSql, 
    __out_opt IRowset** ppirs,
    __out_opt DBROWCOUNT* pcRows,
    __out_opt BSTR* pbstrErrorDescription
    )
{
    Assert(pidbSession);

    HRESULT hr = S_OK;
    IDBCreateCommand* pidbCommand = NULL;
    ICommandText* picmdText = NULL;
    ICommand* picmd = NULL;
    DBROWCOUNT cRows = 0;

    if (pcRows)
    {
        *pcRows = NULL;
    }

    //
    // create the command
    //
    hr = pidbSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&pidbCommand);
    ExitOnFailure(hr, "failed to create database session");
    hr = pidbCommand->CreateCommand(NULL, IID_ICommand, (IUnknown**)&picmd);
    ExitOnFailure(hr, "failed to create command to execute session");

    //
    // set the sql text into the command
    //
    hr = picmd->QueryInterface(IID_ICommandText, (LPVOID*)&picmdText);
    ExitOnFailure(hr, "failed to get command text object for command");
    hr = picmdText->SetCommandText(DBGUID_DEFAULT , wzSql);
    ExitOnFailure1(hr, "failed to set SQL string: %ls", wzSql);

    //
    // execute the command
    //
    hr = picmd->Execute(NULL, (ppirs) ? IID_IRowset : IID_NULL, NULL, &cRows, reinterpret_cast<IUnknown**>(ppirs));
    ExitOnFailure1(hr, "failed to execute SQL string: %ls", wzSql);

    if (DB_S_ERRORSOCCURRED == hr)
    {
        hr = E_FAIL;
    }

    if (pcRows)
    {
        *pcRows = cRows;
    }

LExit:

    if (FAILED(hr) && picmd && pbstrErrorDescription)
    {
        HRESULT hrGetErrors = SqlGetErrorInfo(picmd, IID_ICommandText, 0x409, NULL, pbstrErrorDescription); // TODO: use current locale instead of always American-English
        if (FAILED(hrGetErrors))
        {
            ReleaseBSTR(*pbstrErrorDescription);
        }
    }

    ReleaseObject(picmd);
    ReleaseObject(picmdText);
    ReleaseObject(pidbCommand);

    return hr;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 *
 * NAME : perform 
 *
 * DESCRIPTION : reads from the pipes and execute the command.
 *
 *-----------------------------------------------------------------------------*/
int perform(int bytes)
{
#ifdef CS1_DEBUG
    char debug_buffer[255] = {0};
#endif
    char* buffer = NULL;    // TODO  This buffer scared me ! 
    int read_total = 0;
    ICommand* command  = NULL;
    char previous_command_buffer[MAX_COMMAND_SIZE] = {'\0'};
    unsigned char read = 0;

    for(int i = 0; i != bytes; i++) {
        read = (unsigned char)info_buffer[i];

#ifdef CS1_DEBUG
        std::ostringstream msg;
        msg << "Read from info pipe = " << (unsigned int)read << " bytes";
        Shakespeare::log(Shakespeare::NOTICE, LOGNAME, msg.str());
#endif

        switch (read) 
        {
            case NET2COM_SESSION_ESTABLISHED :
                break;
            case NET2COM_SESSION_END_CMD_CONFIRMATION :
            case NET2COM_SESSION_END_TIMEOUT :
            case NET2COM_SESSION_END_BY_OTHER_HOST :
            {
                int data_bytes = 0;
                while (data_bytes == 0) {
                    data_bytes = commander->ReadFromDataPipe(buffer, read_total);

                    if (data_bytes > 0) {
#ifdef CS1_DEBUG
                          std::ostringstream msg;
                          msg << "Read " << data_bytes << " bytes from ground station: ";
                          memset (debug_buffer,0,255);
                          for(uint8_t z = 0; z < data_bytes; ++z){
                              uint8_t c = buffer[z];
                              snprintf(debug_buffer,5, "0x%02X ", c);
                              msg << debug_buffer;
                          }
                          Shakespeare::log(Shakespeare::NOTICE, LOGNAME, msg.str());
#endif

                        if (data_bytes != read_total) {
                            Shakespeare::log(Shakespeare::ERROR, LOGNAME, "Something went wrong !!");
                            read_total = 0;
                            break;
                        }

                        FILE *fp_last_command = NULL;
                        unsigned int retry = 10000;

                        if (buffer[COMMAND_RESEND_INDEX] == COMMAND_RESEND_CHAR) 
                        {
                            while(retry > 0 && fp_last_command == NULL){
                                fp_last_command = fopen(LAST_COMMAND_FILENAME.c_str(), "r");
                                retry -=1;
                            }

                            if (fp_last_command != NULL) 
                            {
                                fread(previous_command_buffer, sizeof(char), MAX_COMMAND_SIZE, fp_last_command);
                                fclose(fp_last_command);

                                command = CommandFactory::CreateCommand(previous_command_buffer);

                                if (command != NULL) 
                                {
                                    Shakespeare::log(Shakespeare::NOTICE, 
                                                                    LOGNAME, 
                                                                            "Executing command");

                                    size_t size = 0;
                                    char* result  = (char* )command->Execute(&size);
                                    if (result != NULL) 
                                    {
                                        memset(log_buffer,0,MAX_BUFFER_SIZE);
                                        snprintf(log_buffer, MAX_BUFFER_SIZE, "Command output = %s\n", result);
                                        Shakespeare::log(Shakespeare::NOTICE,LOGNAME,log_buffer);

                                        if (size > 0) {
                                            commander->WriteToDataPipe(result, size);
                                        } else { // this branch will be remove as soon as ALL commands returns the SIZE ! TODO
                                                 // Because it is not safe to rely on a NULL terminator in the result buffer.
                                            commander->WriteToDataPipe(result);
                                        }

                                        free(result); // TODO allocate result buffer with new in all icommand subclasses and use delete
                                        result = NULL;
                                    } else {
                                        commander->WriteToInfoPipe(ERROR_EXECUTING_COMMAND);
                                    }

                                    if (command) {
                                        delete command;
                                        command = NULL;
                                    }
                                } else {
                                    commander->WriteToInfoPipe(ERROR_CREATING_COMMAND);
                                }

                                memset(previous_command_buffer, '\0', MAX_COMMAND_SIZE);
                            }
                        } else {

                            while (retry > 0 && fp_last_command == NULL) {
                                fp_last_command = fopen(LAST_COMMAND_FILENAME.c_str(), "w");
                                retry -= 1;
                            }

                            if (fp_last_command != NULL) {
                                fwrite(buffer, sizeof(char), data_bytes, fp_last_command);
                                fclose(fp_last_command);
                            }
                        }

                        free(buffer);
                        buffer = NULL;
                    }

                    sleep(COMMANER_SLEEP_TIME);
                } //end while

                read_total = 0;
                break;
            }
            default:
                    read_total += read;
                    buffer = (char* )realloc(buffer, read_total * sizeof(char)); // todo get rid of this realloc! use new instead
                    memset(buffer, 0, sizeof(char) * read_total);
                break;
        } // end switch
    } // end for

    return 0;
}