summaryrefslogtreecommitdiff
path: root/Plugins/jingle/libjingle/talk/examples/call/console.cc
blob: d489fd830a79a8f82c3f4c1bf7d5bd7a9e6e878d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Jingle call example
 * Copyright 2004--2005, Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#define _CRT_SECURE_NO_DEPRECATE 1

#include <cassert>
#include "talk/base/messagequeue.h"
#include "talk/base/stringutils.h"
#include "talk/examples/call/console.h"
#include "talk/examples/call/callclient.h"

Console::Console(talk_base::Thread *thread, CallClient *client) : 
  client_thread_(thread), client_(client), prompt_(std::string("call")),
  prompting_(true) {
}

void Console::StartConsole() {
  char input_buffer[64];
  for (;;) {
	fgets(input_buffer, sizeof(input_buffer), stdin);
	client_thread_->Post(this, MSG_INPUT, 
		            new talk_base::TypedMessageData<std::string>(input_buffer));
  }
}

void Console::OnMessage(talk_base::Message *msg) {
  switch (msg->message_id) {
    case MSG_START:
      StartConsole();
	  break;
	case MSG_INPUT:
	  talk_base::TypedMessageData<std::string> *data = 
	    static_cast<talk_base::TypedMessageData<std::string>*>(msg->pdata);
	  client_->ParseLine(data->data());
	  break;
  }
}

void Console::Print(const char* str) {
  printf("\n%s", str);
  if (prompting_)
    printf("\n(%s) ", prompt_.c_str());
}

void Console::Print(const std::string& str) {
  Print(str.c_str());
}

void Console::Printf(const char* format, ...) {
  va_list ap;
  va_start(ap, format);

  char buf[4096];
  int size = vsnprintf(buf, sizeof(buf), format, ap);
  assert(size >= 0);
  assert(size < static_cast<int>(sizeof(buf)));
  buf[size] = '\0';
  Print(buf);

  va_end(ap);
}