[ previous ] [ next ] [ threads ]
 To :  yate@v...
 From :  Maciek Kaminski <maciejka@t...>
 Subject :  echo channel
 Date :  Wed, 25 May 2005 13:17:08 +0200
See simple and useless echo channel in attachment.



/**
 * echo.cpp
 * This file is part of the YATE Project http://YATE.null.ro
 *
 * Echo channel
 *
 * Yet Another Telephony Engine - a fully featured software PBX and IVR
 * Copyright (C) 2005 Maciek Kaminski
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include 

#include 

using namespace TelEngine;


static ObjList s_rooms;

class EchoChan : public Channel
{
public:
    EchoChan(const String& name);
    ~EchoChan();
private:
    Message *m_answered;
};

class EchoConfirmator : public Thread
{
public:
  EchoConfirmator(const String &id, const String &targetid) : m_id(id), m_targetid(targetid) {};
  virtual void run();
private:
  String m_id;
  String m_targetid;
};

class EchoConsumer : public DataConsumer
{
public:
    EchoConsumer(DataSource* source)
	: m_source(source) 
        { }
    ~EchoConsumer()
	{ }
    virtual void Consume(const DataBlock& data, unsigned long timeDelta);
private:
    DataSource* m_source;
};

class EchoDriver : public Driver
{
public:
    EchoDriver();
    virtual ~EchoDriver();
    virtual void initialize();
    virtual bool msgExecute(Message& msg, String& dest);
};

INIT_PLUGIN(EchoDriver);

void EchoConsumer::Consume(const DataBlock& data, unsigned long timeDelta)
{

    if (data.null() || !m_source)
	return;
    m_source->Forward(data, timeDelta);
}

void EchoConfirmator::run() {
  ::usleep(50);
  Message* m = new Message("call.answered");
  m->addParam("driver","echo");
  m->addParam("id", m_id);
  m->addParam("targetid", m_targetid);
  Engine::enqueue(m);
}

EchoChan::EchoChan(const String& name)
    : Channel(__plugin)
{
    Debug(this,DebugAll,"EchoChan::EchoChan(%s) %s [%p]",name.c_str(),id().c_str(),this);
    DataSource* source = new DataSource();
    setSource(source);
    source->deref();
    setConsumer(new EchoConsumer(source));
    getConsumer()->deref();
}

EchoChan::~EchoChan()
{
    Debug(this,DebugAll,"EchoChan::~EchoChan() %s [%p]",id().c_str(),this);
    setConsumer();
    setSource();
}

bool EchoDriver::msgExecute(Message& msg, String& dest)
{
    CallEndpoint* ch = static_cast(msg.userData());
    if (ch) {
	EchoChan *c = new EchoChan(dest);
	if (ch->connect(c)) {
            String targetid(msg.getParam("id"));
	    c->deref();
            msg.addParam("driver","echo");
            msg.addParam("targetid", targetid);
	    EchoConfirmator *ec = new EchoConfirmator(c->id(), targetid);
	    if (ec->error()) {
	      Debug(DebugFail,"Error creating confirmator thread %p", ec);
	      delete ec;
	    }
	    else
	      ec->startup();
	    return true;
	}
	else {
	    c->destruct();
	    return false;
	}
    }
    Debug(DebugWarn,"Echo call with no call endpoint!");
    return false;
}

EchoDriver::EchoDriver()
    : Driver("echo", "misc")
{
    Output("Loaded module Echo");
}

EchoDriver::~EchoDriver()
{
    Output("Unloading module Echo");
    s_rooms.clear();
}

void EchoDriver::initialize()
{
    Output("Initializing module Echo");
    setup();
}

/* vi: set ts=8 sw=4 sts=4 noet: */