File: confbus.cc

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (148 lines) | stat: -rw-r--r-- 4,623 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (c) 1993-1995 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by the University of
 *      California, Berkeley and the Network Research Group at
 *      Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
static const char rcsid[] =
    "@(#) $Header: /cs/research/mice/starship/src/local/CVS_repository/vic/confbus.cc,v 1.3 1999/01/08 11:25:42 ucackha Exp $ (LBL)";

#include <stdlib.h>
#include <ctype.h>
#include "config.h"
#include "inet.h"
#include "group-ipc.h"
#include "Tcl.h"

class ConfBus : public TclObject, public IPCHandler {
    public:
	ConfBus(int channel);
	~ConfBus();
	void ipc_input(int type, int frompid, u_char* p, int len);
    protected:
	int command(int argc, const char*const* argv);
	GroupIPC* ipc_;
	char* callback_;
	int value1, value2;
};

static class ConfBusMatcher : public Matcher {
    public:
	ConfBusMatcher() : Matcher("confbus") { }
	TclObject* match(const char* channel) {
		return (new ConfBus(atoi(channel)));
	}
} confbus;

ConfBus::ConfBus(int channel)
	: TclObject(0), IPCHandler(~0), ipc_(0)
{
	value1=0;
	value2=0;
	callback_ = 0;
	ipc_ = new GroupIPC(channel);
	ipc_->attach(this);
}

ConfBus::~ConfBus()
{
	delete ipc_;
	delete callback_;
}

void ConfBus::ipc_input(int, int, u_char* msg, int cc)
{
	int value1, value2;
	if (cc <= 0 || callback_ == 0)
		return;

	if (cc == 4) {
		/*
		 * XXX backward compatibility with vat-3 voice-switching
		 * messages (i.e., pre-RTPv2 protocol).  If the message
		 * is four bytes, then it must be the binary coded IP address
		 * of the site who's audio channel became active.  Otherwise,
		 * it must be an new-style ascii conference bus command.
		 * Note an ascii command is necessarily larger than
		 * four bytes.
		 */
		u_int32_t addr = *(u_int32_t*)msg;
		sprintf((char*)msg, "focus %s", InetNtoa(addr));
	}

	/* if this is a power message then compare it with the last 
	message and if the value is not significantly different then return */
	if (sscanf((char*)msg, "relate_power %*s %d",&value1)!=0) {
		if (value1>0) {
			if (value1<value2) {
				if (value1+10>=value2) return;
				} else
				if (value1-10<=value2) return;
			value2=value1;
		}
	}

	Tcl& tcl = Tcl::instance();
	/*
	 * make sure message is null terminated and check that
	 * it's regular text that tcl can handle.
	 */
	int last = cc - 1;
	if (msg[last] != 0)
		return;
	for (int i = 0; i < last; ++i) {
		if (!isascii(msg[i]) && !isprint(msg[i]))
			return;
	}
	tcl.evalf("%s %s {%s}", callback_, name(), (char*)msg);
}

/*
 * $bus write $type $msg
 * $bus handler $proc
 */
int ConfBus::command(int argc, const char*const* argv)
{
	if (argc == 3) {
		if (strcmp(argv[1], "handler") == 0) {
			const char* proc = argv[2];
			delete callback_;
			callback_ = new char[strlen(proc) + 1];
			strcpy(callback_, proc);
			return (TCL_OK);
		}
		if (strcmp(argv[1], "send") == 0) {
			/*XXX*/
			ipc_->send(0, argv[2]);
			return (TCL_OK);
		}
	}
	return (TclObject::command(argc, argv));
}