Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

testserverframework.cpp

00001 /// @cond EXCLUDEDTESTSOURCES
00002 /***************************************************************************
00003  * The contents of this file are subject to the Mozilla Public             *
00004  * License Version 1.1 (the "License"); you may not use this file          *
00005  * except in compliance with the License. You may obtain a copy of         *
00006  * the License at http://www.mozilla.org/MPL/                              *
00007  *                                                                         *
00008  * Software distributed under the License is distributed on an "AS         *
00009  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or              *
00010  * implied. See the License for the specific language governing            *
00011  * rights and limitations under the License.                               *
00012  *                                                                         *
00013  * The Original Code is Game Network Framework (GaNeF).                    *
00014  *                                                                         *
00015  * The Initial Developers of the Original Code are                         *
00016  * Lars Langer and Emanuel Greisen                                         *
00017  * Copyright (C) 2005. Lars Langer & Emanuel Greisen                       *
00018  * All Rights Reserved.                                                    *
00019  *                                                                         *
00020  * Contributor(s):                                                         *
00021  *   none yet....                                                          *
00022  *                                                                         *
00023  ***************************************************************************/
00024 #include "testserverframework.h"
00025 
00026 #include "testserversnakebite.h"
00027 
00028 TestServerFramework::TestServerFramework()
00029    : ServerFramework(8000)
00030 {
00031    std::cout << "Constructor called" << std::endl;
00032 }
00033 
00034 
00035 TestServerFramework::~TestServerFramework()
00036 {
00037 }
00038 
00039 bool TestServerFramework::onInitialHandshake( ServerPacket * handshake, ServerPacket * reply )
00040 {
00041    return true;
00042 }
00043 
00044 void TestServerFramework::onClientConnected( Client * _client )
00045 {
00046    TestClient * client = static_cast<TestClient *>(_client);
00047 
00048    // First let him know about the existing snakes
00049    // Send all updates (create/update/delete)
00050    for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
00051    {
00052       TestServerSnakeBite * sb = *it;
00053       sb->sendCreateObject(client);
00054    }
00055 
00056    // Some client connected: give him a snake
00057    TestServerSnakeBite * head = new TestServerSnakeBite(this, 5, 5, 0.1, 0, true, client);
00058    addTestServerSnakeBite(head);
00059    client->m_snake_head = head;
00060    for(int i = 0; i < 10; i++)
00061    {
00062       head = new TestServerSnakeBite(this, 10+7*i, 10+7*i, 0.1, head, false, client);
00063       addTestServerSnakeBite(head);
00064    }
00065    client->m_snake_tail = head;
00066 }
00067 
00068 void TestServerFramework::onClientDisconnected( Client * _client )
00069 {
00070    TestClient * client = static_cast<TestClient *>(_client);
00071    // Remove all his snake-bites
00072    for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end();)
00073    {
00074       if((*it)->getOwner() == client)
00075       {
00076          TestServerSnakeBite * bite = *it;
00077          it = snakes.erase(it);
00078          delete bite;
00079       }
00080       else
00081       {
00082          it++;
00083       }
00084    }
00085 }
00086 
00087 void TestServerFramework::addTestServerSnakeBite( TestServerSnakeBite * bite )
00088 {
00089    snakes.push_back(bite);
00090    // Send to the others
00091    bite->sendCreateObjectToAll();
00092    // Send to owner
00093    bite->sendCreateObject(bite->getOwner());
00094    bite->sendUpdate(bite->getOwner(), 30, true); // Let the owner know this is his
00095 }
00096 
00097 
00098 
00099 Client * TestServerFramework::createClient( const ipaddress & addr, int port )
00100 {
00101    return new TestClient(this, addr, port);
00102 }
00103 
00104 void TestServerFramework::mainLoop( )
00105 {
00106    std::cout << "mainLoop()\n";
00107 
00108    while(true)
00109    {
00110       // Get all incomming messages
00111       keepAlive();
00112 
00113       // Move all snake-bites
00114       for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
00115       {
00116          TestServerSnakeBite * sb = *it;
00117          sb->updateMovement();
00118       }
00119 
00120       // Let any head eat colliding tails
00121       for(std::map<unsigned int, Client *>::iterator it1 = getAllClients().begin(); it1 != getAllClients().end(); ++it1)
00122       {
00123          TestClient * c1 = static_cast<TestClient *>(it1->second);
00124          for(std::map<unsigned int, Client *>::iterator it2 = getAllClients().begin(); it2 != getAllClients().end(); ++it2)
00125          {
00126             TestClient * c2 = static_cast<TestClient *>(it2->second);
00127             if(c1 == c2 || c2->m_snake_tail->isHead())
00128                continue;
00129 
00130             if(c1->m_snake_head->dist(*c2->m_snake_tail) < 10.0)
00131             {
00132                TestServerSnakeBite * eaten_tail = c2->m_snake_tail;
00133                // Remove the tail from "c2"
00134                c2->m_snake_tail = eaten_tail->m_nextbite;
00135 
00136                // Add the new tail to "c1"
00137                eaten_tail->m_nextbite = c1->m_snake_tail;
00138                c1->m_snake_tail = eaten_tail;
00139                eaten_tail->m_owner = c1;
00140 
00141                // Tell the new owner about his snake and the old about his loss
00142                eaten_tail->sendUpdate(c1, 30, true);
00143                eaten_tail->sendUpdate(c2, 40, true);
00144 
00145                // Tell everyone about the change in next_bite
00146                eaten_tail->sendUpdateToAll(20, true);
00147             }
00148          }
00149       }
00150 
00151 
00152       // Send all updates (create/update/delete)
00153       for(std::vector<TestServerSnakeBite *>::iterator it = snakes.begin(); it != snakes.end(); ++it)
00154       {
00155          TestServerSnakeBite * sb = *it;
00156          sb->sendUpdateToAll(10, false);
00157          if(sb->isHead())
00158          {
00159             sb->sendUpdate(sb->getOwner(), 30, true);
00160          }
00161       }
00162 
00163 
00164       // Sleep a little
00165       psleep(100);
00166    }
00167 }
00168 
00169 /// @endcond

Generated on Mon Feb 6 12:24:50 2006 for Ganef by  doxygen 1.4.4