serverActivate.cpp

00001 /*
00002   サーバの起動
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "serverActivate.h"
00008 #include "serverActivateUnicode.h"
00009 #include "chatRoom.h"
00010 
00011 
00012 ServerActivate::ServerActivate(void)
00013   : server(NULL), title(NULL), host_text(NULL), ip_text(NULL),
00014     port_text(NULL), meta_text(NULL),
00015     activate_normal(NULL), activate_focused(NULL),
00016     cancel_normal(NULL), cancel_focused(NULL), port_fill(NULL),
00017     server_port(NULL), meta_check(NULL),
00018     activate_btn(NULL), cancel_btn(NULL), menu(NULL) {
00019 }
00020 
00021 
00022 ServerActivate::~ServerActivate(void) {
00023   deleteObject();
00024   delete to_server;
00025   to_server = NULL;
00026 }
00027 
00028 
00029 void ServerActivate::deleteObject(void) {
00030   delete title;
00031   delete host_text;
00032   delete ip_text;
00033   ip_text = NULL;
00034   delete port_text;
00035   delete meta_text;
00036   delete activate_normal;
00037   delete activate_focused;
00038   delete cancel_normal;
00039   delete cancel_focused;
00040   delete port_fill;
00041 
00042   delete server;
00043 }
00044 
00045 
00046 void ServerActivate::recreate(void) {
00047   win->lock();
00048   win->clear(ForeDepth);
00049   deleteObject();
00050   Initialize(0, NULL);
00051   win->unlock();
00052 }
00053 
00054 
00055 int ServerActivate::Initialize(int argc, char *argv[]) {
00056   win->clear(ForeDepth);
00057 
00058   // サーバの仮起動
00059   port = DefaultPort;
00060   if (!server) {
00061     // !!! property の設定を用いるように変更
00062     server =
00063       new ServerManage("localhost", "/~satofumi/nettyping/serverList.php");
00064     for (int i = 0; i < 100; ++i) {
00065       if (server->activate(port)) {
00066         break;
00067       }
00068       ++port;
00069     }
00070     if (!server->isActivated()) {
00071       delete server;
00072       server = NULL;
00073     }
00074   }
00075 
00076   // タイトル文字
00077   title = new DrawSurface(ttf->createText(ServerActivate_title, NormalSize,
00078                                           Fore, Back), true);
00079   win->add((new LabelComponent(title))
00080            ->setPosition(Grid(win->w/2, 90), Middle | Center), ForeDepth);
00081 
00082   // ホストの IP 番号を描画
00083   host_text = new DrawSurface(ttf->createText("Host IP", NormalSize,
00084                                               Fore, Back), true);
00085   if (server) {
00086     std::string ip_name = server->getOwnIP();
00087     if (!ip_name.empty()) {
00088       ip_text = new DrawSurface(ttf->createText(ip_name.c_str(),
00089                                                 NormalSize, Fore, Back), true);
00090     }
00091   }
00092   if (!ip_text) {
00093     ip_text = new DrawSurface(ttf->createText("unknown",
00094                                               NormalSize, Fore, Back), true);
00095   }
00096   int x_offset = 180;
00097   Grid draw_pos(win->w/2 - (x_offset + ip_text->w)/2, 140);
00098   win->add((new LabelComponent(host_text))
00099            ->setPosition(draw_pos, Left), ForeDepth);
00100   win->add((new LabelComponent(ip_text))
00101            ->setPosition(draw_pos + Grid(x_offset, 0), Left), ForeDepth);
00102 
00103   draw_pos.y += host_text->h +8;
00104 
00105   // 接続 port の入力用
00106   port_text = new DrawSurface(ttf->createText("Port", NormalSize,
00107                                               Fore, Back), true);
00108   win->add((new LabelComponent(port_text))
00109            ->setPosition(draw_pos, Left), ForeDepth);
00110 
00111   // 数値の入力コンポーネント
00112   port_fill = new FillSurface(Rect(8, NormalSize), Black);
00113   win->add((new LabelComponent(port_fill))
00114            ->setPosition(draw_pos + Grid(x_offset, 0), Left), ForeDepth);
00115   server_port =
00116     new TextInputComponent(*ttf, SmallSize,
00117                            Rect(NormalSize *3 -16, NormalSize), White, Black);
00118   char port_str[] = "XXXXX";
00119   sprintf(port_str, "%d", port);
00120   server_port->setText(port_str);
00121   server_port->setPosition(draw_pos + Grid(x_offset +8, 0));
00122   server_port->setBufferMax(5);
00123   server_port->disable(true);
00124   win->add(server_port, ForeDepth);
00125   draw_pos.y += port_text->h +8;
00126 
00127   // メタサーバに通知するかのチェックボックス
00128   meta_text = new DrawSurface(ttf->createText(ServerActivate_meta, TinySize,
00129                                               Fore, Back), true);
00130   meta_check = new CheckBoxComponent(*ttf);
00131   meta_check->setNowChecked(true);
00132   meta_check->setLength(18);
00133   meta_check->setColor(White, Black, Pink);
00134   meta_check->setLabel(new LabelComponent(meta_text));
00135   meta_check->activate();
00136   meta_check->setPosition(Grid(win->w - 8, 8), Right | Top);
00137   win->add(meta_check, ForeDepth);
00138   // !!! user に追加する、か?
00139 
00140   // メニュー表示
00141   activate_normal =
00142     new DrawSurface(ttf->createText(ServerActivate_activate, NormalSize,
00143                                     Normal, Back), true);
00144   activate_focused =
00145     new DrawSurface(ttf->createText(ServerActivate_activate, NormalSize,
00146                                     Focused, Back), true);
00147   activate_btn = new ButtonComponent(activate_normal);
00148   activate_btn->setFocusedSurface(activate_focused);
00149 
00150   cancel_normal =
00151     new DrawSurface(ttf->createText(ServerActivate_cancel, NormalSize,
00152                                     Normal, Back), true);
00153   cancel_focused =
00154     new DrawSurface(ttf->createText(ServerActivate_cancel, NormalSize,
00155                                     Focused, Back), true);
00156   cancel_btn = new ButtonComponent(cancel_normal);
00157   cancel_btn->setFocusedSurface(cancel_focused);
00158 
00159   menu = new MenuComponent();
00160   menu->add(activate_btn);
00161   menu->add(cancel_btn);
00162   menu->setOffset(Rect(0, NormalSize + 8));
00163 
00164   menu->addIcon(new LabelComponent(icon));
00165   menu->setIconOffset(Rect(-icon->w -4, -(icon->h - NormalSize)/2));
00166   //menu->defaultSelection(false);
00167   menu->activate(true);
00168 
00169   menu->setPosition(Grid(win->w/2 +16, draw_pos.y + 48), Center | Top);
00170   win->add(menu, ForeDepth);
00171 
00172   return 0;
00173 }
00174 
00175 
00176 int ServerActivate::MainLoop(void) {
00177   bool quit = false;
00178   do {
00179     win->lock();
00180 
00181     if (menu->isDecided()) {
00182       int index = menu->getNowIndex();
00183       win->unlock();
00184       if (index == 0) {
00185         // 起動
00186         if (!server) {
00187           // !!! ネットワークが初期化できません、とか表示して戻る
00188         }
00189         // サーバ処理を別プロセスで処理する
00190         server->processFork(port, meta_check->getNowChecked());
00191         delete server;
00192         server = NULL;
00193 
00194         // 接続処理
00195         win->delay(1);
00196         to_server = new TcpipDevice;
00197         if (to_server->connect("localhost", port) >= 0) {
00198           ChatRoom chat;
00199           chat.run(0, NULL);
00200         }
00201         delete to_server;
00202         to_server = NULL;
00203         recreate();
00204 
00205       } else if (index == 1) {
00206         // キャンセル
00207         quit = true;
00208       }
00209       win->lock();
00210     }
00211     UserInput::userInput_t ui = UserInput::getInputed();
00212     if (ui.isPressedCode(SDLK_TAB)) {
00213       bool now_state = meta_check->getNowChecked();
00214       meta_check->setNowChecked(!now_state);
00215     }
00216     quit |= ui.quit | ui.isPressedCode(SDLK_ESCAPE);
00217     win->unlock();
00218 
00219     SDL_Delay(10);
00220   } while (!quit);
00221 
00222   return 0;
00223 }
00224 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1