• Ingen resultater fundet

1 byte

up to 128 bytes

Fig.2.Packetformatfor transmissionofdata.

3 Implementation

This section describes the implementation of the design into a working Standard ML [8]

library.The implementationconsistsofSMLlibraryles,one foreach ofthelayersdescribed

previously inSect. 2.

3.1 The Communication Layer

TheCommunicationLayerisimplementedwithTCP/IPastheunderlyingtransportprotocol.

It is designed to encapsulate the TCP/IPprotocol and to provide usersof thislayer with a

shielded interface to the network functions provided by TCP/IP. The SML/NJ standard

library [25] contains a structure called Socket in which primitive operations on sockets are

available.The CommunicationLayerisbased on thislibrary.

the CommsLayer structure constitutingthe CommunicationLayer. We describe each of the

primitivesprovidedbytheCommunicationLayerinmore detail below.

signature COMMS_LAYER =

sig

type channel

exception BadAddr of string

val connect : string * int -> channel

val accept : int -> channel

val send : channel * Word8Vector.vector -> unit

val receive : channel * int -> Word8Vector.vector

val disconnect : channel -> unit

end;

Fig.3.SMLsignatureforCommunicationLayer.

Most ofthe implementationoftheCommunicationLayercomesdirectly from[25]. The only

newdatatypeintroducediscalledchannel.ItspurposeistoallowtheConnectionManagement

Layer to mapstring identiers to TCP/IP sockets without usinganyTCP/IP related code.

Below we give abriefdescriptionofeach of theprimitives.

connect Createsaconnection,actingasaclient,toan externalprocess.The rstargument

is used to specify the hostname of theexternal application, and the second argument is

usedto specifythe port number.

accept Waitsforanincomingconnectionontheportspeciedastheargument.Theprimitive

blocks untilan externalapplication connects, and theconnectionis thenestablished.

send Sendsthesequence of bytesspeciedasthesecond argument on thechannel specied

astherst argument.

receive Receivesthenumberofbytesspeciedasthesecond argument onthechannel

spec-ied as the rst argument. The primitive will block until the speciednumber of bytes

have beenreceived on thechannel.

disconnect Closestheconnection speciedastheargument.

3.2 Messaging Layer

The Messaging Layer isimplementedon topof theCommunicationLayer. Figure 4liststhe

MESSAGINGLAYER SMLsignature.ThissignatureisimplementedbytheMessagingLayer

structure constituting theMessaging Layer. The sendfunction implementsthe transmission

ofmessages(speciedasasequenceofbytes)accordingtotheprotocoldiscussedinSect. 2.3.

The data provided is segmented and appropriate headers are added to each segment. This

forms packets of datathat aresent to theexternal process usingthe sendfunctionfrom the

Communication Layer. The receive function implements thereception of messages. It reads

one byte(the headerbyte) andthecorrespondingnumberofpayloadbytesfrom thechannel

usingthereceivefunctionfromtheCommunicationLayer.TheInvalidDataExnexceptionwill

be raisedifreceived datadoeshave theformatspeciedinFig. 2.

sig

type channel

exception InvalidDataExn of string

val send : channel * Word8Vector.vector -> unit

val receive : channel -> Word8Vector.vector

end

Fig.4.SMLsignatureforMessagingLayer.

3.3 Connection Management Layer

TheConnectionManagementLayerbuildsontopoftheCommunicationandMessaging

Lay-ers by providing the abilityand interface to communicate with multiple external processes.

Theconnectionstoragemechanismisimplementedinthislayer.TheConnectionManagement

Layeris implementedindependentlyof TCP/IPandsockets. Insteadituses theservices

pro-videdtheCommunicationLayerandtheMessagingLayertointeractindirectlywithTCP/IP.

It is thefunctionsinthislayerthat wouldnormallybeused inaCPN model.

Figure5 liststheCONN MANAGEMENT LAYER SMLsignature. Thissignatureis

im-plementedbytheConnManagementLayerstructureconstitutingtheConnectionManagement

Layer. The signature species the type Connnection used to identity connections. The type

hasbeen implementedas strings. We describe each of the primitives provided by the

Com-municationLayer inmore detailbelow.

signature CONN_MANAGEMENT_LAYER =

sig

type Connection

exception ElementMissingExn of string

