meta data for this page
  •  

Homework 01 \ CSPA

Complete the exercise exer_udp task B implementation.

Development to point

Entities

Communication

main_server state machine

exer_udb_b.main_client

import socket
import logging 
log = logging.getLogger(__name__)

echo_host = "localhost"
echo_port_client = 23456
echo_port_server = 23456 + 1

def run():
  log.debug("client running ..")
  s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  s.bind( (echo_host,echo_port_client) )

  ## send and receive a message 
  send_msg = "ECHO MESSAGE"
  log.debug("Sending " + send_msg)
  s.sendto(send_msg, (echo_host, echo_port_server) )
  recv_msg = s.recv(4096)
  log.debug("Received " + recv_msg) 
  s.close()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()  

exer_udb_b.main_server

'''
Created on 9.1.2012

@author: hevi


concrete message coding (simple), messages

character base coding as a ascii character set.
message boundaries is based on one UDP message, ie.
application message == UPD message.

common message:
type as char
sid  as string (except on open_session) 
data as string
- fields are separated by space 

open_session
  type: 'o'
  data: host and port   

number
  type: 'n'
  sid:  string
  data: number

result
  type: 'r'
  sid:  string
  data: number

close_session
  type: 'c'
  sid:  string  
  data: None

set_sid
  type: 's'
  sid: string


'''

import socket
import logging 
log = logging.getLogger(__name__)

echo_host = "localhost"
echo_port_client = 23456
echo_port_server = 23456 + 1

class Session(object):
  def __init__(self,host,port,sid):
    self.client_host = host
    self.client_port = port
    self.sid = sid

  def open_session(self):
    msg = "s " + str(self.sid)
    sock.sendto(msg, (self.client_host, self.client_port) )
  
  def number(self,n):
    pass
  
  def close_session(self):
    pass

sessions = list()
sid_counter = 1
sock = None

# echo service
def run():
  log.debug("server running ..")
  sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  sock.bind( (echo_host,echo_port_server) )
  # loop
  while True:
    msg, sender = sock.recvfrom(4096) 
    log.debug("msg " + msg + " ip " + sender[0] + " port " + str(sender[1]))
    #s.sendto(msg, (sender[0], echo_port_client) )
    ## decoding
    parts = msg.split()
    type = parts[0]
    if type == 'o':
      client_host = parts[1]
      client_port = int(parts[2])
      sid = sid_counter
      sid_counter += 1
      session = Session(client_host,client_port,sid)
      sessions.append(session)
      session.open_session()
    elif type == 'n':
      sid = parts[1]
      ## lookup
      session = None
      for s in sessions:
        if s.sid == sid:
          session = s
      if session == None:
        log.error("unknonw sid " + sid)
        continue
      ##
      number = int(parts[2])
      session.number(number)      
    elif type == 'r':
      pass
    elif type == 'c':
      sid = parts[1]
      ## lookup
      session = None
      for s in sessions:
        if s.sid == sid:
          session = s
      if session == None:
        log.error("unknonw sid " + sid)
        continue
      ##
      session.close_session()
      sessions.remove(session)
    else:
      log.error("unknown type " + type)    
  sock.close()
  

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()