/* return 0 on error/blocking, 1 is ok and ready */ static int dev_cmd_wait(struct device *dev, int cmd) { int sleeptime = 10; do { if (cancelled(dev)) return 0; if (!dev_cmd(dev, cmd)) { dev->state = SANE_STATUS_IO_ERROR; return 0; } else if (dev->state) { if (dev->state != SANE_STATUS_DEVICE_BUSY) return 0; else { if (dev->non_blocking) { dev->state = SANE_STATUS_GOOD; return 0; } else { if (sleeptime > 1000) sleeptime = 1000; DBG (4, "(%s) sleeping(%d ms).. [%x %x]\n", str_cmd(cmd), sleeptime, dev->res[4], dev->res[5]); usleep(sleeptime * 1000); if (sleeptime < 1000) sleeptime *= (sleeptime < 100)? 10 : 2; } } /* BUSY */ } } while (dev->state == SANE_STATUS_DEVICE_BUSY); return 1; }
/** Main interface function, loops infinitly and waits for user to input strings in std::in, each command is then sent to the CommandFactory to be made into a command object (assuming the command is valid), the command is then run, and the result is displayed back to the user */ void TwitterClient::startInterface() { while (1) { const short bufsize = 1024; char buf[bufsize]; std::cout << ">> "; std::flush(std::cout); std::cin.getline(buf, bufsize); std::string str_cmd(buf); if (str_cmd == "exit_client") { this->onDisconnect(); break; } twitter::commands::Command * cmd = twitter::commands::CommandFactory::createFactory(str_cmd, this); if (cmd == 0) { std::cerr << "Error: Unknown command name : " << str_cmd << std::endl; } else { bool success = (*cmd).execute(); if (!success) { std::cerr << "Error: " << (*cmd).reason() << std::endl; } delete cmd; //After the command ran, delete it from memory } //if-else } //while std::cout << "Thank you, Goodbye!" << std::endl; }
/* return 0: on error, 1: success */ static int dev_command (struct device *dev, SANE_Byte * cmd, size_t reqlen) { SANE_Status status; size_t sendlen = cmd[3] + 4; SANE_Byte *res = dev->res; assert (reqlen <= sizeof (dev->res)); /* requested len */ dev->reslen = sizeof (dev->res); /* doing full buffer to flush stalled commands */ if (cmd[2] == CMD_SET_WINDOW) { /* Set Window have wrong packet length, huh. */ sendlen = 25; } if (cmd[2] == CMD_READ_IMAGE) { /* Read Image is raw data, don't need to read response */ res = NULL; } dev->state = 0; DBG (4, ":: dev_command(%s[%#x], %lu)\n", str_cmd(cmd[2]), cmd[2], (u_long)reqlen); status = dev->io->dev_request(dev, cmd, sendlen, res, &dev->reslen); if (status != SANE_STATUS_GOOD) { DBG (1, "%s: dev_request: %s\n", __FUNCTION__, sane_strstatus (status)); dev->state = SANE_STATUS_IO_ERROR; return 0; } if (!res) { /* if not need response just return success */ return 1; } /* normal command reply, some sanity checking */ if (dev->reslen < reqlen) { DBG (1, "%s: illegal response len %lu, need %lu\n", __FUNCTION__, (u_long)dev->reslen, (u_long)reqlen); dev->state = SANE_STATUS_IO_ERROR; return 0; } else { size_t pktlen; /* len specified in packet */ if (DBG_LEVEL > 3) dbg_dump(dev); if (dev->res[0] != RES_CODE) { DBG (2, "%s: illegal data header %02x\n", __FUNCTION__, dev->res[0]); dev->state = SANE_STATUS_IO_ERROR; return 0; } pktlen = dev->res[2] + 3; if (dev->reslen != pktlen) { DBG (2, "%s: illegal response len %lu, should be %lu\n", __FUNCTION__, (u_long)pktlen, (u_long)dev->reslen); dev->state = SANE_STATUS_IO_ERROR; return 0; } if (dev->reslen > reqlen) DBG (2, "%s: too big packet len %lu, need %lu\n", __FUNCTION__, (u_long)dev->reslen, (u_long)reqlen); } dev->state = 0; if (cmd[2] == CMD_SET_WINDOW || cmd[2] == CMD_OBJECT_POSITION || cmd[2] == CMD_READ || cmd[2] == CMD_RESERVE_UNIT) { if (dev->res[1] == STATUS_BUSY) dev->state = SANE_STATUS_DEVICE_BUSY; else if (dev->res[1] == STATUS_CANCEL) dev->state = SANE_STATUS_CANCELLED; else if (dev->res[1] == STATUS_CHECK) dev->state = resolv_state((cmd[2] == CMD_READ)? (dev->res[12] << 8 | dev->res[13]) : (dev->res[4] << 8 | dev->res[5])); if (dev->state) DBG (3, "%s(%s[%#x]): => %d: %s\n", __FUNCTION__, str_cmd(cmd[2]), cmd[2], dev->state, sane_strstatus(dev->state)); } return 1; }