exception DupConnNameExn of string

val openConnection : Connection * string * int -> unit

val acceptConnection : Connection * int -> unit

val send : Connection * 'a * ('a -> Word8Vector.vector) -> unit

val receive : Connection * (Word8Vector.vector -> 'a) -> 'a

val closeConnection : Connection -> unit

end

Fig.5.SMLsignatureforConnection ManagementLayer.

openConnection Allows users to connect to external processes as a client. It takes three

input parameters. The rst of these is the unique string identier (of type Connection)

to be associated with the new connection. The second and thirdare the host name and

port number that make up theaddress of an external process. The functionrst checks

to ensure the string identier given is unique, by searching the existing connections. A

DupConnNameExnexceptionisraisedifthisisnotthecase. Thefunctionthenattempts

to create a connection to theexternal process byusingthe primitivesfrom the

Commu-of connections.The returntype of thisfunction istype unit.

acceptConnection Provides serverbehaviour,and allows externalprocesses to connect to

Design/CPN. ThisfunctiontakesaConnection(stringidentier)andaportnumberas

input.Thefunctionchecksthatthegivenstringidentierisunique,andthenlistensonthe

given port forincomingconnection requests. Thiscauses Design/CPNto block untilan

incomingconnection request is received. When thishappens, a connection is established

withtheexternal process requestingthe connection.

send Allowuserstosendanytypeofdatatoexternalprocesses.Thefunctionispolymorphic,

inthesensethatthedatapassedtoitforsendingcanbeofanytype,includinguserdened

types.Threeparametersarepassedtothisfunctionasinput.Therstisastringidentier

forthe connection, the second is thedata to send,and the thirdis a function to encode

thedatatosend.Thepurposeoftheencodingfunctionisto encodethedatatosendinto

asequenceofbytes.Thisallowsthedatatobeofanytype,providedanencodingfunction

existsforthattype.Thesendfunctionretrievestheconnectioncorrespondingto thegiven

string identier. It then invokes the send primitive at the Messaging Layer. The return

type ofthisfunctionis type unit.

receive Allows userstoreceive anytypeofdatafrom anexternalprocess.The receive

func-tionispolymorphicinthesame wayasthesendfunction.Theparameterstothisfunction

areastringidentierandadecodingfunction,todecode thereceivedbytevector intothe

appropriatedatatype.Thefunctionbeginsbyretrievingtheconnectionfrom whichdata

willbereceived.IttheninvokesthereceivefromtheMessagingLayerto receivethedata.

Thepayloaddata(whichwasstoredinthecorrectorderwhen itwasread)isthenpassed

to thedecoding function.The resultingdecodeddataisthen returned.

closeConnection Allowsusersto closeaconnection.Thestringidentieroftheconnection

to be closed is passed to this function as the argument. A search of the connections is

conductedto ensurethataconnection existswiththatstring identier.If theconnection

doesnotexist,an ElementMissingExnexceptionisraised.The connection itself isclosed

bycallingthedisconnectfunctionfrom theCommunicationLayer. Thestoredconnection

informationisthenremovedfrom thelistofconnections. Thereturntypeofthisfunction

istypeunit.

4 Application of Comms/CPN

Anexternalcommunicationinfrastructurewasrequiredaspartoftheresearchprojecton

mod-ellingandanalysis ofavionicsmissionsystemsmentionedintheintroduction.Comms/CPN

was developed for the purpose of providing external visualisation of the simulation of an

AvionicsMissionSystem (AMS)CPN model.

Figure6 illustratesthearchitectureofthevisualisationfacilities,and howComms/CPN

tsinto thisarchitecture. Theidea isthat Comms/CPN willprovidethenecessary

commu-nicationinfrastructuretoallowdatatobesentfromtheCPNmodeltoanexternalanimation

package which will theninterpret thisdata and update theanimation as necessary. The

ar-chitectureconsists ofthree applications(processes). TheDesign/CPNGUI (left), theSML

process (middle), and the external Visualisation Package (right). The Design/CPN GUI

and the simulator part of the SML process communicates (in theusual way) for visualising

the token game during simulation in the Design/CPN GUI. This communication is done

viaTCP/IPusingtheDMO module oftheDesign/CPN simulator.In additionto this,the

Package via Comms/CPN.