program amiproxy; { This file is part of AMIProxyPal. AMIProxyPal 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 3 of the License, or (at your option) any later version. Foobar 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 Foobar. If not, see . } {$mode objfpc}{$H+} uses {$IFDEF UNIX}{$IFDEF UseCThreads} cthreads, {$ENDIF}{$ENDIF} sysutils, XMLCfg, Classes, astamiclient, appprops_bom, clientconnection, ami_client_interfaces, client_handler, client_decorators, astevent_utils, thread_communicator, proxy_client_server, console_out; var AstAMI: TAstAMIClient; sCmd: string; ThreadCom: TThreadCommunicator; AstHelperCom: TThreadCommunicator; ClientList: TClientConnectionList; Queue: TAstMessageQueue; QueueMinder: TOutgoingMinder; ClientServer: TProxyClientServer; procedure ConnectAMI; begin ThreadCom.Lock; try ThreadCom.Msg := 'connect'; finally ThreadCom.Unlock; end; end; procedure DisconnectAMI; begin ThreadCom.Lock; try ThreadCom.Msg := 'disconnect'; finally ThreadCom.Unlock; end; end; procedure SetupAMIClient(var AClient: TAstAMIClient); var Config: TXMLConfig; begin Config := gAppXMLConfig('amiproxy.xml'); try AClient.Port := Config.GetValue('ami/port', '5038'); AClient.Host := Config.GetValue('ami/host', ''); AClient.AMIPassword := Config.GetValue('ami/amipassword', ''); AClient.AMIUserName := Config.GetValue('ami/amiusername', ''); finally; Config.free; end; end; begin SetHeapTraceOutput('heap.txt'); ThreadCom := TThreadCommunicator.Create; AstHelperCom := TThreadCommunicator.Create; ClientList := TClientConnectionList.Create; ClientList.OwnsObjects := false; Queue := TAstMessageQueue.Create; // setup AMI client here AstAMI := TAstAMIClient.Create( ClientList, '', '', ThreadCom, AstHelperCom, Queue); SetupAMIClient(AstAMI); // this object handles proxy client connections ClientServer := TProxyClientServer.Create(ClientList, '4500'); // this object is a helper object that handles messages between AMI client // and proxy clients. QueueMinder := TOutgoingMinder.Create(ClientList, Queue, AstHelperCom); try // write initial message to user through console. WriteInitOut; repeat ReadLn(sCmd); // inspect for other commands than quit if (sCmd) = 'connect ami' then begin ConnectAMI; WriteLn('Connected!'); WriteLn(' '); end else if (sCmd = 'disconnect ami') then begin DisconnectAMI; WriteLn('Disconnected!'); WriteLn(' '); end else if (sCmd = 'help') then begin WriteHelp; end else if (sCmd = 'about') then begin WriteAbout; end; until (sCmd = 'quit'); // Write update to console for shutting down WriteLn(LineEnding + LineEnding); WriteLn('Shutting down Servers please wait...' + LineEnding); finally; if (AstAMI.Connected) then begin DisconnectAMI; end; WriteLn('Terminating Client Connections and Client-Server...'); ClientServer.Terminate; sleep(500); QueueMinder.Terminate; Sleep(500); WriteLn('Terminating Connection to AMI...'); AstAMI.Terminate; Sleep(500); ThreadCom.Free; Queue.Free; AstHelperCom.Free; ClientList.Free; WriteLn('Almost there...'); Sleep(500); WriteLn('Shutting down...'); end; end.