Ararat Synapse

Morfik's Ararat Synapse Package provides an interface to the complete library of classes and functions for network communication application programming.


package-view.png
package-down.png


A Quick Walkthrough

To use Morfik's Ararat Synapse Package, add the package to the project by using the ‘Used Packages’ command on the project ribbon (see Figure 1), or drag the package file onto the application main client area.


security-fig1.png
Figure 1: Used Packages Command


To use this package in your code, first you have to add mfk_Synapse to the ‘Uses’ section of your module. After that you will gain access to all the functionality of the Ararat Synapse Library. The description of all the units, classes, interfaces, objects, records, types, variables, constants, identifiers, functions and procedures you can use with Ararat Synapse can be found here.


Code Examples for Morfik's Ararat Synapse Package

Below are four examples of Morfik's Ararat Synapse Package being implemented in a package demonstration.


Receiveng an email
FX Code

Procedure wm_POP3.mfk_Pop3Send();
Var
    pop: TPOP3Send;
Begin
    //Create POP3 object
    pop := TPoP3Send.Create;
	//Enable SSL|TLS protocol
    pop.autoTLS := true;
	//Specify user's login
    pop.userName := fLogin;
	//Specify password
    pop.password := fPass;
	//Specify target server port (or symbolic name) for POP3 server
    pop.TargetHost:=fHost;
	//Type of authorisation for login to POP3
    pop.authType := pop3AuthAll;
	//Specify POP3 server port
    pop.TargetPort := fPort;
	//Enable all features of SSL protocol
    pop.FullSSL := true;
	//Connect to POP3 server
    pop.Login;
    showMessage('Login : '+pop.resultString);
	//Send LIST command
    pop.list(0);
    showMessage('list : '+pop.resultString);
	//Get list of messages
    MailContent := 'Loaded : ' + pop.FullResult.ToString('<br>', true);
	//Downloading first message
    pop.retr(1);
    showMessage('retr : '+pop.resultString);
	//Get mail content
    MailContent := MailContent + '<br>------<br>' + pop.FullResult.ToString('<br>', true);
	//Disconnect from POP3 server
    pop.logout;
    showMessage('logout : '+pop.resultString);
    pop.Free;
End;


Sending an email
FX Code

Procedure wm_SMTP.MailSend(Const sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd, sFrom, sTo, sSubject, sMesBody: String);
Var
  smtp: TSMTPSend;
  msg_lines: TStringList;
Begin
  msg_lines := TStringList.Create;
  //Create SMTP object
  smtp := TSMTPSend.Create;
  Try
    //Fill in the content of future mail
    msg_lines.Add('From: ' + sFrom);
    msg_lines.Add('To: ' + sTo);
    msg_lines.Add('Subject: ' + sSubject);
    If Length(sMesBody) > 0 Then
    Begin
        msg_lines.Add('');
        msg_lines.Add(sMesBody);
    End;
    //====================================
	//If authorization is required, then fill in username
    smtp.UserName := sSmtpUser;
	//Specify user's password
    smtp.Password := sSmtpPasswd;
	//Specify target server IP (or symbolic name)
    smtp.TargetHost := sSmtpHost;
	//Specify target server port
    smtp.TargetPort := sSmtpPort;
	//Enable SSL|TLS protocols
    smtp.autoTLS := true;
	//Connect to SMTP server
    If Not smtp.Login() Then
        Showmessage('SMTP ERROR: Login:' + smtp.EnhCodeString);
	//If you successfully pass authorization to the remote server
    If smtp.AuthDone Then
    Begin
        //Send MAIL FROM SMTP command to set sender’s e-mail address.
        If Not smtp.MailFrom(sFrom, Length(sFrom))Then
            Showmessage('SMTP ERROR: MailFrom:' + smtp.EnhCodeString);
        //Send RCPT TO SMTP command to set receiver’s e-mail address.
        If Not smtp.MailTo(sTo) Then
            Showmessage('SMTP ERROR: MailTo:' + smtp.EnhCodeString);
		//Send DATA SMTP command and transmit message data.
        If Not smtp.MailData(msg_lines) Then
            Showmessage('SMTP ERROR: MailData:' + smtp.EnhCodeString);
    End;
    //Close SMTP session (QUIT command) and disconnect from SMTP server.
    If Not smtp.Logout() Then
        Showmessage('SMTP ERROR: Logout:' + smtp.EnhCodeString);
  Finally
    msg_lines.Free;
    smtp.Free;
  End;
