📄 obexsender.cpp
字号:
/* -*- c++ -*- --------------------------------------------------------------- Copyright (C) 2005, SWECO, All Rights Reserved. Implementation of OBEXSender class Author: Zsolt Molnar (Zsolt.Molnar@ieee.org) ---------------------------------------------------------------------------*/#include <sstream>#include "OBEXSender.hh"namespace { const char rcs_id[] = "$Id$"; }OBEXSender::_tErrMap OBEXSender::_errMap;pthread_mutex_t OBEXSender::_mapMutex;bool OBEXSender::_isInit = false;// Defined in obex_socket.hextern "C" { int obex_push(void* addr, char* path, char* remote);}OBEXSender::OBEXSender() { if (!_isInit) { pthread_mutex_init(&_mapMutex, NULL); _isInit = true; }}OBEXSender::tSendStateOBEXSender::send(const string& aFileName, const string& aTitle){ if (_port.empty()) { throw OBEXSender_NoTargetError(); } // Create an error code location for obex_push pthread_t tid = pthread_self(); pthread_mutex_lock(&_mapMutex); _errMap[tid] = 0; pthread_mutex_unlock(&_mapMutex); // Until the line is free... while (1) { // Push the message int res = obex_push((void*)((_port + "@9").c_str()), (char*)aFileName.c_str(), (char*)aTitle.c_str()); pthread_mutex_lock(&_mapMutex); int errCode = _errMap[tid]; _errMap.erase(tid); pthread_mutex_unlock(&_mapMutex); if (res == -ENOENT) { throw OBEXSender_NoFileError(aFileName); } // if OK, then the state is ACCEPTED, else... if (res != -1) { return ACCEPTED; } else { // check the error codes. it may be: switch (errCode) { // REFUSED case ECONNREFUSED: return REFUSED; // TIMEOUT case EAGAIN: return TIMEOUT; // BUSY, lets wait... case EBUSY: sleep(OS_BUSY_WAIT_TIME); continue; // ERROR default: return ERROR; } } }}extern "C" { int* __eebt_getErrLocation(const pthread_t aThread) { return &(OBEXSender::_errMap[aThread]); }}#ifdef __TEST__#include <assert.h>#include <iostream>boolOBEXSender::test(const Device& dev){ string message("testmessage.txt"); string title("TEST MESSAGE"); // Connect the dev cout << "Switch on Nokia 6600 (ID: " << dev.devID << ") bluetooth " << "and press enter" << endl << endl; getchar(); setPort(dev.devID); // send the message to dev and accept it cout << "You will get a message. Accept it." << endl << endl; assert(send(message, title) == ACCEPTED); cout << "Check if the message title is \"TEST MESSAGE\" and the content" << " is \"TEST MESSAGE CONTENT\". If so, the test item passed." << endl << endl; sleep(15); // send the message to dev and refuse it cout << "You will get a message. Refuse it." << endl << endl; assert(send(message, title) == REFUSED); sleep(10); // send the message to dev and wait until timeout cout << "You will get a message. Do nothing until timeout." << endl << endl; assert(send(message, title) == TIMEOUT); // send the message to dev and generate error cout << "You will get a message. You must generate an error. So switch off " << "Nokia 6600 (ID: " << dev.devID << ") bluetooth." << endl << endl; sleep(20); assert(send(message, title) == ERROR); cout << "OBEXSender test passed" << endl; // Check nofile bool nofile = false; try { send("atyala", title); } catch(IError& error) { nofile = true; } assert(nofile); return true;}#endif // #ifdef __TEST__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -