int ChatServer::handle_data(ACE_SOCK_Stream *client) { if (DLOG) { printf("\n"); Util::log("[ChatServer::handle_data] START\n"); } // Place the connection into blocking mode since this // thread isn't doing anything except handling this client. client->disable(ACE_NONBLOCK); PacketHandler handler = PacketHandler(*client); ServerPacketListener spl = ServerPacketListener(handler); // Keep handling chat messages until client closes connection // or this thread is asked to cancel itself. ACE_Thread_Manager *mgr = ACE_Thread_Manager::instance(); ACE_thread_t me = ACE_Thread::self(); while (!mgr->testcancel(me) && handler.processPacket(spl) != 0) continue; handler.close(); if (DLOG) Util::log("[ChatServer::handle_data] END\n"); return 0; }
void PacketManager::broadcastPacket(styx::ProtocolObject *protocolObject) { for ( std::list<PacketHandler*>::iterator ix = handlerList.begin(); ix != handlerList.end(); ix ++) { PacketHandler *handler = *ix; if ( handler->handlePacket(protocolObject) ) break; } }
int main() { // Initialize PortHandler instance // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows PortHandler *portHandler = PortHandler::GetPortHandler(DEVICENAME); // Initialize Packethandler instance // Set the protocol version // Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler PacketHandler *packetHandler = PacketHandler::GetPacketHandler(PROTOCOL_VERSION); int dxl_comm_result = COMM_TX_FAIL; // Communication result UINT8_T dxl_error = 0; // Dynamixel error UINT16_T dxl_model_number; // Dynamixel model number // Open port if( portHandler->OpenPort() ) { printf( "Succeeded to open the port!\n" ); } else { printf( "Failed to open the port!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Set port baudrate if( portHandler->SetBaudRate(BAUDRATE) ) { printf( "Succeeded to change the baudrate!\n" ); } else { printf( "Failed to change the baudrate!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Try to ping the Dynamixel // Get Dynamixel model number dxl_comm_result = packetHandler->Ping(portHandler, DXL_ID, &dxl_model_number, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); printf("[ID:%03d] Ping Succeeded. Dynamixel model number : %d\n", DXL_ID, dxl_model_number); // Close port portHandler->ClosePort(); return 0; }
int main() { // Initialize PortHandler instance // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows PortHandler *portHandler = PortHandler::GetPortHandler(DEVICENAME); // Initialize Packethandler instance // Set the protocol version // Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler PacketHandler *packetHandler = PacketHandler::GetPacketHandler(PROTOCOL_VERSION); int dxl_comm_result = COMM_TX_FAIL; // Communication result std::vector<UINT8_T> vec; // Dynamixel data storages // Open port if( portHandler->OpenPort() ) { printf( "Succeeded to open the port!\n" ); } else { printf( "Failed to open the port!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Set port baudrate if( portHandler->SetBaudRate(BAUDRATE) ) { printf( "Succeeded to change the baudrate!\n" ); } else { printf( "Failed to change the baudrate!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Try to broadcast ping the Dynamixel dxl_comm_result = packetHandler->BroadcastPing(portHandler, vec); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); printf("Detected Dynamixel : \n"); for(int i = 0; i < (int)vec.size(); i++) { printf("[ID:%03d]\n", vec.at(i)); } // Close port portHandler->ClosePort(); return 0; }
void NSPacketProc::RegisterPacketProc() { PacketHandler* poPacketHandler = (PacketHandler*)g_poContext->GetPacketHandler(); // 外部消息 poPacketHandler->RegsterExterPacketProc(NSMsgType::eLuaCmdMsg, (void*)OnLuaCmdMsg); // 内部消息 poPacketHandler->RegsterInnerPacketProc(NSMsgType::eLuaRpcMsg, (void*)OnLuaRpcMsg); poPacketHandler->RegsterInnerPacketProc(NSMsgType::eLuaCmdMsg, (void*)OnLuaCmdMsgInner); poPacketHandler->RegsterInnerPacketProc(NSSysCmd::ssRegServiceRet, (void*)OnRegisterRouterCallback); }
void NSPacketProc::RegisterPacketProc() { PacketHandler* poPacketHandler = g_poContext->GetPacketHandler(); poPacketHandler->RegsterInnerPacketProc(NSMsgType::eLuaRpcMsg, (void*)OnLuaRpcMsg); poPacketHandler->RegsterInnerPacketProc(NSMsgType::eLuaCmdMsg, (void*)OnLuaCmdMsg); poPacketHandler->RegsterInnerPacketProc(NSSysCmd::ssRegServiceRet, (void*)OnRegisterRouterCallback); //poPacketHandler->RegsterInnerPacketProc(NSSysCmd::ssClientClose, (void*)OnClientClose); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cRoleStartRunReq, (void*)OnRoleStartRun); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cRoleStopRunReq, (void*)OnRoleStopRun); }
int main(void) { char username[USERNAME_LEN]; char password[PASSWORD_LEN]; puts("**Login**"); printf("Enter username: "******"Enter password: "******"Authing user: %s ..\n", username); PacketHandler pck; string login_string = pck.login_packet(username, password, 1.0); int auth_sock = connect_to("localhost", AUTH_PORT); send(auth_sock, login_string.data(), login_string.length(), 0); string auth_code = pck.auth_code_packet((char *)get_auth_code(auth_sock).c_str()); int serv_sock; if (auth_code.length() > 0) { puts("Connecting to game server.."); serv_sock = connect_to("localhost", SERV_PORT); send(serv_sock, auth_code.data(), auth_code.length(), 0); } else { //prompt to login again exit(0); } for( ; ; ) { read_data(serv_sock); } }
inline ClientHandler(PacketHandler<ClientHandler>& mPacketHandler) : packetHandler(mPacketHandler) { onPacketReceived += [this](sf::Packet mPacket){ packetHandler.handle(*this, mPacket); refreshTimeout(); }; timeoutFuture = std::async(std::launch::async, [this] { while(running) { std::this_thread::sleep_for(800ms); if(!isBusy() || --untilTimeout > 0) continue; HG_LO_VERBOSE("ClientHandler") << "Client (" << uid << ") timed out\n"; onDisconnect(); disconnect(); } }); }
int main() { // Initialize PortHandler instance // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows PortHandler *portHandler = PortHandler::GetPortHandler(DEVICENAME); // Initialize Packethandler instance // Set the protocol version // Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler PacketHandler *packetHandler = PacketHandler::GetPacketHandler(PROTOCOL_VERSION); // Initialize Groupsyncwrite instance GroupSyncWrite groupSyncWrite(portHandler, packetHandler, ADDR_MX_GOAL_POSITION, LEN_MX_GOAL_POSITION); int index = 0; int dxl_comm_result = COMM_TX_FAIL; // Communication result bool dxl_addparam_result = false; // AddParam result int dxl_goal_position[2] = {DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE}; // Goal position UINT8_T dxl_error = 0; // Dynamixel error UINT8_T param_goal_position[2]; UINT16_T dxl1_present_position = 0, dxl2_present_position = 0; // Present position // Open port if( portHandler->OpenPort() ) { printf( "Succeeded to open the port!\n" ); } else { printf( "Failed to open the port!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Set port baudrate if( portHandler->SetBaudRate(BAUDRATE) ) { printf( "Succeeded to change the baudrate!\n" ); } else { printf( "Failed to change the baudrate!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Enable Dynamixel#1 Torque dxl_comm_result = packetHandler->Write1ByteTxRx(portHandler, DXL1_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); else printf("Dynamixel#%d has been successfully connected \n", DXL1_ID); // Enable Dynamixel#2 Torque dxl_comm_result = packetHandler->Write1ByteTxRx(portHandler, DXL2_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); else printf("Dynamixel#%d has been successfully connected \n", DXL2_ID); while(1) { printf("Press any key to continue! (or press ESC to quit!)\n"); if(_getch() == ESC_ASCII_VALUE) break; // Allocate goal position value into byte array param_goal_position[0] = DXL_LOBYTE(dxl_goal_position[index]); param_goal_position[1] = DXL_HIBYTE(dxl_goal_position[index]); // Add Dynamixel#1 goal position value to the Syncwrite storage dxl_addparam_result = groupSyncWrite.AddParam(DXL1_ID, param_goal_position); if( dxl_addparam_result != true ) { fprintf(stderr, "[ID:%03d] groupSyncWrite addparam failed", DXL1_ID); return 0; } // Add Dynamixel#2 goal position value to the Syncwrite parameter storage dxl_addparam_result = groupSyncWrite.AddParam(DXL2_ID, param_goal_position); if( dxl_addparam_result != true ) { fprintf(stderr, "[ID:%03d] groupSyncWrite addparam failed", DXL2_ID); return 0; } // Syncwrite goal position dxl_comm_result = groupSyncWrite.TxPacket(); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); // Clear syncwrite parameter storage groupSyncWrite.ClearParam(); do { // Read Dynamixel#1 present position dxl_comm_result = packetHandler->Read2ByteTxRx(portHandler, DXL1_ID, ADDR_MX_PRESENT_POSITION, &dxl1_present_position, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); // Read Dynamixel#2 present position dxl_comm_result = packetHandler->Read2ByteTxRx(portHandler, DXL2_ID, ADDR_MX_PRESENT_POSITION, &dxl2_present_position, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); printf("[ID:%03d] GoalPos:%03d PresPos:%03d\t[ID:%03d] GoalPos:%03d PresPos:%03d\n", DXL1_ID, dxl_goal_position[index], dxl1_present_position, DXL2_ID, dxl_goal_position[index], dxl2_present_position); }while((abs(dxl_goal_position[index] - dxl1_present_position) > DXL_MOVING_STATUS_THRESHOLD) || (abs(dxl_goal_position[index] - dxl2_present_position) > DXL_MOVING_STATUS_THRESHOLD)); // Change goal position if( index == 0 ) index = 1; else index = 0; } // Disable Dynamixel#1 Torque dxl_comm_result = packetHandler->Write1ByteTxRx(portHandler, DXL1_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); // Disable Dynamixel#2 Torque dxl_comm_result = packetHandler->Write1ByteTxRx(portHandler, DXL2_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); // Close port portHandler->ClosePort(); return 0; }
void NSPacketProc::RegisterPacketProc() { PacketHandler* poPacketHandler = g_poContext->GetPacketHandler(); poPacketHandler->RegsterInnerPacketProc(NSMsgType::eLuaRpcMsg, (void*)OnLuaRpcMsg); poPacketHandler->RegsterInnerPacketProc(NSMsgType::eLuaCmdMsg, (void*)OnLuaCmdMsg); poPacketHandler->RegsterInnerPacketProc(NSSysCmd::ssRegServiceRet, (void*)OnRegisterRouterCallback); poPacketHandler->RegsterInnerPacketProc(NSSysCmd::ssServiceClose, (void*)OnServiceClose); poPacketHandler->RegsterInnerPacketProc(NSSysCmd::ssClientClose, (void*)OnClientClose); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cPlayerRun, (void*)OnPlayerRun); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cPlayerStopRun, (void*)OnPlayerStopRun); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::ppActorStartAttack, (void*)OnActorStartAttack); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::ppActorStopAttack, (void*)OnActorStopAttack); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cActorHurted, (void*)OnActorHurted); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cActorDamage, (void*)OnActorDamage); poPacketHandler->RegsterInnerPacketProc(NSCltSrvCmd::cEveHurted, (void*)OnEveHurted); }
int main(int argc, char* argv[]) { /* * Signals */ boost::asio::io_service signalService; boost::asio::signal_set signals(signalService, SIGINT, SIGTERM, SIGQUIT); signals.async_wait(handle_stop); boost::thread signalThread( boost::bind(&boost::asio::io_service::run, &signalService)); /* * Static Class initializations */ TriggerOptions::Load(argc, argv); MyOptions::Load(argc, argv); try { ZMQHandler::Initialize(Options::GetInt(OPTION_ZMQ_IO_THREADS)); } catch (const zmq::error_t& ex) { LOG_ERROR("Failed to initialize ZMQ because: " << ex.what()); exit(1); } HLTStruct HLTConfParams; HLTriggerManager::fillStructFromXMLFile(HLTConfParams); L1TriggerProcessor::initialize(HLTConfParams.l1); L2TriggerProcessor::initialize(HLTConfParams.l2); FarmStatistics::init(); FarmStatistics farmstats; farmstats.startThread("StatisticsWriter"); /* * initialize NIC handler and start gratuitous ARP request sending thread */ NetworkHandler NetworkHandler(Options::GetString(OPTION_ETH_DEVICE_NAME)); NetworkHandler.startThread("ArpSender"); SourceIDManager::Initialize(Options::GetInt(OPTION_TS_SOURCEID), Options::GetIntPairList(OPTION_DATA_SOURCE_IDS), Options::GetIntPairList(OPTION_L1_DATA_SOURCE_IDS)); BurstIdHandler::initialize(Options::GetInt(OPTION_FIRST_BURST_ID), &onBurstFinished); HandleFrameTask::initialize(); EventSerializer::initialize(); try { StorageHandler::initialize(); } catch(const zmq::error_t& ex) { LOG_ERROR("Failed to initialize StorageHandler because: " << ex.what()); exit(1); } StorageHandler sh; sh.startThread("StorageHandler"); //StrawReceiver::initialize(); L1Builder::initialize(); L2Builder::initialize(); Event::initialize(MyOptions::GetBool(OPTION_PRINT_MISSING_SOURCES)); // Initialize Detector counts. L1=32 because 0=gtk, 1-30 Lkr, 31 MUV // 20 should be changed with a proper dynamic definition of the number of L0 sources DetectorStatistics::init(20,32); DetectorStatistics::clearL0DetectorStatistics(); DetectorStatistics::clearL1DetectorStatistics(); // Get the list of farm nodes and find my position vector<std::string> nodes = Options::GetStringList(OPTION_FARM_HOST_NAMES); std::string myIP = EthernetUtils::ipToString( EthernetUtils::GetIPOfInterface( Options::GetString(OPTION_ETH_DEVICE_NAME))); uint logicalNodeID = 0xffffffff; for (size_t i = 0; i < nodes.size(); ++i) { if (myIP == nodes[i]) { logicalNodeID = i; break; } } if (logicalNodeID == 0xffffffff) { LOG_ERROR( "You must provide a list of farm nodes IP addresses containing the IP address of this node!"); exit(1); } EventPool::initialize( Options::GetInt(OPTION_MAX_NUMBER_OF_EVENTS_PER_BURST), nodes.size(), logicalNodeID, Options::GetInt(OPTION_NUMBER_OF_FRAGS_PER_L0MEP)); l1::L1DistributionHandler::Initialize( Options::GetInt(OPTION_MAX_TRIGGERS_PER_L1MRP), Options::GetInt(OPTION_MIN_USEC_BETWEEN_L1_REQUESTS), Options::GetStringList(OPTION_CREAM_MULTICAST_GROUP), Options::GetInt(OPTION_CREAM_RECEIVER_PORT), Options::GetInt(OPTION_CREAM_MULTICAST_PORT)); /* * Burst Handler */ LOG_INFO("Start burst handler thread."); BurstIdHandler bHandler; bHandler.startThread("BurstHandler"); /* * Monitor */ LOG_INFO("Starting Monitoring Services"); monitoring::MonitorConnector monitor; monitoring::MonitorConnector::setState(INITIALIZING); monitor.startThread("MonitorConnector"); /* * L1 Distribution handler */ l1::L1DistributionHandler l1Handler; l1Handler.startThread("L1DistributionHandler"); /* * Packet Handler */ unsigned int numberOfPacketHandler = NetworkHandler::GetNumberOfQueues(); LOG_INFO("Starting " << numberOfPacketHandler << " PacketHandler threads"); for (unsigned int i = 0; i < numberOfPacketHandler; i++) { PacketHandler* handler = new PacketHandler(i); packetHandlers.push_back(handler); uint coresPerSocket = std::thread::hardware_concurrency() / 2/*hyperthreading*/; uint cpuMask = i % 2 == 0 ? i / 2 : coresPerSocket + i / 2; handler->startThread(i, "PacketHandler", cpuMask, 25, MyOptions::GetInt(OPTION_PH_SCHEDULER)); } //for (unsigned int i = 0; i < 4; i++) { for (unsigned int i = 0; i < std::thread::hardware_concurrency() - numberOfPacketHandler; i++) { TaskProcessor* tp = new TaskProcessor(); taskProcessors.push_back(tp); tp->startThread(i, "TaskProcessor"); } CommandConnector c; c.startThread(0, "Commandconnector", -1, 1); monitoring::MonitorConnector::setState(RUNNING); /* * Join PacketHandler and other threads */ AExecutable::JoinAll(); return 0; }
int main(int argc, char* argv[]) { /* * Signals */ boost::asio::io_service signalService; boost::asio::signal_set signals(signalService, SIGINT, SIGTERM, SIGQUIT); signals.async_wait(handle_stop); boost::thread signalThread( boost::bind(&boost::asio::io_service::run, &signalService)); L1TriggerProcessor::registerDownscalingAlgorithms(); L1TriggerProcessor::registerReductionAlgorithms(); /* * Static Class initializations */ TriggerOptions::Load(argc, argv); MyOptions::Load(argc, argv); ZMQHandler::Initialize(Options::GetInt(OPTION_ZMQ_IO_THREADS)); L1TriggerProcessor::initialize( TriggerOptions::GetDouble(OPTION_L1_BYPASS_PROBABILITY)); L2TriggerProcessor::initialize( TriggerOptions::GetDouble(OPTION_L2_BYPASS_PROBABILITY)); /* * initialize NIC handler and start gratuitous ARP request sending thread */ NetworkHandler NetworkHandler(Options::GetString(OPTION_ETH_DEVICE_NAME)); NetworkHandler.startThread("ArpSender"); SourceIDManager::Initialize(Options::GetInt(OPTION_TS_SOURCEID), Options::GetIntPairList(OPTION_DATA_SOURCE_IDS), Options::GetIntPairList(OPTION_CREAM_CRATES), Options::GetIntPairList(OPTION_INACTIVE_CREAM_CRATES), Options::GetInt(OPTION_MUV_CREAM_CRATE_ID)); BurstIdHandler::initialize(Options::GetInt(OPTION_FIRST_BURST_ID)); HandleFrameTask::initialize(); EventSerializer::initialize(); StorageHandler::initialize(); StrawReceiver::initialize(); L1Builder::initialize(); L2Builder::initialize(); Event::initialize(MyOptions::GetBool(OPTION_PRINT_MISSING_SOURCES), Options::GetBool(OPTION_WRITE_BROKEN_CREAM_INFO)); EventPool::initialize(Options::GetInt( OPTION_MAX_NUMBER_OF_EVENTS_PER_BURST)); cream::L1DistributionHandler::Initialize( Options::GetInt(OPTION_MAX_TRIGGERS_PER_L1MRP), Options::GetInt(OPTION_NUMBER_OF_EBS), Options::GetInt(OPTION_MIN_USEC_BETWEEN_L1_REQUESTS), Options::GetStringList(OPTION_CREAM_MULTICAST_GROUP), Options::GetInt(OPTION_CREAM_RECEIVER_PORT), Options::GetInt(OPTION_CREAM_MULTICAST_PORT)); /* * Monitor */ LOG_INFO<<"Starting Monitoring Services"; monitoring::MonitorConnector monitor; monitoring::MonitorConnector::setState(INITIALIZING); monitor.startThread("MonitorConnector"); /* * L1 Distribution handler */ cream::L1DistributionHandler l1Handler; l1Handler.startThread("L1DistributionHandler"); /* * Packet Handler */ unsigned int numberOfPacketHandler = NetworkHandler::GetNumberOfQueues(); LOG_INFO<< "Starting " << numberOfPacketHandler << " PacketHandler threads" << ENDL; for (unsigned int i = 0; i < numberOfPacketHandler; i++) { PacketHandler* handler = new (tbb::task::allocate_root()) PacketHandler( i); packetHandlers.push_back(handler); uint coresPerSocket = std::thread::hardware_concurrency() / 2/*hyperthreading*/; uint cpuMask = i % 2 == 0 ? i / 2 : coresPerSocket + i / 2; handler->startThread(i, "PacketHandler", cpuMask, 15, MyOptions::GetInt(OPTION_PH_SCHEDULER)); } CommandConnector c; c.startThread(0, "Commandconnector", -1, 0); monitoring::MonitorConnector::setState(RUNNING); /* * Join PacketHandler and other threads */ AExecutable::JoinAll(); return 0; }
/* * This is a demo for using firewall emulator: * enter the project directory: * $g++ main.cpp -o main -lpcap * $./main dump.pcap 5 filter.pcap > procedure.txt */ int main(int argc, char *argv[]) { PacketHandler ph; if(argc<4) { printf("please input test filename, max tcp connections and filter file name!\n"); return 0; } ph.MAXTCPNUM = atoi(argv[2]); printf ("test filename = %s\n", argv[1]); printf("max tcp connections: %u\n", ph.MAXTCPNUM); printf ("filter filename = %s\n", argv[3]); //read the libpcap version static const char *version; version = pcap_lib_version(); printf("version: %s\n\n", version); //show device ph.util.showLocalDev(); //get local mac address ph.util.getLocalMacAddr(); //open the dumped cap file char *dev, errBuff[PCAP_ERRBUF_SIZE]; pcap_t *handle = NULL; handle = pcap_open_offline( argv[1] , errBuff); if (NULL == handle) { printf("error: %s\n", errBuff); return (EXIT_FAILURE); } // write legal packets to filter.pcap pcap_dumper_t *pd = pcap_dump_open(handle, argv[3]); PCAP_PKTHEADER *pktHeader; int status; const u_char *pktData; u_char * args = NULL; do { status = pcap_next_ex(handle, &pktHeader, &pktData ); ph.handle_ethernet(args, pktData, pktHeader, pd); } while (status == 1); // conclusion cout<<"# of received packets: "<<ph.total_packets<<endl; cout<<"# of packets droped by firewall emulator: "<<ph.total_packets - ph.count<<endl; cout<<"drop ratio is: "<<(ph.total_packets - ph.count) / (double)ph.total_packets<<endl; /* cout<<"size: "<<buf.size()<<endl; for(vector<IP_PKT>::iterator it = buf.begin(); it != buf.end(); it++) { IP_PKT tmp = *(it); cout<<tmp.srcPort<<endl<<tmp.destAddr<<endl; cout<<tmp.establish<<endl; cout<<tmp.close<<endl<<endl; } cout<<endl; cout<<"******************************"<<endl; cout<<"# of legal packet is: "<<count<<endl; //close the handle pcap_dump_close(pd); pcap_close(handle); return (EXIT_SUCCESS); */ }
int main() { // Initialize PortHandler instance // Set the port path // Get methods and members of PortHandlerLinux or PortHandlerWindows PortHandler *portHandler = PortHandler::GetPortHandler(DEVICENAME); // Initialize Packethandler instance // Set the protocol version // Get methods and members of Protocol1PacketHandler or Protocol2PacketHandler PacketHandler *packetHandler = PacketHandler::GetPacketHandler(PROTOCOL_VERSION); int index = 0; int dxl_comm_result = COMM_TX_FAIL; // Communication result int dxl_goal_position[2] = {DXL_MINIMUM_POSITION_VALUE, DXL_MAXIMUM_POSITION_VALUE}; // Goal position UINT8_T dxl_error = 0; // Dynamixel error UINT16_T dxl_present_position = 0; // Present position // Open port if( portHandler->OpenPort() ) { printf( "Succeeded to open the port!\n" ); } else { printf( "Failed to open the port!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Set port baudrate if( portHandler->SetBaudRate(BAUDRATE) ) { printf( "Succeeded to change the baudrate!\n" ); } else { printf( "Failed to change the baudrate!\n" ); printf( "Press any key to terminate...\n" ); _getch(); return 0; } // Enable DXL Torque dxl_comm_result = packetHandler->Write1ByteTxRx(portHandler, DXL_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_ENABLE, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); else printf("Dynamixel has been successfully connected \n"); while(1) { printf("Press any key to continue! (or press ESC to quit!)\n"); if(_getch() == ESC_ASCII_VALUE) break; // Write goal position dxl_comm_result = packetHandler->Write2ByteTxRx(portHandler, DXL_ID, ADDR_MX_GOAL_POSITION, dxl_goal_position[index], &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); do { // Read present position dxl_comm_result = packetHandler->Read2ByteTxRx(portHandler, DXL_ID, ADDR_MX_PRESENT_POSITION, &dxl_present_position, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); printf("[ID:%03d] GoalPos:%03d PresPos:%03d\n", DXL_ID, dxl_goal_position[index], dxl_present_position); }while((abs(dxl_goal_position[index] - dxl_present_position) > DXL_MOVING_STATUS_THRESHOLD)); // Change goal position if( index == 0 ) index = 1; else index = 0; } // Disable Dynamixel Torque dxl_comm_result = packetHandler->Write1ByteTxRx(portHandler, DXL_ID, ADDR_MX_TORQUE_ENABLE, TORQUE_DISABLE, &dxl_error); if(dxl_comm_result != COMM_SUCCESS) packetHandler->PrintTxRxResult(dxl_comm_result); else if(dxl_error != 0) packetHandler->PrintRxPacketError(dxl_error); // Close port portHandler->ClosePort(); return 0; }