End;


Translating text using Google translate API
FX Code

Procedure wm_translate.Execute;
Var
    Http : THTTPSend;
    str  : TStringList;
Begin
    //Create HTTPSend object
    Http := THttpSend.Create;
	//Reset headers and document and Mimetype
    Http.Clear;
    //Connects to host defined in URL and accesses resources defined in URL by method.
    if Http.httpMethod('GET','http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='
+encodeURIComponent(fInput)+'&langpair=en%7Ces') Then
    Begin
        str := TStringList.Create;
		//Save stream with document received from HTTP server
        str.loadFromStream(Http.Document);
        fOutput := str.Strings[0];
    End;
End;


Getting resources
FX Code

Procedure wm_GetResource.GetRes;
Const
    SQLINSERT = 'INSERT INTO "tblResources" ("FileName", "FileContent") VALUES (:"FileName", :"FileContent")';
    SQLSELECT = 'SELECT COUNT(t."Id") as countid FROM "tblResources" t Where t."FileName" = :"FileName"';
    SQLDELETE = 'DELETE FROM "tblResources" t Where t."FileName" = :"FileName"';
Var
    Http        : THTTPSend;
    Command     : TSQLCommand;
    HTTPServer  : THTTPServer;
    DBParam     : TDBParam;
    ColumnData  : TMemoryStream;
    DBFileBlob  : TDBFileBlob;
    DBFieldInfo : TDBFieldInfo;
Begin
    //Initialization of Database connection and datafield access
    DBFieldInfo := Catalog.GetTableFieldByName('tblResources', 'FileContent');
    If DBFieldInfo = Nil Then Exit;
    HTTPServer  := THTTPServer.Create(Nil, Nil);
    Command     := HTTPServer.DefaultDBConnection.CreateSQLCommand('');
    ColumnData  := TMemoryStream.Create;
    DBFileBlob  := TDBFileBlob.Create(ColumnData, DBFieldInfo, False, False);
	//Create HTTPSend object
    Http        := THttpSend.Create;
	//Reset headers and document and Mimetype
    Http.Clear;
    with Http do
    Begin
	    //Connects to host defined  in URL and accesses resources defined in URL by method.
        if HTTPMethod('GET',flURL) then
        Try
			//Searching for resources  in the database that have the same name
            Command.SQL.Add(SQLSELECT);
            Command.Prepare;
            Command.ParamByName('"FileName"').AsString  := flName;
            Command.Execute;
			//If resources with the same name are found then
            If Command.fieldByName('countid').asInteger > 0 Then
            Begin
			    //Delete the resource with the same name
                Command.Free;
                Command := HTTPServer.DefaultDBConnection.CreateSQLCommand('');
                Command.SQL.Add(SQLDELETE);
                Command.Prepare;
                Command.ParamByName('"FileName"').AsString  := flName;
                Command.Execute;
            End;
			//Specify SQL query for adding new resources to database
            Command.Free;
            Command := HTTPServer.DefaultDBConnection.CreateSQLCommand('');
            Command.SQL.Add(SQLINSERT);
            Command.Prepare;
            Command.ParamByName('"FileName"').AsString  := flName;
            Try
                DBFileBlob.Clear(True);
				//Place resource stream into blob field
                DBFileBlob.LoadFromFile(flName, Document);
                DBFileBlob.SaveToDBField(Nil);
                DBParam := Command.ParamByName('"FileContent"');
                If DBParam Is TDBFieldBlob Then
                    TDBFieldBlob(DBParam).LoadFromStream(ColumnData);
            Except
            End;
			//Add new resources to database
            Command.Execute;
            flContent := flName;
        Except
            ShowMessage('Error. Cannot display resource');
        End;
        Free;
    End;
    HTTPServer.DefaultDBConnection.DestroySQLCommand(Command);
End;

Technical Documentation

For information on the Ararat Synapse Package internals, please refer to the technical documentation.

See Also

Back to top