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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
#!/usr/bin/perl -w
# osdServer.pl - process incoming text events based on keyword matching and
# display using Ghosd
use strict;
use FileHandle;
use Socket;
use Thread;
use Config;
use Thread qw(async);
if( @ARGV != 1 ){ die "specify a port to listen on " }
my %osdHash = ();
my $proto = "";
my $port = "";
my $port1 = "";
my $sockAddr = "";
my $sockAddr1= "";
my $fullCmd = "";
my $fullCmd1 = "";
# readKeyWordFile reads type, keywords, (text/image) parameters
# text parameters:
# X coordinate, Y coordinate, red, green, blue, font size, fade In, fade Out
# image parameters:
# image filename, X coordinate, Y coordinate
# osdServer.confg format is: type _#_ keywords _#_ osd parameters
sub readOsdFile
{
open(OSDFILE,"/etc/osdServer.config") or die "no osdServer.config file: $!";
while(<OSDFILE>){
# ignore comment lines
if( !/^#/ ){
chomp($_);
my @arrLine = split "_#_";
$arrLine[0] =~ s/ //g;
$osdHash{ lc($arrLine[0]) }{ $arrLine[1] } = $arrLine[2];
}#if not a comment line
}#for each line in file
close(OSDFILE);
}#readOsdFile
sub matchWords
{
my $msgString = $_[0];
my $type = $_[1];
# print "msgstring $msgString \n";
# print "type $type\n " ;
# for the type specified
for my $kwList ( keys %{ $osdHash{$type} } )
{
# for each word
for my $checkWord ( split " ", $kwList )
{
$checkWord =~ s/ //g;
if( $msgString =~ /$checkWord/i )
{
if( $type eq "text" )
{
$fullCmd .= qq{./text '$msgString' $osdHash{text}{$kwList} &};
#print "$fullCmd\n";
}else
{
my @realstring = split(/:::/, $msgString);
print "$realstring[0] \n";
$fullCmd .= qq{/usr/bin/imageGhosd -i $osdHash{image}{$kwList} '$realstring[0]' &};
# print "full $fullCmd\n";
#print "msgstring- $msgString \n";
}
}#if matching word
}#for each check word
}#for each key word list
}#checkText
sub matchWords1
{
my $msgString = $_[0];
my $type = $_[1];
# print "msgstring $msgString \n";
print "Here ia m" ;
# print "type $type\n " ;
# for the type specified
for my $kwList ( keys %{ $osdHash{$type} } )
{
# for each word
for my $checkWord ( split " ", $kwList )
{
$checkWord =~ s/ //g;
if( $msgString =~ /$checkWord/i )
{
if( $type eq "text" )
{
$fullCmd1 .= qq{./text '$msgString' $osdHash{text}{$kwList} &};
#print "$fullCmd\n";
}else
{
my @realstring = split(/:::/, $msgString);
print "$realstring[0] \n";
$fullCmd1 .= qq{/usr/bin/imageGhosd -i $osdHash{image}{$kwList} '$realstring[0]' &};
print "full $fullCmd1\n";
#print "msgstring- $msgString \n";
}
}#if matching word
}#for each check word
}#for each key word list
}
sub sub1 {
while( 1 )
{
my $acceptSock1 = accept(HANDLER, SERVER1) or die "accept: $!\n";
my($acceptPort1, $acceptIpAddr) = sockaddr_in($acceptSock1);
autoflush HANDLER 1;
while(my $msgString1 = <HANDLER>)
{
chomp($msgString1);
matchWords1($msgString1,"text");
matchWords1($msgString1,"image");
$fullCmd1 = `$fullCmd1` unless $fullCmd1 eq "";
$fullCmd1 = "";
}#while handler
close HANDLER or die "close HANDLER: $!\n";
}#while forever
}
# begin main program
readOsdFile();
$proto = getprotobyname('tcp');
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n";
socket(SERVER1, PF_INET, SOCK_STREAM, $proto) or die "socket: $!\n";
$port = $ARGV[0];
$port1 = $port + 1;
$port1 = 5001;
$sockAddr = sockaddr_in($port, INADDR_ANY);
$sockAddr1 = sockaddr_in($port1, INADDR_ANY);
bind(SERVER, $sockAddr) or die "bind: $!\n";
bind(SERVER1, $sockAddr1) or die "bind: $!\n";
listen(SERVER, SOMAXCONN) or die "listen: $!\n";
listen(SERVER1, SOMAXCONN) or die "listen: $!\n";
$Config{usethreads} or die "Recompile Perl with threads to run this program.";
my $thr = threads->new(\&sub1);
#listen foreve
while( 1 )
{
my $acceptSock = accept(HANDLER, SERVER) or die "accept: $!\n";
my($acceptPort, $acceptIpAddr) = sockaddr_in($acceptSock);
autoflush HANDLER 1;
while(my $msgString = <HANDLER>)
{
chomp($msgString);
matchWords($msgString,"text");
matchWords($msgString,"image");
$fullCmd = `$fullCmd` unless $fullCmd eq "";
$fullCmd = "";
}#while handler
close HANDLER or die "close HANDLER: $!\n";
}#while forever
|