Beispiel #1
0
static PyObject *DFSA_chen( PyObject *self, PyObject *args ) {

	Result *result = aloha(chen);
	
	return Py_BuildValue("[(idd)(idd)(idd)(idd)(idd)(idd)(idd)(idd)(idd)(idd)]", 
		result[0].numIteration, result[0].numEmpty, result[0].numCollision,
		result[1].numIteration, result[1].numEmpty, result[1].numCollision,
		result[2].numIteration, result[2].numEmpty, result[2].numCollision,
		result[3].numIteration, result[3].numEmpty, result[3].numCollision,
		result[4].numIteration, result[4].numEmpty, result[4].numCollision,
		result[5].numIteration, result[5].numEmpty, result[5].numCollision,
		result[6].numIteration, result[6].numEmpty, result[6].numCollision,
		result[7].numIteration, result[7].numEmpty, result[7].numCollision,
		result[8].numIteration, result[8].numEmpty, result[8].numCollision,
		result[9].numIteration, result[9].numEmpty, result[9].numCollision
		);
} 
Beispiel #2
0
/** Called when the server is on the same LAN. It uses broadcast to
 *  find and conntect to the server.
 */
void ConnectToServer::handleSameLAN()
{
    // just send a broadcast packet, the client will know our 
    // ip address and will connect
    STKHost* host = STKHost::get();
    host->stopListening(); // stop the listening

    Log::info("ConnectToServer", "Waiting broadcast message.");

    TransportAddress sender;
    // get the sender
    const int LEN=256;
    char buffer[LEN];
    int len = host->receiveRawPacket(buffer, LEN, &sender, 2000);
    if(len<0)
    {
        Log::warn("ConnectToServer",
                  "Received invalid server information message.");
        return;
    }

    BareNetworkString message(buffer, len);
    std::string received;
    message.decodeString(&received);
    host->startListening(); // start listening again
    std::string aloha("aloha_stk");
    if (received==aloha)
    {
        Log::info("ConnectToServer", "LAN Server found : %s",
                   sender.toString().c_str());
#ifndef WIN32
        // just check if the ip is ours : if so, 
        // then just use localhost (127.0.0.1)
        struct ifaddrs *ifap, *ifa;
        struct sockaddr_in *sa;
        getifaddrs(&ifap); // get the info
        for (ifa = ifap; ifa; ifa = ifa->ifa_next)
        {
            if (ifa->ifa_addr->sa_family == AF_INET)
            {
                sa = (struct sockaddr_in *) ifa->ifa_addr;

                // This interface is ours
                if (ntohl(sa->sin_addr.s_addr) == sender.getIP())
                    sender.setIP(0x7f000001); // 127.0.0.1
            }
        }
        freeifaddrs(ifap);
#else
        // Query the list of all IP addresses on the local host
        // First call to GetIpAddrTable with 0 bytes buffer
        // will return insufficient buffer error, and size
        // will contain the number of bytes needed for all
        // data. Repeat the process of querying the size
        // using GetIpAddrTable in a while loop since it
        // can happen that an interface comes online between
        // the previous call to GetIpAddrTable and the next
        // call.
        MIB_IPADDRTABLE *table = NULL;
        unsigned long size = 0;
        int error = GetIpAddrTable(table, &size, 0);
        // Also add a count to limit the while loop - in
        // case that something strange is going on.
        int count = 0;
        while (error == ERROR_INSUFFICIENT_BUFFER && count < 10)
        {
            delete[] table;   // deleting NULL is legal
            table = (MIB_IPADDRTABLE*)new char[size];
            error = GetIpAddrTable(table, &size, 0);
            count++;
        }   // while insufficient buffer
        for (unsigned int i = 0; i < table->dwNumEntries; i++)
        {
            unsigned int ip = ntohl(table->table[i].dwAddr);
            if (sender.getIP() == ip) // this interface is ours
            {
                sender.setIP(0x7f000001); // 127.0.0.1
                break;
            }
        }
        delete[] table;

#endif
        m_server_address.copy(sender);
        m_state = CONNECTING;
    }
}  // handleSameLAN